Exemple #1
0
        //从某个顶点出发进行深度优先遍历
        public void DFSAL(int i)
        {
            visited[i] = 1;
            AdjListNode <T> p = adjList[i].FirstAdj;

            while (p != null)
            {
                if (visited[p.AdjVert] == 0)
                {
                    DFSAL(p.AdjVert);
                }
                p = p.Next;
            }
        }
Exemple #2
0
        //获取边的数目
        public int GetNumOfEdge()
        {
            int i = 0;

            //遍历邻接表数组
            foreach (var nd in adjList)
            {
                AdjListNode <T> p = nd.FirstAdj;

                while (p != null)
                {
                    ++i;
                    p = p.Next;
                }
            }
            return(i / 2);
        }
Exemple #3
0
        //在顶点v1和v2之间添加权值为v的边
        public void SetEdge(Node <T> v1, Node <T> v2, int v)
        {
            //v1或v2不是图的顶点或者v1和v2之间存在边
            if (!IsNode(v1) || !IsNode(v2) || IsEdge(v1, v2))
            {
                Console.WriteLine("Node is not belong to Graph!");
                return;
            }

            //权值不对
            if (v != 1)
            {
                Console.WriteLine("Weight is not right!");
                return;
            }

            //处理顶点v1的邻接表
            AdjListNode <T> p = new AdjListNode <T>(GetIndex(v2));

            //顶点v1没有邻接顶点
            if (adjList[GetIndex(v1)].FirstAdj == null)
            {
                adjList[GetIndex(v1)].FirstAdj = p;
            }
            //顶点v1有邻接顶点
            else
            {
                p.Next = adjList[GetIndex(v1)].FirstAdj;
                adjList[GetIndex(v1)].FirstAdj = p;
            }

            //处理顶点v2的邻接表
            p = new AdjListNode <T>(GetIndex(v1));
            //顶点v2没有邻接顶点
            if (adjList[GetIndex(v2)].FirstAdj == null)
            {
                adjList[GetIndex(v2)].FirstAdj = p;
            }
            //顶点v1有邻接顶点
            else
            {
                p.Next = adjList[GetIndex(v2)].FirstAdj;
                adjList[GetIndex(v2)].FirstAdj = p;
            }
        }
Exemple #4
0
        //判断v1和v2之间是否存在边
        public bool IsEdge(Node <T> v1, Node <T> v2)
        {
            //v1或v2不是图的顶点
            if (!IsNode(v1) || !IsNode(v2))
            {
                Console.WriteLine("Node is not belong to Graph!");
                return(false);
            }
            AdjListNode <T> p = adjList[GetIndex(v1)].FirstAdj;

            while (p != null)
            {
                if (p.AdjVert == GetIndex(v2))
                {
                    return(true);
                }
                p = p.Next;
            }
            return(false);
        }
Exemple #5
0
        //从某个顶点出发进行广度优先遍历
        public void BFSAL(int i)
        {
            visited[i] = 1;
            CSeqQueue <int> cq = new CSeqQueue <int>(visited.Length);

            cq.In(i);

            while (!cq.IsEmpty())
            {
                int             k = cq.Out();
                AdjListNode <T> p = adjList[k].FirstAdj;

                while (p != null)
                {
                    if (visited[p.AdjVert] == 0)
                    {
                        visited[p.AdjVert] = 1;
                        cq.In(p.AdjVert);
                    }
                    p = p.Next;
                }
            }
        }
Exemple #6
0
 //删除顶点v1和v2之间的边
 public void DeleteEdge(Node <T> v1, Node <T> v2)
 {
     //v1或v2不是图的顶点
     if (!IsNode(v1) || !IsNode(v2))
     {
         Console.WriteLine("Node is not belong to Graph!");
         return;
     }
     //顶点v1与v2之间有边
     if (IsEdge(v1, v2))
     {
         //处理顶点v1的邻接表中的顶点v2的邻接表结点
         AdjListNode <T> p   = adjList[GetIndex(v1)].FirstAdj;
         AdjListNode <T> pre = null;
         while (p != null)
         {
             if (p.AdjVert != GetIndex(v2))
             {
                 pre = p;
                 p   = p.Next;
             }
         }
         pre.Next = p.Next;
         //处理顶点v2的邻接表中的顶点v1的邻接表结点
         p   = adjList[GetIndex(v2)].FirstAdj;
         pre = null;
         while (p != null)
         {
             if (p.AdjVert != GetIndex(v1))
             {
                 pre = p;
                 p   = p.Next;
             }
         }
         pre.Next = p.Next;
     }
 }
Exemple #7
0
 //构造器
 public VexNode(Node <T> nd, AdjListNode <T> alNode)
 {
     data     = nd;
     firstAdj = alNode;
 }
Exemple #8
0
 //构造器
 public VexNode(Node <T> nd)
 {
     data     = nd;
     firstAdj = null;
 }
Exemple #9
0
 //构造器
 public VexNode()
 {
     data     = null;
     firstAdj = null;
 }
 //构造器
 public AdjListNode(int vex)
 {
     adjVert = vex;
     next    = null;
 }