1, depth-first (DFS):
1, first select an edge to go, after a number of sides, and then choose a walk, to go up to infinity, rewind;
2, and then select another while walking;
3 features: as long as the current vertex can have side went above the other vertices, go deeper;
2, depth-first algorithm:
1, the raw material: class LinkStack <T>;
2, steps:
1, the starting vertex onto the stack;
2, pop stack vertex V, has determined whether the flag (flag: 2 switch, unlabeled: 3 rpm);
3, numerals vertex V, and pushed onto the stack adjacent vertices in the vertex V;
4, it is judged whether the stack is empty (non-empty: 2 turn, empty: End);
3, exemplary depth-first algorithm:
4, a flowchart of depth-first algorithm:
1, numerals, pressed into adjacent vertices;
5, depth-first algorithm:
1 / * achieve a function of depth-first algorithm; the core idea is visiting Binary Tree core idea (non-recursive version France) * / 2 SharedPointer <the Array < int >> the DFS ( int I) . 3 { . 4 DynamicArray < int > * RET = NULL; // return value is an array . 5 IF (( 0 <= I) && (I <VCount ())) // parameter legitimate . 6 { . 7 LinkStack < int > S; // stack to push the adjacent vertices . 8 LinkQueue < int > R & lt; // pressed queue traversal value . 9 DynamicArray <BOOL > visited (VCount ()); // access tag array 10 . 11 / * labeled initializes a value which access No * / 12 is for ( int J = 0 ; J <visited.length (); J ++ ) 13 is { 14 visited [J] = to false ; 15 } 16 . 17 s.push (I); // initial vertices onto the stack 18 is . 19 the while (s.size ()> 0 ) // there must continue apex 20 is { 21 is int s.top = V (); //The obtained element stack 22 is 23 is s.pop (); // pop the top element 24 25 IF (! Visited [V]) // vertex No access 26 is { 27 SharedPointer <the Array < int >> AJ getAdgacent = (V ); // all neighboring vertex v to give 28 29 / * all pushed onto the stack adjacent vertices v in * / 30 for ( int J = AJ-> length () - . 1 ; J> = 0 ; J, ) 31 is { 32 s.push ((* AJ) [J]); //The adjacent vertices individually pushed onto the stack 33 is } 34 is 35 r.add (v); // be pressed into the queue v 36 37 [ visited [v] = to true ; // will have access flag v 38 } 39 } 40 41 is = toArray RET (R & lt); // queue into an array 42 is } 43 is the else 44 is { 45 throw_exception (InvalidParameterException, " Index I ... iS invalid " ); 46 is } 47 48 return the right; 49 }
6, depth-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.DFS(0); 39 40 for(int i=0; i<sa->length(); i++) 41 { 42 cout << (*sa)[i] << " "; 43 } 44 45 return 0; 46 }
7, how to use the idea of visiting Binary Tree traversal diagram?
1, is visiting Binary Tree ideological depth-first idea used first traversal is recursive completed, depth-first recursive algorithm can be completed;
8, recursive depth-first method to achieve:
1, the problem is divided;
2. Define the function: DFS (graph, vex)
1, as a starting vertex vex Graph vertex depth-first traversal;
3, the function code for the function:
1 / * define recursive functions depth-first algorithm * / 2 Template <typename V, typename E> . 3 void the DFS (Graph <V, E> & G, int V, the Array < BOOL > & visited) . 4 { . 5 IF (( 0 <= V) && (V < g.vCount ())) . 6 { . 7 COUT V << << endl; // access is to print . 8 . 9 visited [V] = to true ; // set the access flag 10 . 11 SharedPointer <the Array < int >> = g.getAdgacent AJ (V); //Get adjacent vertices v 12 is 13 is / * v whether a neighbor vertices, some words recursive depth-first traversal algorithm * / 14 for ( int I = 0 ; I <AJ-> length (); I ++ ) 15 { 16 / * If no access, depth-first traversal proceeds * / . 17 IF (visited [(*! AJ) [I]]) 18 is { . 19 the DFS (G, (* AJ) [I], visited); // recursive call 20 is } 21 is } 22 is } 23 is the else 24 { 25 THROW_EXCEPTION(InvalidParameterException, "Index v is invalid ..."); 26 } 27 }
4, the depth-first code implementation:
1 / * recursive depth-first traversal FIG method, the first parameter is to be traversed FIG second parameter is the start vertex traversal * / 2 Template <typename V, E typename> . 3 void the DFS (Graph <V, E> G &, int V) . 4 { . 5 DynamicArray < BOOL > visited (g.vCount ()); // tag is vertices are visited . 6 . 7 / * for each vertex initial value is set to be accessed * / 8 for ( int I = 0 ; I <visited.length (); I ++ ) . 9 { 10 visited [I] = to false ; . 11 } 12 is 13 is DFS(g, v, visited); 14 }
9 Summary:
1, in accordance with the depth-first "preorder traversal of details" to access the vertices;
2, core depth-first algorithm is to use the stack;
3, breadth-first and depth-first except that only the stack or queue;
4, the depth-first algorithm may be implemented using a recursive manner;
1, using the depth-first stack to complete and is thought preorder traversal, consider using a recursive manner to achieve;
2, when implemented with a stack, consider using recursion;