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
Preface The exploration of DCI technology has bee...
Now that we are working from home due to the pand...
[[381381]] As we recover from Covid-19, we have a...
Any time a network service outage occurs, it can ...
An overlay network is one or more virtual logical...
[51CTO.com original article] The application of i...
The software-defined wide area network (SD-WAN) m...
[[255972]] If you use the popular file explorer a...
2019 has come to an end, and the annual flagships...
AlphaVPS is a foreign hosting company established...
I received the latest promotional email from Host...
On December 3, the highly anticipated 2019 Micros...
Now everyone is talking about 5G, just like when ...
[[392156]] The launch of the digital RMB will def...
"Industrial Internet" has been written ...