This article is reprinted from the WeChat public account "Beta Learns JAVA", the author is Silently9527. Please contact the Beta Learns JAVA public account to reprint this article. PrefaceIn the previous two articles, we used depth-first search to find a path from vertex v to vertex w in the graph. However, depth-first search has a lot to do with vertex input, and the path found is not necessarily the shortest. Usually, we often need to find the shortest path in the graph, such as map function. Here we need to use the breadth-first search algorithm. Breadth-First SearchStill use the previously defined path finding API
In breadth-first search, in order to find the shortest path, we need to traverse all vertices in the order of the starting point, instead of using recursion to achieve it; the idea of the algorithm is:
In this algorithm, in order to save the path, we still need to use an edge array edgeTo[] and use a parent link tree to represent the shortest path from the root node to all connected vertices.
Take the following figure as a column to see the running track of breadth-first search Unit test code:
The execution results are consistent with the running trajectory we analyzed Symbol diagramThe graph algorithms that we have learned in recent articles all use numbers as vertices because it is very simple and convenient to implement these algorithms with numbers. However, in actual scenarios, characters are usually used as vertices instead of numbers, such as: locations on a map, and the relationship between movies and actors. In order to meet the actual scenario, we only need to make a mapping between numbers and strings. At this time, we will think of the map implemented in the previous article (map is implemented through binary tree, red-black tree, hash table, etc., interested students can check it out), and use Map to maintain the mapping relationship between strings and numbers.
Implementation ideas:
The actual process can be determined according to the specific situation. It does not necessarily have to be this kind of string. It can come from a database or a network request. The code is implemented as follows:
All source code in this article has been put into the github repository: https://github.com/silently9527/JavaCore |
<<: 5 Common SD-WAN Challenges and How to Address Them
>>: How to determine whether the protocol is Websocket in Http Header
As connected technology continues to advance, bus...
To fully understand the network and its capabilit...
[[439238]] You are immersed in watching TV series...
[[320474]] According to the latest news from fore...
[[182163]] On January 17, the Ministry of Industr...
I don’t know when it started, but when people men...
[[266702]] Interviewer: What are the ways for par...
The so-called change of perspective is to start f...
Sharktech is a computer room that focuses on high...
The arrival of the New Year is always exciting, e...
Not long ago, an online experience store with &qu...
1. Basic Introduction In the IO flow network mode...
At 10 a.m. on March 23, 2020, the Joint Preventio...
The key technologies of the Internet of Things in...
[[337542]] On August 11, 2020, the DevRun Develop...