• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            eryar

            PipeCAD - Plant Piping Design Software.
            RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
            posts - 603, comments - 590, trackbacks - 0, articles - 0

            深度優先遍歷用鄰接表表示的圖

            DFS the Adjacency List Graph

            eryar@163.com

            一、簡介

            創建了圖之后,我們希望從圖中某個頂點出發訪遍圖中其余頂點,且使每個頂點僅被訪問一次。這一過程就是圖的遍歷(Traversing Graph)。圖的遍歷算法是求解圖的連通性問題、拓樸排序和求解關鍵路徑等算法的基礎。

            深度優先搜索(Depth First Search)是一種遞歸的遍歷方法。其過程為從圖中任意一頂點出發,訪問與其相連接的未被訪問的頂點。因此,遍歷圖的過程實質上是對每個頂點查找其鄰接點的過程。

             

            二、實現代碼

            依然使用鄰接表來表示圖的存儲結構,深度優先遍歷程序如下代碼所示:

              1: //------------------------------------------------------------------------------
            
              2: //	Copyright (c) 2012 eryar All Rights Reserved.
            
              3: //
            
              4: //		File    : Main.cpp
            
              5: //		Author  : eryar@163.com
            
              6: //		Date    : 2012-8-25 17:11
            
              7: //		Version : 0.1v
            
              8: //
            
              9: //	Description : Use Adjacency List data structure to store Digraph.
            
             10: //
            
             11: //==============================================================================
            
             12: 
            
             13: #include <vector>
            
             14: #include <string>
            
             15: #include <iostream>
            
             16: using namespace std;
            
             17: 
            
             18: struct SVertexNode
            
             19: {
            
             20:     bool          bIsVisited;
            
             21:     string        data;
            
             22:     vector<int> vecLoc;
            
             23: };
            
             24: 
            
             25: typedef struct SEdge
            
             26: {
            
             27:     int iInitialNode;
            
             28: 
            
             29:     int iTerminalNode;
            
             30: 
            
             31: }Edge;
            
             32: 
            
             33: typedef struct SGraph
            
             34: {
            
             35:     int iVertexNum;
            
             36:     int iEdgeNum;
            
             37:     vector<SVertexNode> vecVertex;
            
             38: }Graph;
            
             39: 
            
             40: ///////////////////////////////////////////////////////////////////////////////
            
             41: // Functions of Graph
            
             42: void    Initialize(Graph& g, int v);
            
             43: Edge    MakeEdge(int v, int w);
            
             44: void    InsertEdge(Graph& g, const Edge& e);
            
             45: void    ShowGraph(const Graph& g);
            
             46: 
            
             47: // Use Depth First Search method to Traverse the graph.
            
             48: void    DepthFirstSearch(Graph& g);
            
             49: void    DepthFirstSearch(Graph& g, int v);
            
             50: 
            
             51: ///////////////////////////////////////////////////////////////////////////////
            
             52: // Main function.
            
             53: 
            
             54: int main(int agrc, char* argv[])
            
             55: {
            
             56:     Graph   aGraph;
            
             57: 
            
             58:     // Initialize the graph.
            
             59:     Initialize(aGraph, 4);
            
             60: 
            
             61:     // Insert some edges to make graph.
            
             62:     InsertEdge(aGraph, MakeEdge(0, 1));
            
             63:     InsertEdge(aGraph, MakeEdge(0, 2));
            
             64:     InsertEdge(aGraph, MakeEdge(2, 3));
            
             65:     InsertEdge(aGraph, MakeEdge(3, 0));
            
             66: 
            
             67:     // Show the graph.
            
             68:     ShowGraph(aGraph);
            
             69: 
            
             70:     // DFS traverse the graph.
            
             71:     DepthFirstSearch(aGraph);
            
             72: 
            
             73:     return 0;
            
             74: }
            
             75: 
            
             76: ///////////////////////////////////////////////////////////////////////////////
            
             77: 
            
             78: /**
            
             79: * brief	Initialize the graph.
            
             80: *
            
             81: *       v: vertex number of the graph.
            
             82: */
            
             83: void Initialize( Graph& g, int v )
            
             84: {
            
             85:     char    szData[6];
            
             86:     SVertexNode node;
            
             87: 
            
             88:     g.iVertexNum    = v;
            
             89:     g.iEdgeNum      = 0;
            
             90: 
            
             91:     for (int i = 0; i < v; i++)
            
             92:     {
            
             93:         sprintf(szData, "V%d", i+1);
            
             94:         node.data   = szData;
            
             95:         node.bIsVisited = false;
            
             96:         g.vecVertex.push_back(node);
            
             97:     }
            
             98: }
            
             99: 
            
            100: /**
            
            101: * brief	Make an edge by initial node and terminal node.
            
            102: */
            
            103: Edge MakeEdge( int v, int w )
            
            104: {
            
            105:     Edge    e;
            
            106: 
            
            107:     e.iInitialNode  = v;
            
            108:     e.iTerminalNode = w;
            
            109: 
            
            110:     return e;
            
            111: }
            
            112: 
            
            113: /**
            
            114: * brief	Insert an edge to the graph.
            
            115: */
            
            116: void InsertEdge( Graph& g, const Edge& e )
            
            117: {
            
            118:     g.vecVertex.at(e.iInitialNode).vecLoc.push_back(e.iTerminalNode);
            
            119: 
            
            120:     // If the graph is Undigraph, need do something here...
            
            121:     //g.vecVertex.at(e.iTerminalNode).vecLoc.push_back(e.iInitialNode);
            
            122: 
            
            123:     g.iEdgeNum++;
            
            124: }
            
            125: 
            
            126: /**
            
            127: * brief	Show the graph.
            
            128: */
            
            129: void ShowGraph( const Graph& g )
            
            130: {
            
            131:     cout<<"Show the graph: "<<endl;
            
            132: 
            
            133:     for (int i = 0; i < g.iVertexNum; i++)
            
            134:     {
            
            135:         cout<<"Node "<<i<<"("<<g.vecVertex.at(i).data<<")";
            
            136: 
            
            137:         for (int j = 0; j < g.vecVertex.at(i).vecLoc.size(); j++)
            
            138:         {
            
            139:             cout<<"->"<<g.vecVertex.at(i).vecLoc.at(j);
            
            140:         }
            
            141: 
            
            142:         cout<<endl;
            
            143:     }
            
            144: }
            
            145: 
            
            146: void DepthFirstSearch( Graph& g )
            
            147: {
            
            148:     cout<<"Depth First Search the graph:"<<endl;
            
            149: 
            
            150:     for (int i = 0; i < g.iVertexNum; i++)
            
            151:     {
            
            152:         if (!(g.vecVertex.at(i).bIsVisited))
            
            153:         {
            
            154:             DepthFirstSearch(g, i);
            
            155:         }
            
            156:     }
            
            157: }
            
            158: 
            
            159: void DepthFirstSearch(Graph& g, int v)
            
            160: {
            
            161:     int     iAdjacent   = 0;
            
            162:     SVertexNode node    = g.vecVertex.at(v);
            
            163: 
            
            164:     // Visit the vertex and mark it.
            
            165:     cout<<g.vecVertex.at(v).data<<endl;
            
            166:     g.vecVertex.at(v).bIsVisited = true;
            
            167: 
            
            168:     // Visit the adjacent vertex.
            
            169:     for (int i = 0; i < node.vecLoc.size(); i++)
            
            170:     {
            
            171:         iAdjacent   = node.vecLoc.at(i);
            
            172: 
            
            173:         if (!(g.vecVertex.at(iAdjacent).bIsVisited))
            
            174:         {
            
            175:             DepthFirstSearch(g, iAdjacent);
            
            176:         }
            
            177:     }
            
            178: 
            
            179: }
            
            180: 
            
            181: 

            三、輸出結果

              1: Show the graph:
            
              2: Node 0(V1)->1->2
            
              3: Node 1(V2)
            
              4: Node 2(V3)->3
            
              5: Node 3(V4)->0
            
              6: Depth First Search the graph:
            
              7: V1
            
              8: V2
            
              9: V3
            
             10: V4
            AA级片免费看视频久久| 久久大香香蕉国产| 久久有码中文字幕| 国产成人无码精品久久久性色| 中文国产成人精品久久不卡| 国内精品伊人久久久久av一坑 | 狠狠久久亚洲欧美专区 | 久久精品免费网站网| 中文字幕无码久久久| 国产精品99久久久久久人| 久久AAAA片一区二区| 少妇内射兰兰久久| 一本大道久久东京热无码AV| 久久96国产精品久久久| 久久www免费人成看片| 久久国产高清一区二区三区| 久久精品国产久精国产果冻传媒| 国产成人久久久精品二区三区| 97精品依人久久久大香线蕉97 | 亚洲午夜久久久久妓女影院 | 亚洲午夜无码AV毛片久久| 久久精品中文无码资源站| 久久久亚洲AV波多野结衣| 亚洲欧美国产日韩综合久久| 久久国产精品-久久精品| 久久精品亚洲一区二区三区浴池 | 理论片午午伦夜理片久久| 日本精品久久久久中文字幕| 久久精品国产亚洲av日韩| 亚洲AV无码久久精品狠狠爱浪潮 | 青青青青久久精品国产h久久精品五福影院1421 | 久久夜色撩人精品国产| 国产成人精品久久综合| 91精品国产色综久久| 一级做a爰片久久毛片人呢| 国产精品狼人久久久久影院| 热re99久久精品国产99热| 7国产欧美日韩综合天堂中文久久久久 | 久久久久久精品久久久久| 日韩人妻无码一区二区三区久久99| 四虎久久影院|