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.

Slides:



Advertisements
Similar presentations
Closest-Pair Problem: Divide and Conquer
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
Algorithm Design Paradigms
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Divide-and-Conquer Dr. Yingwu Zhu P65-74, p83-88, p93-96, p
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.
CS4413 Divide-and-Conquer
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
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
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Foundations of Algorithms, Fourth Edition
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2004 Simonas Šaltenis E1-215b
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
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.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
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.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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. Outline Introduction Merge Sort Quick Sort Closest pair of points Large integer multiplication.
Divide and Conquer Strategy
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Sorting divide and conquer. Divide-and-conquer  a recursive design technique  solve small problem directly  divide large problem into two subproblems,
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.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer.
CSCE350 Algorithms and Data Structure
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
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
Chapter 4.
Divide-and-Conquer The most-well known algorithm design strategy:
Algorithms Dr. Youn-Hee Han April-May 2013
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

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 recursively 3. Obtain solution to original (larger) instance by combining these solutions

Divide-and-conquer technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n

Divide and Conquer Examples Sorting: mergesort and quicksort O(n 2 )  O(n log n) Sorting: mergesort and quicksort O(n 2 )  O(n log n) Closest-pair algorithm O(n 2 )  O(n log n) Closest-pair algorithm O(n 2 )  O(n log n) Matrix multiplication Strassen’s O(n 3 )  O(n log 7 ) = O(n 2.8 ) Matrix multiplication Strassen’s O(n 3 )  O(n log 7 ) = O(n 2.8 ) Convex hull-QuickHull algorithm O(n 3 )  O(n 2 log n) Convex hull-QuickHull algorithm O(n 3 )  O(n 2 log n)

General Divide and Conquer recurrence: T(n) = aT(n/b) + f (n) where f (n) = Θ(n k ) 1. a < b k T(n) = Θ(n k ) 2. a = b k T(n) = Θ(n k lg n ) 3. a > b k T(n) = Θ( n log b a )

Efficiency of mergesort All cases have same time efficiency: Θ( n log n) All cases have same time efficiency: Θ( n log n) Number of comparisons is close to theoretical minimum for comparison-based sorting: Number of comparisons is close to theoretical minimum for comparison-based sorting: log (n !) ≈ n log n n log (n !) ≈ n log n n Space requirement: Θ( n ) (NOT in-place) Space requirement: Θ( n ) (NOT in-place) Can be implemented without recursion (bottom-up) Can be implemented without recursion (bottom-up)

Quicksort Select a pivot (partitioning element) Select a pivot (partitioning element) Partition the list into two halves Partition the list into two halves –First half: Items less than the pivot –Second half: Items greater than the pivot Exchange the pivot with the last element in the first half Exchange the pivot with the last element in the first half Sort the two sublists Sort the two sublists p A[i]≤p A[i]>p

Efficiency of quicksort Best case: split in the middle Θ( n log n) Best case: split in the middle Θ( n log n) Worst case: sorted array! Θ( n 2 ) Worst case: sorted array! Θ( n 2 ) Average case: random arrays Θ( n log n) Average case: random arrays Θ( n log n)

Efficiency of quicksort Improvements: Improvements: –better pivot selection: median of three partitioning avoids worst case in sorted files –switch to insertion sort on small subfiles –elimination of recursion these combine to 20-25% improvement Considered the method of choice for internal sorting for large files (n ≥ 10000) Considered the method of choice for internal sorting for large files (n ≥ 10000)

Efficiency of QuickHull algorithm If points are not initially sorted by x- coordinate value, this can be accomplished in Θ( n log n) — no increase in asymptotic efficiency class If points are not initially sorted by x- coordinate value, this can be accomplished in Θ( n log n) — no increase in asymptotic efficiency class Other algorithms for convex hull: Other algorithms for convex hull: –Graham’s scan –DCHull also in Θ( n log n)

Closest-Pair Problem: Divide and Conquer Brute force approach requires comparing every point with every other point Brute force approach requires comparing every point with every other point Given n points, we must perform … + n-2 + n-1 comparisons. Given n points, we must perform … + n-2 + n-1 comparisons. Brute force  O(n 2 ) Brute force  O(n 2 ) The Divide and Conquer algorithm yields  O(n log n) The Divide and Conquer algorithm yields  O(n log n) Reminder: if n = 1,000,000 then Reminder: if n = 1,000,000 then n 2 = 1,000,000,000,000 whereas n 2 = 1,000,000,000,000 whereas n log n = 20,000,000 n log n = 20,000,000

Closest-Pair Algorithm Given: A set of points in 2-D

Closest-Pair Algorithm Step 1: Sort the points in one D

Lets sort based on the X-axis O(n log n) using quicksort or mergesort Closest-Pair Algorithm

Step 2: Split the points, i.e., Draw a line at the mid-point between 7 and Closest-Pair Algorithm Sub-Problem 1Sub-Problem 2

Advantage: Normally, we’d have to compare each of the 14 points with every other point. (n-1)n/2 = 13*14/2 = 91 comparisons Closest-Pair Algorithm Sub-Problem 1Sub-Problem 2

Advantage: Now, we have two sub-problems of half the size. Thus, we have to do 6*7/2 comparisons twice, which is 42 comparisons Closest-Pair Algorithm d1 d2 Sub-Problem 1Sub-Problem 2 solution d = min(d1, d2)

Advantage: With just one split we cut the number of comparisons in half. Obviously, we gain an even greater advantage if we split the sub-problems Closest-Pair Algorithm d1 d2 Sub-Problem 1Sub-Problem 2 d = min(d1, d2)

Problem: However, what if the closest two points are each from different sub-problems? Closest-Pair Algorithm d1 d2 Sub-Problem 1Sub-Problem 2

Here is an example where we have to compare points from sub-problem 1 to the points in sub- problem Closest-Pair Algorithm d1 d2 Sub-Problem 1Sub-Problem 2

However, we only have to compare points inside the following “strip.” Closest-Pair Algorithm d1 d2 Sub-Problem 1Sub-Problem 2 dd d = min(d1, d2)

Step 3: But, we can continue the advantage by splitting the sub-problems Closest-Pair Algorithm

Step 3: In fact we can continue to split until each sub-problem is trivial, i.e., takes one comparison Closest-Pair Algorithm

Finally: The solution to each sub-problem is combined until the final solution is obtained Closest-Pair Algorithm

Finally: On the last step the ‘strip’ will likely be very small. Thus, combining the two largest sub- problems won’t require much work Closest-Pair Algorithm

Closest-Pair Algorithm In this example, it takes 22 comparisons to find the closets-pair. The brute force algorithm would have taken 91 comparisons. But, the real advantage occurs when there are millions of points.

Closest-Pair Problem: Divide and Conquer Here is another animation: Here is another animation: stPairApplet/ClosestPairApplet.html stPairApplet/ClosestPairApplet.html stPairApplet/ClosestPairApplet.html stPairApplet/ClosestPairApplet.html