Divide and Conquer. Algorithm Fall Semester

Similar documents
Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Divide-and-Conquer. Dr. Yingwu Zhu

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Unit-2 Divide and conquer 2016

Divide and Conquer 4-0

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

A 0 A 1... A i 1 A i,..., A min,..., A n 1 in their final positions the last n i elements After n 1 passes, the list is sorted.

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

CS 171: Introduction to Computer Science II. Quicksort

We can use a max-heap to sort data.

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Sorting. Task Description. Selection Sort. Should we worry about speed?

Data Structure Lecture#17: Internal Sorting 2 (Chapter 7) U Kang Seoul National University

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Simple Sorting Algorithms

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Divide-and-Conquer CSE 680

Chapter 2 Divid d e-an -Conquer 1

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Better sorting algorithms (Weiss chapter )

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

COSC242 Lecture 7 Mergesort and Quicksort

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Divide and Conquer. Divide and Conquer

MA/CSSE 473 Day 17. Divide-and-conquer Convex Hull. Strassen's Algorithm: Matrix Multiplication. (if time, Shell's Sort)

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

BM267 - Introduction to Data Structures

ITEC2620 Introduction to Data Structures

Lecture 19 Sorting Goodrich, Tamassia

Lecture 8: Mergesort / Quicksort Steven Skiena

Test 1 Review Questions with Solutions

CS473 - Algorithms I

Algorithm Design and Analysis

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

5. DIVIDE AND CONQUER I

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

Data Structures And Algorithms

CSC 421: Algorithm Design & Analysis. Spring 2015

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Divide and Conquer Sorting Algorithms and Noncomparison-based

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Mergesort again. 1. Split the list into two equal parts

Quick Sort. CSE Data Structures May 15, 2002

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

DATA STRUCTURES AND ALGORITHMS

Divide and Conquer. Algorithm D-and-C(n: input size)

CSCE 411 Design and Analysis of Algorithms

Randomized Algorithms, Quicksort and Randomized Selection

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Quicksort (Weiss chapter 8.6)

Sorting Pearson Education, Inc. All rights reserved.

Sorting. Order in the court! sorting 1

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

COMP Data Structures

O(n): printing a list of n items to the screen, looking at each item once.

Sorting. Order in the court! sorting 1

Divide & Conquer Design Technique

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

CS Divide and Conquer

Divide-and-Conquer Algorithms

CS Divide and Conquer

CS483 Analysis of Algorithms Lecture 03 Divide-n-Conquer

QuickSort

CS 171: Introduction to Computer Science II. Quicksort

UNIT-2 DIVIDE & CONQUER

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

CSE 421 Algorithms: Divide and Conquer

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

2/26/2016. Divide and Conquer. Chapter 6. The Divide and Conquer Paradigm

SORTING, SETS, AND SELECTION

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Data Structures and Algorithms CMPSC 465

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Union Find. Data Structures and Algorithms Andrei Bulatov

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Sorting Goodrich, Tamassia Sorting 1

Faster Sorting Methods

Sorting Algorithms. + Analysis of the Sorting Algorithms

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute

Lecture 2: Divide and Conquer

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Searching in General

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Closest Pair of Points. Cormen et.al 33.4

SORTING AND SELECTION

Transcription:

Divide and Conquer Algorithm 2014 Fall Semester

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 2 / 52

Divide-and-Conquer 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 3 / 52

Divide-and-Conquer Examples Sorting: merge sort and quick sort Binary search Multiplication of large integers Matrix multiplication: Strassen s algorithm Closest-pair algorithms Convex-hull algorithms 4 / 52

Mergesort Split array A[0..n-1] in two about equal halves and make copies of each half in arrays B and C Sort arrays B and C recursively Merge sorted arrays B and C into array A as follows: Repeat the following until no elements remain in one of the arrays: compare the first elements in the remaining unprocessed portions of the arrays copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A. 5 / 52

Mergesort Example 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 3 8 2 9 1 7 4 5 2 3 8 9 1 4 5 7 1 2 3 4 5 7 8 9 6 / 52

Quicksort Select a pivot (partitioning element) here, the first element Rearrange the list so that all the elements in the first s positions are smaller than or equal to the pivot and all the elements in the remaining n-s positions are larger than or equal to the pivot (see next slide for an example) p A[i] p A[i] p Exchange the pivot with the last element in the first (i.e., ) subarray the pivot is now in its final position Sort the two subarrays recursively 7 / 52

Quicksort Example 1. 5-3 - 7-6 - 2-1 - 4 p 2. 5-3 - 7-6 - 2-1 - 4 i j p 3. 1-3 - 7-6 - 2-5 - 4 i j p 4. 1-3 - 7-6 - 2-5 - 4 i j p 5. 1-3 - 7-6 - 2-5 - 4 i j p 6. 1-3 - 2-6 - 7-5 - 4 i j p 7. 1-3 - 2-6 - 7-5 - 4 p 8. 1-3 - 2-4 - 7-5 - 6 p Sort sub-list : 1-3 - 2 1-2 - 3 Final Result : 1-2 - 3-4 - 5-6 - 7 8 / 52

In-Class Exercise Quicksort 24 32 11 15 62 3 9 13 22 5 10 9 / 52

Binary Search Very efficient algorithm for searching in sorted array: K vs A[0]... A[m]... A[n-1] If K = A[m], stop (successful search); otherwise, continue searching by the same method in A[0..m-1] if K < A[m] and in A[m+1..n-1] if K > A[m] l 0; r n-1 while l r do m (l+r)/2 if K = A[m] return m else if K < A[m] r m-1 else l m+1 return -1 10 / 52

Binary Search Example Sorted list : 1 7 14 17 26 59 63 77 79 87 88 90 92 96 98 99 Key value : 63 Step 1 ) M=(0+15) div 2 A[7] = 77 Step 2) Is the number greater than 77? (No) M=(0+6) div 2 A[3] = 17 Step 3) Is the number greater than 17? (Yes).. 11 / 52

Multiplication of Large Integers Consider the problem of multiplying two (large) n-digit integers represented by arrays of their digits such as: A = 12525678901357986429 B = 87654321284820912836 12 / 52

Divide & Conquer Algorithm A small example: A B where A = 2135 and B = 4014 A = (21 10 2 + 35), B = (40 10 2 + 14) So, A B = (21 10 2 + 35) (40 10 2 + 14) = 21 40 10 4 + (21 14 + 35 40) 10 2 + 35 14 In general, if A = A 1 A 2 and B = B 1 B 2 (where A and B are n-digit, A 1, A 2, B 1, B 2 are n/2-digit numbers), A B = A 1 B 1 10 n + (A 1 B 2 + A 2 B 1 ) 10 n/2 + A 2 B 2 13 / 52

Divide & Conquer Algorithm Let P 1 = (I h + I l ) x (J h + J l ) = I h xj h + I h x J l + I l xj h + I l xj l P 2 = I h x J h, and P 3 = I l x J l Now, note that P 1 - P 2 P 3 = I h xj h + I h xj l + I l xj h + I l xj l - I h xj h - I l xj l = I h xj l + I l xj h Then we have the following: I x J = P 2 x 2 n + [P 1 - P 2 P 3 ]x 2 n/2 + P 3. 14 / 52

Divide & Conquer exercise Multiplication of Large Integers 11010011 * 01011001 15 / 52

Let I = 11010011, which is 211 in decimal Let J = 01011001, which is 89 in decimal. Then we have I h = 1101, which is 13 in decimal, and I l = 0011, which is 3 in decimal Also we have J h = 0101, which is 5 in decimal, and J l = 1001, which is 9 in decimal 1) Compute I h + I l = 10000, which is 16 in decimal 2) Compute J h + J l = 1110, which is 14 in decimal 3) Recursively multiply (I h + I l ) x (J h + J l ), giving us 11100000, which is 224 in decimal. (This is P 1.) 4) Recursively mutliply I h x J h, giving us 01000001, which is 65 in decimal. (This is P 2.) 5) Recursively multiply I l x J l, giving us 00011011, which is 27 in decimal. (This is P 3.) 6) Compute P 1 - P 2 P 3 using 2 subtractions to yield 10000100, which is 132 in decimal 7) Now compute the product as 01000001x100000000 + 10000100x 00010000 + 00011011 = 0100000100000000 (P 2 x2 8 ) 100001000000 ((P 1 - P 2 - P 3 ) x2 4 ) + 00011011 (P 3 ) -------------------------------- 0100100101011011, which is 18779 in decimal, the correct answer. 16 / 52 (This is also 65x2 8 +132 x2 4 +27.)

Strassen s Matrix Multiplication Strassen observed [1969] that the product of two matrices can be computed as follows: C 00 C 01 A 00 A 01 B 00 B 01 = * C 10 C 11 A 10 A 11 B 10 B 11 C 00 = A 00 B 00 + A 01 B 10 C 01 = A 00 B 01 +A 01 B 11 C 10 = A 10 B 00 + A 11 B 10 C 11 = A 10 B 01 + A 11 B 11 = M 1 + M 4 - M 5 + M 7 M 3 + M 5 M 2 + M 4 M 1 + M 3 - M 2 + M 6 2x2 matrix multiplication can be accomplished in 8 multiplication.(2 log 2 8 =2 3 ) 17 / 52

Basic Matrix Multiplication Algorithm Time analysis void matrix_mult () { } C for (i = 1; i <= N; i++) i, j for (j = 1; j <= N; j++) compute C i,j ; N k 1 a i, k b ThusT ( N ) k, j N N N i 1 j 1 k 1 c cn 3 O( N 3 ) 18 / 52

Strassens s Matrix Multiplication Strassen showed that 2x2 matrix multiplication can be accomplished in 7 multiplication and 18 additions or subtractions. (2 log 2 7 =2 2.807 ) This reduce can be done by Divide and Conquer Approach. 19 / 52

Formulas for Strassen s Algorithm M 1 = (A 00 + A 11 ) (B 00 + B 11 ) M 2 = (A 10 + A 11 ) B 00 M 3 = A 00 (B 01 - B 11 ) M 4 = A 11 (B 10 - B 00 ) C 00 = M 1 + M 4 - M 5 + M 7 C 01 = M 3 + M 5 C 10 = M 2 + M 4 C 11 = M 1 + M 3 - M 2 + M 6 M 5 = (A 00 + A 01 ) B 11 M 6 = (A 10 - A 00 ) (B 00 + B 01 ) M 7 = (A 01 - A 11 ) (B 10 + B 11 ) 20 / 52

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

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

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

Closest-Pair Algorithm Lets sort based on the X-axis O(n log n) using quicksort or mergesort 4 9 1 2 3 5 6 7 8 10 11 12 13 14 24 / 52

Closest-Pair Algorithm Step 2: Split the points, i.e., Draw a line at the mid-point between 7 and 8 4 9 1 2 3 5 6 7 8 10 11 12 13 14 Sub-Problem 1 Sub-Problem 2 25 / 52

Closest-Pair Algorithm 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 4 9 1 2 3 5 6 7 8 10 11 12 13 14 Sub-Problem 1 Sub-Problem 2 26 / 52

Closest-Pair Algorithm 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 solution d = min(d1, d2) 4 9 1 2 d1 3 5 6 7 8 10 d2 11 12 13 14 Sub-Problem 1 Sub-Problem 2 27 / 52

Closest-Pair Algorithm 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. d = min(d1, d2) 4 9 1 2 d1 3 5 6 7 8 10 d2 11 12 13 14 Sub-Problem 1 Sub-Problem 2 28 / 52

Closest-Pair Algorithm Problem: However, what if the closest two points are each from different sub-problems? 4 9 1 2 d1 3 5 6 7 8 10 d2 11 12 13 14 Sub-Problem 1 Sub-Problem 2 29 / 52

Closest-Pair Algorithm Here is an example where we have to compare points from sub-problem 1 to the points in subproblem 2. 4 1 2 d1 3 5 6 7 8 9 10 d2 11 13 14 12 Sub-Problem 1 Sub-Problem 2 30 / 52

Closest-Pair Algorithm However, we only have to compare points inside the following strip. d = min(d1, d2) 4 9 1 2 d1 3 5 6 d 7 d 8 10 d2 11 12 13 14 Sub-Problem 1 Sub-Problem 2 31 / 52

Closest-Pair Algorithm Step 3: But, we can continue the advantage by splitting the sub-problems. 4 9 1 2 3 5 6 7 8 10 11 12 13 14 32 / 52

Closest-Pair Algorithm Step 3: In fact we can continue to split until each sub-problem is trivial, i.e., takes one comparison. 4 9 1 2 3 5 6 7 8 10 11 12 13 14 33 / 52

Closest-Pair Algorithm Finally: The solution to each sub-problem is combined until the final solution is obtained 4 9 1 2 3 5 6 7 8 10 11 12 13 14 34 / 52

Closest-Pair Algorithm Finally: On the last step the strip will likely be very small. Thus, combining the two largest subproblems won t require much work. 4 9 1 2 3 5 6 7 8 10 11 12 13 14 35 / 52

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. 4 9 1 2 3 5 6 7 8 10 11 12 13 14 36 / 52

Closest Pair by Divide & Conquer Divide: Sort halves by x-coordinate. Find vertical line splitting points in half. Conquer: Recursively find closest pairs in each half. Combine: Check vertices near the border to see if any pair straddling the border is closer together than the minimum seen so far. 37 / 52

Closest Pair by Divide & Conquer Step 1 Divide the points given into two subsets S 1 and S 2 by a vertical line x = c so that half the points lie to the left or on the line and half the points lie to the right or on the line. Step 2 Find recursively the closest pairs for the left and right subsets. Step 3 Set d = min{d 1, d 2 } We can limit our attention to the points in the symmetric vertical strip of width 2d as possible closest pair. Let C 1 and C 2 be the subsets of points in the left subset S 1 and of the right subset S 2, respectively, that lie in this vertical strip. The points in C 1 and C 2 are stored in increasing order of their y coordinates, which is maintained by merging during the execution of the next step. Step 4 For every point P(x,y) in C 1, we inspect points in C 2 that may be closer to P than d. There can be no more than 6 such points (because d d 2 )! 38 / 52

Closest Pair by Divide & Conquer: Worst Case The worst case scenario is depicted below: 39 / 52

Closest Pair by Divide & Conquer: Algorithm Divide: draw vertical line L so that roughly ½n points on each side. Conquer: find closest pair in each side recursively. Combine: find closest pair with one point in each side. Return best of 3 solutions. 40 / 52

Convex hull (definition) H is the smallest convex polygon that contains all the points of Q 41 / 52

Convex hull (principle) Decompose the set of points in equal parts (Qleft and Qright) Solve the sub problems respectively on Qleft and Qright Merge both convex hulls Hleft and Hright 42 / 52

Convex hull Algorithm Step 1 : Decomposition Sort the points 43 / 52

Convex hull Algorithm Step 2 : Decomposition Split part Split the points into two sets of equal size 44 / 52

Convex hull Algorithm Step 3 : Solve Sub-Problems Compute the convex hulls on Qleft and Qright 45 / 52

Convex hull Algorithm Step 4 : Merge Both Solutions 46 / 52

Convex hull Algorithm Step 4 : Merge Both Solutions Determine the upper tangent 47 / 52

Convex hull Algorithm Step 4 : Merge Both Solutions Determine the upper tangent 48 / 52

Convex hull Algorithm Step 4 : Merge Both Solutions Determine the upper tangent 49 / 52

Convex hull Algorithm Step 4 : Merge Both Solutions Determine the upper tangent 50 / 52

Convex hull Algorithm Step 4 : Merge Both Solutions Determine the lower tangent 51 / 52

Q & A