advanced counting techniques
play

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


  1. Advanced Counting Techniques CS1200, CSE IIT Madras Meghana Nasre April 9, 2020 CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  2. Advanced Counting Techniques • Principle of Inclusion-Exclusion � • Recurrences and its applications � • Solving Recurrences CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  3. Recurrences Recurrences occur many times especially in analysis of algorithms. CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  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

  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

  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

  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

  8. Some familiar recurrences Fibonacci Sequence Recurrence f ( n ) = n if n = 0 or n = 1 = f ( n − 1) + f ( n − 2) otherwise CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  9. Some familiar recurrences Fibonacci Sequence Recurrence f ( n ) = n if n = 0 or n = 1 = f ( n − 1) + f ( n − 2) otherwise Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  10. Some familiar recurrences Fibonacci Sequence Recurrence f ( n ) = n if n = 0 or n = 1 = f ( n − 1) + f ( n − 2) otherwise Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  11. Repeated Substitution Method : Example 1 assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  12. Repeated Substitution Method : Example 1 assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 � n � T ( n ) = + 1 T 2 � n � = T + 1 + 1 4 CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  13. Repeated Substitution Method : Example 1 assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 � n � T ( n ) = + 1 T 2 � n � n � � = T + 1 + 1 = T + 2 · 1 4 2 2 � n � = + 1 + 1 + 1 T 8 CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  14. Repeated Substitution Method : Example 1 assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 � n � T ( n ) = + 1 T 2 � n � n � � = T + 1 + 1 = T + 2 · 1 4 2 2 � n � n � � = + 1 + 1 + 1 = + 3 · 1 T T 8 2 3 . . . CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  15. Repeated Substitution Method : Example 1 assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 � n � T ( n ) = + 1 T 2 � n � n � � = T + 1 + 1 = T + 2 · 1 4 2 2 � n � n � � = + 1 + 1 + 1 = + 3 · 1 T T 8 2 3 . . . � n � = T + k · 1 observing a pattern 2 k CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  16. Repeated Substitution Method : Example 1 assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 � n � T ( n ) = + 1 T 2 � n � n � � = T + 1 + 1 = T + 2 · 1 4 2 2 � n � n � � = + 1 + 1 + 1 = + 3 · 1 T T 8 2 3 . . . � n � = T + k · 1 observing a pattern 2 k At this step we can guess that T ( n ) = log n + 1 . Recall n = 2 k for k ≥ 0. CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  17. Repeated Substitution Method : Example 1 assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 � n � T ( n ) = + 1 T 2 � n � n � � = T + 1 + 1 = T + 2 · 1 4 2 2 � n � n � � = + 1 + 1 + 1 = + 3 · 1 T T 8 2 3 . . . � n � = T + k · 1 observing a pattern 2 k At this step we can guess that T ( n ) = log n + 1 . Recall n = 2 k for k ≥ 0. How do we confirm the guess? CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  18. Repeated Substitution Method : Example 1 assume n = 2 k for k ≥ 0 Binary Search Recurrence T ( n ) = 1 if n = 1 � n � = T + 1 otherwise 2 � n � T ( n ) = + 1 T 2 � n � n � � = T + 1 + 1 = T + 2 · 1 4 2 2 � n � n � � = + 1 + 1 + 1 = + 3 · 1 T T 8 2 3 . . . � n � = T + k · 1 observing a pattern 2 k At this step we can guess that T ( n ) = log n + 1 . Recall n = 2 k 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

  19. Repeated Substitution Method : Example 2 Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  20. Repeated Substitution Method : Example 2 Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise T ( n ) = 2 T ( n − 1) + 1 = 2(2 T ( n − 2) + 1) + 1 2 2 T ( n − 2) + 2 + 1 = CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  21. Repeated Substitution Method : Example 2 Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise T ( n ) = 2 T ( n − 1) + 1 = 2(2 T ( n − 2) + 1) + 1 2 2 T ( n − 2) + 2 + 1 = 2 2 (2 T ( n − 3) + 1) + 2 + 1 = 2 3 T ( n − 3) + 2 2 + 2 + 1 = CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  22. Repeated Substitution Method : Example 2 Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise T ( n ) = 2 T ( n − 1) + 1 = 2(2 T ( n − 2) + 1) + 1 2 2 T ( n − 2) + 2 + 1 = 2 2 (2 T ( n − 3) + 1) + 2 + 1 = 2 3 T ( n − 3) + 2 2 + 2 + 1 = . . . k − 1 2 k T ( n − k ) + � 2 i = observing a pattern i =0 CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  23. Repeated Substitution Method : Example 2 Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise T ( n ) = 2 T ( n − 1) + 1 = 2(2 T ( n − 2) + 1) + 1 2 2 T ( n − 2) + 2 + 1 = 2 2 (2 T ( n − 3) + 1) + 2 + 1 = 2 3 T ( n − 3) + 2 2 + 2 + 1 = . . . k − 1 2 k T ( n − k ) + � 2 i = observing a pattern i =0 CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  24. Repeated Substitution Method : Example 2 Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise T ( n ) = 2 T ( n − 1) + 1 = 2(2 T ( n − 2) + 1) + 1 2 2 T ( n − 2) + 2 + 1 = 2 2 (2 T ( n − 3) + 1) + 2 + 1 = 2 3 T ( n − 3) + 2 2 + 2 + 1 = . . . k − 1 2 k T ( n − k ) + � 2 i = observing a pattern i =0 At this step we can guess that T ( n ) = 2 n − 1 . CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  25. Repeated Substitution Method : Example 2 Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise T ( n ) = 2 T ( n − 1) + 1 = 2(2 T ( n − 2) + 1) + 1 2 2 T ( n − 2) + 2 + 1 = 2 2 (2 T ( n − 3) + 1) + 2 + 1 = 2 3 T ( n − 3) + 2 2 + 2 + 1 = . . . k − 1 2 k T ( n − k ) + � 2 i = observing a pattern i =0 At this step we can guess that T ( n ) = 2 n − 1 . How do we confirm the guess? CS1200, CSE IIT Madras Meghana Nasre Advanced Counting Techniques

  26. Repeated Substitution Method : Example 2 Towers of Hanoi Recurrence T ( n ) = 1 if n = 1 = 2 T ( n − 1) + 1 otherwise T ( n ) = 2 T ( n − 1) + 1 = 2(2 T ( n − 2) + 1) + 1 2 2 T ( n − 2) + 2 + 1 = 2 2 (2 T ( n − 3) + 1) + 2 + 1 = 2 3 T ( n − 3) + 2 2 + 2 + 1 = . . . k − 1 2 k T ( n − k ) + � 2 i = observing a pattern i =0 At this step we can guess that T ( n ) = 2 n − 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

Recommend


More recommend