CS4102 Algorithms Fall 2020 Workshop 2: Recurrence Relations - - PowerPoint PPT Presentation

β–Ά
cs4102 algorithms
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS4102 Algorithms

Fall 2020

Workshop 2: Recurrence Relations

slide-2
SLIDE 2

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-3
SLIDE 3

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-4
SLIDE 4

How efficient is it?

  • π‘ˆ π‘œ = π‘‘π‘π‘£π‘œπ‘’(π‘œ) + π‘ˆ

π‘œ 2

  • π‘ˆ π‘œ = 5 + π‘ˆ

π‘œ 2

  • Base case: π‘ˆ 1 = 1

Supposing the counting always requires looking at 5 inches of string

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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?
slide-7
SLIDE 7

Binary Search

  • How does it work?

– Input – Output

  • Implementation
  • What is its β€œrunning time”?

– Units?

slide-8
SLIDE 8

Binary Search Running Time

  • Counting Comparisons
  • 𝐷 π‘œ = 𝑦 + 𝐷

π‘œ 2

  • 𝑦 =? (worst case number of comparisons)
slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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.

slide-13
SLIDE 13

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!
slide-14
SLIDE 14

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

slide-15
SLIDE 15

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π‘œ

slide-16
SLIDE 16

How many exactly?

  • If we have 1 disk:

– β„Ž 1 =?

  • If we have 2 disks:

– β„Ž 2 =?

  • If we have 3 disks:

– β„Ž 3 =?

  • Pattern?
slide-17
SLIDE 17

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.

– 2 2𝑙 βˆ’ 1 + 1 = 2𝑙+1 βˆ’ 1