CS4102 Algorithms Fall 2020 Workshop 2: Recurrence Relations - - PowerPoint PPT Presentation
CS4102 Algorithms Fall 2020 Workshop 2: Recurrence Relations - - PowerPoint PPT Presentation
CS4102 Algorithms Fall 2020 Workshop 2: Recurrence Relations 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? Rope End Finding 1. Set
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?
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
How efficient is it?
- π π = πππ£ππ’(π) + π
π 2
- π π = 5 + π
π 2
- Base case: π 1 = 1
Supposing the counting always requires looking at 5 inches of string
Letβs solve the recurrence!
π π = 5 + π(π 2 ) π 1 = 1 5 + π(π 4 ) 5 + π(π 8 ) 1
log2 π π π = 5
log2π π=0
+ 1 = 5 log2 π + 1
Letβs talk about this
Structure of the Recursion
- Idea: cut the string in half until its length is no more than 1
inch long.
- What did we do between cuts?
β Count the βcrossing strandsβ β We estimate we need to look at 5 inches of string to do this
- Overall: we check 5 inches of string per cut.
- How many times must I cut it?
Binary Search
- How does it work?
β Input β Output
- Implementation
- What is its βrunning timeβ?
β Units?
Binary Search Running Time
- Counting Comparisons
- π· π = π¦ + π·
π 2
- π¦ =? (worst case number of comparisons)
What if the length is odd?
- What happens if the length of the list is odd?
- π· 13 = 4 + π·
13 2
= 4 + C 6.5
- We canβt have a list of length 6.5, how do we count
comparisons?
1 2 3 4 5 6 7 8 9 10 11 12 13
What if the length is odd?
- What happens if the length of the list is odd?
- π· 17 = 4 + π·
13 2
- Certainly, π· 13 β₯ π· 8
- Also, π· 13 β€ π·(16)
- What is π· 16 β π· 8 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
What if the length is odd?
- Just round up to the next power of 2, weβre not going to be
much worse than that (asymptotically)!
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Towers of Hanoi
On the step of the altar in the temple of Benares, for many, many years Brahmins have been moving a tower of sixty-four golden disks from one pole to another, one by one, never placing a larger
- n top of a smaller. When all the disks have been transferred the
Tower and the Brahmins will fall, and it will be the end of the world.
How many moves does it take?
- To solve for π discs:
β Move everything on top of the largest disc (π β 1) β Move the largest disc β Move everything back on top of the largest disc
- Define β: β β β s.t. β(π) gives the number of moves required
to move π discs
- β π = 2β π β 1 + 1
- β 1 = 1
- Recursive definition!
How many moves?
14
π
β π = 2β(π β 1) + 1
π β 1 π β 1 π β 2 π β 2 π β 2 π β 2
β¦ β¦ β¦ β¦
1 1 1
β¦
1 1 1
Move the largest disk Move a stack of π disks Move a stack of π β 1 disks Move a stack of π β 1 disks
Move a stack of π β 2 disks Move the largest disk Move a stack of π β 2 disks Move a stack of π β 2 disks Move the largest disk Move a stack of π β 2 disks
β(π β 1) moves β(π β 1) moves 1 move
How many moves?
15
π levels
- f recursion
π
β π = 2β(π β 1) + 1
π β 1 π β 1 π β 2 π β 2 π β 2 π β 2
β¦ β¦ β¦ β¦
1 1 1
β¦
1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
Number of moves doubles per level Should be β 2π
How many exactly?
- If we have 1 disk:
β β 1 =?
- If we have 2 disks:
β β 2 =?
- If we have 3 disks:
β β 3 =?
- Pattern?
Claim: Hanoi requires 2π β 1 moves
- To Show: β π = 2β π β 1 + 1 = 2π β 1
- Base Case:β 1 = 1 = 21 β 1
- Inductive Hypothesis: Assume h π = 2π β 1 for arbitrary π
- Inductive Step: Show that β π + 1 = 2π+1 β 1
β β π + 1 = 2β π + 1 Definition of β β 2β π + 1 = 2(2π β 1) + 1
- Ind. Hyp.