Shortest Path Algorithms
Shortest Path Algorithms
and Floyd-Warshall
Duke COMPSCI 309s
Siyang Chen
Spring 2014
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
General Graph Search
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
General Graph Search
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
General Graph Search
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
General Graph Search
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
General Graph Search
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
General Graph Search
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
General Graph Search
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
General Graph Search
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Dijkstra’s Algorithm
Given a graph G = (V , E ) where edges have nonnegative lengths,
and a source node s ∈ V , Dijkstra’s algorithm finds the shortest
path from s to every other node.
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Dijkstra’s Algorithm
Given a graph G = (V , E ) where edges have nonnegative lengths,
and a source node s ∈ V , Dijkstra’s algorithm finds the shortest
path from s to every other node.
A standard implementation of Dijkstra’s algorithm is the following:
▶ For all v ∈ V , dv ← ∞
▶ ds ← 0
▶ q.add(s)
▶ While q is not empty:
▶ v ← q.popFirst()
▶ For all neighbours u of v , such that dv + e(v , u) ≤ du :
▶ q.remove(u)
▶ du ← dv + e(v , u)
▶ q.add(u)
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Dijkstra’s Algorithm
Given a graph G = (V , E ) where edges have nonnegative lengths,
and a source node s ∈ V , Dijkstra’s algorithm finds the shortest
path from s to every other node.
A standard implementation of Dijkstra’s algorithm is the following:
▶ For all v ∈ V , dv ← ∞
▶ ds ← 0
▶ q.add(s)
▶ While q is not empty:
▶ v ← q.popFirst()
▶ For all neighbours u of v , such that dv + e(v , u) ≤ du :
▶ q.remove(u)
▶ du ← dv + e(v , u)
▶ q.add(u)
Runtime?
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Dijkstra’s Algorithm
Given a graph G = (V , E ) where edges have nonnegative lengths,
and a source node s ∈ V , Dijkstra’s algorithm finds the shortest
path from s to every other node.
A standard implementation of Dijkstra’s algorithm is the following:
▶ For all v ∈ V , dv ← ∞
▶ ds ← 0
▶ q.add(s)
▶ While q is not empty:
▶ v ← q.popFirst()
▶ For all neighbours u of v , such that dv + e(v , u) ≤ du :
▶ q.remove(u)
▶ du ← dv + e(v , u)
▶ q.add(u)
▶ For all v ∈ V , dv ← ∞
▶ ds ← 0
▶ q.add(s)
▶ While q is not empty:
▶ v ← q.popFirst()
▶ For all neighbours u of v , such that dv + e(v , u) ≤ du :
▶ q.remove(u)
▶ du ← dv + e(v , u)
▶ q.add(u)
In the case when the graph may have negative edges, we must use
the Bellman-Ford algorithm:
▶ For all v ∈ V , dv ← ∞
▶ ds ← 0
▶ For t ∈ {1, . . . , |V |}:
▶ For all edges (v , u):
▶ du ← min{du , dv + e(v , u)}
▶ If there is an edge (v , u) such that dv + e(v , u) < du :
▶ Throw NEGATIVE CYCLE FOUND
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Bellman-Ford
In the case when the graph may have negative edges, we must use
the Bellman-Ford algorithm:
▶ For all v ∈ V , dv ← ∞
▶ ds ← 0
▶ For t ∈ {1, . . . , |V |}:
▶ For all edges (v , u):
▶ du ← min{du , dv + e(v , u)}
▶ If there is an edge (v , u) such that dv + e(v , u) < du :
▶ Throw NEGATIVE CYCLE FOUND
Runtime?
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Bellman-Ford
In the case when the graph may have negative edges, we must use
the Bellman-Ford algorithm:
▶ For all v ∈ V , dv ← ∞
▶ ds ← 0
▶ For t ∈ {1, . . . , |V |}:
▶ For all edges (v , u):
▶ du ← min{du , dv + e(v , u)}
▶ If there is an edge (v , u) such that dv + e(v , u) < du :
▶ Throw NEGATIVE CYCLE FOUND
Runtime? O(|E | · |V |)
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Bellman-Ford
In the case when the graph may have negative edges, we must use
the Bellman-Ford algorithm:
▶ For all v ∈ V , dv ← ∞
▶ ds ← 0
▶ For t ∈ {1, . . . , |V |}:
▶ For all edges (v , u):
▶ du ← min{du , dv + e(v , u)}
▶ If there is an edge (v , u) such that dv + e(v , u) < du :
▶ Throw NEGATIVE CYCLE FOUND
Runtime? O(|E | · |V |)
One additional reason to use Bellman-Ford over Dijkstra is that it’s
much easier to implement.
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Floyd-Warshall
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Floyd-Warshall
Runtime?
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Floyd-Warshall
Runtime? O(|V |3 )
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Floyd-Warshall
Runtime? O(|V |3 )
Again, Floyd-Warshall is less efficient, but much easier to
implement than all-pairs Dijkstra.
. . . . . . . . . . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..