Today’s Material Recursive (Divide & Conquer) Algorithms Design

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Back to Sorting – More efficient sorting algorithms.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
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.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
Factorial Recursion stack Binary Search Towers of Hanoi
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
While loop Side-by-side View recursion. int sum(int n) { int total = 0; int i = 1; while (i
While loop Side-by-side View recursion. int sum(int n) { int total = 0; int i = 1; while (i
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Analysis of Recursive Algorithms
Searching Arrays Linear search Binary search small arrays
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Fundamental in Computer Science Recursive algorithms 1.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Asymptotic Notations Iterative Algorithms and their analysis
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Iterative Algorithm Analysis & Asymptotic Notations
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Analysis of Algorithms
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
Lecture 12. Searching Algorithms and its analysis 1.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
February 4, 2005 Searching and Sorting Arrays. Searching.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
CSC 205 Programming II Lecture 9 More on Recursion.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Lecture 28 CSE 331 Nov 9, Mini project report due WED.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
RECURSION Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
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.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Introduction to Algorithms: Divide-n-Conquer Algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Recursion notes Chapter 8.
CS 3343: Analysis of Algorithms
Chapter 4 Divide-and-Conquer
CS 3343: Analysis of Algorithms
Recursive Binary Search
Intro to Recursion.
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 201 Fundamental Structures of Computer Science
CSE 373 Data Structures and Algorithms
Recursion Think ESCHER.
Divide & Conquer Algorithms
Running Time Exercises
ITEC324 Principle of CS III
Presentation transcript:

Today’s Material Recursive (Divide & Conquer) Algorithms Design Analysis

Divide & Conquer Strategy Very important strategy in computer science: Divide problem into smaller parts Independently solve the parts Combine these solutions to get overall solution P P1 P2 ......................... Pn P11 P12 P1n ... P21 P22 P2n Pn1 Pn2 Pnn ... ... ................................................................................................................................................ Cases Base P P P P ................... P P P P P P P P

Divide & Conquer Strategy (cont) /* Solve a problem P */ Solve(P){ /* Base case(s) */ if P is a base case problem return the solution immediately /* Divide P into P1, P2, ..Pn each of smaller scale (n>=2) */ /* Solve subproblems recursively */ S1 = Solve(P1); /* Solve P1 recursively to obtain S1 */ S2 = Solve(P2); /* Solve P2 recursively to obtain S2 */ … Sn = Solve(Pn); /* Solve Pn recursively to obtain Sn */ /* Merge the solutions to subproblems */ /* to get the solution to the original big problem */ S = Merge(S1, S2, …, Sn); /* Return the solution */ return S; } //end-Solve

Computing 1+2+..+N Recursively Consider the problem of computing the sum of the number from 1 to n: 1+2+3+..+n Here is how we can think recursively: In order to compute Sum(n) = 1+2+..+n compute Sum(n-1) = 1+2+..+n-1 (a smaller problem of the same type) Add n to Sum(n-1) to compute Sum(n) i.e., Sum(n) = Sum(n-1) + n; We also need to identify base case(s) A base case is a subproblem that can easily be solved without further dividing the problem If n = 1, then Sum(1) = 1;

Computing 1+2+..+N Recursively /* Computes 1+2+3+…+n */ int Sum(int n){ int partialSum = 0; /* Base case */ if (n == 1) return 1; /* Divide and conquer */ partialSum = Sum(n-1); /* Merge */ return partialSum + n; } /* end-Sum */ main(String args[]){ int x = 0; x = Sum(4); println(“x: ” + x); return 0; } /* end-main */

Recursion Tree for Sum(4) main /* Computes 1+2+3+…+n */ int Sum(int n){ int partialSum = 0; /* Base case */ if (n == 1) return 1; /* Divide and conquer */ partialSum = Sum(n-1); /* Merge */ return partialSum + n; } /* end-Sum */ main(String args[]){ int x = Sum(4); println(“Sum: ” + Sum(4)); } /* end-main */ =10 return 6+4 x=Sum(4) Sum(4) partialSum=Sum(3) =6 return 3+3 Sum(3) partialSum=Sum(2) =3 return 1+2 Sum(2) partialSum=Sum(1) =1 Sum(1) return 1

Running Time for Sum(n) /* Computes 1+2+3+…+n */ int Sum(int n){ int partialSum = 0; /* Base case */ if (n == 1) return 1; /* Divide and conquer */ partialSum = Sum(n-1); /* Merge */ return partialSum + n; } /* end-Sum */ 1 if n =1 (Base case) T(n) = T(n-1) + 1 if n>1

Computing an Recursively /* Computes a^n */ double Power(double a, int n){ double partialResult; /* Base cases */ if (n == 0) return 1; else if (n == 1) return a; /* partialResult = a^(n-1) */ partialResult = Power(a, n-1); /* Merge */ return partialResult*a; } /* end-Power */ We can combine divide, conquer & merge into a single statement /* Computes a^n */ double Power(double a, int n){ /* Base cases */ if (n == 0) return 1; else if (n == 1) return a; return Power(a, n-1)*a; } /* end-Power */

Recursion Tree for Power(3, 4) main /* Computes a^n */ double Power(double a, int n){ /* Base cases */ if (n == 0) return 1; else if (n == 1) return a; return a * Power(a, n-1); } /* end-Power */ main(String args[]){ double x; x = Power(3, 4); } /* end-main */ =81 return 81 x=Power(3,4) Power(3,4) return 3*Power(3,3) =81 return 27 Power(3,3) return 3*Power(3,2) =27 return 9 Power(3,2) return 3*Power(3,1) =9 Power(3,1) return 3

Running Time for Power(a, n) /* Computes a^n */ double Power(double a, int n){ /* Base cases */ if (n == 0) return 1; else if (n == 1) return a; return a * Power(a, n-1); } /* end-Power */ 1 if n <=1 (Base case) T(n) = T(n-1) + 1 if n>1

Fibonacci Numbers Fibonacci numbers are defined as follows F(0) = 0 F(n) = F(n-1) + F(n-2) /* Computes nth Fibonacci number */ int Fibonacci(int n){ /* Base cases */ if (n == 0) return 0; if (n == 1) return 1; return Fibonacci(n-1) + Fibonacci(n-2); } /* end-Fibonacci */

Recursion Tree for F(5) F(5) 5 F(4)+F(3) F(4) 3 F(3) 2 F(3)+F(2) 1 2 F(2) F(2) 1 F(1) 1 F(2)+F(1) F(1)+F(0) F(1)+F(0) F(2) 1 F(1) 1 F(1) 1 F(0) F(1) 1 F(0) F(1)+F(0) F(1) 1 F(0)

Summation I Compute the sum of N numbers A[1..N] Stopping rule (Base Case): If N == 1 then sum = A[1] Key Step Divide: Consider the smaller A[1] and A[2..N] Conquer: Compute Sum(A[2..N]) Merge: Sum(A[1..N]) = A[1] + Sum(A[2..N])

Recursive Calls of Summation I A[1..N] A[1] A[2..N] A[2] A[3..N] A[3] A[4..N] A[N-1..N] A[N-1] A[N]

Summation I – Code 1 if N =1 (Base case) T(n) = T(n-1) + 1 if N>1 /* Computes the sum of an array of numbers A[0..N-1] */ int Sum(int A[], int index, int N){ /* Base case */ if (N == 1) return A[index]; /* Divide & Conquer */ int localSum = Sum(A, index+1, N-1); /* Merge */ return A[0] + localSum; } //end-Sum 1 if N =1 (Base case) T(n) = T(n-1) + 1 if N>1 Time to find the sum of n-1 numbers Time to combine the results

Summation II Compute the sum of N numbers A[1..N] Stopping rule: If N == 1 then sum = A[1] Key Step Divide: Consider the smaller A[1..N/2] and A[N/2+1..N] Conquer: Compute Sum(A[1..N/2]) and Sum(A[N/2+1..N]) Merge: Sum(A[1..N]) = Sum(A[1..N/2]) + Sum(A[N/2+1..N])

Recursive Calls of Summation II A[1..N] N N/2 A[1..N/2] A[N/2+1..N] N/4 A[1..N/4] A[N/4+1..N/2] N/8

Summation II – Code 1 if N =1 (Base case) T(n) = /* Computes the sum of an array of numbers A[0..N-1] */ int Sum(int A[], int index1, int index2){ /* Base case */ if (index2-index1 == 1) return A[index1]; /* Divide & Conquer */ int middle = (index1+index2)/2; int localSum1 = Sum(A, index1, middle); int localSum2 = Sum(A, middle, index2); /* Merge */ return localSum1 + localSum2; } //end-Sum 1 if N =1 (Base case) T(n) = T(n/2) + T(n/2) + 1 if N>1

Linear Search Find a key in an array of numbers A[0..N-1] Stopping rules (Base Cases): if (N == 0) return false; if (key == A[0]) return true; Key Step Divide & Conquer Search key in A[1..N-1]

Linear Search – Code 1 if N <=1 (Base cases) T(n) = /* Searches a key in A[0..N-1] */ bool LinearSearch(int A[], int index, int N, int key){ /* Base cases */ if (N == 0) return false; /* Unsuccessful search */ if (key == A[0]) return true; /* Success */ /* Divide & Conquer & Merge */ return LinearSearch(A, index+1, N-1, key); } //end-LinearSearch 1 if N <=1 (Base cases) T(n) = T(n-1) + 1 if N>1

Binary Search Find a key in a sorted array of numbers A[0..N-1] Stopping rules (Base Cases): if (N == 0) return false; if (key == A[N/2]) return true; Key Step if (key < A[N/2]) Search A[0..N/2-1] else Search A[N/2+1..N-1]

Binary Search – Code 1 if N <=1 (Base cases) T(n) = /* Searches a key in sorted array A[0..N-1] */ bool BinarySearch(int A[], int index1, int index2, int key){ int middle = (index1+index2)/2; int N = index2-index1; /* Base cases */ if (key == A[middle]) return true; /* Success */ if (N == 1) return false; /* Unsuccessful search */ /* Conquer & Merge */ else if (key < A[middle]) return BinarySearch(A, index1, middle, key); else return BinarySearch(A, middle, index2, key); } //end-BinarySearch 1 if N <=1 (Base cases) T(n) = T(n/2) + 1 if N>1