深度优先搜索算法(Depth first search)
2011-03-11 15:57
309 查看
Depth-first search (DFS) for undirected graphs
Depth-first search, or DFS, is a way to traverse the graph. Initially it allows visiting vertices of the graph only, but there are hundreds of algorithms for graphs, which are based on DFS. Therefore, understanding the principles of depth-first search is quite important to move ahead into the graph theory. The principle of the algorithm is quite simple: to go forward (in depth) while there is such possibility, otherwise to backtrack.Algorithm
In DFS, each vertex has three possible colors representing its state:white: vertex is unvisited;
gray: vertex is in progress;
black: DFS has finished processing the vertex.
NB. For most algorithms boolean classification unvisited / visited is quite enough, but we show general case here.
Initially all vertices are white (unvisited). DFS starts in arbitrary vertex and runs as follows:
Mark vertex u as gray (visited).
For each edge (u, v), where u is white, run depth-first search for u recursively.
Mark vertex u as black and backtrack to the parent.
Example. Traverse a graph shown below, using DFS. Start from a vertex with number 1.
Source graph. | |
Mark a vertex 1 as gray. | |
There is an edge (1, 4) and a vertex 4 is unvisited. Go there. | |
Mark the vertex 4 as gray. | |
There is an edge (4, 2) and vertex a 2 is unvisited. Go there. | |
Mark the vertex 2 as gray. | |
There is an edge (2, 5) and a vertex 5 is unvisited. Go there. | |
Mark the vertex 5 as gray. | |
There is an edge (5, 3) and a vertex 3 is unvisited. Go there. | |
Mark the vertex 3 as gray. | |
There are no ways to go from the vertex 3. Mark it as black and backtrack to the vertex 5. | |
There is an edge (5, 4), but the vertex 4 is gray. | |
There are no ways to go from the vertex 5. Mark it as black and backtrack to the vertex 2. | |
There are no more edges, adjacent to vertex 2. Mark it as black and backtrack to the vertex 4. | |
There is an edge (4, 5), but the vertex 5 is black. | |
There are no more edges, adjacent to the vertex 4. Mark it as black and backtrack to the vertex 1. | |
There are no more edges, adjacent to the vertex 1. Mark it as black. DFS is over. |
If a graph is disconnected, DFS won't visit all of its vertices. For details, see finding connected components algorithm.
Complexity analysis
Assume that graph is connected. Depth-first search visits every vertex in the graph and checks every edge its edge. Therefore, DFS complexity is O(V + E). As it was mentioned before, if an adjacency matrix is used for a graph representation, then all edges, adjacent to a vertex can't be found efficiently, that results in O(V2) complexity. You can find strong proof of the DFS complexity issues in [1].Code snippets
In truth the implementation stated below gives no yields. You will fill an actual use of DFS in further tutorials.enum VertexState { White, Gray, Black }; … void Graph::DFS() { VertexState *state = new VertexState[vertexCount]; for (int i = 0; i < vertexCount; i++) state[i] = White; runDFS(0, state); delete [] state; } void Graph::runDFS(int u, VertexState state[]) { state[u] = Gray; for (int v = 0; v < vertexCount; v++) if (isEdge(u, v) && state[v] == White) runDFS(v, state); state[u] = Black; }
相关文章推荐
- PHP实现深度优先搜索算法(DFS,Depth First Search)详解
- [数据结构]深度优先搜索算法(Depth-First-Search,DFS)
- 深度优先搜索算法(DFS,Depth First Search)的PHP实现
- DFS --- Depth First Search 深度优先搜索算法
- 深度优先算法(depthFirstSearch,DFS)
- POJ2225Asteroids![Depth first search]
- Depth-First Search
- DepthFirstSearch & BreadthFirstSearch
- C++ Depth-First-Search
- 用栈实现深度搜索(Depth_first_search)
- 图——深度优先搜索(Graph - Depth First Search)
- Depth-First-Search(DFS伪代码)
- Depth-first Search -- Leetcode problem100. Same Tree
- 深度优先搜索(Depth-First-Search,DFS)
- DFS-深度优先搜索(Depth First Search)—1
- 无向图的Depth-first search和Breadth-first Search
- Depth-first Search -- Leetcode problem112. Path Sum
- 图的遍历——深度优先搜索(Depth First Search)
- (Basic algorithm学习笔记)《基础算法四》- 深度优先搜索(Depth First Search,DFS )
- 部分和问题(Depth-First-Search):