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

            一、簡(jiǎn)介

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

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

             

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

            依然使用鄰接表來(lái)表示圖的存儲(chǔ)結(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

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


            一级做a爱片久久毛片| 99久久国产综合精品女同图片| 久久99国产精品二区不卡| 久久婷婷综合中文字幕| 欧美一级久久久久久久大片| AV无码久久久久不卡蜜桃| 国内精品久久久久| 一本久久精品一区二区| 久久久精品午夜免费不卡| 亚洲v国产v天堂a无码久久| 亚洲午夜久久久久久久久久| 好久久免费视频高清| 女人高潮久久久叫人喷水| 国产精品一久久香蕉国产线看| 久久人人爽人爽人人爽av| 国产精品美女久久久m| 色综合久久夜色精品国产| 国产高潮国产高潮久久久91| 中文字幕日本人妻久久久免费| 久久国产成人亚洲精品影院| 99精品久久精品| 影音先锋女人AV鲁色资源网久久| 国产亚州精品女人久久久久久 | 99久久无色码中文字幕| 欧美成人免费观看久久| 久久99精品久久久久久不卡| 国产精品99久久精品| 人妻精品久久无码专区精东影业| 日韩久久久久中文字幕人妻 | 99久久99久久精品国产片果冻| 94久久国产乱子伦精品免费| 久久国产高潮流白浆免费观看| 成人久久免费网站| 亚洲国产精品成人久久| 99久久国产宗和精品1上映 | 久久久久无码精品国产不卡| 一本色道久久综合狠狠躁| 亚洲午夜久久久久妓女影院 | 国产成人精品久久综合| 色噜噜狠狠先锋影音久久| 久久久久夜夜夜精品国产|