Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Week 6 - Wednesday CS322.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
A simple example finding the maximum of a set S of n numbers.
Recursion Lecture 18: Nov 18.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
CS 2210 (22C:19) Discrete Structures Advanced Counting
Part 5. Computational Complexity (3)
1 Copyright M.R.K. Krishna Rao Solving Recurrence Relations Steps for solving a linear homogeneous recurrence relation of degree 2 : Step #1.
Second-Order Differential
Recursion Sections 7.1 and 7.2 of Rosen Fall 2008 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:
Discrete Structures Chapter 6 Recurrence Relations
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Recursion.
Analysis of Recursive Algorithms
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.
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
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.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Analysis of Recursive Algorithms October 29, 2014
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
Divide-and-Conquer 7 2  9 4   2   4   7
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Project 2 due … Project 2 due … Project 2 Project 2.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
1 Section 5.5 Solving Recurrences Any recursively defined function ƒ with domain N that computes numbers is called a recurrence or recurrence relation.
15.1 CompSci 102© Michael Frank Today’s topics Recurrence relationsRecurrence relations –Stating recurrences –LiHoReCoCo Reading: Sections Reading:
Module #17: Recurrence Relations Rosen 5 th ed., §
Solving Second-Order Recursive Relations Lecture 36 ½ Section 8.3 Wed, Apr 19, 2006.
RECURRENCE Sequence Recursively defined sequence
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)?
Recursion. Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Chapter 8 Recursion. 8.3 More Recurrence Second-Order Recurrence Definition – A second-order linear homogeneous recurrence relation with constant coefficients.
7.2 Solving Linear Recurrence Relations Some of these recurrence relations can be solved using iteration or some other ad hoc technique. However, one important.
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Foundations II: Data Structures and Algorithms
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Recurrence Relations. Outline Recurrence relationsSolving recurrence relationsRecurrence and Divide-and-conquer algorithmsGenerating functions
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
1Computer Sciences Department. Objectives Recurrences.  Substitution Method,  Recursion-tree method,  Master method.
RECURRENCE Sequence Recursively defined sequence
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
1 RECURRENCE 1. Sequence 2. Recursively defined sequence 3. Finding an explicit formula for recurrence relation.
Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Discrete Math For Computing II. Contact Information B. Prabhakaran Department of Computer Science University of Texas at Dallas Mail Station EC 31, PO.
CSG523/ Desain dan Analisis Algoritma
CSG523/ Desain dan Analisis Algoritma
Modeling with Recurrence Relations
Divide-and-Conquer 6/30/2018 9:16 AM
CS 3343: Analysis of Algorithms
Recursion.
Divide-and-Conquer 7 2  9 4   2   4   7
Chapter 7 Advanced Counting Techniques
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Solving Recurrence Lecture 19: Nov 25

Some Recursive Programming (Optional?)

Test 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)? 2.What if I call hello(-1)? 3.What if the order of printf() and hello() is reversed?

Computing Sum of Arithmetic Progression int AP(int n) { if (n==0) return 0; else return (n+AP(n-1)); } Many programs can be written in a recursive way. The way of thinking is different. Always try to reduce it to smaller problems.

Computing Exponential Function int EX(int n) { if (n==0) return 1; else return (EX(n-1)+EX(n-1)); } How many function calls if I run EX(n)? This function is to compute 2 n. 2 n times. If we replace the last line by return 2EX(n-1), then the program will compute the same thing, but there will be only n function calls.

Move 1,2 (n)::= Move 1,3 (n-1); biggest disk 1  2; Move 3,2 (n-1) Tower of Hanoi To move the biggest disk, we must first move the disks on top to another post.

Tower of Hanoi T11(a,b) { T10(a,c); printf(“Move Disk 11 from a to b\n”); T10(c,b); } Suppose we already have a function T10(a,b) to move 10 disks from pole a to pole b. It is then easy to write a program for T11(a,b) In general you can write exactly the same program for Tn, if we are given a program for Tn-1. Instead of writing one program for each n, we can take n as an input and write a recursive program.

Tower of Hanoi Tower_of_Hanoi(int origin, int destination, int buffer, int number) { if (n==0) return; Tower_of_Hanoi(origin, buffer, destination, number-1); printf(“Move Disk #%d from %d to %d\n”, number, origin, destination); Tower_of_Hanoi(buffer, destination, origin, number-1); } This is the power of recursive thinking. The program is very short, yet it is not easy to write it without recursion

Tower of Hanoi T(A,C,B,3) Tower_of_Hanoi(origin, destination, buffer, number) Move 3 from A to C. T(A,B,C,2) T(B,C,A,2) move 2 from A to B move 2 from B to C T(A,C,B,1) T(C,B,A,1) T(B,A,C,1) T(A,C,B,1) move 1 from A to C move 1 from C to B move 1 from B to A move 1 from A to C

Generate Strings Can you write a program to generate all n-bit strings with k ones? There are many ways to do it. (1)Generate all n-bit strings and output those strings with k ones. - Too slow (2) Write k for-loops. - That’s okay if k is known, but what if k is part of the input? (3) Write a program to write a program with k for loops. - That’s okay, but can we do it more elegantly? (4) Write a recursive program for it.

Generate Strings The idea of the recursive program is simple. First we generate all strings which begin with 1, and then generate all strings which begin with 0. Can you write a program to generate all n-bit strings with k ones? For strings which begin with 1, we still have n-1 bits and k-1 ones left. For strings which begin with 0, we still have n-1 bits and k ones left. Let’s say the program is called gen(n,k). gen(n-1,k-1) gen(n-1,k) Sounds familiar?

Generate Strings Can you write a program to generate all N-bit strings with K ones? int solution[N]; (an array holding an N-bit string, from solution[0] to solution[n-1]) gen(int n, int k) (n is # bits left, and k is # ones left) { if (n==0) (no more bits left) print solution; (write a for loop to print out the N-bits in solution) return; if (k > 0) solution[N-n] = 1; (generate the strings beginning with one) gen(n-1,k-1); if (n > k) (do it only if there are enough places for the ones) solution[N-n]=0; (generate the strings beginning with zero) gen(n-1,k); }

Programming Exercises Can you write a recursive program to generate all permutations of n elements? Can you write a recursive program for merge-sort?

Solving Recurrence

Warm Up a 0 =1, a k = a k a 1 = a a 2 = a = (a 0 + 2) + 2 = a a 3 = a = (a 0 + 4) + 2 = a a 4 = a = (a 0 + 6) + 2 = a See the pattern is a k = a 0 + 2k = 2k+1 You can verify by induction.

Solving Hanoi Sequence a 1 =1, a k = 2a k a 2 = 2a = 3 a 3 = 2a = 2(2a 1 + 1) + 1 = 4a = 7 a 4 = 2a = 2(4a 1 + 3) + 1 = 8a = 15 a 5 = 2a = 2(8a 1 + 7) + 1 = 16a = 31 a 6 = 2a = 2(16a ) + 1 = 32a = 63 Guess the pattern is a k = 2 k -1 You can verify by induction.

Solving Merge Sort Recurrence T 2k = 2T k + 2k If we could guess that T k is k log 2 k, then we can prove that T 2k is 2k log 2 (2k). This is because T 2k = 2T k + 2k = 2klog 2 k + 2k = 2k(log 2 k + 1) = 2k(log 2 k + log 2 2) = 2klog 2 2k

Solving Merge Sort Recurrence T 2k = 2T k + 2k How could we guess T k = k log 2 k in the first place? If there are n numbers there are log 2 n levels. In each level i we need to solve 2 i-1 merge problems. Each merge problem in level i has two subseqences of n/2 i numbers, and so can be solved in n/2 i-1 steps. So each level requires a total of (2 i-1 )(n/2 i-1 )=n steps. Since there are log 2 n levels, the total number of steps is at most nlog 2 n. Level 3 Level 1 Level 2

Solving Fibonacci Sequence a 0 =0, a 1 =1, a k = a k-1 + a k-2 a 2 = a 1 + a 0 = 1 a 3 = a 2 + a 1 = 2a 1 + a 0 = 2 a 4 = a 3 + a 2 = 2a 2 + a 1 = 3a 1 + 2a 0 = 3 a 5 = a 4 + a 3 = 2a 3 + a 2 = 3a 2 + 2a 1 = 5a 1 + 3a 0 = 5 a 6 = a 5 + a 4 = 2a 4 + a 3 = 3a 3 + 2a 2 = 5a 2 + 3a 1 = 8a 1 + 5a 0 = 8 a 7 = a 6 + a 5 = 2a 5 + a 4 = 3a 4 + 2a 3 = 5a 3 + 3a 2 = 8a 2 + 5a 1 = 13a 1 + 8a 0 = 13 See the pattern a n = a n-k a k+1 + a n-k-1 a k but this does not give a formula for computing a n

Second Order Recurrence Relation In the textbook it is called “second-order linear homogeneous recurrence relation with constant coefficients”. a k = Aa k-1 + Ba k-2 A and B are real numbers and B≠0 For example, Fibonacci sequence is when A=B=1. To solve this equation, first we will find some solutions, and later show that they “generate” all possible solutions.

Geometric-Sequence Solution a k = Aa k-1 + Ba k-2 Find solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) t k = At k-1 + Bt k-2 That is, suppose a k =t k t 2 = At + B t 2 - At – B = 0 So t is a root of the quadratic equation t 2 - At – B = 0.

Example a k = a k-1 + 2a k-2 Find solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) So t must be a root of the quadratic equation t 2 - t – 2 = 0. This implies that t=2 or t=-1. So solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) are: (i) (1,2,4,8,16,32,64,…) (ii) (1,-1,1,-1,1,-1,…)

Example a k = a k-1 + 2a k-2 So solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) are: (i) (1,2,4,8,16,32,64,…) (ii) (1,-1,1,-1,1,-1,1,…) Are there other solutions? Try (2,1,5,7,17,31,65,…) (0,3,3,9,15,33,63,…) (4,5,13,23,49,95,193,…) How to obtain these solutions?

Linear Combination of Two Solutions If (r 0,r 1,r 2,r 3,…) and (s 0,s 1,s 2,s 3,…) are solutions to a k = Aa k-1 + Ba k-2, then the sequence (a 0,a 1,a 2,a 3,…) defined by the formula a k = Cr k + Ds k also satisfies the same recurrence relation for any C and D. This says that any linear combination of two solutions for the recurrence relation is also a solution for the recurrence. (This is easy to check.)

Linear Combination of Two Solutions Are there other solutions than a k = Cr k + Ds k ? Any solution to a k = Aa k-1 + Ba k-2 is determined by the first two terms a 0 and a 1 Suppose we already have two solutions (r 0,r 1,r 2,r 3,…) and (s 0,s 1,s 2,s 3,…). If (r 0,r 1 ) and (s 0,s 1 ) are linearly independent, then any possible (a 0,a 1 ) is a linear combination of (r 0,r 1 ) and (s 0,s 1 ), and thus any solution to the recurrence relation a k = Aa k-1 + Ba k-2 is a linear combination of (r 0,r 1,r 2,r 3,…) and (s 0,s 1,s 2,s 3,…).

Distinct-Roots Theorem Suppose a sequence (a 0,a 1,a 2,a 3,…) satisfies a recurrence relation a k = Aa k-1 + Ba k-2 If t 2 - At – B = 0 has two distinct roots r and s, then a n = Cr n + Ds n for some C and D. If we are given a 0 and a 1, then C and D are uniquely determined. The theorem says that all the solutions of the recurrence relation are a linear combination of the two series (1,r,r 2,r 3,r 4,…,r n,…) and (1,s,s 2,s 3,s 4,…,s n,…) defined by the distinct roots of t 2 - At – B = 0, because the two series are linearly independent.

Solving Fibonacci Sequence a 0 =0, a 1 =1, a k = a k-1 + a k-2 First solve the quadratic equation t 2 - t – 1 = 0. So the distinct roots are:

Solving Fibonacci Sequence a 0 =0, a 1 =1, a k = a k-1 + a k-2 By the distinct-roots theorem, the solutions satisfy the formula: To figure out C and D, we substitute the value of a 0 and a 1 :

Solving these two equations, we get: Therefore: Solving Fibonacci Sequence

Quick Summary Recursion is a very useful and powerful technique in computer science. It is very important to learn to think recursively, by reducing the problem into smaller problems. This is a necessary skill to acquire to become a professional programmer. Make sure you have more practices in setting up recurrence relations.