Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest path between two given nodes in unweighted graph/tree

I am looking for an algorithm to determine the shortest path between two nodes in an unweighted graph by using the adjacency matrix . I know Dijkstra and Bellman - Ford, but none that found are specific to determine the shortest path between two given nodes.

Any help is greatly apreciated

like image 515
Dorin Rusu Avatar asked Jan 18 '26 18:01

Dorin Rusu


1 Answers

One simple option is to run a breadth-first search starting at the first node until the second node is found. If you store parent pointers for each node, you can then read off the path from the first node to the second. Moreover, this runs in linear time in the size of the graph.

Hope this helps!

like image 101
templatetypedef Avatar answered Jan 20 '26 18:01

templatetypedef