Preface
This article does not have a very detailed algorithm verification process. The introduction provides a detailed proof process. This article only describes the concept of "breadth-first and depth-first searches and some simple applications ".
The Traversal Algorithms of the two graphs are applied in other graph algorithms and are basic graph algorithms.
Extended search
The breadth-first search (BFS) can be described as "simple taste". Specifically, each vertex only accesses its adjacent nodes (if its adjacent nodes are not accessed) record the adjacent node and end the access to the vertex after accessing the adjacent node.
Breadth First uses the "first-in-first-out" queue, which stores the first detected node for next processing, we ignore it-we do not put it into the queue because the node we found again:
- The process is complete;
- Or not processed in the queue.
Algorithm Guide Wheel uses a clever method for both searches. It uses white to mark undiscovered nodes and gray to mark the first detected node, black black is used to mark the second detected node.
So there is:
?
Code sample
| 010203040506070809101112131415 |
BFS(G,s) for each vertex v in V[G] status[v] = WHITE /****** Other initialization ******/ status[s] = GRAY // S is the origin queue q Join (Q, S ); while Q is not empty T = departure (Q ); for each vertex v in Adj[t] // Vertex adjacent to T if status[v] = WHITE // Only unaccessed operations status[v] = GRAY // Mark as the first access /****** Other operations ******/ Join (Q, V) status[t] = BLACK // This point has been processed |
The Guide Wheel also adds the access length and parent node operations to the "Others" of the above pseudo code. This calculates the minimum number of steps from the source point to other vertex paths and their specific paths.
A simple application of breadth-first search:
If there is a problem, every village is connected over the bridge. First, let's give a map of the village and ask how many bridges do village a and village B need at least? This problem can easily be converted to the above BFS problem.
Deep Priority Search
Deep Priority Search (DFS) can be described as "Breaking the sandpot to the end" in the image. Specifically, after accessing a vertex, I access its next adjacent vertex, until the current vertex is accessed or does not have any adjacent vertex.
Similarly, the introduction to algorithms uses "smart practices" and three colors to mark three states. However, these three statuses are different from the extended preference search:
- White unaccessed Vertex
- Gray: the vertex on a deep search path, that is, when it is discovered
- Black: After all the adjacent vertices of This vertex are accessed, the access vertex ends.
?
Code sample
| 010203040506070809101112131415 |
DFS(G,s) for each vertex v in V(G) status[v] = WHITE /****** Other initialization ******/ for each vertex v in V(G) if(status[v]==WHITE) DFS-VISIT(v) DFS-VISIT(v) status[v] = GRAY for each vertex t in Adj(v) if status[t] = WHITE DFS-VISIT(t) /****** Other operations ******/ status[v] = BLACK |
By adding a timestamp to each vertex during the DFS search process, you can achieve topological sorting. Topology Sorting needs:
Each vertex has two timestamps, which are defined as follows:
- When a vertex is discovered, mark the first timestamp of the vertex;
- Mark the second timestamp of the vertex at the end of its access. The timestamp can be marked with a simple 123456 mark, as long as the size can be distinguished.
Therefore, you will find that the earlier the point is found, the smaller the first timestamp, but the larger the second timestamp. Summary
Both algorithms are O (V + E) and are selected as needed. When using the white gray and black logo, I suddenly understood how to use deep priority search to determine whether a ring exists in the directed graph.
Reproduced http://daoluan.net/blog/dfs-and-bfs/