Advanced Counting Techniques CS1200, CSE IIT Madras Meghana Nasre - - PowerPoint PPT Presentation

advanced counting techniques
SMART_READER_LITE
LIVE PREVIEW

Advanced Counting Techniques CS1200, CSE IIT Madras Meghana Nasre - - PowerPoint PPT Presentation

Advanced Counting Techniques CS1200, CSE IIT Madras Meghana Nasre April 9, 2020 CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques Advanced Counting Techniques Principle of Inclusion-Exclusion Recurrences and its


slide-1
SLIDE 1

Advanced Counting Techniques

CS1200, CSE IIT Madras Meghana Nasre April 9, 2020

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-2
SLIDE 2

Advanced Counting Techniques

  • Principle of Inclusion-Exclusion
  • Recurrences and its applications
  • Solving Recurrences

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-3
SLIDE 3

Recurrences

Recurrences occur many times especially in analysis of algorithms.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-4
SLIDE 4

Recurrences

Recurrences occur many times especially in analysis of algorithms. Our goal: To obtain a closed form solution to the recurrence.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-5
SLIDE 5

Recurrences

Recurrences occur many times especially in analysis of algorithms. Our goal: To obtain a closed form solution to the recurrence. Why closed form?

  • We may want the 1000-th term which depends on the 999-th term.

Computing that with the recursive formulation is tedious.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-6
SLIDE 6

Recurrences

Recurrences occur many times especially in analysis of algorithms. Our goal: To obtain a closed form solution to the recurrence. Why closed form?

  • We may want the 1000-th term which depends on the 999-th term.

Computing that with the recursive formulation is tedious.

  • The recurrence may denote the running time of your algorithm. We want

an estimate on running time.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-7
SLIDE 7

Recurrences

Recurrences occur many times especially in analysis of algorithms. Our goal: To obtain a closed form solution to the recurrence. Why closed form?

  • We may want the 1000-th term which depends on the 999-th term.

Computing that with the recursive formulation is tedious.

  • The recurrence may denote the running time of your algorithm. We want

an estimate on running time. Today’s class: (Some) Techniques to solve recurrences.

There is no single recipe to solve all recurrences. However, we will show techniques that apply to a wide variety.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-8
SLIDE 8

Some familiar recurrences

Fibonacci Sequence Recurrence f (n) = n if n = 0 or n = 1 = f (n − 1) + f (n − 2)

  • therwise

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-9
SLIDE 9

Some familiar recurrences

Fibonacci Sequence Recurrence f (n) = n if n = 0 or n = 1 = f (n − 1) + f (n − 2)

  • therwise

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-10
SLIDE 10

Some familiar recurrences

Fibonacci Sequence Recurrence f (n) = n if n = 0 or n = 1 = f (n − 1) + f (n − 2)

  • therwise

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-11
SLIDE 11

Repeated Substitution Method : Example 1

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-12
SLIDE 12

Repeated Substitution Method : Example 1

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

T(n) = T n 2

  • + 1

= T n 4

  • + 1 + 1

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-13
SLIDE 13

Repeated Substitution Method : Example 1

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

T(n) = T n 2

  • + 1

= T n 4

  • + 1 + 1

= T n 22

  • + 2 · 1

= T n 8

  • + 1 + 1 + 1

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-14
SLIDE 14

Repeated Substitution Method : Example 1

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

T(n) = T n 2

  • + 1

= T n 4

  • + 1 + 1

= T n 22

  • + 2 · 1

= T n 8

  • + 1 + 1 + 1

= T n 23

  • + 3 · 1

. . .

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-15
SLIDE 15

Repeated Substitution Method : Example 1

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

T(n) = T n 2

  • + 1

= T n 4

  • + 1 + 1

= T n 22

  • + 2 · 1

= T n 8

  • + 1 + 1 + 1

= T n 23

  • + 3 · 1

. . . = T n 2k

  • + k · 1
  • bserving a pattern

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-16
SLIDE 16

Repeated Substitution Method : Example 1

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

T(n) = T n 2

  • + 1

= T n 4

  • + 1 + 1

= T n 22

  • + 2 · 1

= T n 8

  • + 1 + 1 + 1

= T n 23

  • + 3 · 1

. . . = T n 2k

  • + k · 1
  • bserving a pattern

At this step we can guess that T(n) = log n + 1 . Recall n = 2k for k ≥ 0.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-17
SLIDE 17

Repeated Substitution Method : Example 1

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

T(n) = T n 2

  • + 1

= T n 4

  • + 1 + 1

= T n 22

  • + 2 · 1

= T n 8

  • + 1 + 1 + 1

= T n 23

  • + 3 · 1

. . . = T n 2k

  • + k · 1
  • bserving a pattern

At this step we can guess that T(n) = log n + 1 . Recall n = 2k for k ≥ 0. How do we confirm the guess?

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-18
SLIDE 18

Repeated Substitution Method : Example 1

Binary Search Recurrence

assume n = 2k for k ≥ 0

T(n) = 1 if n = 1 = T n 2

  • + 1
  • therwise

T(n) = T n 2

  • + 1

= T n 4

  • + 1 + 1

= T n 22

  • + 2 · 1

= T n 8

  • + 1 + 1 + 1

= T n 23

  • + 3 · 1

. . . = T n 2k

  • + k · 1
  • bserving a pattern

At this step we can guess that T(n) = log n + 1 . Recall n = 2k for k ≥ 0. How do we confirm the guess? Ex: Use induction on n to prove the guess.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-19
SLIDE 19

Repeated Substitution Method : Example 2

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-20
SLIDE 20

Repeated Substitution Method : Example 2

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

T(n) = 2T(n − 1) + 1 = 2(2T(n − 2) + 1) + 1 = 22T(n − 2) + 2 + 1

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-21
SLIDE 21

Repeated Substitution Method : Example 2

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

T(n) = 2T(n − 1) + 1 = 2(2T(n − 2) + 1) + 1 = 22T(n − 2) + 2 + 1 = 22(2T(n − 3) + 1) + 2 + 1 = 23T(n − 3) + 22 + 2 + 1

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-22
SLIDE 22

Repeated Substitution Method : Example 2

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

T(n) = 2T(n − 1) + 1 = 2(2T(n − 2) + 1) + 1 = 22T(n − 2) + 2 + 1 = 22(2T(n − 3) + 1) + 2 + 1 = 23T(n − 3) + 22 + 2 + 1 . . . = 2kT(n − k) +

k−1

  • i=0

2i

  • bserving a pattern

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-23
SLIDE 23

Repeated Substitution Method : Example 2

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

T(n) = 2T(n − 1) + 1 = 2(2T(n − 2) + 1) + 1 = 22T(n − 2) + 2 + 1 = 22(2T(n − 3) + 1) + 2 + 1 = 23T(n − 3) + 22 + 2 + 1 . . . = 2kT(n − k) +

k−1

  • i=0

2i

  • bserving a pattern

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-24
SLIDE 24

Repeated Substitution Method : Example 2

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

T(n) = 2T(n − 1) + 1 = 2(2T(n − 2) + 1) + 1 = 22T(n − 2) + 2 + 1 = 22(2T(n − 3) + 1) + 2 + 1 = 23T(n − 3) + 22 + 2 + 1 . . . = 2kT(n − k) +

k−1

  • i=0

2i

  • bserving a pattern

At this step we can guess that T(n) = 2n − 1 .

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-25
SLIDE 25

Repeated Substitution Method : Example 2

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

T(n) = 2T(n − 1) + 1 = 2(2T(n − 2) + 1) + 1 = 22T(n − 2) + 2 + 1 = 22(2T(n − 3) + 1) + 2 + 1 = 23T(n − 3) + 22 + 2 + 1 . . . = 2kT(n − k) +

k−1

  • i=0

2i

  • bserving a pattern

At this step we can guess that T(n) = 2n − 1 . How do we confirm the guess?

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-26
SLIDE 26

Repeated Substitution Method : Example 2

Towers of Hanoi Recurrence T(n) = 1 if n = 1 = 2T(n − 1) + 1

  • therwise

T(n) = 2T(n − 1) + 1 = 2(2T(n − 2) + 1) + 1 = 22T(n − 2) + 2 + 1 = 22(2T(n − 3) + 1) + 2 + 1 = 23T(n − 3) + 22 + 2 + 1 . . . = 2kT(n − k) +

k−1

  • i=0

2i

  • bserving a pattern

At this step we can guess that T(n) = 2n − 1 . How do we confirm the guess? Ex: Use induction on n to prove the guess.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-27
SLIDE 27

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-28
SLIDE 28

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 3(3T(n − 2) + 2(n − 1)) + 2n

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-29
SLIDE 29

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 3(3T(n − 2) + 2(n − 1)) + 2n = 32T(n − 2) + 2(3(n − 1) + n))

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-30
SLIDE 30

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 3(3T(n − 2) + 2(n − 1)) + 2n = 32T(n − 2) + 2(3(n − 1) + n)) work out and observe a pattern . . .

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-31
SLIDE 31

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 3(3T(n − 2) + 2(n − 1)) + 2n = 32T(n − 2) + 2(3(n − 1) + n)) work out and observe a pattern . . . = 3kT(n − k) + 2

k−1

  • i=0

3i(n − i)

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-32
SLIDE 32

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 3(3T(n − 2) + 2(n − 1)) + 2n = 32T(n − 2) + 2(3(n − 1) + n)) work out and observe a pattern . . . = 3kT(n − k) + 2

k−1

  • i=0

3i(n − i) = 3nT(0) + 2

n−1

  • i=0

3i(n − i) = 2n n−1

  • i=0

3i

  • − 2

n−1

  • i=0

3ii

  • we need to solve n−1

i=0 3ii

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-33
SLIDE 33

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 2n n−1

  • i=0

3i

  • − 2

n−1

  • i=0

3ii

  • we need to solve n−1

i=0 3ii

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-34
SLIDE 34

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 2n n−1

  • i=0

3i

  • − 2

n−1

  • i=0

3ii

  • we need to solve n−1

i=0 3ii

A sub-problem to solve first. n−1

  • i=0

3ii

  • =

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-35
SLIDE 35

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 2n n−1

  • i=0

3i

  • − 2

n−1

  • i=0

3ii

  • we need to solve n−1

i=0 3ii

A sub-problem to solve first. n−1

  • i=0

3ii

  • = 3n(2n − 3) + 3

4

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-36
SLIDE 36

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 2n n−1

  • i=0

3i

  • − 2

n−1

  • i=0

3ii

  • we need to solve n−1

i=0 3ii

A sub-problem to solve first. n−1

  • i=0

3ii

  • = 3n(2n − 3) + 3

4 this needs a derivation and proof! In general: good to know sum of commonly occurring sums.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-37
SLIDE 37

Repeated Substitution Method : Example 3

Another recurrence T(n) = if n = 0 = 3T(n − 1) + 2n

  • therwise

T(n) = 3T(n − 1) + 2n = 2n n−1

  • i=0

3i

  • − 2

n−1

  • i=0

3ii

  • we need to solve n−1

i=0 3ii

= 2n · 3n + 3n+1 − 4n − 3 4 needs to be proved by induction A sub-problem to solve first. n−1

  • i=0

3ii

  • = 3n(2n − 3) + 3

4 this needs a derivation and proof! In general: good to know sum of commonly occurring sums.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-38
SLIDE 38

Repeated Substitution Method : Learnings

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-39
SLIDE 39

Repeated Substitution Method : Learnings

  • An elementary method to solve recurrences.

elementary does not mean simple, but a something that does not need background

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-40
SLIDE 40

Repeated Substitution Method : Learnings

  • An elementary method to solve recurrences.

elementary does not mean simple, but a something that does not need background

  • Need to observe a pattern.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-41
SLIDE 41

Repeated Substitution Method : Learnings

  • An elementary method to solve recurrences.

elementary does not mean simple, but a something that does not need background

  • Need to observe a pattern.
  • Oversimplification may make us miss the pattern.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-42
SLIDE 42

Repeated Substitution Method : Learnings

  • An elementary method to solve recurrences.

elementary does not mean simple, but a something that does not need background

  • Need to observe a pattern.
  • Oversimplification may make us miss the pattern.
  • Creativity and experience with summation of series help.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-43
SLIDE 43

Repeated Substitution Method : Learnings

  • An elementary method to solve recurrences.

elementary does not mean simple, but a something that does not need background

  • Need to observe a pattern.
  • Oversimplification may make us miss the pattern.
  • Creativity and experience with summation of series help.
  • However, the pattern has to be observed for each recurrence and there is

no generic rule. Are there some recurrences that can be solved by a formula?

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-44
SLIDE 44

Repeated Substitution Method : Learnings

  • An elementary method to solve recurrences.

elementary does not mean simple, but a something that does not need background

  • Need to observe a pattern.
  • Oversimplification may make us miss the pattern.
  • Creativity and experience with summation of series help.
  • However, the pattern has to be observed for each recurrence and there is

no generic rule. Are there some recurrences that can be solved by a formula? Ex: Solve by repeated substitution T(n) = 2 if n = 0 = 2

  • T(n − 1)
  • therwise

T(n) = 12 if n = 0 = 20 if n = 1 = 2T(n − 1) − T(n − 2)

  • therwise

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-45
SLIDE 45

Linear recurrences

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-46
SLIDE 46

Linear Homogeneous Recurrences with constant coefficients

an = c1an−1 + c2an−2 + . . . + ckan−k

1 ≤ i ≤ k, ci is a real number and ck = 0

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-47
SLIDE 47

Linear Homogeneous Recurrences with constant coefficients

an = c1an−1 + c2an−2 + . . . + ckan−k

1 ≤ i ≤ k, ci is a real number and ck = 0

  • Linear because an−1, an−2 . . . appear in separate terms and to the first

power.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-48
SLIDE 48

Linear Homogeneous Recurrences with constant coefficients

an = c1an−1 + c2an−2 + . . . + ckan−k

1 ≤ i ≤ k, ci is a real number and ck = 0

  • Linear because an−1, an−2 . . . appear in separate terms and to the first

power.

  • Homogeneous because degree of every term is the same. There is no

constant term.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-49
SLIDE 49

Linear Homogeneous Recurrences with constant coefficients

an = c1an−1 + c2an−2 + . . . + ckan−k

1 ≤ i ≤ k, ci is a real number and ck = 0

  • Linear because an−1, an−2 . . . appear in separate terms and to the first

power.

  • Homogeneous because degree of every term is the same. There is no

constant term.

  • Constant coefficients because c1, c2 . . . are reals which do not depend on n.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-50
SLIDE 50

Linear Homogeneous Recurrences with constant coefficients

an = c1an−1 + c2an−2 + . . . + ckan−k

1 ≤ i ≤ k, ci is a real number and ck = 0

  • Linear because an−1, an−2 . . . appear in separate terms and to the first

power.

  • Homogeneous because degree of every term is the same. There is no

constant term.

  • Constant coefficients because c1, c2 . . . are reals which do not depend on n.

Examples:

  • T(n) = 2T(n − 1) and T(0) = 1.
  • T(n) = T(n − 1) + T(n − 2) and T(0) = 0, T(1) = 1.

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-51
SLIDE 51

Linear Homogeneous Recurrences with constant coefficients

an = c1an−1 + c2an−2 + . . . + ckan−k

1 ≤ i ≤ k, ci is a real number and ck = 0

  • Linear because an−1, an−2 . . . appear in separate terms and to the first

power.

  • Homogeneous because degree of every term is the same. There is no

constant term.

  • Constant coefficients because c1, c2 . . . are reals which do not depend on n.

Examples:

  • T(n) = 2T(n − 1) and T(0) = 1.
  • T(n) = T(n − 1) + T(n − 2) and T(0) = 0, T(1) = 1.

Non Examples:

  • T(n) = nT(n − 1) and T(0) = 1 does not have constant coefficients
  • T(n) = T(n − 1) · T(n − 2) and T(0) = 0, T(1) = 1. not linear
  • T(n) = n · T(n − 1) and T(1) = 1. does not have constant coefficients

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

slide-52
SLIDE 52

Linear Homogeneous Recurrences with constant coefficients

an = c1an−1 + c2an−2 + . . . + ckan−k

1 ≤ i ≤ k, ci is a real number and ck = 0

Our goal: To derive a systematic way of solving such recurrences. Upcoming next week

  • A technique to solve linear homogeneous equations.
  • A technique to solve linear non-homogeneous equations.
  • References: Section 8.2[KR]

CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques