1. Problem description and understanding
The strategy followed by depth-first Search,dfs, as its name Depth, is to search as "deeper" in the diagram. In depth-first search, the newly discovered vertex v is explored if the vertex is not explored from its starting edge. When all the edges of V have been explored, search for "backtracking" to the vertex from which Vertex v is found. This process continues until all vertices that are from the source point are found. If there are no vertices found in the figure, repeat the search with one of the new source points until all the vertices are discovered. is slightly different from the source vertex specified in the BFS. DFS Search Trails G π Will form a forest-depth priority forest.
In the depth-first search process, you track two times for each vertex: Discovery time d[u] and finish Time F [u]. D[u] Record First discovery (U changed from white to gray) moment, F [u] records completion of V adjacency table detection (turn black) moment.
Input: Figure G=<V,E> 。
Output: G's depth priority forest G π and the discovery and completion times of each vertex in the search process.
- Algorithm pseudo-code
DFS (G)1 for eachVertex u? V[G]2 DoColor[u]←white3? [U]←nil4 Time←05S←?6 for eachVertex s? V[G]7 Do ifColor[s] = White8 ThenColor[s]←gray9d[s]← Time← Time+1TenPUSH (s, s) One whileS≠? A DoU←top (S) - if? v? Adj[u] andCOLOR[V] = White - ThenColor[v]←gray the? [V]←u -d[v]← Time← Time+1 -PUSH (S, v) - ElseColor[u]←black +f [u]← Time← Time+1 -POP (S) + returnDf and?
The process of Dfs applying to a forward graph
3. Program implementation
/************************************ @file: graph.h*@ brif: Algorithm implementation of adjacency table of graph *@ author:sf* @data: 20150704* @version 1.0 *************************************/#ifndef _graph_h#define _GRAPH_H#include <list>using namespace STD;structVertex//adjacency table node structure{DoubleWeight//Edge weights intIndex//adjacency vertex};classgraph{ Public: list<vertex>*adj;//adjacency table array intN//Vertex countGraph (Double*a,intn); ~graph ();};#endif //_graph_h
#include "stdafx.h"#include "Graph.h"Graph::graph (float*a,intN): N (N)//a is the adjacency matrix of graphs{adj =New list<vertex>[n]; for(inti =0; I < n;i++)//For each vertex i for(intj =0; J < n;j++)if(a[i*n+j]!=0.0{Vertex node = {a[i*n + j], J};//a[i,j]=weight Edge Weight J, adjacency node numberAdj[i].push_back (node); }}graph::~graph () {Delete[] adj; adj = NULL;}
#ifndef _dfs_h#define _DFS_H/************************************ @file:d fs.h*@ brif: Depth-First search algorithm implementation *@ author:sf* @data: 20150708* @version 1.0*************************************/#include "Graph.h"#include <stack>struct parameter3{int* first;int* second;int* Third;};/************************************ @function:d fs*@ brif: Graph of adjacency table diagram of Depth-first search (Depth, DFS) algorithm implementation *@ input PARAM:G graph adjacency table *@ output PARAM:PI g depth priority forest D: Discovery Time F: Finish time *@ author:sf* @data: 20150708* @version 1.0*************************************/parameter3 DFS (const graph& g);#endif
#include "dfs.h"enumVertex_color{white,gray,black};typedef enumVertex_color color; Parameter3 DFS (Constgraph& g) {intn = g.n, u, V, S; Color *color =NewColor[n];int*pi =New int[n], *d =New int[n], *f =New int[n], time =0; Fill (pi, pi + N,-1); Fill (color, color + N, white); Stack<int>S//Stack list<vertex>:: Iterator *pos =New list<vertex>:: Iterator[n]; for(U =0; U < n; ++u) Pos[u] = G.adj->begin (); for(s =0; s < n;++s) {if(Color[s]==white)//Create a depth-first tree with vertex s as root{Color[s] = GRAY; D[s] = ++time; S.push (s); while(! S.empty ()) {u = s = S.top (); list<vertex>:: iterator P; p = Pos[u]; while(G.adj[n].end ()!=p)//The current program has some problems in accessing this end of the iterator is an error that has not yet been resolved{v = (*p). Index;if(Color[v] = = white) Break;Else++p;find the unknown point in the adjacency point of//u} Pos[u] = p;if(Pos[u]! = G.adj[n].end ())//Find a white vertex and press it into the stack{Color[v] = GRAY; D[V] = ++time; PI[V] = u; S.push (v); }Else//otherwise complete access to U{Color[u] = BLACK; F[u] = ++time; S.pop (); Pos[u] = G.adj[n].begin (); } } } }Delete[]color;Delete[]pos; Parameter3 Reparams; Reparams.first = PI; Reparams.second = D; Reparams.third = f;returnReparams;}
//DFS.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include "dfs.h"#include "Graph.h"#include <iostream>using namespace STD;int_tmain (intARGC, _tchar* argv[]) {ints =1, n =8; PARAMETER3 ret;DoubleA[] = {0,1,0,0,1,1,1,0,0,0,0,1,0,0,0,1,0,1,0,0,1,0,0,1,1,0,0,0,0,1,1,1,1,1,1,0}; Graph g (A,6); ret = DFS (g); for(inti =0; I <6; ++i) {if(i! = s) {cout<< I <<";"<<"Parent"<< Ret.first[i];cout<<"Discover/finish:"<< ret.second[i]<<"/"<<ret.third[i] << Endl; }} System ("Pause");return(exit_success);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Depth-First search algorithm for graphs DFS