Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer.

Slides:



Advertisements
Similar presentations
CS Divide and Conquer/Recurrence Relations1 Divide and Conquer.
Advertisements

Lecture 3: Parallel Algorithm Design
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
A simple example finding the maximum of a set S of n numbers.
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.
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
Divide-and-Conquer The most-well known algorithm design strategy:
The Divide-and-Conquer 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.
Convex Hulls in Two Dimensions Definitions Basic algorithms Gift Wrapping (algorithm of Jarvis ) Graham scan Divide and conquer Convex Hull for line intersections.
CS4413 Divide-and-Conquer
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
The Divide-and-Conquer Strategy
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Theory of Algorithms: Divide and Conquer
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
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.
UMass Lowell Computer Science Advanced Algorithms Computational Geometry Prof. Karen Daniels Spring, 2007 Lecture 3 Chapter 3: 2D Convex Hulls Friday,
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Lecture 4: Divide and Conquer III: Other Applications and Examples Shang-Hua Teng.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Lecture 6 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng.
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.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Divide and Conquer.
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.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Dynamic Programming.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
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.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Chapter 4 Divide-and-Conquer
Chapter 4 Divide-and-Conquer
Convex Hull.
CSCE350 Algorithms and Data Structure
Divide and Conquer – and an Example QuickSort
CSCE 411 Design and Analysis of Algorithms
Chapter 4: Divide and Conquer
Unit-2 Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer Algorithms Part I
Divide-and-Conquer The most-well known algorithm design strategy:
Divide & Conquer Algorithms
Introduction to Algorithms
CSC 380: Design and Analysis of Algorithms
Presentation transcript:

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Definition –Recursion lends itself to a general problem-solving technique (algorithm design) called divide & conquer Divide the problem into 1 or more similar sub-problems Conquer each sub-problem, usually using a recursive call Combine the results from each sub-problem to form a solution to the original problem –Algorithmic Pattern: DC( problem ) solution =  if ( problem is small enough ) solution = problem.solve() else children = problem.divide() for each c in children solution = solution + c.solve() return solution Divide Conquer Combine

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Applicability –Use the divide-and-conquer algorithmic pattern when ALL of the following are true: The problem lends itself to division into sub-problems of the same type The sub-problems are relatively independent of one another (ie, no overlap in effort) An acceptable solution to the problem can be constructed from acceptable solutions to sub-problems

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Well-Known Uses –Searching Binary search –Sorting Merge Sort Quick Sort –Mathematics Polynomial and matrix multiplication Exponentiation Large integer manipulation –Points Closest Pair Merge Hull Quick Hull

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (1) –Example: Binary Search Find the location of an element in a sorted collection of n items (assuming it exists) binarySearch( A, low, high, target ) mid = (high – low) / 2 if ( A[mid] == target ) return mid else if ( A[mid] < target ) return binarySearch(A, mid+1, high, target) else return binarySearch(A, low, mid-1, target)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (2) –Example: MergeSort Sort a collection of n items into increasing order mergeSort( A, low, high ) if ( low < high ) mid = (high – low) / 2 mergeSort(A, low, mid) mergeSort(A, mid+1, high) merge(A, low, mid, mid+1, high)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

How it actually runs The recursion of the right list doesn’t actually occur until the recursion on the left list returns This leads to a left to right processing of the recursion “tree”

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

Algorithmics: Divide & Conquer (3) –Example: QuickSort Sort a collection of n items into increasing order –Algorithm steps: Break the list into 2 pieces based on a pivot –The pivot is usually the first item in the list All items smaller than the pivot go in the left and all items larger go in the right Sort each piece (recursion again) Combine the results together

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

Algorithmics: Divide & Conquer (4) –Example: Exponentiation Calculate a b for integer b  0 exp( a, b ) if ( b == 0 ) return 1 else if ( b is even ) return exp(a, b / 2) 2 else return a * exp(a, b – 1)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (5) –Example: Closest-Pair Problem (1) Find the closest pair of points in a collection of n points in a given plane (use Euclidean distance) Assume n = 2 k If n = 2, answer is distance between these two points Otherwise, –sort points by x-coordinate –split sorted points into two piles of equal size (i.e., divide by vertical line l) Three possibilities: closest pair is –in Left side –in Right side –between Left and Right side Application: Closest pair of airplanes at a given altitude.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (5) –Example: Closest-Pair Problem (2) Let d = min( d L, d R ) –only need to see if there are points in Left and Right that are closer than d –only need to check points within 2d of l –sort points by y-coordinate –can only be eight points in the 2d x d slice distance between closest points in left and right sides

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (1) –Definitions: A convex hull of a set of 2D points is the shape taken by a rubber band stretched around nails pounded into the plane at each point. A convex hull of a set S of points is the smallest polygon P for which each point of S is either on the boundary or in the interior of P. A convex hull of a set S of points is the smallest set   S | if x 1, x 2   and   [0,1] then x =  x 1 + (1 -  )x 2   Applications: collision avoidance in robotics; mapping the surface of an object (like an asteroid); analyzing images of soil particles; estimating percentage of lung volume occupied by pneumonia

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (2) –QuickHull Algorithm (1): This is actually a half-space hull finder –You start with the leftmost (A) and rightmost (B) points and all the points above them –It finds the half hull to the top side One can then find the entire hull by running the same algorithm on the bottom points

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (3) –QuickHull Algorithm (2): Pick the point C that is furthest from the line AB Form two lines – AC and CB and place all points above AC into one list and all points above CB into another list. Ignore the inner points

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (4) –QuickHull Algorithm (3): Recursively conquer each list of points. Return the line AB for each eventual empty list of points Combine resulting AB lines into polygon

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (5) –MergeHull Algorithm (1): This convex hull algorithm is similar to Merge Sort (obviously given the name!) To make the algorithm as fast as possible we first need to sort the points from left to right (based on x-coordinate) After that we apply a divide-and-conquer technique

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (6) –MergeHull Algorithm (2): Split the points into two equal halves Conquer each half to get convex hull for that half (stop when you reach a single point)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (7) –MergeHull Algorithm (3): Combine the 2 small hulls together into 1 large hull –First find the top and bottom tangent lines to the hulls »A special “walking” algorithm is used to find the tangent lines in linear time –Then use these tangent lines to create the full hull