This is questions regarding bellman ford algorithm in graph . | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

This is questions regarding bellman ford algorithm in graph .

What if we use one variable as bool type ( let say check , marks it false in starting ) , if there will be any updation in distance then we mark it as true otherwise it will be false . Then if we achieve shortest path before n-1 times , then we know it. It will be optimised approch . https://code.sololearn.com/cFSz6HWorK3y/?ref=app

15th Dec 2022, 5:30 PM
Abhay mishra
Abhay mishra - avatar
1 Answer
0
Yes, this is an optimized approach to the Bellman-Ford algorithm. Instead of running the algorithm for n-1 times, you can check the boolean variable to determine if any changes have been made to the distances. If the boolean is still false after one iteration, then you know that no more updates can be made, and that you have found the shortest path.
11th Jan 2023, 9:49 PM
Aditya Dixit