Traversing Graph - Figure

Breadth-first traversal (BFS)

  • Process Analysis
    • Initialize a queue. A visit apex, the apex of the A team, after the team. Simultaneous access vertex B, F and A enqueue adjacent vertices
    • Access Node B, the Node B dequeued simultaneously with the adjacent node B, and has not been visited vertex C, I, G enqueue

    • Access node F, node F will be dequeued, while node F will be adjacent to, and have not been visited vertex G, E enqueue. After Similarly, until the queue is empty. The resulting sequence is a team traversal sequence
  • BFS Code
    • . 1  BOOL visited [MaxVertexNum];         // access tag array
       2  // adjacency matrix breadth traversal algorithm 
      . 3  void BFSTraverse (MGraph G)
       . 4  {
       . 5      Queue Q;     // subsidiary queue 
      . 6      for ( int I = 0 ; I <G. vexnum; I ++ )
       . 7      {
       . 8          visited [I] = to false ;     // access tag array initialization 
      . 9      }
       10      InitQueue (Q);     // initialize auxiliary queue 
      . 11      for ( int I = 0; I <G.vexnum; I ++)     // from 0 traversing the apex 
      12 is          IF (visited [I])!     // for each connected component called once the BFS 
      13 is              the BFS (G, I);     // V I unvisited over from V I start the BFS 
      14  }
       15  
      16  void the BFS (MGraph G, int V)
       . 17  {
       18 is      // starting from the vertex v, FIG first traversal G, the algorithm by means of an auxiliary queue Q 
      . 19      Visit (V);     // access node v 
      20 is      visited [v] = to true ;     // to make access v mark 
      21 is      Enqueue (Q, v);         // vertex v queues
      22 is      the while (! IsEmpty (Q))
       23 is      {
       24          the Dequeue (Q, v);         // vertex v dequeue 
      25          for ( int W = FirstNeighbor (G, v); W> = 0 ; W = NextNeighbor (G, v , W)) // detect all v adjacent point 
      26 is          {
       27              IF (visited [W])!     // W v has not visited for the adjacent node 
      28              {
       29                  visit (W);     // visits vertices W 
      30                  visited [w] = to true ;     // make access to vertex w marks 
      31                 Enqueue (Q, w);         // vertex w enqueue 
      32              }
       33          }
       34      }
       35 }

Depth-first traversal (DFS)

  • Analysis procedure: first accessing a start vertex in FIG v, then starting from v, v accessing and not adjacent to each vertex is visited W . 1 , and then accessing W . 1 according to any one of the abutment and has not been visited vertex W 2 , .... repeat the process. When can not continue to access, in turn returned to the vertex most recently accessed, if it has not been visited adjacent vertices, beginning from the apex above the search process continues until all the vertices of the figure are visited so far.
  • DFS Code
    • 1  / * depth-first traversal algorithm * / 
      2  BOOL visited [MaxVertexNum];         // access tag array 
      . 3  void DFSTraverse (MGraph G)
       . 4  {
       . 5      for ( int V = 0 ; V <G.vexnum; ++ V)
       . 6          visited [V] = to false ;         // initialize tag array access 
      . 7      for ( int V = 0 ; V <G.vexnum; V ++)     // from 0 traversing vertices 
      . 8      {
       . 9          IF (! visited [V])         // vertex v also asked visited, the access
      10              the DFS (G, V);
       . 11      }
       12 is  }
       13 is  
      14  void the DFS (MGraph G, int V)
       15  {
       16      // starting from the vertex v, thought recursive depth-first traversal of FIG 
      . 17      Visit (V);
       18 is      visited [ v] = to true ;
       . 19      for ( int W = FirstNeighbor (G, v); W> = 0 ; W = NextNeighbor (G, v, W)) // detect all v adjacent point 
      20 is          IF ! ( visited [W] )
       21 is          {
       22 is              the DFS (G, W);
       23 is          }
       24 }

Guess you like

Origin www.cnblogs.com/KBryant/p/11617232.html