Theory of Algorithms: Divide and Conquer

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

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.
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
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Chapter 3: 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.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
CS 171: Introduction to Computer Science II Quicksort.
1 Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
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.
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.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication.
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.
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.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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 A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
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.
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.
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.
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.
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
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
CSCE350 Algorithms and Data Structure
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer
Advanced Sorting Methods: Shellsort
CO 303 Algorithm Analysis And Design Quicksort
Divide-and-Conquer The most-well known algorithm design strategy:
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
Advanced Sorting Methods: Shellsort
Presentation transcript:

Theory of Algorithms: Divide and Conquer

Objectives To introduce the divide-and-conquer mind set To show a variety of divide-and-conquer solutions: Merge Sort Quick Sort Convex Hull by Divide-and-Conquer Strassen’s Matrix Multiplication To discuss the strengths and weaknesses of a divide-and-conquer strategy

Divide-and-Conquer Best known algorithm design strategy: Divide instance of problem into two or more smaller instances Solve smaller instances recursively Obtain solution to original (larger) instance by combining these solutions Recurrence Templates apply Recurrences are of the form T(n) = aT(n/b) + f (n), a  1, b  2 Silly Example (Addition): a0 + …. + an-1 = (a0 + … + an/2-1) + (an/2 + … an-1)

Divide-and-Conquer Illustrated A PROBLEM OF SIZE n SUBPROBLEM 1 OF SIZE n/2 SUBPROBLEM 2 OF SIZE n/2 A SOLUTION TO SUBPROBLEM 1 A SOLUTION TO SUBPROBLEM 2 A SOLUTION TO THE ORIGINAL PROBLEM

Mergesort Algorithm: Merging: Split A[1..n] in half and put copy of each half into arrays B[1.. n/2 ] and C[1.. n/2 ] Recursively MergeSort arrays B and C Merge sorted arrays B and C into array A Merging: REPEAT until no elements remain in one of B or C Compare 1st elements in the rest of B and C Copy smaller into A, incrementing index of corresponding array Once all elements in one of B or C are processed, copy the remaining unprocessed elements from the other array into A

Mergesort Example 7 2 1 6 4 7 2 1 6 4 7 2 1 6 4 7 2 4 6 2 7 1 2 7 1 2 4 6 7

Efficiency of Mergesort Recurrence: C(n) = 2 C(n/2) + Cmerge(n) for n > 1, C(1) = 0 Cmerge(n) = n - 1 in the worst case All cases have same efficiency: (n log n) Number of comparisons is close to theoretical minimum for comparison-based sorting: log n! ≈ n lg n - 1.44 n Space requirement: ( n ) (NOT in-place) Can be implemented without recursion (bottom-up) even if not analyzing in detail, show the recurrence for mergesort in worst case: T(n) = 2 T(n/2) + (n-1) worst case comparisons for merge

Quicksort Select a pivot (partitioning element) Rearrange the list into two sublists: All elements positioned before the pivot are ≤ the pivot Those positioned after the pivot are > the pivot Requires a pivoting algorithm Exchange the pivot with the last element in the first sublist The pivot is now in its final position QuickSort the two sublists p A[i]≤p A[i]>p

The Partition Algorithm

Quicksort Example 5 3 1 9 8 2 7 2 3 1 8 9 7 i j i j i j 5 3 1 9 8 2 7 5 3 1 9 8 2 7 2 3 1 8 9 7 i j i j i j 5 3 1 9 8 2 7 2 1 3 8 7 9 i j i j i j 5 3 1 2 8 9 7 2 1 3 8 7 9 i j j i j i 5 3 1 2 8 9 7 1 2 3 7 8 9 j i 2 3 1 5 8 9 7 Recursive Call Quicksort (2 3 1) & Quicksort (8 9 7) Recursive Call Quicksort (1) & Quicksort (3) Recursive Call Quicksort (7) & Quicksort (9)

Worst Case Efficiency of Quicksort In the worst case all splits are completely skewed For instance, an already sorted list! One subarray is empty, other reduced by only one: Make n+1 comparisons Exchange pivot with itself Quicksort left = Ø, right = A[1..n-1] Cworst = (n+1) + n + … + 3 = (n+1)(n+2)/2 - 3 = (n2) p A[i]≤p A[i]>p

General Efficiency of Quicksort Efficiency Cases: Best: split in the middle — ( n log n) Worst: sorted array! — ( n2) Average: random arrays — ( n log n) Improvements (in combination 20-25% faster): Better pivot selection: median of three partitioning avoids worst case in sorted files Switch to Insertion Sort on small subfiles Elimination of recursion Considered the method of choice for internal sorting for large files (n ≥ 10000)

Objectives To introduce the divide-and-conquer mind set To show a variety of divide-and-conquer solutions: Merge Sort Quick Sort Convex Hull by Divide-and-Conquer Strassen’s Matrix Multiplication To discuss the strengths and weaknesses of a divide-and-conquer strategy

QuickHull Algorithm Sort points by increasing x-coordinate values Identify leftmost and rightmost extreme points P1 and P2 (part of hull) Compute upper hull: Find point Pmax that is farthest away from line P1P2 Quickhull the points to the left of line P1Pmax Quickhull the points to the left of line PmaxP2 Similarly compute lower hull Pmax L P2 L P1

Finding the Furthest Point Given three points in the plane p1 , p2 , p3 Area of Triangle =  p1 p2 p3 = 1/2  D  D =  D  = x1 y2 + x3 y1 + x2 y3 - x3 y2 - x2 y1 - x1 y3 Properties of  D  : Positive iff p3 is to the left of p1p2 Correlates with distance of p3 from p1p2 x1 y1 1 x2 y2 x3 y3

Efficiency of Quickhull Finding point farthest away from line P1P2 is linear in the number of points This gives same efficiency as quicksort: Worst case: (n2) Average case: (n log n) If an initial sort is required, this can be accomplished in ( n log n) — no increase in asymptotic efficiency class Alternative Divide-and-Conquer Convex Hull: Graham’s scan and DCHull Also ( n log n) but with lower coefficients

Strassen’s Matrix Multiplication Strassen observed [1969] that the product of two matrices can be computed as follows: Op Count: Each of M1, …, M7 requires 1 mult and 1 or 2 add/sub Total = 7 mul and 18 add/sub Compared with brute force which requires 8 mults and 4 add/sub. But is asymptotic behaviour is NB n/2  n/2 submatrices are computed recursively by the same method C00 C01 C10 C11 A00 A01 A10 A11 B00 B01 B10 B11 = * M1 + M4 -M5+M7 M3+ M5 M2 + M4 M1 + M3 - M2 + M6 =

Efficiency of Strassen’s Algorithm If n is not a power of 2, matrices can be padded with zeros Number of multiplications: M(n) = 7 M(n/2) for n > 1, M(1) = 1 Set n = 2k, apply backward substitution M(2k) = 7M(2k-1) = 7 [7 M(2k-2)] = 72 M(2k-2) = … = 7k M(n) = 7log n = nlog 7 ≈ n2.807 Number of additions: A(n)  (nlog 7) Other algorithms closer to the lower limit of n2 multiplications, but are even more complicated

Strengths and Weaknesses of Divide-and-Conquer Generally improves on Brute Force by one base efficiency class Easy to analyse using the Recurrence Templates Weaknesses: Often requires recursion, which introduces overheads Can be inapplicable and inferior to simpler algorithmic solutions