The traversal algorithm of graphs
The graph uses adjacency table storage, where vertex nodes and edge nodes are as follows:
Vertex nodes [vername,adjacent] edge nodes [Veradj,cost,link]
Where Vername is the vertex v node name, adjacent is the address of its first adjacency vertex. The Veradj is the position of the node in the Head table, and link is the address of the next adjacent vertex of Vertex v.
Depth-First traversal
Set g= (v,e) is the figure, V (G) ={0,1,2...n-1}, the process of depth-first traversal of figure G is similar to the tree's first root traversal, whose process is:
First, access to the given starting vertex v0, from v0 to access one of its not visited adjacency Vertex v1, and then from the V1 to access its not visited the adjacent top bottom v2 ... This continues until a vertex is reached, and the vertex no longer has an unreachable adjacency vertex. It then goes back to the last visited vertex to see if it has an unreachable adjacency vertex. If so, the adjacency vertex is accessed and the aforementioned similar access is made from it. If not, then further backtracking. When all vertices are accessed, the entire depth-first traversal process is completed.
Recursive algorithm
The Depthfirstsearch (v,visited)//visited is an array that represents the access of each vertex, and the initial value of the visited array is 0.
DFSearch1. [Initialize]
Print (v).
Visited (v) =1.
P=adjacent (Head[v]).//adjacent () is the head pointer of the Benking that holds the vertex, and the vertex table name is head
DFSearch2. [Depth-first traversal]
While P!=null do
{IF Visited[p->veradj]!=1 Then
Depthfirstsearch (p->veradj,visited).
P=p->link.}.
_______________________________________________________________________________________________________________ _
Non-recursive algorithm
The depth-first traversal iterative algorithm of graphs uses a stack s to store the access process. When the vertex v enters the stack, Visited[v]=1, initially, the starting vertex v0 into the stack, its corresponding visited[v0]=1, the iterative process is as follows:
(1) Check whether the stack s is empty, if NULL then the iteration ends.
(2) POPs a vertex v from the stack and accesses v.
(3) Press the visited[i]=0 adjacent vertex of V into the stack and place its visited value at 1.
(4) Implementation steps (1)
DFS (head,v0,visited.).
DFS1. [Initialize]
For i==0 to N-1 do visited[i]=0.
Visited[v0]=1.
s<=v0.//pressing the initial vertex into the stack
dfs.2[non-recursive depth-first traversal]
While S is not empty do
{v<=s.//popup stack top element
PRINT (v).
P=p->adjacent (Head[v]).
While P!=null do
{IF visited[p->veradj]==0 Then
{s<= (P->VERADJ).
Visited[p->veradj]=1.
}
P=p->link.
}}.
_______________________________________________________________________________________________________________ _
Breadth-First traversal
The breadth-first traversal of graphs is similar to the hierarchical traversal of trees. Suppose a vertex v0, and in turn accesses each of the v0 neighboring points that have never been visited. The adjacent vertices are then accessed from each of these adjacency points, until all the points in the diagram have been accessed. The sequence of depth and breadth-first traversal of graphs is not unique, but is related to the order of adjacency table.
Recursive algorithm
Breadthfirstsearch (v,visited)//visited Array initial value is 0
BreadthFirstSearch1. [Initialize]
Print (v).
Visited (v) =1.
P=adjacent (Head[v]).//adjacent () is the head pointer of the Benking that holds the vertex, and the vertex table name is head
While P!=null do
{PRINT (P).
P=p->link.
}
BreadthFirstSearch2. [Recursive breadth-first traversal]
P=adjacent (Head[v]).
While P!=null do
{Breadthfirstsearch (p->veradj,visited).
P=p->link.
}
_______________________________________________________________________________________________________________ _
Non-recursive algorithm
Using queues
BFS (head,v0,visited)
BFS1. [Initialize]
For i==0 to N-1 do visited[i]=0.
Visited[v0]=1.
q<=v0.//to queue the initial vertex
bfs.2[non-recursive depth-first traversal]
While q is not empty do
{v<=q.//the team
PRINT (v).
P=p->adjacent (Head[v]).
While P!=null do
{IF visited[p->veradj]==0 Then
{q<= (P->VERADJ).
Visited[p->veradj]=1.
}
P=p->link.
}}.
The traversal algorithm of graphs