Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Chapter 6 Advanced Counting 6.1 Recurrence Relations.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
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.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Analysis of Recursive Algorithms
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
When confronted with a new problem there are two questions you should ask: 1. Is this problem like, or a special case of, a problem that I already know.
Advanced Counting Techniques
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Chapter 4: Induction and Recursion
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Module #14: Recursion Rosen 5 th ed., §§ In this class, we will study recursion, one of the most important topics in computer science. In the last.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Data Structures & Algorithms Recursion and Trees Richard Newman.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
CHAPTER TWO RECURRENCE RELATION
COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
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.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Chapter 4: Induction and Recursion
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Induction and Recursion
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Algorithm Analysis (for Divide-and-Conquer problems)
CS1120: Recursion.
Chapter 8: Recursion Java Software Solutions
Applied Discrete Mathematics Week 9: Integer Properties
Chapter 8: Recursion Java Software Solutions
Recursion.
Presentation transcript:

Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007

Meaning of recursion To recur means to happen again. In computer science, we say that a subroutine (function or procedure) is recursive when it calls itself one or more times. We call the programming technique by using recursive subroutine(s) as recursion. The correctness, time complexity and space complexity can be proved by mathematical induction.

Concept of recursion Consider the function. F() temp←2*F() RETURN temp When F() is called, F()=temp=2*F() It will not solve the equation. It continues. =2*(2*F()) =2*(2*(2*F()))... temp Stack

Concept of recursion The concept of parameter(s) is needed. This can be implemented by:  Pass by value  Pass by pointer  Pass by reference  Use of global variable

Concept of recursion Consider the function F(n) temp←n*F(n-1) RETURN temp When F(1) is called, F(1)=temp=1*F(0) =1*(0*F(-1)) It will not simplify the expression. It continues. =1*(0*(-1*F(-2)))... n, temp Stack

Concept of recursion The concept of terminating condition(s) is/are needed. It is determined by the parameter(s).

Concept of recursion Consider the function F(n) IF (n=0) RETURN 1 ELSE RETURN n*F(n-1) When F(2) is called, F(2)=2*F(1) =2*(1*F(0)) =2*(1*(1)) =2*(1*1) =2*1 =2 n n n Stack

Structure of recursion Similar to mathematic induction, recursion requires the following two components  Recurrence relation(s)  Base case(s) Example F(n) IF (n=0) RETURN 1//Base Case ELSE RETURN n*F(n- 1) //Recurrence Relation

Recurrence in mathematical functions Factorial __ Permutation __ Combination __ Pascal relationship __ Integer power __

Problem Discussion S is a string of N bits (either ‘0’ or ‘1’). Furthermore, there is no two consecutive ‘0’s in S. Find the number of solutions to S.  Denote S(N) as any strings of N bits with no two consecutive ‘0’s.  When N>=2, N is either Beginning with ‘1’, followed by S(N-1) Beginning with ’01’, followed by S(N-2)  S(1) must either be ‘0’ or ‘1’.  S(0) is the null string ‘’.

Problem Discussion  Let f(N) be the number of solutions to S(N).

Problem Discussion This puzzle is called the Tower of Hanoi.  There are three pegs and N disks of different sizes.  Initially all disks are stacked on the same peg, with the smaller disk on top of the larger disk.  In each step, you can move a disk from the top of a stack to the top of another peg, provided that this will not result in a larger disk stacked on top of a smaller disk.  Your goal is to move all disks to another peg.

Problem Discussion Suppose N=4

Problem Discussion Suppose procedure  MOVE_DISK(N,A,B) moves a single disk N from A to B within one step.  F(N,X,Y,Z) can move a stack of N disks from peg X to peg Z via peg Y F(N,X,Y,Z) where N≥1  F(N-1,X,Z,Y)  MOVE_DISK(N,X,Z)  F(N-1,Y,Z,X)

Problem Discussion Given a sequence of n distinct integers. Output all permutations of the sequence. Let F(n) be a procedure that permute a[1..n].  When n>0, for all 1≤i ≤ n, swap a[i] to a[n] and then permute a[1..n-1] by calling F(n-1) F(n) IF(n=0) OUTPUT ELSE FOR i = 1 to n SWAP(a[i],a[n]) F(n-1) SWAP(a[i],a[n])

Problem Discussion Suppose a[ ]=(7,8,9) F(0) * * * * * * F(1) i=1 i=1 i=1 i=1 i=1 i=1 F(2) i=1___i=2___ i=1___i=2___ i=1___i=2___ F(3) i=1____________i=2____________i=3____________ i i i Stack a[ ] Output: 897, 987, 978, 798, 879, 789

Complexity of recursion First Order Linear Recurrence  In particular, suppose a>1, c≠-1

Complexity of recursion Recurrence Iteration and Recursion Tree _ f(4) =1+2f(3) =1+2+4f(2) = f(1) = f(0)

Complexity of recursion __ f(8) =8+f(7) =8+7+f(6) =8+7+6+f(5) = f(4) = f(3) = f(2) = f(1) = f(0)

Complexity of recursion __ f(256) =256+f(128) = f(64) = f(32) = f(16) = f(8) = f(4) = f(2) = f(1)

Complexity of recursion __ f(32) =32+2(f(16)) =32+2(16)+4(f(8)) =32+2(16)+4(8)+8(f(4)) =32+2(16)+4(8)+8(4)+16(f(2)) =32+2(16)+4(8)+8(4)+16(2)+32(f(1))

Complexity of recursion Master Theorem  In particular, suppose b>1, _ _ _

Problem Discussion Compute B p mod M  B,P are integers in [0, ]  M is an integer in [1,46340] f(p)=B*f(p-1)  B p = B*B p-1  Θ(p)

Problem Discussion Notice that Θ(log p)

Properties of recursion NP solutions, including exponential, factorial and combinatorial, are usually easy to be implemented by recursion. It requires more memory than iteration. Every recursive function can be transformed into iterative function. Every iterative function can be transformed into recursive function.

Meaning of Divide and Conquer In military, “divide and conquer” is a kind of strategy. In computer science, “divide and conquer” is a programming technique by breaking down a problem into one or more sub problems. “Divide and conquer” is usually implemented by recursion.

Structure of Divide and Conquer Divide  Break a problem into sub problem(s) Conquer  Solve all sub problem(s) Combine  Solve the problem using the results of the sub problem(s)

Problem Discussion You are given a 2 k * 2 k square board with one square taken away. An “L piece” is made up of 3 squares sharing a common corner. You can freely rotate the any of these pieces. You are required to cover the board with these pieces without overlapping.

Problem Discussion Example

Problem Discussion Illustration

Recursion outside Computer Science Names  PHP : PHP Hypertext Preprocessor  GNU : GNU is Not Unix Mathematics  Definition of the set of non negative numbers N 0 is in N If n is in N, then n+1 is in N. Images  Fractals

Challenge Problems Consider the following recursive function defined on pairs of nonnegative numbers. Find the closed form of the function. Prove your answer algebraically.

Challenge Problems Consider the following recursive function defined on triple of nonnegative numbers. Find the closed form of the function. Prove your answer geometrically.

Challenge Problems Find a closed form solution for the n th Fibonacci Number. Prove the Master Theorem.

Challenge Problems Consider the following algorithm.  F(x,p) IF (p=0) RETURN 1 IF (p=1) RETURN x BINARY_SEARCH for the largest int q s.t. q 2 ≤p RETURN F(F(x,q),q)*F(x,p-q 2 ) What is the purpose of F(x,p)? Prove your answer.

Challenge Problems Describe an efficient non recursive algorithm to compute B p mod M.  B,P are integers in [0, ]  M is an integer in [1,46340] State the time and space complexity.

Challenge Problems Consider the problem “Tower of Hanoi”. Describe an algorithm which requires the minimum number of moves. Prove that it requires the minimum number of moves. Show that this sequence of moves is unique. Calculate the minimum number of moves required in terms of n.

Challenge Problems Consider the problem “Tower of Hanoi”. Suppose you have to move a stack from Peg 0 to Peg 2 via Peg 1. Suppose the i th largest disk is on Peg p i (t) after t moves. Denote h i (t) be the number of disks under the i th largest disk after t moves. Consider the algorithm that requires the minimum number of moves. Show that p i (t)+h i (t)+i is always odd.

Challenge Problems Consider the problem “L pieces”. Explain whether it is possible to have a faster algorithm in terms of complexity. Calculate the worst case minimum of colours required such that no two pieces sharing a common edge has the same colour.

Reference