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;
                        }
                    }
    
                }
            }
  • 相关阅读:
    Codeforces 872B:Maximum of Maximums of Minimums(思维)
    Codeforces 849A:Odds and Ends(思维)
    洛谷 P2397:yyy loves Maths VI (mode)(摩尔投票算法)
    POJ 3264:Balanced Lineup(区间最值查询ST表&线段树)
    Atcoder ABC137D:Summer Vacation(贪心)
    POJ 3278:The merchant(LCA&DP)
    Codeforces Gym-100985C: MaratonIME plays Nim(交互题&博弈)
    ZOJ 1141:Closest Common Ancestors(LCA)
    浅谈C++运算符重载
    初学c++动态联编
  • 原文地址:https://www.cnblogs.com/james1207/p/3304104.html
Copyright © 2011-2022 走看看