0% found this document useful (0 votes)
10 views31 pages

13 ShortestPath

The document discusses shortest path algorithms including Dijkstra's algorithm and Floyd-Warshall's algorithm. It provides definitions of path, shortest path, and properties of shortest paths. It also describes how to find the single-source shortest path using Dijkstra's greedy strategy and how to find the all-pairs shortest path using Floyd-Warshall's dynamic programming approach.

Uploaded by

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

13 ShortestPath

The document discusses shortest path algorithms including Dijkstra's algorithm and Floyd-Warshall's algorithm. It provides definitions of path, shortest path, and properties of shortest paths. It also describes how to find the single-source shortest path using Dijkstra's greedy strategy and how to find the all-pairs shortest path using Floyd-Warshall's dynamic programming approach.

Uploaded by

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

Design and Analysis of Algorithms

Shortest Path
Dr. Lê Nguyên Khôi
The VNU University of Engineering and Technology
Contents

 Path definition
 Shortest path properties
 Single-source shortest path
 All-pairs shortest path

Design and Analysis of Algorithms 1


Path – Definition
Graph
 Path
 a sequence of vertices
 2 consecutive vertices , connected by
an edge in
 is a path from to
 Cycle
 a path with
 whereas, the first vertices are distinct and

Design and Analysis of Algorithms 2


Path – Weight
Consider a directed graph with
edge-weight function . The weight
of path is defined as:

Example:

Design and Analysis of Algorithms 3


Shortest Path – Definition

A shortest path from to is a path of


minimum weight from to .
The shortest-path weight from to is
defined as:

Note: if no path from to


exists

Design and Analysis of Algorithms 4


Shortest Path – Properties

Optimal substructure
 A sub-path of a shortest path is the
shortest path
 is the shortest path
from to
 is the sub-path of
from to với
 is the shortest path from to

Design and Analysis of Algorithms 5


Shortest Path – Properties

Optimal substructure – Proof


 Divide into sub-paths
 Then,
 Consider path from to , with weight

 Then, is a path from to


with weight smaller than
 Contradiction with the hypothesis is the
shortest path from to

Design and Analysis of Algorithms 6


Shortest Path – Properties

Triangle inequality
 For all vertices , we have

Design and Analysis of Algorithms 7


Shortest Path – Properties

Negative weight
 If a graph contains a negative-weight cycle,
then some shortest paths may not exist

Design and Analysis of Algorithms 8


Single-Source Shortest Path

 Problem. From a given source vertex


, find the shortest-path weights
for all
 If all edge weights are
nonnegative, all shortest-path weights
must exist
 Greedy strategy to find the optimal
solution
 As shortest path has the optimal substructure
property

Design and Analysis of Algorithms 9


Single-Source Shortest Path

 Greedy strategy:
1. Maintain a set of vertices whose shortest-
path distances from are known
2. At each step add to the vertex
whose distance estimate from is minimal
3. Update the distance estimates of vertices
adjacent to

Design and Analysis of Algorithms 10


Greedy Strategy

Initialization
0
2 9

3 5 2

1 4 1

1 4
8

Design and Analysis of Algorithms 11


Greedy Strategy

Add to
0
2 9

3 5 2

1 4 1

1 4
8

Design and Analysis of Algorithms 12


Greedy Strategy

Add to
0
2 9

3 5 2

1 4 1

1 4
8

Design and Analysis of Algorithms 13


Greedy Strategy

Add to
0
2 9

3 5 2

1 4 1

1 4
8

Design and Analysis of Algorithms 14


Greedy Strategy

Add to
0
2 9

3 5 2

1 4 1

1 4
8

store the weight of the shortest path


from to for all
Design and Analysis of Algorithms 15
Dijkstra’s Algorithm
 Consider special path from to only
consist of vertices in
 : store the path weight from to
 Initialization , ,
with

Design and Analysis of Algorithms 16


Dijkstra’s Algorithm

: store the path weight from to


 Initialization , ,
with
 Each step:
 Choose vertex such that minimal
 Add to , then store the weight of the shortest
path from to
 Then, recalculate for all

 Repeat, until contains all vertices in

Design and Analysis of Algorithms 17


Dijkstra’s Algorithm – Exercise

Design and Analysis of Algorithms 18


Dijkstra’s Algorithm – Pseudocode

for each do

while do times

for each do times


if then

Design and Analysis of Algorithms 19


Unweighted Graph
Suppose that for all .
Can Dijkstra’s algorithm be improved?
 Use a simple FIFO queue instead of a priority queue
Breadth First Search
while do

for each do
if then

Complexity Analysis:
Design and Analysis of Algorithms 20
Breadth First Search

Design and Analysis of Algorithms 21


All-Pairs Shortest Path

Single-Source Shortest Path


 Dijkstra’s algorithm
All-Pairs Shortest Path
 Dijkstra’s Algorithms times
Greedy strategy
 Floyd-Warshall’s Algorithms
Dynamic programming

Design and Analysis of Algorithms 22


Floyd-Warshall’s Algorithm
, set empty,
15
1 4
5 1 2 3 4

5 1 0 5 ∞ ∞
50 30 15
5
2 50 0 15 5
3 30 ∞ 0 15
15
2 3 4 15 ∞ 5 0

Design and Analysis of Algorithms 23


Floyd-Warshall’s Algorithm
1 2 3 4
1 0 5 ∞ ∞

15 2 50 0 15 5
1 4 3 30 ∞ 0 15
5 4 15 ∞ 5 0
5 15
5 50 30
1 2 3 4
1 0 5 ∞ ∞
15
2 3 2 50 0 15 5
3 30 35 0 15
4 15 20 5 0

Design and Analysis of Algorithms 24


Floyd-Warshall’s Algorithm
1 2 3 4
1 0 5 ∞ ∞

15 2 50 0 15 5
1 4 3 30 35 0 15
5 4 15 20 5 0
5 15
5 50 30
1 2 3 4
1 0 5 20 10
15
2 3 2 50 0 15 5
3 30 35 0 15
4 15 20 5 0

Design and Analysis of Algorithms 25


Floyd-Warshall’s Algorithm
1 2 3 4
1 0 5 20 10

15 2 50 0 15 5
1 4 3 30 35 0 15
5 4 15 20 5 0
5 15
5 50 30
1 2 3 4
1 0 5 20 10
15
2 3 2 45 0 15 5
3 30 35 0 15
4 15 20 5 0

Design and Analysis of Algorithms 26


Floyd-Warshall’s Algorithm
1 2 3 4
1 0 5 20 10

15 2 45 0 15 5
1 4 3 30 35 0 15
5 4 15 20 5 0
5 15
5 50 30
1 2 3 4
1 0 5 15 10
15
2 3 2 20 0 10 5
3 30 35 0 15
4 15 20 5 0

Design and Analysis of Algorithms 27


Floyd-Warshall’s Algorithm

Define weight of a shortest path


from to with intermediate vertices
belong to the set

Thus,
Initialization

Design and Analysis of Algorithms 28


Floyd-Warshall’s Algorithm
 If vertex is on the shortest path from to , then the
path from to and the path from to is the shortest
path
 If is the weight of the path not containing , then
this path only consist of vertices in , thus

 If is the weight of the path not containing , then


the sub-path from to has weight , and the
sub-path from to has weight
 Thus

Design and Analysis of Algorithms 29


Floyd-Warshall Algorithm – Pseudocode

for to do
for to do
for to do
if then

 Run in time
 Simple to code
 Efficient in practice
Design and Analysis of Algorithms 30

You might also like