Figure - breadth-first traversal graphs

1, the front two Bowen realized adjacency matrix data structure implemented method of FIG adjacency lists and selects a suitable type of FIG under what circumstances? To compare the time complexity at an angle;

 

2, versus time complexity analysis:

 

       1, the adjacency matrix method is better performance, higher efficiency, and more concerned about the performance, select adjacency matrix;

       2. Matrix Method list on space usage better when tight environmental resources, memory is relatively small, select adjacency list method;

      

3, small conclusion:

       1, MatrixGraph rich memory resources suitable for the occasion (better performance);

       2, ListGraph suitable for memory resource-constrained occasions (to save space);

      

4, FIG traversal:

       1, one vertex from the graph, along the edges of some other vertices access figures, so that each vertex is accessed at most once;

       2. Note: Starting from a vertex is traversed not immediately have access to all the vertices in the graph;

              1, when the originating vertex when no adjacent vertices;

             

5, FIG traversal manner:

       1, breadth-first (Breadth First Search):

              1, in order to traverse the level of the binary tree traversal thoughts on map;

       2, depth-first (Depth First Search):

              1, the binary tree preorder traversal FIG thoughts;

             

6, breadth-first (BFS):

 

       1, breadth-first tree algorithm requires raw materials: the queue;

             

7, breadth-first algorithm:

       1, the raw material: class LinkQueue <T>;

       2, steps:

              1, is pressed into the starting vertex queue;

              2, the team point V eject head, has determined whether flag (flag: 2 switch, unlabeled: 3 rpm);

                     1, each vertex can be accessed only once, if the team found that the head has been marked point mark, it has been visited by a direct throw away;

              3, marking the apex, the apex and the adjacent vertices V pressed into the queue;

                     1, the operation has not visited vertices;

              4, it is determined whether the queue is empty (non-empty: 2 turn, empty: End);

 

8, breadth-first algorithm examples:

      

9, breadth-first algorithm flow chart:

 

       1, more than an array of markers tree, because the tree can not be a child node of this node's father or ancestors, but it can be a map, so to mark the array;

 

10, breadth-first algorithm:

1     / * breadth-first algorithm, the parameter i for the starting vertex numbers, the number of the figure that has the unique visibility; in a hierarchical manner access point; breadth-first and depth-first except that the only stacks or queues, storage queue using breadth-first adjacent vertices compared, using a depth compared with the adjacent vertices stack storage priority; in order to maintain the consistency of the interface, which modeled hierarchy tree traversal operation setting function interface * / 
2      SharedPointer <the Array < int >> the BFS ( int I)
 . 3      {
 . 4          DynamicArray < int > RET * = NULL;   // store the returned vertex numbers, the order to access the vertex numbers of the reaction 
. 5  
. 6          IF (( 0 <= I) && (I < VCount ()))
 . 7          {
 . 8              LinkQueue < int > Q;   // store map will likely be traversed nodes 
. 9              LinkQueue <int > R & lt;   // node after traversing FIG storage 
10              DynamicArray < BOOL > visited (VCount ()); // is stored whether vertices traversed labeled 
. 11  
12 is              / * initial value setting flag array * / 
13 is              for ( int I = 0 ; I <visited.length (); I ++ )
 14              {
 15                  visited [I] = to false ;
 16              }
 . 17  
18 is              q.add (I);   // added to the queue will be possible to access the node element 
19  
20              / * loop that runs through the beginning * / 
21              the while(Q.length ()> 0 )
 22 is              {
 23 is                  int V = q.front ();   // get vertex values head of the queue 
24                  q.remove ();   // out vertex 
25  
26 is                  IF (visited [! v])   // if it is being traversed 
27                  {
 28                      SharedPointer <the array < int >> AJ = getAdgacent (v);   // get the adjacent vertices and vertex v into an array of 
29  
30                      / * will be placed adjacent vertices queue * / 
31 is                      for ( int J = 0 ; J <AJ-> length (); J ++ )
32                      {
 33 is                          q.add ((* AJ) [J]);   // the adjacent vertices on the queue 
34 is                      }
 35  
36                      r.add (V);   // vertices go into the access queue 
37 [  
38 is                      visited [ V] = to true ;   // access flag to access 
39                  }
 40              }
 41 is  
42 is              RET = toArray (R & lt);   // queue into an array 
43 is          }
 44 is          the else 
45          {
 46 is             THROW_EXCEPTION(InvalidParameterException, "Index i is invalid ...");
47         }
48 
49         return ret;
50     }

 

11, breadth-first algorithm test code:

 1 #include <iostream>
 2 #include "MatrixGraph.h"
 3 
 4 using namespace std;
 5 using namespace DTLib;
 6 
 7 int main()
 8 {
 9     MatrixGraph<9, char, int> g;
10     const char* VD = "ABEDCGFHI";
11 
12     for(int i=0; i<9; i++)
 13 is      {
 14          g.setVertex (I, VD [I]);   // set values related to the vertex string of the content of VD 
15     }
 16  
. 17      g.setEdge ( 0 , . 1 , 0 );   // undirected FIG. special digraph, the adjacency matrix symmetry between each point, where a weight of 0, only concerned is connected, is not concerned with the weight 
18 is      g.setEdge ( . 1 , 0 , 0 );
 . 19      g.setEdge ( 0 , . 3 , 0 );
 20 is      g.setEdge ( . 3 , 0 , 0 );
 21 is     g.setEdge(0, 4, 0);
22     g.setEdge(4, 0, 0);
23     g.setEdge(1, 2, 0);
24     g.setEdge(2, 1, 0);
25     g.setEdge(1, 4, 0);
26     g.setEdge(4, 1, 0);
27     g.setEdge(2, 5, 0);
28     g.setEdge(5, 2, 0);
29     g.setEdge(3, 6, 0);
30     g.setEdge(6, 3, 0);
31     g.setEdge(4, 6, 0);
32     g.setEdge(6, 4, 0);
33     g.setEdge(6, 7, 0);
34     g.setEdge(7, 6, 0);
35     g.setEdge(7, 8, 0);
36    g.setEdge(8, 7, 0);
37 
38    SharedPointer< Array<int> > sa = g.BFS(0);
39 
40     for(int i=0; i<sa->length(); i++)
41     {
42         cout << (*sa)[i] << " ";
43     }
44     
45     return 0;
46 } 

 

12, reference numeral may represent the only figure is the apex of the flag, to make good use;

 

13, when the other parent is implemented, you can call the child class to achieve pure virtual function, because when the child class to create an object, all functions in the parent class are inherited to the subclass of them;

 

14 Summary:

       1, MatrixGraph applicable to resource-rich places;

       2, ListGraph applicable to resource-constrained occasions;

              1, time complexity is high;

       3, in accordance with breadth-first "layered approach" to designated access;

              1, the same level tree traversal;

       4, the core of breadth-first algorithm is to use a queue;

              1, is pressed into the adjacent vertexes queue;

Guess you like

Origin www.cnblogs.com/dishengAndziyu/p/10926424.html