Upload
others
View
12
Download
0
Embed Size (px)
Citation preview
Shortest Path
AlgorithmsChapters 24, 25
Example: Positive Weights
s
t
y
x
z
3
6
2 7
6
3
412
5
Example: Negative Weights
s
t
y
x
z
6
5
7
9
2
-38
7
-2
-4
Shortest path problem
Given a weighted, directed graph πΊ = π, πΈ ,
with weight function π€:πΈ β β. The weight
π€ π of a path π = π£0, π£1, β¦ , π£π is the sum of
the weights of its constituent edges
π€ π = Οπ=1π π€ π£πβ1, π£π
Shortest-path weight πΏ π’, π£ from π’ to π£ is
πΏ π’, π£ = ΰ΅min π€ π : π’βππ£ if there is a path from π’ to π£
β ππ‘βπππ€ππ π
The shortest path is any path with shortest
path weight
Problem Variants
Single-source single-destination shortest path
Single-source all-destinations shortest paths
All-sources single-destination shortest paths
All-pairs shortest paths
Optimal Substructure
π£0
π£1 π£πShortest
path from
π£0 to π£π
Optimal Substructure
π£0
π£1 π£πShortest
path from
π£0 to π£ππ£π
π£π
Shortest path from π£π to π£π
Optimal Substructure
π£0
π£1 π£πShortest
path from
π£0 to π£ππ£π
π£π
Assume that this is not the
shortest path from π£π to π£π
And this is the shortest path
from π£π to π£π
π£π
π£π
Optimal Substructure
π£0
π£1 π£πShortest
path from
π£0 to π£ππ£π
π£π
Assume that this is not the
shortest path from π£π to π£π
And this is the shortest path
from π£π to π£π
π£π
π£π π£π
π£0
π£1
Then this new path is
better than the optimal
answer from π£0 to π£πwhich is a contradiction
Cycles in Shortest Path
Negative weight cycles?
Make the shortest path undefined
Positive weight cycles?
Can always be removed to produce a shorter path
0-weight cycles?
Can always be removed while still having a
shortest path
Conclusion: We can disregard cycles in our
solutions
Shortest path representation
For single-source all-destinations shortest
path
π£0
π£1
π£2
π£3
π£5
π£4
π£6
Shortest path representation
For single-source all-destinations shortest
path
For each vertex π£π, we store its predecessor
π£π . π in the shortest-path tree and the cost of
the shortest path π£π . π = πΏ π , π£π
π£0
π£1
π£2
π£3
π£5
π£4
π£6
Triangle Inequality
For any edge π’, π£ β πΈ, we have
πΏ π , π£ β€ πΏ π , π’ + π€ π’, π£
Question: What is the upper limit of the size
of the shortest path in terms of number of
edges?
Bellman-Ford
Algorithm
Initialization
Relax
Relaxation
π’
π£2
π
π
π
5
3
1
4
2
π3
π = ππ = 9
π = ππ = 4
Relaxation
π’
π£2
π
π
π
5
3
1
4
2
π3
π = π’π = 6
π = ππ = 4
Bellman-Ford Algorithm
π β 1iterations
πΈiterations
Running time is π π β πΈ
Detecting Negative Cycles
Assume the input graph contains a negative-
weight cycle π = π£0, π£1, β¦ , π£π where π£0 = π£π
Οπ=1π π€ π£πβ1, π£π < 0 // weight of the cycle
For the sake of contradiction, assume that
βπ: π£π . π β€ π£πβ1. π + π€ π£πβ1, π£π // inequality
Οπ=1π π£π . π β€ Οπ=1
π π£πβ1. π + π€ π£πβ1, π£π
Οπ=1π π£π . π β€ Οπ=1
π π£πβ1. π + Οπ=1π π€ π£πβ1, π£π
Οπ=1π π€ π£πβ1, π£π β₯ 0 // Contradiction
Dijkstraβs Algorithm
Dijkstraβs Algorithm
Bellman-Ford algorithm assumes no negative
cycles but can handle negative-weight edges
In many real applications, even negative-
weight edges are not allowed
E.g., road networks
In this case, Dijkstraβs algorithm can find the
shortest paths more efficiently
Dijkstraβs AlgorithmInit-Single-Source(G,s)
π = β // Shortest path tree vertices
π = β // Min-heap
For each vertex π’ β πΊ. π
Insert(π, π’)
While π β β
π’ =Extract-Min(π)
π = π βͺ π’
For each vertex π£ β πΊ. πππ π’
Relax(π’, π£, π€)
If π£. π changes
Decrease-Key(π, π£, π£. π)
Dijkstraβs AlgorithmInit-Single-Source(G, s)
π = β // Shortest path tree vertices
π = β // Min-heap
For each vertex π’ β πΊ. π
Insert(π, π’)
While π β β
π’ =Extract-Min(π) // π πππ π
π = π βͺ π’
For each vertex π£ β πΊ. πππ π’ // πΈ times in total
Relax(π’, π£, π€) // π 1 ππππππ‘πππ
If π£. π changes
Decrease-Key(π, π£, π£. π) // π πππ π
Correctness of Dijkstraβs
π
π
π’
π§
Shortest path from
s to z
What Dijkstraβs find
For contradiction, letβs assume
that it is incorrect, i.e., not
shortest path
Correctness of Dijkstraβs
π
π
π’
π₯
Real shortest path
π¦
π¦. π = πΏ π , π¦
β€ πΏ π , π’
β€ π’. π
π’. π β€ π¦. πAt the time when π’was chosen to expand
π¦. π = πΏ π , π¦ = πΏ π , π’ = π’. π
π’. π = πΏ π , π’
All-pairs Shortest
PathsChapter 25
π£1
π£3
π£2
π£4
π£5
π£6
10
75
10 4 3 1
4
4
2
23
Floyd-Warshall
AlgorithmSection 25.2
Floyd-Warshall Algorithm
Let π π, π be the shortest path from any pair
of vertices π, π
An intermediate vertex π is any vertex on the
path π π, π , where π β π and π β π
In this case, π π, π = π π, π ||π π, π
Now, let us define ππ π, π as the shortest
path between π, π such that all the
intermediate vertices are in the set
π£1, π£2, β¦ , π£πBy definition, π π, π = ππ π, π
Shortest paths representation
π π, π ππ ππ ππ ππ
ππ 0
ππ 0
ππ 0
ππ 0
π π, π ππ ππ ππ ππ
ππ NIL
ππ NIL
ππ NIL
ππ NIL
Single-source all-destinations
shorts paths (π = π£3)
All-sources single-destination
shortest paths (π = π£2)
Floyd-Warshall Algorithm
π π
π£π
π1 π2
π1 = ππ π, π£π = ππβ1(π, π£π) π2 = ππ π£π, π = ππβ1(π, π£π)
ππ π, π = ππ π, π£π ||ππ(π£π , π)
Recursive Formula
ππ π, π = ΰ΅
π, π
ππβ1 π, π
π = 0π β ππ π, π
ππβ1 π, π£π ||ππβ1(π£π , π) π β ππ π, π
ππ π, π = απ€ π, π π = 0
min ππβ1 π, π , ππβ1 π, π + ππβ1 π, π π β₯ 1
Pseudo-Code
1. FLOYD-WARSHALL(π)
2. π = π.rows
3. π0 π, π = β;βπ, π
4. for π = 1 to π
5. let π·π = ππ π, π be a new π Γ π matrix
6. for π=1 to π
7. for π=1 to π
8. ππ π, π = min ππ π, π , ππβ1 π, π + ππβ1 π, π
9. return π·π
π£1
π£3
π£2
π£4
π£5
π£6
10
75
10 4 3 1
4
4
2
23
All-pairs Shortest
PathBased on Matrix Multiplication
Recursive Formulation
Let πππ(π)
be the minimum weight path from π to
π of at most π edges.
πππ(0)
= α0 ππ π = πβ ππ π β π
ππππ = min πππ
πβ1 , min1β€πβ€π
πππ(πβ1)
+π€ππ
= min1β€πβ€π
ππππβ1 +π€ππ
Pseudo-code
1. EXTEND-SHORTEST-PATHS(πΏ, π)
2. π = πΏ. rows
3. let πΏβ² = (πβ²ππ) be a new π Γ π matrix
4. for π = 1 to π
5. for π = 1 to π
6. πππβ = β
7. for π = 1 to π
8. πππβ = min(πππβ, πππ + πππ)
9. return πΏβ
Analogy with Matrix Multiplication
πΆ = π΄ Γ π΅
πππ = Οπ=1π πππ β πππ
If πΆ = π΄ Γ π΄
πππ = Οπ=1π πππ β πππ
For the shortest path algorithm
ππππ = min1β€πβ€π πππ
πβ1 + ππππβ1
Equivalent to πΏπ = πΏπβ1 βπΏπβ1
Where β is a redefined matrix multiplication
operation based on addition and minimum