Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dijkstra's algorithm when all edges have same weight

Tags:

dijkstra

If all edges had the same weight in a given graph, will Dijkstra's algorithm still find the shortest path between 2 vertices? Thanks!

like image 204
sinou Avatar asked Jul 21 '26 04:07

sinou


2 Answers

Yes dijkstra algorithm can find the shortest path even when all edges have the same weight. dijkstra has time complexity O((V+E)logV).Instead you should choose BFS algorithm to do the same thing,because BFS has time complexity O(V+E),so BFS is asymptotically faster than dijkstra.

like image 185
tanmoy Avatar answered Jul 22 '26 18:07

tanmoy


Yes it would, But you might want to take a look at Breadth-first search, wich solves the case you are refering to. To find the path, you can make a recursive function that starts in the destiny node with flagged distance n, and moves to one of the neightbour nodes with flagged distance n-1

like image 35
germangb Avatar answered Jul 22 '26 19:07

germangb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!