Recursion Lecture 18: Nov 18.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Week 6 - Wednesday CS322.
Advanced Counting Techniques
Chapter Recurrence Relations
Chapter 6 Advanced Counting 6.1 Recurrence Relations.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Lecture 3: Parallel Algorithm Design
1 More Counting by Mapping Lecture 16: Nov 7. 2 This Lecture Division rule Catalan number.
Recursive Definitions and Structural Induction
Analysis of Algorithms
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Recursion Lecture 17: Nov 11. Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
6.Advanced Counting Techniques 1 Copyright M.R.K. Krishna Rao 2003 Ch 6. Recurrence Relations A recurrence relation for the sequence {a n } is an equation.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Copyright © Cengage Learning. All rights reserved.
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
Week 6 - Monday CS322.
Divide-and-Conquer 7 2  9 4   2   4   7
An Example in the Design and Analysis of an Algorithm Demonstrates: -- Recurrence Relations -- Design Techniques -- The fact that analysis provide useful.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
Module #20 - Recurrences 1 Ch. 7 Recurrence Relations Rosen 6 th ed., §§7.1.
R. Johnsonbaugh Discrete Mathematics 7 th edition, 2009 Chapter 7 Recurrence Relations Instructor Tianping Shuai.
Module #17: Recurrence Relations Rosen 5 th ed., §
Recursion. Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do if I call hello(10)?
Chapter 7 Advance Counting Techniques. Content Recurrence relations Generating function The principle of inclusion-exclusion.
Section 2.4. Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations.
Mathematical Induction I Lecture 4: Sep 16. This Lecture Last time we have discussed different proof techniques. This time we will focus on probably the.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Generating Functions and Counting Trees. Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve.
Discrete Mathematics Recurrence Relations Chapter 5 R. Johnsonbaugh
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Divide and Conquer Prudence Wong
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Lecture 3: Parallel Algorithm Design
COMP108 Algorithmic Foundations Divide and Conquer
COMP108 Algorithmic Foundations Divide and Conquer
Introduction to Recurrence Relations
CS2210:0001Discrete Structures Induction and Recursion
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Recursion Lecture 18: Nov 18

Plan Recursion is one of the most important techniques in computer science. The main idea is to “reduce” a problem into the same but smaller problems. Setting up recurrences Fibonacci recurrence Problem solving recurrences Catalan recurrences

Recursively Defined Sequences We can define a sequence by specifying relation between the current term and the previous terms. Arithmetic sequence: (a, a+d, a+2d, a+3d, …, ) recursive definition: a0=a, ai+1=ai+d Geometric sequence: (a, ra, r2a, r3a, …, ) recursive definition: a0=a, ai+1=rai Harmonic sequence: (1, 1/2, 1/3, 1/4, …, ) recursive definition: a0=1, ai+1=iai/(i+1)

Rabbit Populations The Rabbit Population A mature boy/girl rabbit pair reproduces every month. Rabbits mature after one month. wn::= # newborn pairs after n months rn::= # reproducing pairs after n months Start with a newborn pair: w0 =1, r0 = 0 How many rabbits after n months?

Rabbit Populations wn::= # newborn pairs after n months rn::= # reproducing pairs after n months r1 = 1 rn = rn-1 + wn-1 wn = rn-1 so rn = rn-1 + rn-2 It was Fibonacci who was studying rabbit population growth. We will compute the closed form for rn in the next lecture.

Warm Up We will solve counting problems by setting up recurrence relations. First we use recursion to count something we already know, to get familiar with this approach. Let us count the number of elements in pow(Sn) where Sn = {a1, a2, …, an} is an n-element set. Let rn be the size of pow(Sn). Then r1 = 2, where pow(S1) = {Ф, {a1}} r2 = 4, where pow(S2) = {Ф, {a1}, {a2}, {a1,a2}}

Warm Up Let rn be the size of pow(Sn) where Sn = {a1, a2, …, an} is an n-element set. Then r1 = 2, where pow(S1) = {Ф, {a1}} r2 = 4, where pow(S2) = {Ф, {a1}, {a2}, {a1,a2}} The main idea of recursion is to define rn in terms of the previous ri. How to define r3 in terms of r1 and r2? pow(S3) = the union of {Ф, {a1}, {a2}, {a1,a2}} and {a3, {a1,a3}, {a2,a3}, {a1,a2,a3}} So r3 = 2r2. while the lower sets are obtained by adding a3 to the upper sets.

Warm Up Let rn be the size of pow(Sn) where Sn = {a1, a2, …, an} is an n-element set. The main idea of recursion is to define rn in terms of the previous ri. pow(Sn) = the union of Sn-1 = {Ф, {a1}, {a2}, …, {a1,a2,…,an-1}} and {an, {a1,an}, {a2,an}, …, {a1,a2,…,an-1,an}} while the lower sets are obtained by adding an to the upper sets. Every subset is counted exactly once. So rn = 2rn-1. Solving this recurrence relation will show that rn = 2n (geometric sequence).

Number of Bit Strings without a Specific Pattern How many n-bit string without the bit pattern 11? Let rn be the number of such strings. e.g. r1 = |{0, 1}| = 2, r2 = |{00, 01, 10}| = 3 r3 = |{000, 001, 010, 100, 101}| = 5 r4 = |{0000, 0001, 0010, 0100, 0101, 1000, 1001, 1010}| = 8 Can you see the pattern? r4 = |{0000, 0001, 0010, 0100, 0101} union {1000, 1001, 1010}| = 5 + 3 = 8

Number of Bit Strings without a Specific Pattern How many n-bit string without the bit pattern 11? Let rn be the number of such strings. How do we compute it using r1,r2,…,rn-1? Case 1: The first bit is 0. Then any (n-1)-bit string without the bit pattern 11 can be appended to the end to form a n-bit string without 11. So in this case there are exactly rn-1 such n-bit strings. 00000000000000000 00000000000000001 … 1010101010101010101 The set of all (n-1)-bit strings without 11. Totally rn-1 of them. +

Number of Bit Strings without a Specific Pattern How many n-bit string without the bit pattern 11? Let rn be the number of such strings. How do we compute it using r1,r2,…,rn-1? Case 2: The first bit is 1. Then the second bit must be 0, because we can’t have 11. Then any (n-2)-bit string without the bit pattern 11 can be appended to the end to form a n-bit string without 11. So in this case there are exactly rn-2 such n-bit strings. 0000000000000000 0000000000000001 … 101010101010101010 The set of all (n-2)-bit strings without 11. Totally rn-2 of them. 10 +

Number of Bit Strings without a Specific Pattern How many n-bit string without the bit pattern 11? 00000000000000000 00000000000000001 … 1010101010101010101 The set of all (n-1)-bit strings without 11. Totally rn-1 of them. + 0000000000000000 0000000000000001 … 101010101010101010 The set of all (n-2)-bit strings without 11. Totally rn-2 of them. 10 + Every string without the bit pattern 11 is counted exactly once. Therefore, rn = rn-1 + rn-2

Exercise How many n-bit string without the bit pattern 111?

Domino Given a 2xn puzzle, how many ways to fill it with dominos (2x1 tiles)? E.g. There are 3 ways to fill a 2x3 puzzle with dominos. Let rn be the number of ways to fill a 2xn puzzle with dominos. How do we compute it using r1,r2,…,rn-1?

Domino Given a 2xn puzzle, how many ways to fill it with dominos (2x1 tiles)? Let rn be the number of ways to fill a 2xn puzzle with dominos. Case 1: put the domino vertically rn-1 to fill the remaining 2x(n-1) puzzle Case 2: put the domino horizontally rn-2 to fill the remaining 2x(n-2) puzzle Therefore, rn = rn-1 + rn-2

Plan Setting up recurrences Fibonacci recurrence Problem solving recurrences Catalan recurrences

Tower of Hanoi The goal is to move all the disks to post 3. The rule is that a bigger disk cannot be placed on top of a smaller disk.

Tower of Hanoi It is easy if we only have 2 disks. A total of 3 steps.

Tower of Hanoi It is not difficult if we only have 3 disks. Continue in next page

Tower of Hanoi It is not difficult if we only have 3 disks. A total of seven steps.

Tower of Hanoi Can you write a program to solve this problem? Think recursively!

Tower of Hanoi Suppose you already have a program for 3 disks. Can you use it to solve 4 disks? In order to move the largest disk, I have to move the top 3 disks first, and I can use the program for 3 disks. Then I move the largest disk. Then I can use the program to move the 3 disks from pole 2 to pole 3. Since the program requires 7 steps, the total number of steps is 15.

Tower of Hanoi This recursion is true for any n. In order to move the largest disk, I must move the top n-1 disks first, and I can use the program for n-1 disks. Then I move the largest disk. Then I can use the program again to move the n-1 disks from pole 2 to pole 3. Since the program requires rn-1 steps, the total number of steps is 2rn-1 + 1. rn = 2rn-1 + 1

Exercise Can we use fewer steps if we have 4 poles? What if we have a constraint that a disk cannot move between pole 1 and pole 3? How many steps are needed in this case?

Merge Sort Given a sequence of n numbers, how many steps are required to sort them into non-decreasing order? One way to sort the number is called the “bubble sort”, in which every step we search for the smallest number, and move it to the front. This algorithm could take up to roughly n2/2 steps. For example, if we are given the reverse sequence n,n-1,n-2,…,1. Every time it will search to the end to find the smallest number, and the algorithm takes roughly n+(n-1)+(n-2)+…+1 = n(n+1)/2 steps. Can we design a faster algorithm? Think recursively!

Merge Sort Suppose we have a program to sort n/2 numbers. We can use it to sort n numbers in the following way. 3 8 4 7 2 1 6 5 Divide the sequence into two halves. Use the program to sort the two halves independently. 3 4 7 8 1 2 5 6 With these two sorted sequences of n/2 numbers, we can merge them into a sorted sequence of n numbers easily!

Merge Sort Claim. Suppose we have two sorted sequence of k numbers. We can merge them into a sorted sequence of 2k numbers in 2k steps. Proof by example: 3 5 7 8 9 10 1 2 4 6 11 12 To decide the smallest number in the two sequences, we just need to look at the “heads” of the two sequences. So, in one step, we can extend the sorted sequence by one number. So the total number of steps for 2k numbers is 2k.

Merge Sort 3 5 7 8 9 10 1 2 4 6 11 12 1 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 5 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 5 6 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 5 6 7 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 5 6 7 8 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 5 6 7 8 9 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 5 6 7 8 9 10 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 5 6 7 8 9 10 11 3 5 7 8 9 10 1 2 4 6 11 12 1 2 3 4 5 6 7 8 9 10 11 12

Merge Sort Claim. Suppose we have two sorted sequence of k numbers. We can merge them into a sorted sequence of 2k numbers in 2k steps. Suppose we can sort k numbers in Tk steps. Then we can sort 2k numbers in 2Tk + 2k steps. Therefore, T2k = 2Tk + 2k. If we solve this recurrence (which we will do later), then we see that T2n ≈ n log2 n. This is significantly faster than bubble sort!

Remark This is one example of a “divide and conquer” algorithm. This idea is very powerful. It can be used to design faster algorithms for some basic problems such as integer multiplications, matrix multiplications, etc. More on that in CSC 3160.

Plan Setting up recurrences Fibonacci recurrence Problem solving recurrences Catalan recurrences

Parenthesis How many valid ways to add n pairs of parentheses? E.g. There are 5 valid ways to add 3 pairs of parentheses. ((())) (()()) (())() ()(()) ()()() Let rn be the number of ways to add n pairs of parentheses. How do we compute it using r1,r2,…,rn-1?

Parenthesis How many valid ways to add n pairs of parentheses? Let rn be the number of ways to add n pairs of parentheses. Case 1: ()-------------------- So there are rn-1 in this case. rn-1 ways to add the remaining n-1 pairs. Case 2: (--)------------------ So there are rn-2 in this case. 1 way to add 1 pair rn-2 ways to add the remaining n-2 pairs. Case 3: (----)---------------- So there are 2rn-3 in this case. 2 ways to add 2 pairs rn-3 ways to add the remaining n-3 pairs.

Parenthesis How many valid ways to add n pairs of parentheses? Let rn be the number of ways to add n pairs of parenthese. Case k: (----------)---------- rk ways to add k pairs rn-k-1 ways to add the remaining n-k-1 pairs. By the product rule, there are rkrn-k-1 ways in case k. The cases are depended on the position of the matching close parenthesis of the first open parenthesis, and so these cases are disjoint. Therefore, by the sum rule,

Stairs An n-stair is the collection of squares (x,y) with x >= y. For example 1-stair, 2-stair, and 3-stair are like this: How many ways to fill up the n-stair by exactly n rectangles?? For example, there are 5 ways to fill up the 3-stair by 3 rectangles.

Stairs Let rn be the number of ways to fill the n-stair by n rectangles. How do we compute it using r1,r2,…,rn-1? 6 5 4 3 2 1 Given the n-stair, the first observation is that the positions on the diagonal (red numbers) must be covered by different rectangles. Since there are n positions in the diagonal and we can only use n rectangles, each rectangle must cover exactly one red number.

Stairs Let rn be the number of ways to fill the n-stair by n rectangles. How do we compute it using r1,r2,…,rn-1? 6 5 4 3 2 1 o Consider the rectangle R that covers the bottom right corner (marked with o). We consider different cases depending on which red number that R contains.

Stairs Let rn be the number of ways to fill the n-stair by n rectangles. How do we compute it using r1,r2,…,rn-1? 6 5 4 3 2 1 o Suppose R contains 3. Then observe that the 6-stair is divided into 3 parts. One part is the rectangle R. The other two parts are a 2-stair and a 3-stair. Therefore, in this case, the number of ways to fill in the remaining parts is equal to r2r3

Stairs Let rn be the number of ways to fill the n-stair by n rectangles. How do we compute it using r1,r2,…,rn-1? 6 5 4 3 2 1 o Similarly suppose R contains 2. Then the rectangle “breaks” the stair into a 1-stair and a 4-stair. Therefore, in this case, the number of ways to fill in the remaining parts is equal to r1r4

Stairs Let rn be the number of ways to fill the n-stair by n rectangles. How do we compute it using r1,r2,…,rn-1? n … … i … 1 o In general suppose the rectangle contains i Then the rectangle “breaks” the stair into a (i-1)-stair and a (n-i)-stair. Therefore, in this case, the number of ways to fill in the remaining parts is equal to ri-1rn-i

Stairs The number of ways to fill in the remaining parts is equal to ri-1rn-i n … … i … 1 o Rectangle R containing different i will form different configurations, and each configuration must correspond to one of these cases. Therefore the total number of ways is equal to

Catalan Number How many valid ways to add n pairs of parentheses? Since the recursion for the stair problem is the same as the recursion for the parentheses problem, so we know that the answer is This is another way to show that the answer to a problem is the Catalan number. Try the examples in the wiki page (Catalan number).