SLIDE 1 Academic Integrity
- Collaboration Encouraged!
– Groups of up to 5 per assignment (you + 4) – List your collaborators (by UVA computing ID)
- Write-ups/code written independently
– DO NOT share written notes / pictures / code – DO NOT share documents (ex: Overleaf)
- Be able to explain any solution you submit!
- DO NOT seek published solutions online
SLIDE 2 Late Policy
- By default, late submissions not accepted
- If something comes up that prevents you from submitting
quality work on time, let me know what’s going on
SLIDE 4 Regrades
- Conducted using the submission system:
– Submit within 5 days of receiving your grade – Request a regrade if the rubric was misapplied
SLIDE 5 Course webpage
- www.cs.virginia.edu/~njb2b/cs4102/su20
SLIDE 6 Extra “credit”
- Given for extraordinary acts of engagement
– Good questions/comments – Quality discussions – Analysis of current events – References to arts and music – Extra credit projects – Slide corrections – Etc. Just ask!
- Submit to me via email
- Will be used for qualitative grade adjustments
SLIDE 7
Warm up Can you cover an 8 × 8 grid with 1 square missing using “trominoes?”
Can you cover this? With these?
CS4102 Algorithms
Fall 2020
SLIDE 8 Where does it end?
- I have a pile of string
- I have one end of the string in my hand
- I need to find the other end
- How can I do this efficiently?
SLIDE 9 Rope End Finding
- 1. Set aside the already obtained end
- 2. Separate the pile of rope into 2 piles, note which
connects to the known end (call it pile A, the
- ther pile B)
- 3. Count the number of strands crossing the piles
- 4. If the count is even, pile A contains the end, else
pile B does
Repeat on pile with end
SLIDE 10 How efficient is it?
𝑜 2
𝑜 2
SLIDE 11
Let’s solve the recurrence!
𝑈 𝑜 = 5 + 𝑈(𝑜 2 ) 𝑈 1 = 1 5 + 𝑈(𝑜 4 ) 5 + 𝑈(𝑜 8 ) 1
log2 𝑜 𝑈 𝑜 = 5
log2𝑜 𝑗=0
+ 1 = 5 log2 𝑜 + 1
SLIDE 12 Algorithm Running Times
- We can’t just measure running time with a number
- Why shouldn’t we say: “The running time of this algorithm is 8.”?
- Units: 8 what? Seconds?
- What if the input is large?
- Algorithm running times are functions
– Domain: “sizes” of the algorithm’s input – Co-Domain: “counts” of operations
- We want to be able to say “algorithm 1 is faster than algorithm 2”
- How can we compare functions?
SLIDE 13 Asymptotic Notation*
– At most within constant of for large 𝑜 – {functions 𝑔|∃ constants 𝑑, 𝑜0 > 0 s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 ≤ 𝑑 ⋅ (𝑜)}
– At least within constant of for large 𝑜 – {functions 𝑔|∃ constants 𝑑, 𝑜0 > 0s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 ≥ 𝑑 ⋅ (𝑜)}
– “Tightly” within constant of for large 𝑜 – Ω 𝑜 ∩ 𝑃( 𝑜 )
*CLRS Chapter 3
SLIDE 14
𝑔(𝑜) = 𝑃( 𝑜 ) 𝑔(𝑜) = Θ( 𝑜 ) 𝑔(𝑜) = Ω( 𝑜 )
SLIDE 15 Asymptotic Notation
– Below any constant of for large 𝑜 – {functions 𝑔|∀ constants 𝑑, ∃𝑜0 s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 < 𝑑 ⋅ (𝑜)}
– Above any constant of for large 𝑜 – {functions 𝑔|∀ constants 𝑑, ∃𝑜0 s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 > 𝑑 ⋅ (𝑜)}
?
– 𝑝( 𝑜 ) ∩ 𝜕( 𝑜 ) = ∅
SLIDE 16 Asymptotic Notation Example
SLIDE 17 Asymptotic Notation Example
– Technique: Find 𝑑, 𝑜0 > 0 s.t. ∀𝑜 > 𝑜0, 𝑜 log 𝑜 ≤ 𝑑 ⋅ 𝑜2 – Proof: Let 𝑑 = 1, 𝑜0 = 1. Then, 𝑜0 log 𝑜0 = 1 log 1 = 0, 𝑑 𝑜0
2 = 1 ⋅ 12 = 1,
0 ≤ 1. ∀𝑜 ≥ 1, log 𝑜 < 𝑜 ⇒ 𝑜 log 𝑜 ≤ 𝑜2 □ Direct Proof!
SLIDE 18 Asymptotic Notation Example
SLIDE 19 Asymptotic Notation Example
– Technique: Contradiction – Proof: Assume 𝑜2 ∈ 𝑃 𝑜 . Then ∃𝑑, 𝑜0 > 0 s. t. ∀𝑜 > 𝑜0, 𝑜2 ≤ 𝑑𝑜 Let us derive constant 𝑑. For all 𝑜 > 𝑜0 > 0, we know: 𝑑𝑜 ≥ 𝑜2, 𝑑 ≥ 𝑜. Since 𝑑 is lower bounded by 𝑜, it is not a constant.
- Contradiction. Therefore 𝑜2 ∉ 𝑃 𝑜 . □
Proof by Contradiction!
SLIDE 20 Asymptotic Notation Example
= {functions 𝑔 ∶ ∀ constants 𝑑 > 0, ∃𝑜0 s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 < 𝑑 ⋅ (𝑜)}
– given any 𝑑 find a 𝑜0 > 0 s.t. ∀𝑜 > 𝑜0, 𝑜 log 𝑜 < 𝑑 ⋅ 𝑜2 – Find a value of 𝑜 in terms of 𝑑:
- 𝑜 log 𝑜 < 𝑑 ⋅ 𝑜2
- log 𝑜 < 𝑑 ⋅ 𝑜
- log 𝑜
𝑜
< 𝑑
– For a given 𝑑, select any value of 𝑜0 such that
log 𝑜 𝑜
< 𝑑
SLIDE 21
Trominoes Puzzle Solution
What about larger boards? 2𝑜 2𝑜
SLIDE 22
Trominoes Puzzle Solution
Divide the board into quadrants
SLIDE 23
Trominoes Puzzle Solution
Place a tromino to occupy the three quadrants without the missing piece
SLIDE 24
Trominoes Puzzle Solution
Each quadrant is now a smaller subproblem
SLIDE 25
Trominoes Puzzle Solution
Solve Recursively
SLIDE 26
Divide and Conquer
Our first algorithmic technique!
SLIDE 27
Trominoes Puzzle Solution
SLIDE 28 Divide and Conquer*
– Break the problem into multiple subproblems, each smaller instances of the original
– If the suproblems are “large”:
- Solve each subproblem recursively
– If the subproblems are “small”:
- Solve them directly (base case)
- Combine:
– Merge together solutions to subproblems
*CLRS Chapter 4
When is this a good strategy?
SLIDE 29 Analyzing Divide and Conquer
- 1. Break into smaller subproblems
- 2. Use recurrence relation to express recursive running time
- 3. Use asymptotic notation to simplify
- Divide: 𝐸(𝑜) time,
- Conquer: recurse on small problems, size 𝑡
- Combine: C(𝑜) time
- Recurrence:
– 𝑈 𝑜 = 𝐸 𝑜 + 𝑈(𝑡) + 𝐷(𝑜)
SLIDE 30 Recurrence Solving Techniques
Tree Guess/Check “Cookbook”
30
?
SLIDE 31 Merge Sort
– Break 𝑜-element list into two lists of 𝑜 2 elements
– If 𝑜 > 1:
- Sort each sublist recursively
– If 𝑜 = 1:
- List is already sorted (base case)
- Combine:
– Merge together sorted sublists into one sorted list
31
SLIDE 32 Merge
- Combine: Merge sorted sublists into one sorted list
- We have:
– 2 sorted lists (𝑀1, 𝑀2) – 1 output list (𝑀𝑝𝑣𝑢)
While (𝑀1 and 𝑀2 not empty): If 𝑀1 0 ≤ 𝑀2[0]: 𝑀𝑝𝑣𝑢.append(𝑀1.pop()) Else: 𝑀𝑝𝑣𝑢.append(𝑀2.pop()) 𝑀𝑝𝑣𝑢.append(𝑀1) 𝑀𝑝𝑣𝑢.append(𝑀2)
32
𝑃(𝑜)
SLIDE 33 Analyzing Merge Sort
- 1. Break into smaller subproblems
- 2. Use recurrence relation to express recursive running time
- 3. Use asymptotic notation to simplify
- Divide: 0 comparisons
- Conquer: recurse on 2 small subproblems, size 𝑜
2
- Combine: 𝑜 comparisons
- Recurrence:
– 𝑈 𝑜 = 2 𝑈
𝑜 2 + 𝑜
33
SLIDE 34 Recurrence Solving Techniques
Tree Guess/Check “Cookbook”
34
?
SLIDE 35 Tree method
35
𝑜 total / level log2 𝑜 levels
𝑜
𝑈 𝑜 = 2𝑈(𝑜 2 ) + 𝑜
𝑈 𝑜 = 𝑜
log2 𝑜 𝑗=1
= 𝑜 log2 𝑜
𝑜 2 𝑜 2 𝑜 4 𝑜 4 𝑜 4 𝑜 4
… … … …
1 1 1
…
1 1 1
𝑜 𝑜 2 𝑜 2 𝑜 4 𝑜 4 𝑜 4 𝑜 4 1 1 1 1 1 1