0% found this document useful (0 votes)
18 views25 pages

Shortest Path

Uploaded by

nmjoja244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

Shortest Path

Uploaded by

nmjoja244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

IDATA2302 — Algorithms & Data Structures

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

Bergen Oppdal Hamar


290 km

400 km

Oslo
Agenda
• Weighted Graphs

• Single Source to all


• Dijsktra’s Algorithm

• 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> {

private static class Edge<T> {


T source;
T end; represents a single weighted edge
float weight;

Edge(T source, T end, float weight) {


this.source = source;
this.end = end;
this.weight = weight;
}
}

private HashMap<T, HashMap<T, Edge<T>>> vertices; adjacency “table” of edges

public Graph() {
this.vertices = new HashMap<T, HashMap<T, Edge<T>>>();
}
// …

5
Breadth-first Search 4 edges

3 edges

2 edges

• If all edges weight 1


1 edge

• Breadth-first search source S


• expands progressively
• until it reaches the destination

• Minimum path w.r.t. edge count


D destination

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

• No negative weight! 𝒘𝒙 unknown


y vertex

9
How to Store Paths?
To From

Modle Trondheim Bergen Ålesund


Hamar Oppdal

Modle Ålesund
Røros
Ålesund
Oppdal Oslo

Oslo

Bergen Oppdal Hamar Røros Hamar

Trondheim Modle
Ålesund Oppdal
Oslo

10
Dijsktra’s Algorithm
Minimum Total
From
220 km Weight
Modle Trondheim Bergen ∞
1115 Ålesund

154 km Hamar 690


∞ Oppdal
82 km
Modle ∞
772 Ålesund
Røros
Ålesund
Oppdal 400
∞ Oslo
425 km 290 km 260 km
Oslo 0

Bergen Oppdal Hamar Røros ∞


950 Hamar
290 km
Trondheim 992
∞ 1104? Modle
400 km
Ålesund 690
∞ Oppdal
Oslo
Next, we pick the closeest unvisited vertex!
START
11
private void initialize(HashMap<T, Float> weights,

The Code HashMap<T, T> path) {


for (var anyVertex: vertices.keySet()) {
weights.put(anyVertex, Float.POSITIVE_INFINITY);
Dijsktra’s Algorithm if (anyVertex == source) {
weights.put(source, 0f);
}
} private T minimumWeight(List<T> candidates,
public Map<T, T> dijkstra(T source) { } Map<T, Float> weights) {
var weights = new HashMap<T, Float>(); if (candidates.size() == 1) {
var path = new HashMap<T, T>(); return candidates.get(0);
initialize(weights, path); }
var minimum = candidates.get(0);
var unknowns = new LinkedList<T>(vertices.keySet()); for(int index=1 ; index<candidates.size() ; index++) {
var current = candidates.get(index);
while (!unknowns.isEmpty()) {
if (weights.get(current) < weights.get(minimum)) {
var current = minimumWeight(unknowns, weights);
private void minimum = current;
adjust(HashMap<T, Float> weights,
for (var anyNeighbour: neighboursOf(current)) { } HashMap<T, T> path
if (unknowns.contains(anyNeighbour)) { } T current,
adjust(weights, path, current, anyNeighbour); return minimum; T neighbour) {
} } var newWeight = weights.get(current)
} + weightBetween(current, neighbour);
unknowns.remove(current); if (newWeight < weights.get(neighbour)) {
weights.put(neighbour, newWeight);
}
path.put(neighbour, current);
}
return path; }
}

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

• The existence of a shortest path imply 3


the absence of negative circuits

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

• updates of B requires an update of E -4 1


C B E

• Worst Case:
• Chain -5
• propagations

D
• Idea:
• Propagate all links, times
• Edge order does not matter!

16
Bellman-Ford Algorithm

• Start just like Dijkstra


• Repeat :
• Propagate all edges, if needed

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

Iteration Count: 3412

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); }

for (int i=1 ; i<vertices.size() ; i++) {


for (var anyEdge: allEdges()) {
propagate(weights, paths, anyEdge);
}
}
ensureNoNegativeCycle(allEdges(), weights);
return paths;
}

19
Takeaway

The Bellman-Ford Algorithm handles


negative weights
and
detects negative cycles

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

Map<Pair<T>, Float> floydWarhsall() {


var matrix = new HashMap<Pair<T>, Float>(); Adjacency matrix

void initialize(Map<Pair<T>, Float> matrix) {


initialize(matrix);
for (var anySource: vertices) {
for(var anyVertex: vertices()) { for (var anyTarget: vertices) {
for (var anySource: vertices()) matrix.put(new
{ Pair<T>(anySource, anyTarget),
Float.POSITIVE_INFINITY);
for (var anyTarget: vertices()) {
}
adjust(matrix, anyVertex, anySource, anyTarget);
}
} void adjust(Map<Pair<T>,
for(var Float>
eachEdge: allEdges()) { matrix,
} T givenVertex,
matrix.put(new T source, T target)
Pair<T,T>(eachEdge.start, {
eachEdge.end),
var newWeight = matrix.get(new Pair<T>(source, givenVertex)) +
eachEdge.weight);
} matrix.get(new Pair<T>(givenVertex, target));
}
if (newWeight
for (var anyVertex:< vertices)
matrix.get(new
{ Pair<T>(source, target))) {
matrix.put(new
matrix.put(new Pair<T>(source,anyVertex),
Pair<T>(anyVertex, target),
return matrix;
0); newWeight);
} } }
} }
23
Recap
• Weighted Graphs • All Sources Shortest Paths
• Edges have cost/weight • Floyld-Warshall Algorithm
• No negative cycles
• Single source Shortest paths • O(V^3)
• Dijkstra’s Algorithm
• No negative weights
• O(E + V)
• Bellman-Ford Algorithm
• No negative cycles
• O(V * E)

24
Thank You!
Questions, Comments, or
Ideas?

Franck Chauvel
Axbit & NTNU

[email protected]

You might also like