zoukankan      html  css  js  c++  java
  • 第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;
                        }
                    }
    
                }
            }
  • 相关阅读:
    MATLAB 模板匹配
    ACDSee15 教你如何轻松在图片上画圈圈、画箭头、写注释
    Qt 显示一个窗体,show()函数和exec()函数有什么区别?
    Qt 将窗体变为顶层窗体(activateWindow(); 和 raise() )
    Qt QSS样式化 菜单Qmenu&QAction
    Qt 获取文件夹中的文件夹名字
    Qt 删除文件夹或者文件
    欧洲终于承认“工业4.0”失败,互联网经济严重落后中美
    深入浅出数据结构
    浅谈城市大脑与智慧城市发展趋势
  • 原文地址:https://www.cnblogs.com/james1207/p/3304104.html
Copyright © 2011-2022 走看看