Site Overlay

邻接表转矩阵算法

/// <summary>
/// 邻接表转矩阵算法
/// </summary>
/// <param name="alist">邻接表</param>
/// <returns>矩阵</returns>
public static bool[,] AdjacencyListToMatrix(LinkedList<LinkedList<Node>> alist)
{
    var n = alist.Count;
    var retMat = new bool[n,n];
    var currentHead = alist.First();
    // 遍历邻接表的各个项
    while (currentHead != null)
    {
        var currentFirst = currentHead.First;
        var currentNext = currentFirst.Next;
        // 内层表的遍历
        while (currentNext != null)
        {                    
            retMat[currentFirst.Value.Index, currentNext.Value.Index] = true;
            currentNext = currentNext.Next;
        }
    }
    return retMat;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注