• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

第6章 图

6.3 图的遍历

6.3.1 深度优先遍历

以下图为例,其深度优先遍历输出应该为:

1 -> 3 -> 2 -> 5 -> 4 -> 6 -> 7 -> 9 -> 8 -〉10

图的深度优先遍历类似于树的先序遍历,是树的先序遍历的推广。要借助一个辅助数组标记已经遍历过的顶点。

以邻接表为例实现图的深度优先遍历:GraphAdjList<T>类的定义在上一篇图的邻接表存储结构博客中

 

        // public class GraphAdjList<T> : IGraph<T>
        public void Print()
        {
            Print(this.vexList, NodeNum);
        }

        public void Print(VexListNode<T>[] vexListNodes, int nodeNum)
        {
            bool[] markers = new bool[nodeNum];
            //类似于二叉树的非递归深度优先遍历,借用一个stack实现
            Stack<VexListNode<T>> stack=new Stack<VexListNode<T>>();

            for (int i = 0; i < nodeNum; i++)
            {
                if (markers[i])
                    continue;

                stack.Push(vexListNodes[i]);

                while (stack.Count != 0)
                {
                    VexListNode<T> currentVexListNode = stack.Pop();
                    GraphNode<T> node = currentVexListNode.Node;
                    int vexIndex = IsNode(node);

                    if (markers[vexIndex] == true)
                        continue;

                    Console.Write(node.Value + "  ");
                    markers[vexIndex] = true;

                    AdjListNode<T> currentAdj=currentVexListNode.FirstAdj;
                    while (currentAdj != null)
                    {
                        stack.Push(vexListNodes[currentAdj.AdjVexIndex]);
                        currentAdj = currentAdj.Next;
                    }
                }

            }
        }

6.3.2 广度优先遍历

图的广度优先遍历类似于树的层序便利

以下图为例,其广度优先遍历应该为:

1 -> 3 -> 4 -> 2-> 5 -> 6 -> 7 -> 9 -> 10 -> 8

 


以邻接表为例实现图的广度优先遍历:GraphAdjList<T>类的定义在上一篇图的邻接表存储结构博客中

        // public class GraphAdjList<T> : IGraph<T>
        public void Print2()
        {
            Print2(this.vexList, NodeNum);
        }

        public void Print2(VexListNode<T>[] vexListNodes, int nodeNum)
        {
            bool[] markers = new bool[nodeNum];
            //类似于二叉树的广度优先遍历,借用用一个queue实现
            Queue<VexListNode<T>> queue = new Queue<VexListNode<T>>();

            for (int i = 0; i < nodeNum; i++)
            {
                if (markers[i])
                    continue;

                queue.Enqueue(vexListNodes[i]);

                while (queue.Count != 0)
                {
                    VexListNode<T> currentVexNode = queue.Dequeue();
                    GraphNode<T> node = currentVexNode.Node;
                    int indexer = IsNode(node);
                    AdjListNode<T> currentAdjNode = currentVexNode.FirstAdj;

                    if (markers[indexer])
                        continue;

                    Console.Write(node.Value);
                    markers[indexer] = true;

                    while (currentAdjNode != null)
                    {
                        queue.Enqueue(vexListNodes[currentAdjNode.AdjVexIndex]);
                        currentAdjNode = currentAdjNode.Next;
                    }
                }

            }
        }
posted @ 2013-09-05 19:19  Class Xman  阅读(147)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3