39
Shortest Path Algorithms Chapters 24, 25

Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Shortest Path

AlgorithmsChapters 24, 25

Page 2: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Example: Positive Weights

s

t

y

x

z

3

6

2 7

6

3

412

5

Page 3: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Example: Negative Weights

s

t

y

x

z

6

5

7

9

2

-38

7

-2

-4

Page 4: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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

Page 5: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with 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

Page 6: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Optimal Substructure

𝑣0

𝑣1 π‘£π‘˜Shortest

path from

𝑣0 to π‘£π‘˜

Page 7: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Optimal Substructure

𝑣0

𝑣1 π‘£π‘˜Shortest

path from

𝑣0 to π‘£π‘˜π‘£π‘–

𝑣𝑗

Shortest path from 𝑣𝑖 to 𝑣𝑗

Page 8: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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 𝑣𝑗

𝑣𝑖

𝑣𝑗

Page 9: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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

Page 10: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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

Page 11: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Shortest path representation

For single-source all-destinations shortest

path

𝑣0

𝑣1

𝑣2

𝑣3

𝑣5

𝑣4

𝑣6

Page 12: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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

Page 13: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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?

Page 14: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Bellman-Ford

Algorithm

Page 15: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Initialization

Page 16: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Relax

Page 17: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Relaxation

𝑒

𝑣2

𝑐

𝑠

π‘Ž

5

3

1

4

2

𝑏3

πœ‹ = 𝑐𝑑 = 9

πœ‹ = π‘Žπ‘‘ = 4

Page 18: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Relaxation

𝑒

𝑣2

𝑐

𝑠

π‘Ž

5

3

1

4

2

𝑏3

πœ‹ = 𝑒𝑑 = 6

πœ‹ = π‘Žπ‘‘ = 4

Page 19: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Bellman-Ford Algorithm

𝑉 βˆ’ 1iterations

𝐸iterations

Running time is 𝑂 𝑉 β‹… 𝐸

Page 20: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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

Page 21: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Dijkstra’s Algorithm

Page 22: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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

Page 23: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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(𝑄, 𝑣, 𝑣. 𝑑)

Page 24: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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(𝑄, 𝑣, 𝑣. 𝑑) // 𝑂 π‘™π‘œπ‘” 𝑉

Page 25: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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

Page 26: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Correctness of Dijkstra’s

𝑆

𝑠

𝑒

π‘₯

Real shortest path

𝑦

𝑦. 𝑑 = 𝛿 𝑠, 𝑦

≀ 𝛿 𝑠, 𝑒

≀ 𝑒. 𝑑

𝑒. 𝑑 ≀ 𝑦. 𝑑At the time when 𝑒was chosen to expand

𝑦. 𝑑 = 𝛿 𝑠, 𝑦 = 𝛿 𝑠, 𝑒 = 𝑒. 𝑑

𝑒. 𝑑 = 𝛿 𝑠, 𝑒

Page 27: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

All-pairs Shortest

PathsChapter 25

Page 28: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

𝑣1

𝑣3

𝑣2

𝑣4

𝑣5

𝑣6

10

75

10 4 3 1

4

4

2

23

Page 29: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Floyd-Warshall

AlgorithmSection 25.2

Page 30: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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, 𝑝 𝑖, 𝑗 = 𝑝𝑛 𝑖, 𝑗

Page 31: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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)

Page 32: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Floyd-Warshall Algorithm

𝑖 𝑗

π‘£π‘˜

𝑝1 𝑝2

𝑝1 = π‘π‘˜ 𝑖, π‘£π‘˜ = π‘π‘˜βˆ’1(𝑖, π‘£π‘˜) 𝑝2 = π‘π‘˜ π‘£π‘˜, 𝑗 = π‘π‘˜βˆ’1(𝑗, π‘£π‘˜)

π‘π‘˜ 𝑖, 𝑗 = π‘π‘˜ 𝑖, π‘£π‘˜ ||π‘π‘˜(π‘£π‘˜ , 𝑗)

Page 33: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Recursive Formula

π‘π‘˜ 𝑖, 𝑗 = ࡞

𝑖, 𝑗

π‘π‘˜βˆ’1 𝑖, 𝑗

π‘˜ = 0π‘˜ βˆ‰ π‘π‘˜ 𝑖, 𝑗

π‘π‘˜βˆ’1 𝑖, π‘£π‘˜ ||π‘π‘˜βˆ’1(π‘£π‘˜ , 𝑗) π‘˜ ∈ π‘π‘˜ 𝑖, 𝑗

π‘‘π‘˜ 𝑖, 𝑗 = ቐ𝑀 𝑖, 𝑗 π‘˜ = 0

min π‘‘π‘˜βˆ’1 𝑖, 𝑗 , π‘‘π‘˜βˆ’1 𝑖, π‘˜ + π‘‘π‘˜βˆ’1 π‘˜, 𝑗 π‘˜ β‰₯ 1

Page 34: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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 𝐷𝑛

Page 35: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

𝑣1

𝑣3

𝑣2

𝑣4

𝑣5

𝑣6

10

75

10 4 3 1

4

4

2

23

Page 36: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

All-pairs Shortest

PathBased on Matrix Multiplication

Page 37: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

Recursive Formulation

Let 𝑙𝑖𝑗(π‘š)

be the minimum weight path from 𝑖 to

𝑗 of at most π‘š edges.

𝑙𝑖𝑗(0)

= α‰Š0 𝑖𝑓 𝑖 = π‘—βˆž 𝑖𝑓 𝑖 β‰  𝑗

π‘™π‘–π‘—π‘š = min 𝑙𝑖𝑗

π‘šβˆ’1 , min1β‰€π‘˜β‰€π‘›

π‘™π‘–π‘˜(π‘šβˆ’1)

+π‘€π‘˜π‘—

= min1β‰€π‘˜β‰€π‘›

π‘™π‘–π‘˜π‘šβˆ’1 +π‘€π‘˜π‘—

Page 38: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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 𝐿’

Page 39: Shortest Path Algorithms - UCR Computer Science and ...eldawy/20WCS141/slides/CS141-6-Shortest Path.pdf · Shortest path problem Given a weighted, directed graph 𝐺= , , with weight

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