1. Recursive definition of depth first traversal
Suppose that the initial state of a given graph G is that none of the vertices have been accessed. If you select a vertex v in G as the initial starting point (the source point), the depth-first traversal can be defined as follows: first access to the starting point V, marked as visited, and then from V to search each adjacent point W of v. If W has not been visited, then the new starting point for W continues with the depth first traversal until all the vertices (also called vertices reachable from the source point) in the diagram with the source point v have been accessed. If there is still an unreachable vertex in the diagram, another vertex that has not yet been accessed is repeated as the new source point until all the vertices in the diagram have been accessed.
The depth-first traversal of a graph is similar to a tree's forward traversal. The search method is characterized by searching the depth direction as much as possible. This search method is called depth first search (Depth-first). Accordingly, traversing a graph with this method is naturally called the depth-first traversal of graphs.
2. Basic realization Idea:
(1) Access vertex v;
(2) Selecting a vertex w from the inaccessible adjacency point of V and proceeding with the depth first traversal from the W;
(3) Repeat the above two steps until all vertices in the diagram and V have paths communicated are accessed.
3. Pseudo-code
Recursive implementation
(1) Access vertex v;visited[v]=1;//algorithm before execution visited[n]=0
(2) The first adjacency point of w= Vertex v;
(3) while (W exists)
If (w is not accessed)
The algorithm is executed recursively from vertex W;
The next adjacency point of w= Vertex v;
Non-recursive implementation
(1) Stack s initialization; visited[n]=0;
(2) Access vertex v;visited[v]=1 vertex v into stack s
(3) while (stack S is not empty)
x= stack s top element (not out of stack);
If (exists and finds the adjacency point of an inaccessible x W)
Access to W;visited[w]=1;
W into the stack;
Else
x out stack;
Breadth-First traversal
1. Breadth-First traversal definition
The breadth-first traversal BFS algorithm is a layered search process, which is similar to the sequence traversal algorithm of a tree and requires a queue to maintain the sequence of traversed vertices in order to access the vertex's adjacent vertices in the order of the teams.
2. Basic realization Idea
(1) Vertex v into the queue.
(2) Continue execution when the queue is not empty, otherwise the algorithm ends.
(3) Out of the queue to obtain a team head point v; Access Vertex v and Mark Vertex v has been accessed.
(4) To find the first contiguous vertex of Vertex v Col.
(5) If the adjacency vertex of V Col is not visited, then col into the queue.
(6) To continue to look for another new vertex col node V, go to step (5).
Until all the unreachable adjacency points of Vertex v are processed. Go to step (2).
The breadth-first traversal graph is the vertex v as the starting point, from the near to the far, sequentially accesses and the V has the path to connect and the path length is 1, 2, ... The vertices. In order to be accessed by the adjacency point of the vertex accessed before the vertex, the vertex of the queue storage access must be set.
3. Pseudo-code
(1) initialization of queue q;visited[n]=0;
(2) Access vertex v;visited[v]=1, vertex v into queue q;
(3) while (queue Q is not empty)
v= Queue Q's head element out of the team;
The first adjacency point of w= Vertex v;
while (W exists)
If W is not accessed, access vertex w;
Visited[w]=1;
Vertex w into the queue q;
The next contiguous point of the w= Vertex v.
Turn from: http://www.cnblogs.com/biyeymyhjob/archive/2012/07/18/2596983.html
Recursive depth precedence traversal: divided into first order, middle sequence, sequential traversal
Pre-sequence Traversal void preordertraverse (bitree t) { if T { printf ("%c ", T->ch); preordertraverse (t->lchild); preordertraverse (t->rchild); } } /Central sequence traversal void inordertraverse (bitree t) { if (t) { inordertraverse, t >lchild); printf ("%c ", T->ch); inordertraverse (t->rchild); } } //sequence traversal void postordertraverse (bitree t) { if (T) { Postordertraverse (T->lchild); postordertraverse ( T->rchild); printf ("%c ", T->ch); } }
Non-recursive depth precedence traversal: The tree is traversed along the depth of the node, as deep as possible to search the branches of the tree. The first sequence traversal is as follows, the preface and the following