0% found this document useful (0 votes)
14 views51 pages

Cs3230-Lec03a-Full Version

Uploaded by

eugeneongwx
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)
14 views51 pages

Cs3230-Lec03a-Full Version

Uploaded by

eugeneongwx
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/ 51

CS3230 – Design and Analysis of Algorithms

(S1 AY2024/25)
Lecture 3a: Proof of Correctness
Correctness of an algorithm
• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.
• Iterative algorithms.
• Recursive algorithms. 𝐈𝐈𝐈𝐈𝐈𝐈𝐈𝐈(𝑛𝑛)
• If 𝑛𝑛 ≤ 1
• return 𝑛𝑛
𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛) • Else,
• If 𝑛𝑛 ≤ 1, return 𝑛𝑛. • prev2 = 0
• Else, return 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 1 + 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 2 . • prev1 = 1
• for 𝑖𝑖 = 2 to 𝑛𝑛
• temp = prev1
• prev1 = prev1+prev2
• prev2 = temp
• return prev1
𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 ( some condition is met )
• 𝐃𝐃𝐃𝐃 some work
Iterative algorithms 𝐅𝐅𝐅𝐅𝐅𝐅 (𝑖𝑖 = 1, 2, … up to 𝑖𝑖 = 𝑛𝑛)
• 𝐃𝐃𝐃𝐃 some work

• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

• Analysis of an iterative algorithm:


• Loop invariant:
• Some desirable conditions that should be satisfied at the start of each iteration.
• Initialization:
• The loop invariant is true at the start of the first iteration.
• Maintenance:
• If the loop invariant is satisfied at the start of the current iteration, then the loop
invariant must be satisfied at the start of the next iteration. Equivalently, the end of
• Termination: the current iteration.
• Loop invariant at the end of the last iteration → The algorithm outputs a correct answer.
𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 ( some condition is met )
• 𝐃𝐃𝐃𝐃 some work
Iterative algorithms 𝐅𝐅𝐅𝐅𝐅𝐅 (𝑖𝑖 = 1, 2, … up to 𝑖𝑖 = 𝑛𝑛)
• 𝐃𝐃𝐃𝐃 some work

• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

• Analysis of an iterative algorithm:


• Loop invariant:
• Some desirable conditions that should be satisfied at the start of each iteration.
• Initialization:
• The loop invariant is true at the start of the first iteration.
• Maintenance:
• If the loop invariant is satisfied at the start of the current iteration, then the loop
invariant must be satisfied at the start of the next iteration. Equivalently, the end of
• Termination: the current iteration.
• Loop invariant at the end of the last iteration → The algorithm outputs a correct answer.
𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 ( some condition is met )
• 𝐃𝐃𝐃𝐃 some work
Iterative algorithms 𝐅𝐅𝐅𝐅𝐅𝐅 (𝑖𝑖 = 1, 2, … up to 𝑖𝑖 = 𝑛𝑛)
• 𝐃𝐃𝐃𝐃 some work

• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

• Analysis of an iterative algorithm:


• Loop invariant:
• Some desirable conditions that should be satisfied at the start of each iteration.
• Initialization:
• The loop invariant is true at the start of the first iteration.
• Maintenance:
• If the loop invariant is satisfied at the start of the current iteration, then the loop
invariant must be satisfied at the start of the next iteration. Equivalently, the end of
• Termination: the current iteration.
• Loop invariant at the end of the last iteration → The algorithm outputs a correct answer.
𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 ( some condition is met )
• 𝐃𝐃𝐃𝐃 some work
Iterative algorithms 𝐅𝐅𝐅𝐅𝐅𝐅 (𝑖𝑖 = 1, 2, … up to 𝑖𝑖 = 𝑛𝑛)
• 𝐃𝐃𝐃𝐃 some work

• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

• Analysis of an iterative algorithm:


• Loop invariant:
• Some desirable conditions that should be satisfied at the start of each iteration.
• Initialization:
• The loop invariant is true at the start of the first iteration.
• Maintenance:
• If the loop invariant is satisfied at the start of the current iteration, then the loop
invariant must be satisfied at the start of the next iteration. Equivalently, the end of
• Termination: the current iteration.
• Loop invariant at the end of the last iteration → The algorithm outputs a correct answer.
𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 ( some condition is met )
• 𝐃𝐃𝐃𝐃 some work
Iterative algorithms 𝐅𝐅𝐅𝐅𝐅𝐅 (𝑖𝑖 = 1, 2, … up to 𝑖𝑖 = 𝑛𝑛)
• 𝐃𝐃𝐃𝐃 some work

• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

• Analysis of an iterative algorithm:


• Loop invariant: Induction hypothesis
• Some desirable conditions that should be satisfied at the start of each iteration.
• Initialization: Base case
• The loop invariant is true at the start of the first iteration.
• Maintenance: Inductive step
• If the loop invariant is satisfied at the start of the current iteration, then the loop
invariant must be satisfied at the start of the next iteration.
• Termination: The proof by induction implies the correctness of the algorithm
• Loop invariant at the end of the last iteration → The algorithm outputs a correct answer.
Fibonacci numbers
• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

𝐈𝐈𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
If 𝑛𝑛 ≤ 1, then the algorithm is correct.
• If 𝑛𝑛 ≤ 1
• 𝐅𝐅𝐅𝐅𝐅𝐅 0 = 0
• return 𝑛𝑛 • 𝐅𝐅𝐅𝐅𝐅𝐅 1 = 1
• Else,
• prev2 = 0
• prev1 = 1
• for 𝑖𝑖 = 2 to 𝑛𝑛
• temp = prev1
• prev1 = prev1+prev2
• prev2 = temp
• return prev1
Fibonacci numbers
• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

Now, consider the case of 𝑛𝑛 ≥ 2.


𝐈𝐈𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
• If 𝑛𝑛 ≤ 1
• return 𝑛𝑛 Loop invariant:
• Else, • At the start of iteration 𝑖𝑖,
• prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 2
• prev2 = 0
• prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1
• prev1 = 1
• for 𝑖𝑖 = 2 to 𝑛𝑛
• temp = prev1
• prev1 = prev1+prev2
• prev2 = temp
• return prev1
Fibonacci numbers
• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

Now, consider the case of 𝑛𝑛 ≥ 2.


𝐈𝐈𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
• If 𝑛𝑛 ≤ 1
• return 𝑛𝑛 Loop invariant:
• Else, • At the start of iteration 𝑖𝑖,
• prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 2
• prev2 = 0
• prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1
• prev1 = 1
• for 𝑖𝑖 = 2 to 𝑛𝑛 Initialization:
• temp = prev1 • At the start of iteration 𝑖𝑖 = 2,
• prev1 = prev1+prev2 • prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 0 = 0
• prev2 = temp • prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 1 = 1
• return prev1
Fibonacci numbers
• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

Now, consider the case of 𝑛𝑛 ≥ 2.


𝐈𝐈𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
• If 𝑛𝑛 ≤ 1
• return 𝑛𝑛 Loop invariant: Maintenance:
• Else, • At the start of iteration 𝑖𝑖, • Suppose at the start of iteration 𝑖𝑖,
• prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 2 • prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 2
• prev2 = 0
• prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1 • prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1
• prev1 = 1 • Then at the end of the iteration,
• for 𝑖𝑖 = 2 to 𝑛𝑛 Initialization: • prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1
𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1
𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1 • temp = prev1 • At the start of iteration 𝑖𝑖 = 2, • prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖
𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1 + 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 2
𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 • prev1 = prev1+prev2 • prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 0 = 0
𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1
𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1 • prev2 = temp • prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 1 = 1
• return prev1
Fibonacci numbers
• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

Now, consider the case of 𝑛𝑛 ≥ 2.


𝐈𝐈𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
• If 𝑛𝑛 ≤ 1
• return 𝑛𝑛 Loop invariant: Maintenance:
• Else, • At the start of iteration 𝑖𝑖, • Suppose at the start of iteration 𝑖𝑖,
• prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 2 • prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 2
• prev2 = 0
• prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1 • prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1 𝑖𝑖 = 𝑛𝑛
• prev1 = 1 • Then at the end of the iteration,
• for 𝑖𝑖 = 2 to 𝑛𝑛 Initialization: • prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖 − 1
• temp = prev1 • At the start of iteration 𝑖𝑖 = 2, • prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑖𝑖
• prev1 = prev1+prev2 • prev2 = 𝐅𝐅𝐅𝐅𝐅𝐅 0 = 0
• prev2 = temp • prev1 = 𝐅𝐅𝐅𝐅𝐅𝐅 1 = 1 Termination:
• return prev1 • The algorithm returns 𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛).
Question 1 @ VisuAlgo online quiz

• What is a suitable loop invariant to analyze this sorting algorithm?


at the start of iteration 𝑗𝑗
• 𝐴𝐴 is sorted.

• 𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted.

• 𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .

• (𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted) and (𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 ).

𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .

VisuAlgo (Selection sort): https://visualgo.net/en/sorting?mode=Selection


Answer

• What is a suitable loop invariant to analyze this sorting algorithm?


✗ Initialization ← In general, 𝐴𝐴 is not sorted at the start of the algorithm.
✗• 𝐴𝐴 is sorted. ✓ Maintenance
• 𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted. ✓ Termination

• 𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .

• (𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted) and (𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 ).

𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .

VisuAlgo (Selection sort): https://visualgo.net/en/sorting?mode=Selection


Answer

• What is a suitable loop invariant to analyze this sorting algorithm?


✗• 𝐴𝐴 is sorted.
✓ Initialization
✗• 𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted. ✗ Maintenance ← Maintenance fails if 𝐴𝐴 𝑗𝑗 − 1 is larger
✓ Termination than the smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• 𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .

• (𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted) and (𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 ).

𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .

VisuAlgo (Selection sort): https://visualgo.net/en/sorting?mode=Selection


Answer

• What is a suitable loop invariant to analyze this sorting algorithm?


✗• 𝐴𝐴 is sorted.
✗• 𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted. ✓ Initialization
✓ Maintenance
✗• 𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 . ✗ Termination ← The final loop invariant
is vacuously true.
• (𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted) and (𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 ).

𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .

VisuAlgo (Selection sort): https://visualgo.net/en/sorting?mode=Selection


Answer

• What is a suitable loop invariant to analyze this sorting algorithm?


✗• 𝐴𝐴 is sorted.
✗• 𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted.
Exercise:
✗• 𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 . ✓ Initialization
✓ Maintenance
✓• (𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted) and (𝑥𝑥 ≤ 𝑦𝑦 for all 𝑥𝑥 ∈ 𝐴𝐴 1. . 𝑗𝑗 − 1 and 𝑦𝑦 ∈ 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 ). ✓ Termination

𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .

VisuAlgo (Selection sort): https://visualgo.net/en/sorting?mode=Selection


Weighted directed graphs
• Let 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 be a directed graph.
• Each edge 𝑒𝑒 ∈ 𝐸𝐸 has a positive weight 𝑤𝑤 𝑒𝑒 .
• If 𝑢𝑢, 𝑣𝑣 ∈ 𝐸𝐸, then 𝑤𝑤 𝑢𝑢, 𝑣𝑣 = 𝑤𝑤 𝑒𝑒 , where 𝑒𝑒 = 𝑢𝑢, 𝑣𝑣 .
• If 𝑢𝑢, 𝑣𝑣 ∉ 𝐸𝐸, then 𝑤𝑤 𝑢𝑢, 𝑣𝑣 = ∞.

𝑤𝑤
5 1
1
𝑢𝑢 𝑣𝑣
1 3

𝑠𝑠
Single-source shortest paths
• Let 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 be a directed graph.
• Each edge 𝑒𝑒 ∈ 𝐸𝐸 has a positive weight 𝑤𝑤 𝑒𝑒 .
• If 𝑢𝑢, 𝑣𝑣 ∈ 𝐸𝐸, then 𝑤𝑤 𝑢𝑢, 𝑣𝑣 = 𝑤𝑤 𝑒𝑒 , where 𝑒𝑒 = 𝑢𝑢, 𝑣𝑣 .
• If 𝑢𝑢, 𝑣𝑣 ∉ 𝐸𝐸, then 𝑤𝑤 𝑢𝑢, 𝑣𝑣 = ∞.
• Goal: Given a source 𝑠𝑠 ∈ 𝑉𝑉, compute the shortest-path distance from
𝑠𝑠 to all vertices.
𝑤𝑤
5 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑠𝑠 =0
1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢 =1
𝑢𝑢 𝑣𝑣
𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 =2
1 3 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑤𝑤 =3
𝑠𝑠
Dijkstra’s algorithm
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅

𝑤𝑤
5 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑠𝑠 =0
1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢 =1
1 𝑢𝑢 𝑣𝑣 3
𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 =2
1 3 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑤𝑤 =3
𝑠𝑠
𝟎𝟎

VisuAlgo (Dijkstra): https://visualgo.net/en/sssp?slide=7


Dijkstra’s algorithm
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
∞ 6
𝑤𝑤 𝑤𝑤
5 1 5 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑠𝑠 =0
1 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢 =1
1 𝑢𝑢 𝑣𝑣 3 𝟏𝟏 𝑢𝑢 𝑣𝑣 2
𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 =2
1 3 1 3 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑤𝑤 =3
𝑠𝑠 𝑠𝑠
𝟎𝟎 𝟎𝟎

VisuAlgo (Dijkstra): https://visualgo.net/en/sssp?slide=7


Dijkstra’s algorithm
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
∞ 6 3
𝑤𝑤 𝑤𝑤 𝑤𝑤
5 1 5 1 5 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑠𝑠 =0
1 1 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢 =1
1 𝑢𝑢 𝑣𝑣 3 𝟏𝟏 𝑢𝑢 𝑣𝑣 2 𝟏𝟏 𝑢𝑢 𝑣𝑣 𝟐𝟐
𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 =2
1 3 1 3 1 3 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑤𝑤 =3
𝑠𝑠 𝑠𝑠 𝑠𝑠
𝟎𝟎 𝟎𝟎 𝟎𝟎

VisuAlgo (Dijkstra): https://visualgo.net/en/sssp?slide=7


Dijkstra’s algorithm
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
∞ 6 3 𝟑𝟑
𝑤𝑤 𝑤𝑤 𝑤𝑤 𝑤𝑤
5 1 5 1 5 1 5 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑠𝑠 =0
1 1 1 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢 =1
1 𝑢𝑢 𝑣𝑣 3 𝟏𝟏 𝑢𝑢 𝑣𝑣 2 𝟏𝟏 𝑢𝑢 𝑣𝑣 𝟐𝟐 𝟏𝟏 𝑢𝑢 𝑣𝑣 𝟐𝟐
𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 =2
1 3 1 3 1 3 1 3 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑤𝑤 =3
𝑠𝑠 𝑠𝑠 𝑠𝑠 𝑠𝑠
𝟎𝟎 𝟎𝟎 𝟎𝟎 𝟎𝟎

VisuAlgo (Dijkstra): https://visualgo.net/en/sssp?slide=7


Loop invariant:
• At the start of an iteration: The computed
distances are correct.
Proof of correctness • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
∞ 6 3 𝟑𝟑
𝑤𝑤 𝑤𝑤 𝑤𝑤 𝑤𝑤
5 1 5 1 5 1 5 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑠𝑠 =0
1 1 1 1 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢 =1
1 𝑢𝑢 𝑣𝑣 3 𝟏𝟏 𝑢𝑢 𝑣𝑣 2 𝟏𝟏 𝑢𝑢 𝑣𝑣 𝟐𝟐 𝟏𝟏 𝑢𝑢 𝑣𝑣 𝟐𝟐
𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 =2
1 3 1 3 1 3 1 3 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑤𝑤 =3
𝑠𝑠 𝑠𝑠 𝑠𝑠 𝑠𝑠
𝟎𝟎 𝟎𝟎 𝟎𝟎 𝟎𝟎

VisuAlgo (Dijkstra): https://visualgo.net/en/sssp?slide=7


Loop invariant:
• At the start of an iteration: The computed
distances are correct.
Proof of correctness • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢
𝑑𝑑 𝑠𝑠 = 0 is correct.

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
✓ Initialization
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Maintenance
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Termination
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
Loop invariant:
• At the start of an iteration: The computed
distances are correct.
Proof of correctness • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
✓ Initialization
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Maintenance
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Termination
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
At the end of the computation, 𝑅𝑅 = 𝑉𝑉
Loop invariant:
• At the start of an iteration: The computed
distances are correct.
Proof of correctness • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉 Claim: For the selected vertex 𝑣𝑣:


• 𝑑𝑑 𝑠𝑠 = 0 • 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
✓ Initialization
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Maintenance
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Termination
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
Loop invariant:
• At the start of an iteration: The computed
distances are correct.
Proof of correctness • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉 Claim: For the selected vertex 𝑣𝑣:


• 𝑑𝑑 𝑠𝑠 = 0 • 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
✓ Initialization
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Maintenance
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Termination
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅

Proof: Consider a shortest path 𝑃𝑃 from 𝑠𝑠 to 𝑣𝑣.


𝑠𝑠 𝑣𝑣

All vertices in 𝑃𝑃 ∖ 𝑣𝑣 must be in 𝑅𝑅.


• Otherwise, we should have selected the first vertex that is not in 𝑅𝑅.
Loop invariant:
• At the start of an iteration: The computed
distances are correct.
Proof of correctness • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉 Claim: For the selected vertex 𝑣𝑣:


• 𝑑𝑑 𝑠𝑠 = 0 • 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
✓ Initialization
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Maintenance
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Termination
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅

Proof: Consider a shortest path 𝑃𝑃 from 𝑠𝑠 to 𝑣𝑣.


𝑠𝑠 𝑦𝑦 𝑥𝑥 𝑣𝑣

All vertices in 𝑃𝑃 ∖ 𝑣𝑣 must be in 𝑅𝑅.


• Otherwise, we should have selected the first vertex that is not in 𝑅𝑅.
𝑥𝑥
𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣 ≥ 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 > 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑥𝑥 = 𝑑𝑑 𝑦𝑦 + 𝑤𝑤 𝑦𝑦, 𝑥𝑥 ≥ 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑥𝑥
𝑢𝑢∈𝑅𝑅 𝑢𝑢∈𝑅𝑅
𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑦𝑦
Loop invariant:
• At the start of an iteration: The computed
distances are correct.
Proof of correctness • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉 Claim: For the selected vertex 𝑣𝑣:


• 𝑑𝑑 𝑠𝑠 = 0 •✓ 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
✓ Initialization
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Maintenance
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Termination
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅

Proof: Consider a shortest path 𝑃𝑃 from 𝑠𝑠 to 𝑣𝑣.


𝑠𝑠 𝑢𝑢𝑢 𝑣𝑣

All vertices in 𝑃𝑃 ∖ 𝑣𝑣 must be in 𝑅𝑅.

𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣 ≥ 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 = 𝑑𝑑 𝑢𝑢′ + 𝑤𝑤 𝑢𝑢′, 𝑣𝑣 ≥ 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣


𝑢𝑢∈𝑅𝑅 𝑢𝑢∈𝑅𝑅
𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢𝑢
Loop invariant:
• At the start of an iteration: The computed
distances are correct.
Proof of correctness • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉 Claim: For the selected vertex 𝑣𝑣:


• 𝑑𝑑 𝑠𝑠 = 0 •✓ 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
✓ Initialization
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Maintenance
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
✓ Termination
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
Question 2 @ VisuAlgo online quiz
Who is the Master of Algorithms pictured below?

• Stephen Cook

• Edsger Dijkstra

• Robert Tarjan

• Avi Wigderson
Answer
Edsger Dijkstra
• 1972 Turing Award.

• Advocacy of structured programming:


• A programming paradigm that makes use of
structured control flow as opposed to unstructured
jumps to different sections.
• “Go To statement considered harmful”

“Computer Science is no more about computers


than astronomy is about telescopes.”
— Edsger Dijkstra

Source:
• https://en.wikipedia.org/wiki/Edsger_W._Dijkstra
• https://amturing.acm.org/award_winners/dijkstra_1053701.cfm
Answer
What is the shortest way to travel from Rotterdam to Groningen, in
general: from given city to given city. It is the algorithm for the shortest
path, which I designed in about twenty minutes. One morning I was
shopping in Amsterdam with my young fiancée, and tired, we sat down
on the café terrace to drink a cup of coffee and I was just thinking about
whether I could do this, and I then designed the algorithm for the
shortest path. As I said, it was a twenty-minute invention. In fact, it was
published in '59, three years later. The publication is still readable, it is,
in fact, quite nice. One of the reasons that it is so nice was that I
designed it without pencil and paper. I learned later that one of the
advantages of designing without pencil and paper is that you are almost
forced to avoid all avoidable complexities. Eventually, that algorithm
became to my great amazement, one of the cornerstones of my fame.

— Edsger Dijkstra, in an interview with Philip L. Frana,


Communications of the ACM, 2001

Source:
• https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
Efficiency
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅
Efficiency
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉 Observation: No need to compute
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣 𝑥𝑥 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅 𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣 from scratch for every iteration.
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅

Adding 𝑣𝑣 to 𝑅𝑅 can only affect the 𝑥𝑥-value of these vertices.


• 𝑥𝑥 𝑧𝑧 ← 𝐦𝐦𝐦𝐦𝐦𝐦 𝑥𝑥 𝑧𝑧 , 𝑑𝑑 𝑣𝑣 + 𝑤𝑤 𝑣𝑣, 𝑧𝑧 13 10
𝑧𝑧 𝑧𝑧
10 13 𝟗𝟗 1 1 1
8 𝑣𝑣 9 8 𝑣𝑣 𝟗𝟗
2 2
𝟓𝟓 𝟕𝟕 𝟓𝟓 𝟕𝟕
Efficiency
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉 Observation: No need to compute
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣 𝑥𝑥 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅 𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣 from scratch for every iteration.
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑥𝑥 𝑣𝑣 = ∞ for all 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑠𝑠
• 𝑅𝑅 = 𝑠𝑠
• For all 𝑧𝑧 ∈ 𝑉𝑉 such that 𝑠𝑠, 𝑧𝑧 ∈ 𝐸𝐸
• 𝑥𝑥 𝑧𝑧 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑥𝑥 𝑧𝑧 , 𝑑𝑑 𝑠𝑠 + 𝑤𝑤 𝑠𝑠, 𝑧𝑧
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝑥𝑥 𝑣𝑣
• 𝑑𝑑 𝑣𝑣 = 𝑥𝑥 𝑣𝑣
• Add 𝑣𝑣 to 𝑅𝑅
• For all 𝑧𝑧 ∈ 𝑉𝑉 ∖ 𝑅𝑅 such that 𝑣𝑣, 𝑧𝑧 ∈ 𝐸𝐸
• 𝑥𝑥 𝑧𝑧 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑥𝑥 𝑧𝑧 , 𝑑𝑑 𝑣𝑣 + 𝑤𝑤 𝑣𝑣, 𝑧𝑧
Efficiency
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0
• 𝑅𝑅 = 𝑠𝑠
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉 Observation: No need to compute
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣 𝑥𝑥 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣
𝑢𝑢∈𝑅𝑅 𝑢𝑢∈𝑅𝑅
• 𝑑𝑑 𝑣𝑣 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑑𝑑 𝑢𝑢 + 𝑤𝑤 𝑢𝑢, 𝑣𝑣 from scratch for every iteration.
𝑢𝑢∈𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 0 𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑥𝑥 𝑣𝑣 = ∞ for all 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑠𝑠 • 𝑅𝑅 = ∅
• 𝑅𝑅 = 𝑠𝑠 • 𝑥𝑥 𝑣𝑣 = ∞ for all 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑠𝑠
• For all 𝑧𝑧 ∈ 𝑉𝑉 such that 𝑠𝑠, 𝑧𝑧 ∈ 𝐸𝐸 • 𝑥𝑥 𝑠𝑠 = 0
• 𝑥𝑥 𝑧𝑧 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑥𝑥 𝑧𝑧 , 𝑑𝑑 𝑠𝑠 + 𝑤𝑤 𝑠𝑠, 𝑧𝑧 • 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉 • Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝑥𝑥 𝑣𝑣
• Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝑥𝑥 𝑣𝑣 • 𝑑𝑑 𝑣𝑣 = 𝑥𝑥 𝑣𝑣
• 𝑑𝑑 𝑣𝑣 = 𝑥𝑥 𝑣𝑣 • Add 𝑣𝑣 to 𝑅𝑅
• Add 𝑣𝑣 to 𝑅𝑅 • For all 𝑧𝑧 ∈ 𝑉𝑉 ∖ 𝑅𝑅 such that 𝑣𝑣, 𝑧𝑧 ∈ 𝐸𝐸
• For all 𝑧𝑧 ∈ 𝑉𝑉 ∖ 𝑅𝑅 such that 𝑣𝑣, 𝑧𝑧 ∈ 𝐸𝐸 • 𝑥𝑥 𝑧𝑧 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑥𝑥 𝑧𝑧 , 𝑑𝑑 𝑣𝑣 + 𝑤𝑤 𝑣𝑣, 𝑧𝑧
• 𝑥𝑥 𝑧𝑧 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑥𝑥 𝑧𝑧 , 𝑑𝑑 𝑣𝑣 + 𝑤𝑤 𝑣𝑣, 𝑧𝑧
Efficiency
• The algorithm can be implemented using a priority queue.

𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑅𝑅 = ∅
• 𝑥𝑥 𝑣𝑣 = ∞ for all 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑠𝑠
𝑉𝑉 insert • 𝑥𝑥 𝑠𝑠 = 0
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
𝑉𝑉 extract-min • Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝑥𝑥 𝑣𝑣
• 𝑑𝑑 𝑣𝑣 = 𝑥𝑥 𝑣𝑣
• Add 𝑣𝑣 to 𝑅𝑅
• For all 𝑧𝑧 ∈ 𝑉𝑉 ∖ 𝑅𝑅 such that 𝑣𝑣, 𝑧𝑧 ∈ 𝐸𝐸
At most 𝐸𝐸 decrease-key • 𝑥𝑥 𝑧𝑧 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑥𝑥 𝑧𝑧 , 𝑑𝑑 𝑣𝑣 + 𝑤𝑤 𝑣𝑣, 𝑧𝑧
Efficiency
• The algorithm can be implemented using a priority queue.
Insert Extract-min Decrease-key Time complexity of
Dijkstra’s algorithm
Binary heap 𝑂𝑂 log 𝑛𝑛 𝑂𝑂 log 𝑛𝑛 𝑂𝑂 log 𝑛𝑛 𝑂𝑂 𝐸𝐸 + 𝑉𝑉 log 𝑉𝑉
Fibonacci heap 𝑂𝑂 1 𝑂𝑂 log 𝑛𝑛 amortized 𝑂𝑂 1 amortized 𝑂𝑂 𝐸𝐸 + 𝑉𝑉 log 𝑉𝑉
https://en.wikipedia.org/wiki/Priority_queue
Amortized = average over 𝑛𝑛 operations.
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑅𝑅 = ∅
• 𝑥𝑥 𝑣𝑣 = ∞ for all 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑠𝑠
𝑉𝑉 insert • 𝑥𝑥 𝑠𝑠 = 0
• 𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖𝐖 𝑅𝑅 ≠ 𝑉𝑉
𝑉𝑉 extract-min • Select 𝑣𝑣 ∈ 𝑉𝑉 ∖ 𝑅𝑅 to minimize 𝑥𝑥 𝑣𝑣
• 𝑑𝑑 𝑣𝑣 = 𝑥𝑥 𝑣𝑣
• Add 𝑣𝑣 to 𝑅𝑅
• For all 𝑧𝑧 ∈ 𝑉𝑉 ∖ 𝑅𝑅 such that 𝑣𝑣, 𝑧𝑧 ∈ 𝐸𝐸
At most 𝐸𝐸 decrease-key • 𝑥𝑥 𝑧𝑧 = 𝐦𝐦𝐦𝐦𝐦𝐦 𝑥𝑥 𝑧𝑧 , 𝑑𝑑 𝑣𝑣 + 𝑤𝑤 𝑣𝑣, 𝑧𝑧
𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
Recursive algorithms • If 𝑛𝑛 ≤ 1, return 𝑛𝑛.
• Else, return 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 1 + 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 2 .

• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

• Analysis of a recursive algorithm:


• Base case: without any recursive calls
• Show that the algorithm is correct for the base case.
• Inductive step: with recursive calls
• Assume that the algorithm is correct for any input of size smaller than 𝑛𝑛.
• Show that the algorithm is correct for any input of size 𝑛𝑛.
𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
Recursive algorithms • If 𝑛𝑛 ≤ 1, return 𝑛𝑛.
• Else, return 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 1 + 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 2 .

• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

If 𝑛𝑛 ≤ 1, the algorithm is correct:


• Analysis of a recursive algorithm: • 𝐅𝐅𝐅𝐅𝐅𝐅 0 = 0
• Base case: • 𝐅𝐅𝐅𝐅𝐅𝐅 1 = 1
• Show that the algorithm is correct for the base case.
• Inductive step:
• Assume that the algorithm is correct for any input of size smaller than 𝑛𝑛.
• Show that the algorithm is correct for any input of size 𝑛𝑛.
𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
Recursive algorithms • If 𝑛𝑛 ≤ 1, return 𝑛𝑛.
• Else, return 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 1 + 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 2 .

• Goal: For a given algorithm 𝒜𝒜, prove that it is correct.

If 𝑛𝑛 ≤ 1, the algorithm is correct:


• Analysis of a recursive algorithm: • 𝐅𝐅𝐅𝐅𝐅𝐅 0 = 0
• Base case: • 𝐅𝐅𝐅𝐅𝐅𝐅 1 = 1
• Show that the algorithm is correct for the base case.
• Inductive step:
• Assume that the algorithm is correct for any input of size smaller than 𝑛𝑛.
• Show that the algorithm is correct for any input of size 𝑛𝑛.
If 𝑛𝑛 > 1, the algorithm is correct:
• 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 = 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 1 + 𝐅𝐅𝐅𝐅𝐅𝐅 𝑛𝑛 − 2
Searching in a sorted array
• Input:
• A sorted array 𝐴𝐴.
• Two indices:
• lb (lower bound)
• ub (upper bound)
• A number 𝑥𝑥.

• Goal:
• Decide if 𝑥𝑥 ∈ 𝐴𝐴 lb. . ub If lb > ub , the answer is NO.
Searching in a sorted array
• Input: 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, ub, 𝑥𝑥
• A sorted array 𝐴𝐴. • If lb > ub , return NO.
• Else
• Two indices: lb+ub
• lb (lower bound) • mid ← .
2
• ub (upper bound) • If 𝑥𝑥 = 𝐴𝐴 mid , return YES.
• If 𝑥𝑥 > 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, mid + 1, ub, 𝑥𝑥 .
• A number 𝑥𝑥.
• If 𝑥𝑥 < 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, mid − 1, 𝑥𝑥 .

• Goal:
• Decide if 𝑥𝑥 ∈ 𝐴𝐴 lb. . ub
Searching in a sorted array
𝑥𝑥 = 14
𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, ub, 𝑥𝑥
mid = 5
• If lb > ub , return NO.
2 7 14 33 41 50 77 80 82 • Else
lb = 1 ub = 9 lb+ub
• mid ← .
2
• If 𝑥𝑥 = 𝐴𝐴 mid , return YES.
mid = 2 • If 𝑥𝑥 > 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, mid + 1, ub, 𝑥𝑥 .
2 7 14 33 41 50 77 80 82 • If 𝑥𝑥 < 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, mid − 1, 𝑥𝑥 .
lb = 1 ub = 4

mid = 3
2 7 14 33 41 50 77 80 82
lb = 3 ub = 4

YES
Proof of correctness
• Induction on the array size: 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, ub, 𝑥𝑥
• 𝑛𝑛 = ub − lb + 1 • If lb > ub , return NO.
• Else
lb+ub
• mid ← .
2
• If 𝑥𝑥 = 𝐴𝐴 mid , return YES.
• If 𝑥𝑥 > 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, mid + 1, ub, 𝑥𝑥 .
• If 𝑥𝑥 < 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, mid − 1, 𝑥𝑥 .
Proof of correctness
• Induction on the array size: 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, ub, 𝑥𝑥
• 𝑛𝑛 = ub − lb + 1 • If lb > ub , return NO.
• Else
lb+ub
• Base case: • mid ←
2
.
• If 𝑛𝑛 < 1, the algorithm • If 𝑥𝑥 = 𝐴𝐴 mid , return YES.
correctly returns NO. • If 𝑥𝑥 > 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, mid + 1, ub, 𝑥𝑥 .
• If 𝑥𝑥 < 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, mid − 1, 𝑥𝑥 .
Proof of correctness
• Induction on the array size: 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, ub, 𝑥𝑥
• 𝑛𝑛 = ub − lb + 1 • If lb > ub , return NO.
• Else
lb+ub
• Inductive step: • mid ←
2
.
• Assume the algorithm works • If 𝑥𝑥 = 𝐴𝐴 mid , return YES.
correctly for any input of size • If 𝑥𝑥 > 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, mid + 1, ub, 𝑥𝑥 .
smaller than 𝑛𝑛. • If 𝑥𝑥 < 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, mid − 1, 𝑥𝑥 .

If 𝑥𝑥 = 𝐴𝐴 mid , the answer must be YES.

If 𝑥𝑥 > 𝐴𝐴 mid , then:


• 𝑥𝑥 ∈ 𝐴𝐴 lb. . ub if and only if 𝑥𝑥 ∈ 𝐴𝐴 mid + 1. . ub . 𝐴𝐴 is sorted
• Therefore, the answer must be 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, mid + 1, ub, 𝑥𝑥 . Induction hypothesis

The case of 𝑥𝑥 < 𝐴𝐴 mid is similar.


Efficiency
• Input size: 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, ub, 𝑥𝑥
• 𝑛𝑛 = ub − lb + 1 • If lb > ub , return NO.
• Else
• Subproblem input size: • mid ←
2
lb+ub
.
• ≤ 𝑛𝑛/2 • If 𝑥𝑥 = 𝐴𝐴 mid , return YES.
• If 𝑥𝑥 > 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, mid + 1, ub, 𝑥𝑥 .
• If 𝑥𝑥 < 𝐴𝐴 mid , 𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁𝐁 𝐴𝐴, lb, mid − 1, 𝑥𝑥 .
• Depth of recursion:
• 𝑂𝑂 log 𝑛𝑛

• Time complexity:
• 𝑇𝑇 𝑛𝑛 ∈ 𝑂𝑂 log 𝑛𝑛
Acknowledgement
• The slides are modified from previous editions of this course and
similar course elsewhere.
• List of credits:
• Diptarka Chakraborty
• Yi-Jun Chang
• Erik Demaine
• Steven Halim
• Sanjay Jain
• Wee Sun Lee
• Charles Leiserson
• Hon Wai Leong
• Wing-Kin Sung

You might also like