Shortest Path
Shortest Path
Shortest Paths
An Introduction to Routing Problems
Week 10 / Lecture 2
Franck Chauvel
axbit & NTNU Go to www.menti.com and use the
[email protected] code 1147 7932
Weighted Graphs
220 km
Modle Trondheim
82 km 154 km
Røros
Ålesund What is the shortest path from
260 km
Hamar to Modle?
425 km 290 km
400 km
Oslo
Agenda
• Weighted Graphs
• Bellman-Ford Algorithm
• All pairs
• Floyd-Warshall algorithm
• Routing Problems
3
Weighted Graphs
weight function
graph
𝐺=(𝑉 , 𝐸 , 𝑤)
Set of vertices Set of edges
(nodes) (links)
𝐸 ∈𝑉 ×𝑉
4
With Adjacency List
public class Graph<T> {
public Graph() {
this.vertices = new HashMap<T, HashMap<T, Edge<T>>>();
}
// …
5
Breadth-first Search 4 edges
3 edges
2 edges
6
Dijkstra’s Algorithm
When labels are not only “1”s…
7
Tracking Weights
To MCW
• Minimum Cumulative Weight Bergen ∞
(MCW)
Hamar ∞
Modle ∞
• Initially:
• every other vertex is infinity Oppdal ∞
400
• source has 0 source Oslo 0
Røros ∞
• Update: Edge S to T Trondheim ∞
Ålesund ∞
8
Traversal Strategy
visited
vertices 𝒘𝒂 𝒘 𝒙 ≥ 𝒘 𝒂 ⇒ 𝒘 𝒙 +𝒘 𝒙 → 𝒂 >𝒘 𝒂
a 𝒂
• Prioritize the most
unknown
promizing “unvisited” x vertex
node so far s 𝒘𝒙
• Update only links to new 𝒚
vertices source
9
How to Store Paths?
To From
Modle Ålesund
Røros
Ålesund
Oppdal Oslo
Oslo
Trondheim Modle
Ålesund Oppdal
Oslo
10
Dijsktra’s Algorithm
Minimum Total
From
220 km Weight
Modle Trondheim Bergen ∞
1115 Ålesund
12
Takeaway
Dijkstra’s Algorithm
computes the shortest paths
to all other vertices!
13
What about Circuits? A
4
1 B
• Circuits yield an infinity of paths
C 5
• Circuits are irrelevant 2
• if weights are positive
D
14
Negative Weights
MCW From
-4 1 Vertex A 0
C B E
Vertex B ∞2
-1 A
C No propagation!
3 2
-5
1 Vertex C -1
∞3 A
D
4
D A Vertex D 4
∞ A
Vertex E 1
∞ A
15
Propagation
• Worst Case:
• Chain -5
• propagations
D
• Idea:
• Propagate all links, times
• Edge order does not matter!
16
Bellman-Ford Algorithm
17
Slow Motion
Bellman-Ford Algorithm
MCW From
𝑒2 : − 4 𝑒1 : 1 Vertex A 0
C B E
Vertex B -2
-5
∞ A
C
𝑒4 : 3 𝑒6 : 2 1
𝑒 3 : −5 Vertex C -1
3
∞ A
D
𝑒5 : 4 𝑒7 : 1
D A Vertex D ∞
4 A
Vertex E -4
∞01 A
B
18
Code void propagate(Map<T, Float> weights,
void ensureNoNegativeCycle(List<Edge<T>> edges,
Map<T, T> paths,
Map<T, Float> weights)
Bellman-Ford Algorithm Edge<T> edge) {
throws NoNegativeCycle {
var newWeight = weights.get(edge.source)
for (var anyEdge: edges) {
+ edge.weight;
var newWeight = weights.get(anyEdge.source)
if (newWeight < weights.get(edge.end)) {
public Map<T,T> bellmanFord(T source) throws + anyEdge.weight;
NegativeCycleFound {
weights.put(edge.end, newWeight);
if (newWeight < weights.get(anyEdge.end)) {
paths.put(edge.end, edge.source);
var weights = new HashMap<T, Float>(); throw new NegativeCycleFound();
}
var paths = new HashMap<T, T>(); } Same as in Dijkstra’s algorithm
}
}
initialize(weights, paths); }
19
Takeaway
20
How about finding shortest paths
between all pairs of vertices?
Floyd-Warshall Algorithm
21
The Idea
Floyd-Warshall Algorithm
adjacency matrix
k
A B C D E
𝑊 𝑖 ,𝑘 𝑊 𝑘, 𝑗
A 0 2 3 4 1
B ∞ 0 ∞ ∞ 1
i j
C ∞ -4 0 ∞ ∞ 𝑊𝑖, 𝑗
D ∞ ∞ -5 0 ∞
E ∞ ∞ ∞ ∞ 0 𝑊 𝑖 , 𝑗 =min(W i , j , W i , k +W k , j)
22
The Code
Floyd-Warshall Algorithm
24
Thank You!
Questions, Comments, or
Ideas?
Franck Chauvel
Axbit & NTNU