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
Sharktech Shark Data Center VPS 50% discount code...
Recently, Frost & Sullivan, a global authorit...
Recently, Riverbed Technology announced the launc...
[[395563]] The State Council Information Office h...
With the increase in data analysis, media traffic...
Apple's move to develop its own wireless comm...
A few days ago, we shared the information about D...
[[397144]] After many twists and turns, the numbe...
Before delving into the details of layer 3 switch...
Hello everyone, I am Bernie, an IT pre-sales engi...
Note: This article describes how to intelligently...
666clouds recently launched an event for New Year...
1. Classification of optical fiber Optical fibers...
The tribe shared information about Justg in Augus...
Kvmla is a Chinese hosting company founded in 201...