Java programming realizes the complete code of depth first search and breadth first search based on graph
To understand the 15puzzle problem, we learned about depth first search and breadth first search. Let's talk about depth first search (DFS). The purpose of depth first search is to first search the paths farthest from the starting vertex, while breadth first search is to first search the paths closest to the starting vertex. I wonder what's the difference between deep first search and backtracking? Baidu once said that backtracking is a kind of deep search. The difference is that backtracking does not retain the search tree. What about breadth first search (BFS)? What are its applications? A: the shortest path, wine distribution, eight digital problems, etc. To get back to business, here the author uses Java to simply realize the wide search and deep search. Deep search is implemented by graph + stack, and wide search is implemented by graph + queue. The code is as follows:
1. Create a new nodirectiongraph representing the "undirected graph" class
2. Then there is an array simulated stack arraystack
3. In a chainqueue simulated by a linked list
4. Test_ BFS_ DFS
The test diagram structure here is as follows:
@H_ 404_ 38@
The operation results are as follows:
Here we need to explain the operation results of the above deep search and wide search, in which 0, 1, 2, 3... Correspond to a, B, C, D It's a little windy. Excuse me ~ ~ o~~~
summary
The above is the whole content of this paper on Java programming to realize graph based depth first search and breadth first search for complete code. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!