Dijkistra Algo and Bellman-Ford Algorithm
Dijkistra Algo and Bellman-Ford Algorithm
DIJKSTRA’S ALGORITHM
BELLMAN-FORD ALGORITHM
Pankaj kumar
212211009
Index
• PART 1 : History
• PART 2 : Introduction – Dijkstra’s Algorithm
– Pseudocode
– Run Dijkstra’s Algorithm on a Graph
– Problem with Dijkstra’s Algorithm- Negative weight Edge
– Negative weight Edge in real life
• PART 5 : Application
• PART 6 : References
2
History
• What is the shortest way to travel from Rotterdam to Groningen, in general: from given city to given city.
It is the algorithm for the shortest path, which I designed in about twenty minutes. One morning I was shopping in
Amsterdam with my young fiancée, and tired, we sat down on the café terrace to drink a cup of coffee and I was just
thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a twenty-
minute invention. In fact, it was published in '59, three years later. The publication is still readable, it is, in fact, quite
nice. One of the reasons that it is so nice was that I designed it without pencil and paper. I learned later that one of the
advantages of designing without pencil and paper is that you are almost forced to avoid all avoidable complexities.
Eventually, that algorithm became to my great amazement, one of the cornerstones of my fame.
— Edsger Dijkstra, in an interview with Philip L. Frana, Communications of the ACM, 2001
3
Introduction – Dijkstra’s Algorithm
• Dijkstra's algorithm is an algorithm for finding the shortest path between
nodes in a graph, which may represent, for example, road network.
• The algorithm exists in many variants. Dijkstra's original algorithm found the
shortest path between two given nodes, but a more common variant fixes a
single node as the "source" node and finds shortest paths from the source to all
other nodes in the graph, producing a shortest-path tree.
• For a given source node in the graph, the algorithm finds the shortest path
between that node and every other. It can also be used for finding the shortest
paths from a single node to a single destination node by stopping the algorithm
once the shortest path to the destination node has been determined.
4
Pseudocode for Dijkstra’s Algorithms
5
Run Dijkstra’s algorithm on a Graph
• Q = {1,2,3,4,5,6}
• Prev = {×, ×, ×, ×, ×, ×}
• Dist = {∞, ∞, ∞, ∞, ∞, ∞}
• Condition for update: dist[u]+Graph.Edges(u,v)< dist[v]
• Time Complexity: O(V+ElogV)
Q Dist Prev
2,3,4,5,6 0, ∞, ∞, ∞, ∞, ∞ 1, ×, ×, ×, ×, ×
2,3,4,5,6 0, 7, 9, ∞, ∞, 14 1, 1, 1, ×, ×, 1
3,4,5,6 0, 7, 9, 22, ∞, 14 1, 1, 1, 2, ×, 1
4,5,6 0, 7, 9, 20, ∞, 11 1, 1, 1, 3, ×, 3
4,5 0, 7, 9, 20, 20, 11 1, 1, 1, 3, 6, 3
5 0, 7, 9, 20, 20, 11 1, 1, 1, 3, 6, 3
0, 7, 9, 20, 20, 11 1, 1, 1, 3, 6, 3
6
Problem with Dijkstra’s Algorithm -Negative weight Edge
Dist = {0,10,2}
7
Negative weight Edge in Real life
• Negative weight edges might seem useless at first but they can
explain a lot of phenomena like cashflow, the heat released/absorbed
in a chemical reaction, etc.
• For instance, if there are different ways to reach from one chemical
A to another chemical B, each method will have sub-reactions
involving both heat dissipation and absorption.
• If we want to find the set of reactions where minimum energy is
required, then we will need to be able to factor in the heat absorption
as negative weights and heat dissipation as positive weights.
8
Bellman-Ford Algorithm
• Bellman Ford algorithm helps us find the shortest path from a vertex
to all other vertices of a weighted graph.
• It is similar to Dijkstra's algorithm but it can work with graphs in
which edges can have negative weights.
• It is more time consuming than Dijkstra’s algorithm.
• Dynamic Programming Approach is taken to implement the
algorithm.
• It is able to detect negative edge cycle.
9
Pseudo Code for Bellman-Ford Algorithm
10
Run Bellman-Ford Algorithm on a Graph
• Vertices = {A,B,C}, Edges = {(A,B),(B,C),
(C,B)}
• Predecessor = {×, ×, ×}
• Distance = {∞, ∞, ∞, }
• Condition for update: dist[u]+weight(u,v)<
dist[v]
• Time Complexity: O(VE)
11
Application
12
References
• Dijkstra, E. W. (1959). A note on two problems in connexion with
graphs. Numerische mathematik, 1(1), 269-271.
• Bellman, R. (1958). On a routing problem. Quarterly of applied
mathematics, 16(1), 87-90.
• Wang, H., Yu, Y., & Yuan, Q. (2011, July). Application of Dijkstra
algorithm in robot path-planning. In 2011 second international
conference on mechanic automation and control engineering (pp. 1067-
1069). IEEE.
• Cavendish, D., & Gerla, M. (1998, September). Internet QoS routing
using the Bellman-Ford algorithm. In International Conference on High
Performance Networking (pp. 627-646). Springer, Boston, MA.
13
14