Algorithm Analysis (for Divide-and-Conquer problems)

Slides:



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

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.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
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.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Main Index Contents 11 Main Index Contents Lecture 4 CSN 331 – Fall 2004 Chapter 15 Fibonacci (again) Dynamic Programming Permutations Eight Queens BacktrackingSummary.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Analysis of Recursive Algorithms October 29, 2014
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Analysis of Algorithms
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Project 2 due … Project 2 due … Project 2 Project 2.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
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.
Recurrences – II. Comp 122, Spring 2004.
Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion.
CS212: Data Structures and Algorithms
Recursion.
Equal costs at all levels
Introduction to Algorithms: Divide-n-Conquer Algorithms
Recursion Ali.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
RECURSION.
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 10.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
T(n) = aT(n/b) + cn = a(aT(n/b/b) + cn/b) + cn 2
Data Structures and Algorithms
Programming application CC213
CS1120: Recursion.
7.Recursion Recursion is the name given for expression anything in terms of itself. Recursive function is a function which calls itself until a particular.
Module 1-10: Recursion.
Divide and Conquer (Merge Sort)
Using The Master Method Case 1
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Algorithms Recurrences.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
ITEC324 Principle of CS III
Recursive Thinking.
Presentation transcript:

Algorithm Analysis (for Divide-and-Conquer problems) Recursion Master theorem

Recursive Function Recursion is a technique in which we break down a problem into one or more subproblems that are similar in form to the original problem. A recursive function is one that calls itself (either directly or indirectly). It has two components: Base case(s): non-recursive operations Recursive case(s): involve recursive calls

Example: Power Function Xn - raise a number to a positive integer power Base Case Correctness of recursive function, (page 2) Recursive case

Stopping Conditions for Recursive Algorithms Use a recursive function to implement a recursive algorithm. The design of a recursive function consists of 1. One or more stopping conditions that can be directly evaluated for certain arguments. 2. One or more recursive steps in which a current value of the function can be computed by repeated calling of the function with arguments that will eventually arrive at a stopping condition.

Implementing the Recursive Power Function double power(double x, int n) // n is a non-negative integer { if (n == 0) return 1.0; // stopping condition else return x * power(x,n-1); // recursive step } Picturing recursive (Page 5)

Implementing the Recursive factorial Function The factorial of a nonnegative integer n is the product of all positive integers less than or equal to n. Recursive factorial(): int factorial(int n) // n is a non-negative integer { if (n == 0) return 1; // stopping condition else return n* factorial(n-1); // recursive step }

Fibonacci numbers Fibonacci numbers are the sequence of integers: 0,1,1,2,3,5,8,13,21,34 The first two terms are 0 and 1 by definition. Then, each next term is the sum of the two previous terms. Recursive fib(): int fib(int n) // n is a non-negative integer { if (n <= 1) return n; // stopping condition else return fib(n-1)+ fib(n-2); // recursive step }

Fibonacci Number using iteration int fibiter(int n) { // integers to store previous two Fibonacci value int oneback = 1, twoback = 0, current; int i; // return is immediate for first two numbers if (n <= 1) return n; else // compute successive terms beginning at 3 for (i = 2; i <= n; i++) current = oneback + twoback; twoback = oneback;// update for next calculation oneback = current; } return current;

Tower of Hanoi Puzzle Tower of Hanoi problem: there are a stack of n graduated disks and a set of three needles called A, B, and C. Originally, n disks are placed on needle A. The objective is to move the disks one at a time from needle to needle until the process rebuilds the original stack, but on needle C, such that at no time is a larger disk on the top of a a smaller one.

Solving the Tower of Hanoi Puzzle using Recursion Stage 1: Stage 2: [Class 3] Recursion code implementation of the Tower of Hanoi Puzzle: book page 158, & note (last page) Stage 3

Master Method/Theorem Theorem: for T(n) = aT(n/b)+f(n), n/b may be n/b or n/b. where a  1, b>1 are positive integers, f(n) be a non-negative function. If f(n)=O(nlogba-) for some >0, then T(n)= (nlogba). If f(n)= (nlogba), then T(n)= (nlogba lg n). If f(n)=(nlogba+) for some >0, and if af(n/b) cf(n) for some c<1 and all sufficiently large n, then T(n)= (f(n)).

Implications of Master Theorem Comparison between f(n) and nlogba , the larger one determines the solution Must be asymptotically smaller (or larger) by a polynomial, i.e., n for some >0. In case 3, the “regularity” must be satisfied, i.e., af(n/b) cf(n) for some c<1 . There are gaps between 1 and 2: f(n) is smaller than nlogba, but not polynomially smaller. between 2 and 3: f(n) is larger than nlogba, but not polynomially larger. in case 3, if the “regularity” fails to hold.

Where Are the Gaps f(n), case 3, at least polynomially larger Gap between case 3 and 2 c1 nlogba f(n), case 2: within constant distances c2 n Gap between case 1 and 2 f(n), case 1, at least polynomially smaller Note: 1. for case 3, the regularity also must hold. 2. if f(n) is lg n smaller, then fall in gap in 1 and 2 3. if f(n) is lg n larger, then fall in gap in 3 and 2 4. if f(n)=(nlogbalgkn), then T(n)=(nlogbalgk+1n).

Application of Master Theorem T(n) = 9T(n/3)+n; a=9,b=3, f(n) =n nlogba = nlog39 =  (n2) f(n)=O(nlog39-) for =1 By case 1, T(n) = (n2). T(n) = T(2n/3)+1 a=1,b=3/2, f(n) =1 nlogba = nlog3/21 =  (n0) =  (1) By case 2, T(n)= (lg n).

Application of Master Theorem T(n) = 3T(n/4)+nlg n; a=3,b=4, f(n) =nlg n nlogba = nlog43 =  (n0.793) f(n)= (nlog43+) for 0.2 Moreover, for large n, the “regularity” holds for c=3/4. af(n/b) =3(n/4)lg (n/4)  (3/4)nlg n = cf(n) By case 3, T(n) = (f(n))= (nlg n).

Exception to Master Theorem T(n) = 2T(n/2)+nlg n; a=2,b=2, f(n) =nlg n nlogba = nlog22 =  (n) f(n) is asymptotically larger than nlogba , but not polynomially larger because f(n)/nlogba = lg n, which is asymptotically less than n for any >0. Therefore, this is a gap between 2 and 3.