This paper also refers to the introduction of the algorithm and the algorithm.
I've never been able to understand depth-first search and breadth-first search, always afraid to touch them, but after reading the two books mentioned above, the enlightened, immediately can understand further.
1. Depth first Search 1.1 Maze Search
In the book of algorithms, the author writes a very good story. This story immediately made me understand the idea of depth-first search.
How do you find a way out of this maze, as shown in figure 1-1 ? Methods See Figure 1-2.
Fig. 1-1 Equivalent Maze model
An ancient way to explore the maze without getting lost (at least to the legends of Theseus and Minotaur) is called the Tremaux search , as shown in. To explore all the passages in the maze, we need:
1) Select an unmarked channel and lay a rope on the way you walked;
2) Mark all the intersections and passages that you have passed for the first time;
3) when coming to a marked junction, return to the last intersection with a rope;
4) Continue to roll back when the exit to the junction has no accessible channel.
The rope guarantees that you can always find a way out, and the markings guarantee that you will not pass the same route or the same junction two times.
Figure 1-2 Tremaux Explore
1.2 Depth-First search
The Java code given by the algorithm author is as follows:
1 Public classDepthfirstsearch2 {3 Private Boolean[] marked;4 Private intcount;5 6 PublicDepthfirstsearch (Graph G,ints)7 {8marked =New Boolean[G.V ()];9 DFS (G, s);Ten } One A Private voidDFS (Graph G,intv) - { -MARKED[V] =true; thecount++; - for(intW:g.adj (v)) - { - if(!Marked[w]) + { - DFS (G, w); + } A } at } - - Public BooleanMarkedintW) - { - returnMarked[w]; - } in - Public intcount () to { + returncount; - } the}
Dfs.java
Specific examples such as 1-3:
Figure 1-3 using a depth-first exploration trajectory to find all vertices with vertex 0 connectivity
1.3 Finding the path
The Java code given by the algorithm author is as follows:
1 Public classdepthfirstpaths2 {3 Private Boolean[] marked;//Have DFS () been called for this vertex?4 Private int[] Edgeto;//Last vertex in known path to this vertex5 Private Final intS//Source6 PublicDepthfirstpaths (Graph G,ints)7 {8marked =New Boolean[G.V ()];9Edgeto =New int[G.V ()];Ten This. S =s; One DFS (G, s); A } - - Private voidDFS (Graph G,intv) the { -MARKED[V] =true; - for(intW:g.adj (v)) - if(!Marked[w]) + { -EDGETO[W] =v; + DFS (G, w); A } at } - - Public BooleanHaspathto (intv) - { - returnMarked[v]; - } in - PublicIterable<integer> Pathto (intv) to { + if(!haspathto (v))return NULL; -stack<integer> Path =NewStack<integer>(); the for(intx = V; X! = s; x =Edgeto[x]) * Path.push (x); $ Path.push (s);Panax Notoginseng returnpath; - } the}
Dfs.java
Figure 1-4 Calculation trajectory of Pathto (5)
Figure 1-5 using a depth-first search trajectory to find all paths with a starting point of 0
2. Breadth First Search 2.1 Maze Search
Depth-First search is like a person walking a maze, breadth-first search seems to be a group of people walking in all directions in the maze, everyone has their own rope. When new forks are present, it can be assumed that an explorer can split into more people to search for them. When two explorers meet, they merge (and continue to use the first-arriving rope ). such as 2-1:
Figure 2-1 Breadth-first maze search
2.2 Breadth First Search
The Java code for using breadth-first search to find the path in the graph is given by the algorithm author as follows:
1 Public classbreadthfirstpaths2 {3 Private Boolean[] marked;//is a shortest path to this vertex known?4 Private int[] Edgeto;//Last vertex in known path to this vertex5 Private Final intS//Source6 7 PublicBreadthfirstpaths (Graph G,ints)8 {9marked =New Boolean[G.V ()];TenEdgeto =New int[G.V ()]; One This. S =s; A BFS (G, s); - } - the Private voidBFS (Graph G,ints) - { -queue<integer> queue =NewQueue<integer>(); -Marked[s] =true;//Mark the source +Queue.enqueue (s);//And put it on the queue. - while(!q.isempty ()) + { A intv = queue.dequeue ();//Remove next vertex from the queue. at for(intW:g.adj (v)) - if(!marked[w])//for every unmarked adjacent vertex, - { -EDGETO[W] = v;//save last edge on a shortest path, -MARKED[W] =true;//mark it because path is known, -Queue.enqueue (w);//and add it to the queue. in } - } to } + - Public BooleanHaspathto (intv) the { * returnMarked[v]; $ }Panax Notoginseng - PublicIterable<integer> Pathto (intv) the //same as for DFS. +}
Bfs.java
An example is as follows:
Figure 2-2 Using breadth-first search to find the results of all paths with a starting point of 0
Figure 2-3 using the breadth-first search trajectory to find all paths with a starting point of 0
3. Connected Components
The specific code is already hosted on GitHub.
"Diagram" of the introduction to algorithms: Depth-First search, Width-first search, and connected components