Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Divide-and-conquer: closest pair (Chap.33) Given a set of points, find the closest pair (measured in Euclidean distance) Brute-force method: O(n 2 ). Divide-and-conquer.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer The most-well known algorithm design strategy:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 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.
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.
CS4413 Divide-and-Conquer
Reminder: Closest-Pair Problem Given a collection P of n points in  × , obtain the two points p1 and p2 such that their distance is less or equal that.
Theory of Algorithms: Divide and Conquer
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
8/29/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk.
Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2004 Simonas Šaltenis E1-215b
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Analysis of Recursive Algorithms
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
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.
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.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
1 Calculating Polynomials We will use a generic polynomial form of: where the coefficient values are known constants The value of x will be the input and.
Divide and Conquer.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
Convex Hull. What is the Convex Hull? Imagine a set of points on a board with a nail hammered into each point. Now stretch a rubber band over all the.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
Divide-and-Conquer. Outline Introduction Merge Sort Quick Sort Closest pair of points Large integer multiplication.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Advanced Algorithms Analysis and Design
Chapter 2. Divide-and-conquer Algorithms
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 4 Divide-and-Conquer
Chapter 5 Divide & conquer
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
Convex Hull.
Divide-And-Conquer-And-Combine
CSCE350 Algorithms and Data Structure
Divide-and-Conquer The most-well known algorithm design strategy:
Lecture 3 / 4 Algorithm Analysis
Lecture 30 CSE 331 Nov 11, 2016.
Divide-And-Conquer-And-Combine
CS200: Algorithms Analysis
Chapter 4 Divide-and-Conquer
Divide and Conquer Algorithms Part I
Lecture 30 CSE 331 Nov 10, 2017.
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer (Merge Sort)
3. Brute Force Selection sort Brute-Force string matching
CMPS 3120: Computational Geometry Spring 2013
Introduction to Algorithms
The Selection Problem.
Richard Anderson Lecture 14 Divide and Conquer
Divide-and-conquer approach
Given a list of n  8 integers, what is the runtime bound on the optimal algorithm that sorts the first eight? O(1) O(log n) O(n) O(n log n) O(n2)
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Chapter 2 Recursive Algorithms

2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating permutations Recursion and stacks

3 Prerequisites Before beginning this chapter, you should be able to: –Read and create recursive algorithms –Identify comparison and arithmetic operations –Use basic algebra

4 Goals At the end of this chapter you should be able to: –Create the recurrence relation for a recursive algorithm –Convert a simple recurrence relation into closed form –Explain the closest pair algorithm

5 Goals (continued) –Explain the convex hull algorithm –Generate permutations both recursively and iteratively –Explain the relationship between recursion and stacks

6 Recursive Algorithms Recursive algorithms solve the problem by solving smaller versions of the problem –If the smaller versions are only a little smaller, the algorithm can be called a reduce and conquer algorithm –If the smaller versions are about half the size of the original, the algorithm can be called a divide and conquer algorithm

7 Recursive Algorithm Analysis The analysis depends on –the preparation work to divide the input –the size of the smaller pieces –the number of recursive calls –the concluding work to combine the results of the recursive calls

8 Generic Recursive Algorithm Recurse( data, N, solution ) // data a set of input values // N the number of values in the set // solution the solution to this problem if N ≤ SizeLimit then DirectSolution( data, N, solution ) else DivideInput( data, N, smallerSets, smallerSizes, numberSmaller ) for i = 1 to numberSmaller do Recurse(smallerSets[i], smallerSizes[i], smallSolution[i]) end for CombineSolutions(smallSolution, numberSmaller, solution) end if

9 Generic Recursive Algorithm Analysis The recursive algorithm does the following amount of work: Where –DIR(N) is the amount of work done by the direct solution –DIV(N) is the amount of work done by the division of the problem –COM(N) is the amount of work done to combine the solutions

10 Divide and Conquer Example Largest( list, start, end ) if start ≥ end then return list[end] end if middle = (start + end) / 2 first = Largest( list, start, middle ) second = Largest( list, middle+1, end ) if first > second then return first else return second end if

11 Divide and Conquer Example Analysis This algorithm does: –One addition and division to calculate middle –Two recursive calls with lists about half the original size –One comparison to decide between the largest of the first half and the largest of the second half

12 Divide and Conquer Example Analysis The number of comparisons of list values done by this example is given by:

13 Recurrence Relation Form A recurrence relation is a recursive form of an equation, for example: A recurrence relation can be put into an equivalent closed form without the recursion

14 Converting Recurrence Relations Begin by looking at a series of equations with decreasing values of n:

15 Converting Recurrence Relations Now, we substitute back into the first equation:

16 Converting Recurrence Relations We stop when we get to T(1): How many “+ 2” terms are there? Notice we increase them with each substitution.

17 Converting Recurrence Relations We must have n – 1 of the “+ 2” terms because there was one at the start and we did n – 2 substitutions: So, the closed form of the equation is:

18 Approximating Recurrence Relations For recurrence relations of the form T(N) = a * T(N/b) + f(N) where f(N) = Θ(N d ), the closed form can be approximated by:

19 Closest Pair Problem Given a set of N points in space, which two are the closest? A brute force method calculates the distance between every pair of points using: But this does (N 2 – N)/2 distance calculations

20 Brute Force Algorithm smallDist = ∞ for i = 1 to N do for j = i+1 to N do dist = sqrt( (xi – xj) 2 + (yi – yj) 2 ) if dist < smallDist then smallDist = dist first = i second = j end if end for end for

21 A Divide and Conquer Solution Divide the set of points in into a right and left half Find the closest pair in the right and left half (recursively) Determine (d s ) the shortest of these two distances Check if there is a pair of points within d s units but on opposite sides of the dividing line that are closer

22 Divide and Conquer Example

23 Divide and Conquer Example

24 Divide and Conquer Example

25 Divide and Conquer Example

26 Divide and Conquer Closest Pair Part 1 ClosestPair( Px, Py, d, p1, p2) // Px the points sorted by x coordinate // Py the points sorted by y coordinate // d the shortest distance between points p1 and p2 // p1 the first point // p2 the second point if sizeOf(Px)  3 then find d, p1, and p2 by brute force return end if construct Lx, Ly, Rx, Ry ClosestPair(Lx, Ly, dl, L1, L2) ClosestPair(Rx, Ry, dr, R1, R2)

27 Divide and Conquer Closest Pair 2 d = minimum(dl, dr) if d == dl then p1 = L1 p2 = L2 else p1 = R1 p2 = R2 end if construct Mx and My for i = 1 to sizeOf(My)-1 do for j = i+1 to i+7 (with j  sizeOf(My)) do temp = distance(Myi, Myj) if temp < d then d = temp p1 = Myi p2 = Myj end if end for end for

28 Divide and Conquer Closest Point Analysis The combination step looks at the seven points closest to those points within d s of the dividing line The points are divided into two halves Therefore, the recurrence relation is T(N) = 2 * T(N/2) +  (N) This algorithm is  (N lg N)

29 Convex Regions A region is convex if a line connecting every pair of points in the region lies entirely within the region

30 Convex Hull A convex hull for a set of points is the smallest convex region including all of the points All of the points lie on one side of each edge of the convex hull

31 Brute Force Convex Hull Consider the line defined by the first pair of points If all of the other points lie on one side of that line, the line is part of the convex hull Repeat this process for every other pair of points This process is O(N 3 )

32 A Divide and Conquer Solution The leftmost and rightmost points are on the convex hull Use the line between these points to divide the set of points into an upper and lower set Find the convex hull of the two parts The convex hull of the entire set is the convex hulls of these two parts without the dividing line

33 A Divide and Conquer Solution To find the convex hull of each part –Find the point farthest away from the dividing line –If the points are inside the triangle formed with this point, this is the convex hull –If there are points outside one of these two edges, recursively find the convex hull of those points –If necessary, repeat for the other edge

34 Divide and Conquer Example

35 Divide and Conquer Example

36 Divide and Conquer Example

37 Divide and Conquer Convex Hull quickHull(S, ps, pe) if S == {} then return [] // the empty list else pf = point of S farthest from the dividing line PR = set of points to the right of the line between ps and pf PL = set of points to the right of the line between pf and pe return quickHull(PR, ps, pf) + [pf] + quickHull(PL, pf, pe) end if

38 Divide and Conquer Convex Hull Analysis If the algorithm divides the points into two halves, the recurrence relation is T(N) = 2 * T(N/2) +  (N) and the closed form is  (N lg N) In the worst case, all the points wind up on one side of each dividing line and the algorithm is then O(N 2 )

39 Permutations The permutation of a set of elements is an ordering of those elements The permutations of the numbers from 1 to 3 are [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], and [3, 2, 1] If there are N elements in the set, there are N! permutations

40 Recursive Permutations Swap pairs of list values during the recursion Output the list when the “bottom” of the recursion is reached The algorithm by Heap will permute the elements at the front of the list and will then swap in the next element It then permutes the front elements again

41 Recursive Permutations Algorithm heapPermute(n) // list is a global variable if n == 0 then output list else for i = 1 to n do heapPermute(n-1) if n is odd then swap list[1] and list[n] else swap list[i] and list[n] end if end for end if

42 Iterative Permutations Permutations can be listed in lexical order The position within this list is a permutation’s rank in the range [0, N! – 1] It is possible to iteratively generate the permutation from this rank number by using the factorial of the values from 1 to N

43 Iterative Permutations Algorithm list[size] = 1 for j = 1 to size – 1 do d = (rank mod f[j + 1]) / f[j] rank = rank – d * f[j] list[size – j] = d+1 for i = size – j + 1 to size do if list[i] > d then list[i] = list[i] + 1 end if end for end for

44 Recursion and Stacks Every subprogram has an Activation Record (AR) that includes the space needed for parameters, local variables, a return value, and a place to return control when done Every time a subprogram is called during execution, an instance of this AR is created and placed on the system stack

45 Recursion and Stacks When a subprogram completes, its AR instance (ARI) is removed from the system stack For recursive subprograms, there will be multiple ARIs on the stack – one for each call

46 Recursion and Stacks A recursive subprogram could be rewritten to remove recursion by using a programmer created stack to keep track of what would be on the system stack Tracing recursive subprograms is easier if you simulate the system stack by drawing boxes when subprograms are called and crossing them out when the subprogram finishes