Design and Analysis of Algorithms
Lecture 11
• Shortest paths in graphs with positive weights
1
Algorithms-II : CS345A
Recap of last lecture
2
Problem Definition
Input: A directed graph with and a source vertex
Aim:
• Compute for all
• Compute for all
3
An example to get
an insight into this problem
4
𝒔
G
𝒒
12
6
16
11
9
4
7
5
An example to get
an insight into this problem
Let us look at the neighborhood of
5
𝒔
𝒖
𝒙
𝒚
𝒛 𝒗
3
5
12
10
7
G
An example to get
an insight into this problem
 The shortest path to vertex is edge (,).
6
𝒔
𝒖
𝒙
𝒚
𝒛 𝒗
3
5
12
10
7
G
¿ 𝟎
The nearest vertex of must be ’s neighbor.
Designing a greedy algorithm for shortest paths
What should be the greedy step such that
“we can retrieve distance from in using distance from in ” ?
7
𝑮
𝑮 ′
instance of size
Greedy
step
instance of size
Opt()
Opt()
?
We know the distance to .
Question: Can you remove vertex without affecting the distance from ?
An example to get
an insight into this problem
8
𝒔
𝒖
𝒙
𝒚
𝒛
3
5
12
10
7
G
3
2
8
21
𝒓
𝒕
𝒗
Since we have computed
distance to , perhaps we can
remove .
No. This is because there could be vertices
whose shortest path from passes through .
An example to get
an insight into this problem
9
𝒙
𝒚
𝒛
5
12
10
7
G’
3
2
8
21
𝒓
𝒕
+3
+3
+3
+3
𝒔
𝒗
An example to get
an insight into this problem
Theorem: For each ,
10
𝒙
𝒚
𝒛
5
12
10
7
G’
6
5
11
24
𝒓
𝒕
𝒔
𝒗
How to compute instance
Let (,) be the least weight edge from in =(, ).
Transform into as follows.
1. For each edge (,)ϵ ,
add edge (,);
(,) ??
2. In case of two edges from to any vertex , keep only the lighter edge.
3. Remove vertex .
Theorem: For each ,
 an algorithm for distances from with time complexity.
11
(,) + (,);
Can you see some negative
points of this algorithm ?
Shortcomings of the algorithm
• No insight into the (beautiful) structure of shortest paths.
• Just convinces that we can solve the shortest paths problem in polynomial time.
• Very few options to improve the time complexity.
• Silent about a compact data structure for storing all shortest paths from the
source.
We shall now design a very insightful algorithm based on properties of shortest paths.
12
PROPERTIES OF A SHORTEST PATH
13
Optimal subpath property
Consider any shortest path .
= 51
Lemma 1: Every subpath of a shortest path is also a shortest path.
14
𝒔
?
< 27
𝒂 𝒈 𝒙
12 3 9
𝒚 𝒖
15
5
7
Can you realize
an assumption
made by you in
this proof ?
Homework: Write a complete and formal proof for Lemma 1
Proof:
If there is a shorter path from to ,
replacing the current path using the shorter path
will lead to a shorter path from to .
Exploiting the positive weight on edges
Consider once again a shortest path .

This leads to an alternate proof to the following assertion :
The nearest vertex of must be ’s neighbor.
15
𝒂 𝒈 𝒙
𝒚 𝒖
𝒔
¿ 𝟎 ¿ 𝟎 ¿ 𝟎 ¿ 𝟎 ¿ 𝟎 ¿ 𝟎
(1)
More insights …
Let : th nearest vertex from .
= .
Consider the shortest path .
Lemma 2: must be of the form for some .
16
𝒔 𝒙
What picture
captures all shortest
paths from ?
What can we say
about ?
must be for some .
shortest
Complete picture of all shortest paths ?
Shortest paths tree
17
𝒔𝟏
𝒔𝟐
𝒔
𝒔𝟒
𝒔𝟓
𝒔𝟑
𝒔𝟕
𝒔𝟔
Designing the algorithm …
Lemma 2: must be of the form for some .
Question: Can we use Lemma 2 to design an algorithm ?
Incremental way to compute shortest paths.
Ponder over it before going to the next slide.
18
shortest
Suppose we have computed , ,…, .
How to find out ?
19
𝒔𝟏
𝒔𝟐
𝒔
𝒔𝟒
𝒔𝟑
G
𝒗
If ,
How will the shortest
path look like ?
Or
Or
Using Lemma 2
We need to just focus on
the edges incident on from , ,…, .
How to use it to find ?
Isn’t it nice  ?
Ponder over its usefulness before proceeding further.
Let us define a function
Consider any
Suppose we have computed , ,…, .
How to find out ?
For each
is ??
20
𝒔𝟏
𝒔𝟐
𝒔
𝒔𝟒
𝒔𝟑
G
𝒗
the vertex with minimum value of .
min
¿ ¿
(𝜹(𝒔 ,𝒔 𝒋)+𝝎(𝒔𝒋 ,𝒗))
51
76
101
90
82
62
88
How to find ?
Using Lemma 2
Dijkstra’s algorithm
Dijkstra-algo(,)
 ;
 ;
For = 0 to do
{  vertex from with minimum value of ;
;
move from to ;
For each do
{  ;
For each with do
min( , )
}
}
21
a lot of re-computation
 for all ;
Computing labels for the
next iteration
Only neighbors of
What are the vertices
whose value may change
in this iteration ?
Dijkstra’s algorithm
Dijkstra-algo(,)
 ;
 ;
For = 0 to do
{  vertex from with minimum value of ;
;
move from to ;
For each with do
{
min( , )
}
}
22
1 extract-min
operation
deg() Decrease-key
operations
 for all ;
Homework
• What data structure is to be used to efficiently implement the Dijkstra’s
algorithm?
• Write a neat pseudocode in terms of the data structure.
• What is the time complexity of the implementation ?

Lecture-11-CS345A-2023 of Design and Analysis

  • 1.
    Design and Analysisof Algorithms Lecture 11 • Shortest paths in graphs with positive weights 1 Algorithms-II : CS345A
  • 2.
    Recap of lastlecture 2
  • 3.
    Problem Definition Input: Adirected graph with and a source vertex Aim: • Compute for all • Compute for all 3
  • 4.
    An example toget an insight into this problem 4 𝒔 G 𝒒 12 6 16 11 9 4 7 5
  • 5.
    An example toget an insight into this problem Let us look at the neighborhood of 5 𝒔 𝒖 𝒙 𝒚 𝒛 𝒗 3 5 12 10 7 G
  • 6.
    An example toget an insight into this problem  The shortest path to vertex is edge (,). 6 𝒔 𝒖 𝒙 𝒚 𝒛 𝒗 3 5 12 10 7 G ¿ 𝟎 The nearest vertex of must be ’s neighbor.
  • 7.
    Designing a greedyalgorithm for shortest paths What should be the greedy step such that “we can retrieve distance from in using distance from in ” ? 7 𝑮 𝑮 ′ instance of size Greedy step instance of size Opt() Opt() ?
  • 8.
    We know thedistance to . Question: Can you remove vertex without affecting the distance from ? An example to get an insight into this problem 8 𝒔 𝒖 𝒙 𝒚 𝒛 3 5 12 10 7 G 3 2 8 21 𝒓 𝒕 𝒗 Since we have computed distance to , perhaps we can remove . No. This is because there could be vertices whose shortest path from passes through .
  • 9.
    An example toget an insight into this problem 9 𝒙 𝒚 𝒛 5 12 10 7 G’ 3 2 8 21 𝒓 𝒕 +3 +3 +3 +3 𝒔 𝒗
  • 10.
    An example toget an insight into this problem Theorem: For each , 10 𝒙 𝒚 𝒛 5 12 10 7 G’ 6 5 11 24 𝒓 𝒕 𝒔 𝒗
  • 11.
    How to computeinstance Let (,) be the least weight edge from in =(, ). Transform into as follows. 1. For each edge (,)ϵ , add edge (,); (,) ?? 2. In case of two edges from to any vertex , keep only the lighter edge. 3. Remove vertex . Theorem: For each ,  an algorithm for distances from with time complexity. 11 (,) + (,); Can you see some negative points of this algorithm ?
  • 12.
    Shortcomings of thealgorithm • No insight into the (beautiful) structure of shortest paths. • Just convinces that we can solve the shortest paths problem in polynomial time. • Very few options to improve the time complexity. • Silent about a compact data structure for storing all shortest paths from the source. We shall now design a very insightful algorithm based on properties of shortest paths. 12
  • 13.
    PROPERTIES OF ASHORTEST PATH 13
  • 14.
    Optimal subpath property Considerany shortest path . = 51 Lemma 1: Every subpath of a shortest path is also a shortest path. 14 𝒔 ? < 27 𝒂 𝒈 𝒙 12 3 9 𝒚 𝒖 15 5 7 Can you realize an assumption made by you in this proof ? Homework: Write a complete and formal proof for Lemma 1 Proof: If there is a shorter path from to , replacing the current path using the shorter path will lead to a shorter path from to .
  • 15.
    Exploiting the positiveweight on edges Consider once again a shortest path .  This leads to an alternate proof to the following assertion : The nearest vertex of must be ’s neighbor. 15 𝒂 𝒈 𝒙 𝒚 𝒖 𝒔 ¿ 𝟎 ¿ 𝟎 ¿ 𝟎 ¿ 𝟎 ¿ 𝟎 ¿ 𝟎 (1)
  • 16.
    More insights … Let: th nearest vertex from . = . Consider the shortest path . Lemma 2: must be of the form for some . 16 𝒔 𝒙 What picture captures all shortest paths from ? What can we say about ? must be for some . shortest
  • 17.
    Complete picture ofall shortest paths ? Shortest paths tree 17 𝒔𝟏 𝒔𝟐 𝒔 𝒔𝟒 𝒔𝟓 𝒔𝟑 𝒔𝟕 𝒔𝟔
  • 18.
    Designing the algorithm… Lemma 2: must be of the form for some . Question: Can we use Lemma 2 to design an algorithm ? Incremental way to compute shortest paths. Ponder over it before going to the next slide. 18 shortest
  • 19.
    Suppose we havecomputed , ,…, . How to find out ? 19 𝒔𝟏 𝒔𝟐 𝒔 𝒔𝟒 𝒔𝟑 G 𝒗 If , How will the shortest path look like ? Or Or Using Lemma 2 We need to just focus on the edges incident on from , ,…, . How to use it to find ? Isn’t it nice  ? Ponder over its usefulness before proceeding further. Let us define a function Consider any
  • 20.
    Suppose we havecomputed , ,…, . How to find out ? For each is ?? 20 𝒔𝟏 𝒔𝟐 𝒔 𝒔𝟒 𝒔𝟑 G 𝒗 the vertex with minimum value of . min ¿ ¿ (𝜹(𝒔 ,𝒔 𝒋)+𝝎(𝒔𝒋 ,𝒗)) 51 76 101 90 82 62 88 How to find ? Using Lemma 2
  • 21.
    Dijkstra’s algorithm Dijkstra-algo(,)  ; ; For = 0 to do {  vertex from with minimum value of ; ; move from to ; For each do {  ; For each with do min( , ) } } 21 a lot of re-computation  for all ; Computing labels for the next iteration Only neighbors of What are the vertices whose value may change in this iteration ?
  • 22.
    Dijkstra’s algorithm Dijkstra-algo(,)  ; ; For = 0 to do {  vertex from with minimum value of ; ; move from to ; For each with do { min( , ) } } 22 1 extract-min operation deg() Decrease-key operations  for all ;
  • 23.
    Homework • What datastructure is to be used to efficiently implement the Dijkstra’s algorithm? • Write a neat pseudocode in terms of the data structure. • What is the time complexity of the implementation ?