• <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

            深度優(yōu)先遍歷用鄰接表表示的圖

            DFS the Adjacency List Graph

            eryar@163.com

            一、簡介

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

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

             

            二、實(shí)現(xiàn)代碼

            依然使用鄰接表來表示圖的存儲結(jié)構(gòu),深度優(yōu)先遍歷程序如下代碼所示:

              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: 

            三、輸出結(jié)果

              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

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久午夜免费视频| 久久久久99精品成人片欧美| 国产毛片久久久久久国产毛片| 久久久久噜噜噜亚洲熟女综合| 狠狠综合久久综合88亚洲| 久久久91精品国产一区二区三区| 久久无码精品一区二区三区| 久久久亚洲欧洲日产国码二区| 岛国搬运www久久| 亚洲精品无码久久一线| 久久精品无码一区二区三区免费| 亚洲精品美女久久777777| 久久国产免费直播| 久久国产精品77777| 一本色道久久88综合日韩精品 | 久久婷婷激情综合色综合俺也去| 一本久久久久久久| 久久久无码人妻精品无码| 亚洲欧美成人久久综合中文网| 97久久久精品综合88久久| 久久天天躁狠狠躁夜夜躁2014| 国产精品成人99久久久久| 国内精品久久久人妻中文字幕| 亚洲精品乱码久久久久久不卡| 国产成人久久精品二区三区| 久久成人国产精品二三区| 久久久久99精品成人片试看| 久久精品国产99国产精品导航| 欧美性大战久久久久久| 久久99亚洲综合精品首页| 一本伊大人香蕉久久网手机| 国产人久久人人人人爽 | 99久久精品免费观看国产| 久久99热只有频精品8| 欧美牲交A欧牲交aⅴ久久| 2020久久精品亚洲热综合一本| 亚洲国产成人久久综合区| 看全色黄大色大片免费久久久| 久久99精品久久久久久秒播| 久久91精品综合国产首页| 久久久久国产|