Lec 16
Lec 16
Complexity
Prasad Chaugule
1 / 26
What is an algorithm?
2 / 26
How to prove that your algorithm is correct (or incorrect)?
3 / 26
• Prove or Disprove: ⌈x + y ⌉ = ⌈x⌉ + ⌈y ⌉
4 / 26
• Prove or Disprove: ⌈x + y ⌉ = ⌈x⌉ + ⌈y ⌉
4 / 26
• Prove or Disprove: ⌈x + y ⌉ = ⌈x⌉ + ⌈y ⌉
4 / 26
• Prove or Disprove: ⌈x + y ⌉ = ⌈x⌉ + ⌈y ⌉
4 / 26
5 / 26
Use Mathematical Induction to prove the correctness
6 / 26
Mathematical Induction
7 / 26
Solution
8 / 26
Algorithm - The cent problem
9 / 26
Loop invariant
10 / 26
Loop invariant examples
11 / 26
Loop invariant: BST
12 / 26
Greedy Algorithm Example
13 / 26
One possible Algorithm?
14 / 26
Arguing its incorrectness by counterexample
15 / 26
Another attempt!
16 / 26
Correctness of greedy interval scheduling solution
17 / 26
Big O Notation
Definition
A function f (n) is O(g (n)), if for k ≥ k0 , f (n) ≤ c × g (n).
18 / 26
Big O Notation
Definition
A function f (n) is O(g (n)), if for k ≥ k0 , f (n) ≤ c × g (n).
18 / 26
Time complexity
19 / 26
Time complexity - continued
20 / 26
Time complexity– continued
21 / 26
Examples
22 / 26
Examples
22 / 26
Examples
22 / 26
Examples
22 / 26
Examples
22 / 26
Examples
22 / 26
Examples
22 / 26
What is the complexity?
23 / 26
What is the complexity?
24 / 26
What is the complexity?
25 / 26
26 / 26