lecture$16$ induc=on$and$recurrence$relaons$

33
CSE 21 Math for Algorithms and Systems Analysis Lecture 16 Induc=on and Recurrence Rela=ons

Upload: others

Post on 24-Mar-2022

0 views

Category:

Documents


0 download

TRANSCRIPT

CSE  21  Math  for  Algorithms  and  Systems  

Analysis  Lecture  16  

Induc=on  and  Recurrence  Rela=ons  

Outline  

•  Induc=ve  Proofs  •  Determining  recurrence  rela=ons  •  Solving  first-­‐order  linear  recurrences  

General  Procedure  for  Proof  by  Induc=on  

•  Goal:  show  that  

•  Proof  outline  – Step  1:      – Step  2:  Given  

     Show    

f(n) = g(n), ∀n ≥ 0, n ∈ Z

f(0) = g(0)

f(n− 1) = g(n− 1)

f(n) = g(n)

Example  –  Fibonacci  Numbers  

•  First  7  Fibonacci  numbers:  0,  1,  1,  2,  3,  5,  8  •  The  nth  Fibonacci  number  is  given  by  the  recurrence:  

F (n) = F (n− 1) + F (n− 2), F (0) = 0, F (1) = 1

Fibonacci  Iden==es  n�

k=1

F (k) = F (n+ 2)− 1

Base  Case:  0�

k=1

F (0) = 0 = 1− 1 = F (2)− 1

Fibonacci  Induc=ve  Step  

Assume :n−1�

k=1

F (k) = F (n+ 1)− 1

Goal :n�

k=1

F (k) = F (n+ 2)− 1

n�

k=1

F (k) = F (n) +n−1�

k=1

F (k)

= F (n) + F (n+ 1)− 1

= F (n+ 2)− 1

Fibonacci  Proof  #2  

F (n)2 = F (n+ 1)F (n− 1) + (−1)n+1

Base  Case:  

F (1) = 1 = (1)(0) + (−1)2 = F (2)F (0) + (−1)2

Fibonacci  Proof  #2  Induc=ve  Step  Assume : F (n− 1)2 = F (n)F (n− 2) + (−1)n

Goal : F (n)2 = F (n+ 1)F (n− 1) + (−1)n+1

F (n)2 = F (n)(F (n− 1) + F (n− 2))

= F (n)F (n− 1) + F (n)F (n− 2)

= F (n− 1)(F (n) + F (n− 1))− (−1)n

= F (n− 1)F (n+ 1) + (−1)n+1

= F (n)F (n− 1) + F (n− 1)2 − (−1)n

Determining  Recurrence  Rela=onships  

•  OWen  it  is  helpful  to  expression  quan==es  recursively  

•  Examples:  – Running  =mes  of  recursive  algorithms  

– Op=mal  solu=ons  to  logic  puzzles  – Determining  the  number  of  sequences  in  {0,1}n  with  certain  constraints  on  the  arrangements  

Merge  Sort:  T(n)  =  2T(n/2)  +  n  

Towers  of  Hanoi  

Towers  of  Hanoi  

•  Goal:  move  the  en=re  stack  of  n  disks  from  the  star=ng  peg  to  another  peg  

•  Constraints:  – Only  the  topmost  disk  on  any  peg  can  be  moved  – A  larger  disk  must  never  be  on  top  of  a  smaller  disk  

•  How  can  we  determine  the  minimum  number  of  moves  to  solve  this  puzzle?  

Towers  of  Hanoi  

•  Strategy:  let’s  try  to  define  a  recursive  formula  •  Let  f(n)  be  the  minimum  number  of  moves  to  solve  the  tower  of  Hanoi  puzzle  with  n  disks  

•  f(1)  =  1  •  What  about  f(n)?  

Towers  of  Hanoi  •  Let’s  try  to  come  up  with  a  configura=on  the  disks  must  pass  through  in  any  solu=on  to  the  puzzle  

Star=ng  Configura=on  

Necessary  Configura=on  

Goal  Peg  

Construct  a  Recurrence  for  the  Tower  of  Hanoi  -­‐  Workspace  

Compute  a  Few  Values  of  The  Tower  of  Hanoi  Recurrence  

n   H(n)  

1  

2  

3  

4  

5  

Arrangements  of  Blue  and  Red  Tiles  

•  Suppose  we  want  to  create  a  sequence  of  blue  and  red  =les  such  that  no  blue  =le  is  in  between  two  red  =les      

•  How  many  sequences  of  length  n  are  there  that  obey  this  constraint?  

Sequences  of  Red  and  Blue  Tiles  

•  f(1)  =  2,  f(2)  =  4,  f(3)  =  7  •  Sequence  start:  

 How  many  sequences  can  follow?  f(n-­‐1)  

How  many  sequences  can  follow?  

Introducing  a  New  Recurrence  

•  Let  g  be  the  number  of  sequences  of  length  n  that:  

•  Do  not  start  with:  

•  Do  not  contain:  

•  g(1)  =  2,  g(2)  =  3  

Arrangements  of  Blue  and  Red  Tiles  

•  Now  we  try  to  construct  a  recurrence  for  g  •  Start  Sequence      How  many  can  follow?  

g(n-­‐1)  

f(n-­‐2)  

Back  to  our  original  recurrence  

•  f(1)  =  2,  f(2)  =  4,  f(3)  =  7  •  Sequence  start:  

 How  many  sequences  can  follow?  f(n-­‐1)  

How  many  sequences  can  follow?  g(n-­‐1)  

Mul=choose  and  Recursion  

•  We  are  dividing  50  cookies  between  8  math  majors  and  4  biology  majors  

•  Addi=onally  we  require  that  each  math  major  gets  no  more  than  3  cookies  

•  How  many  such  alloca=ons  are  there?    

Workspace  

More  Complicated  Towers  of  Hanoi  

•  Addi=onal  Movement  Constraints  (only  moves  between  A  and  B,  B  and  C,  and  C  and  A  are  allowed)  

A  

Goal  Peg  

B   C  

Recurrence  1  

A  

Goal  Peg  

B   C  

Necessary  Configura=on  

Workspace  –  Recurrence  2  

A  

Goal  Peg  

B   C  

Necessary  Configura=on  1  

Necessary  Configura=on  2  

A  

Goal  Peg  

B   C  

Workspace  

A  

Goal  Peg  

B   C  

Necessary  Configura=on  3  

Finding  Closed  form  Expressions  for  Recursive  Formulas  

•  Suppose  we  have  the  following  recursive  formula  

•  Let’s  compute  some  values  

n   a(n)  0   1  

1   5  

2   17  

3   53  

4   161  

a(n) = 3a(n− 1) + 2, a(0) = 1

Given  a  candidate  solu=on,  how  can  we  prove  it  is  correct?  

•  Show  via  induc=on  that:   a(n) = 2× 3n − 1

Workspace  

Solving  First-­‐Order  Linear  Recurrences  

•  Given  a  recursive  formula  of  the  form  

•  A  non-­‐recursive  formula  is  given  as  

a(n) = ba(n− 1) + c

a(n) =

�a(0)− c

1− b

�bn +

c

1− b

Proof  Via  Induc=on  

•  Base  case:  

•  Induc=ve  step  a(0) =

�a(0)− c

1− b

�b0 +

c

1− b= a(0)

Assume : a(n− 1) =

�a(0)− c

1− b

�bn−1 +

c

1− b

Goal : a(n) =

�a(0)− c

1− b

�bn +

c

1− b

a(n) = ba(n− 1) + c

= b

��a(0)− c

1− b

�bn−1 +

c

1− b

�+ c

Induc=on  Con=nued  

=

�a(0)− c

1− b

�bn +

bc

1− b+

c(1− b)

1− b

=

�a(0)− c

1− b

�bn +

bc+ c− cb

1− b

=

�a(0)− c

1− b

�bn +

c

1− b

=

�a(0)− c

1− b

�bn +

bc

1− b+

c(1− b)

1− b

= b

��a(0)− c

1− b

�bn−1 +

c

1− b

�+ c

General  Procedure  For  Proving  a  Solu=on  to  a  Recurrence  Rela=on  

•  Show  it  works  for  the  base  case  •  Show  that  it  sa=sfies  the  recurrence  by  plugging  in  the  proposed  solu=on