Cs3230-Lec03a-Full Version
Cs3230-Lec03a-Full Version
(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
𝐈𝐈𝐅𝐅𝐅𝐅𝐅𝐅(𝑛𝑛)
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.
• 𝐴𝐴 1. . 𝑗𝑗 − 1 is sorted.
𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .
𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .
𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .
𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .
𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒𝐒 𝐴𝐴 1. . 𝑛𝑛
• For 𝑗𝑗 = 1 to 𝑛𝑛 − 1
• Select 𝑠𝑠 ∈ {𝑗𝑗, 𝑗𝑗 + 1, … , 𝑛𝑛} so that 𝐴𝐴 𝑠𝑠 is a smallest number in 𝐴𝐴 𝑗𝑗. . 𝑛𝑛 .
• Swap 𝐴𝐴 𝑗𝑗 and 𝐴𝐴 𝑠𝑠 .
𝑤𝑤
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
𝑠𝑠
𝟎𝟎
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 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
𝑠𝑠 𝑠𝑠 𝑠𝑠 𝑠𝑠
𝟎𝟎 𝟎𝟎 𝟎𝟎 𝟎𝟎
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 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 • ∀𝑢𝑢 ∈ 𝑅𝑅, 𝑑𝑑 𝑢𝑢 = 𝐝𝐝𝐝𝐝𝐝𝐝𝐝𝐝 𝑠𝑠, 𝑢𝑢
• Stephen Cook
• Edsger Dijkstra
• Robert Tarjan
• Avi Wigderson
Answer
Edsger Dijkstra
• 1972 Turing Award.
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.
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 𝑅𝑅
𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃𝐃 𝐺𝐺 = 𝑉𝑉, 𝐸𝐸 , 𝑠𝑠 ∈ 𝑉𝑉
• 𝑑𝑑 𝑠𝑠 = 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:
• 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, 𝑥𝑥 .
• 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