I am writing a method in Java to find the shortest path between two nodes in a graph. The parameters are the following
I have written the following code:
public static String shortestDistance(List<edge> edges, int index1, int index2) {
String shortest = "";
for (int i = 0; i < edges.size(); i++) {
edge e = edges.get(i);
if (e.src == index1) {
//shortest path here
shortest = shortest + e.src + ", ";
}
}
return shortest;
}
My goal is to return a string containing a list of the shortest possible path. How do I begin to implement an algorithm to search for the shortest path between the two indexes?
You can use Breadth First Search to find the shortest path from one node to another in a graph.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.