4. teach you a thorough understanding of BFS and DFS Priority Search Algorithms
Author: July January 1, 2011
---------------------------------
My reference: Introduction to Algorithms
My statement: Personal Original, reprinted please indicate the source.
OK.
There are many articles on such BFS and DFS algorithms on the Internet. However, there is no such reason.
After reading this article, I think,
You will have a thorough understanding of the breadth-first search and depth-first search of graphs.
---------------------
We started with BFS:
First, let's take a look at the overview of this BFS breadth-first search algorithm in the introduction to algorithms.
Introduction to algorithms, second edition, Chinese translation, 324th pages.
Breadth-first search (BFS)
The Prime Minimum Spanning Tree Algorithm and Dijkstra single-source shortest path algorithm both adopt the same idea as the BFS algorithm.
// U for v's ancestors or parents.
BFS (G, s)
1 for each vertex uε V [G]-{s}
2 do color [u] ← WHITE
3 d [u] ← ∞
4 π [u] ← NIL
// Except for the Source Vertex s, line 1-4 sets each vertex to white and d [u] of each vertex u to infinity,
// Set the parent of each vertex to NIL.
5 color [s] gradient GRAY
// Set Source Vertex s to gray in Row 3, because the Source Vertex has been found at the beginning of the process.
6 d [s] defaults 0 // initialize d [s] to 0.
7 π [s] ← NIL // set the parent vertex of the Source Vertex to NIL.
8. Q: Why?
9 ENQUEUE (Q, s) // enter the queue
// Lines 8th and 9 initialize the queue Q so that it only contains the Source Vertex s.
10 while Q =ø
11 do u shortdequeue (Q) // leaves the queue
// Row 3: Determine the gray vertex u in the Q header of the queue header and remove it from Q.
12 for each v ε Adj [u] // for loop test each vertex v in the u's adjacent table
13 do if color [v] = WHITE
14 then color [v] GRAY // set it to GRAY
15 d [v] ← d [u] + 1 // The distance is set to d [u] + 1
16 π [v] ← u // u is the parent of the vertex.
17 ENQUEUE (Q, v) // insert to queue
18 color [u] BLACK // u is set to BLACK
The demonstration process of the "by" and "Link" is clear and easy to understand:
Traverse the demo address with breadth first:
Html> http://sjjg.js.zwu.edu.cn/SFXX/sf1/gdyxbl.html
Bytes -----------------------------------------------------------------------------------------------------------------
OK. Next, we will explain the deep Priority Search Algorithm in detail.
Deep prioritization algorithm DFS
// U for v's ancestors or parents.
DFS (G)
1 for each vertex uε V [G]
2 do color [u] ← WHITE
3 π [u] ← NIL
// In line 1-3, all vertices are set to white, and all π fields are initialized to NIL.
4 time counter 0 // reset time counter
5 for each vertex uε V [G]
6 do if color [u] = WHITE
7 then DFS-VISIT (u) // call the DFS-VISIT to access u, u becomes a new tree in the depth-first Forest
// Line 5-7, search the vertices in V in sequence, and call the DFS-VISIT to access the vertices when the white vertices are found.
// Each vertex u corresponds to a time point d [u] and a time point f [u].
DFS-VISIT)
1 color [u] starting with GRAY // u is found and set to white
2 time elapsed time + 1 // time Increments
3 d [u] <-time // record the time when u was found
4 for each v ε Adj [u] // check and access each adjacent point v of u
5 do if color [v] = WHITE // if v is WHITE, recursively access v.
6 then π [v] ← u // set u to v's ancestors
7 DFS-VISIT (v) // recursive depth, access neighbor node v
8 color [u] <-BLACK // u is set to BLACK, indicating that both the u and Its Adjacent contacts have been accessed.
9 f [u] processing time + 1 // The access completion time is recorded in f [u.
// Complete
Line 1-3, line 5-7 occupies the cycle time of O (V), which does not include the time when the DFS-VISIT was called.
For each vertex v (-V, the process DFS-VISIT is called in sequence only, because this process is called only for white vertices.
Line 4-7: the execution time is O (E ).
Therefore, the total execution time is O (V + E ).
The following link provides a demo system for deep Priority Search:
Figure depth-first traversal Demonstration System:
Http://sjjg.js.zwu.edu.cn/SFXX/sf1/sdyxbl.html
====================
Finally, let's look at the Recursive Implementation and non-Recursive Implementation of deep priority search.
1. Implement DFS recursion:
Void dftR (PGraphMatrix inGraph)
{
PVexType v;
AssertF (inGraph! = NULL, "in dftR, pass in inGraph is null ");
Printf ("= start of dft recursive version = ");
For (v = firstVertex (inGraph); v! = NULL; v = nextVertex (inGraph, v ))
If (v-> marked = 0)
DfsR (inGraph, v );
Printf ("= end of dft recursive version = ");
}
Void dfsR (PGraphMatrix inGraph, PVexType inV)
{
PVexType v1;
AssertF (inGraph! = NULL, "in dfsR, inGraph is null ");
AssertF (inV! = NULL, "in dfsR, inV is null ");
InV-> marked = 1;
Visit (inV );
For (v1 = firstAdjacent (inGraph, inV); v1! = NULL; v1 = nextAdjacent (inGraph, inV, v1 ))
// When v1 is the adjacent contact of v.