Divide-and-Conquer. Dr. Yingwu Zhu

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

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

CSC Design and Analysis of Algorithms

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

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

Divide and Conquer. Algorithm Fall Semester

Divide and Conquer 4-0

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

CS Divide and Conquer

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

CS Divide and Conquer

Unit-2 Divide and conquer 2016

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

CS473 - Algorithms I

CSCE 411 Design and Analysis of Algorithms

We can use a max-heap to sort data.

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

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

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

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

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.

Quick Sort. CSE Data Structures May 15, 2002

Lecture 8: Mergesort / Quicksort Steven Skiena

Divide-and-Conquer CSE 680

Lecture 19 Sorting Goodrich, Tamassia

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

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

Advanced Algorithms. Problem solving Techniques. Divide and Conquer הפרד ומשול

5. DIVIDE AND CONQUER I

CS483 Analysis of Algorithms Lecture 03 Divide-n-Conquer

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

CS 171: Introduction to Computer Science II. Quicksort

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

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

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

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

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

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

Divide and Conquer. Divide and Conquer

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSC Design and Analysis of Algorithms

Quicksort (Weiss chapter 8.6)

The divide and conquer strategy has three basic parts. For a given problem of size n,

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

Module 2: Classical Algorithm Design Techniques

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

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

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

Divide and Conquer Sorting Algorithms and Noncomparison-based

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.

Divide-and-Conquer. Combine solutions to sub-problems into overall solution. Break up problem of size n into two equal parts of size!n.

UNIT-2 DIVIDE & CONQUER

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

COSC242 Lecture 7 Mergesort and Quicksort

EECS 2011M: Fundamentals of Data Structures

Data Structures and Algorithms

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Lecture 7. Transform-and-Conquer

COMP 250 Fall recurrences 2 Oct. 13, 2017

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

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

100 points total. CSE 3353 Homework 2 Spring 2013

Randomized Algorithms, Quicksort and Randomized Selection

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

Test 1 Review Questions with Solutions

Multiple-choice (35 pt.)

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

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

4. Sorting and Order-Statistics

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

Algorithms - Ch2 Sorting

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Data Structures and Algorithms CSE 465

Sorting Goodrich, Tamassia Sorting 1

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Faster Sorting Methods

Better sorting algorithms (Weiss chapter )

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

CPS 616 TRANSFORM-AND-CONQUER 7-1

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

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

COMP Data Structures

CPSC 320 Midterm 2 Thursday March 13th, 2014

Divide & Conquer Design Technique

CSC 273 Data Structures

BM267 - Introduction to Data Structures

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

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

DATA STRUCTURES AND ALGORITHMS

QuickSort

Sorting Algorithms Day 2 4/5/17

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

Lecture 2: Getting Started

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

CSC 421: Algorithm Design & Analysis. Spring 2015

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication

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

Transcription:

Divide-and-Conquer Dr. Yingwu Zhu

Divide-and-Conquer The most-well known algorithm design technique: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances independently and recursively When to stop? 3. Obtain solution to original (larger) instance by combining these solutions

Divide-and-Conquer Technique (cont.) a problem of size n subproblem 1 of size n/2 Divide subproblem 2 of size n/2 a solution to subproblem 1 a solution to subproblem 2 a solution to the original problem

A General Template //S is a large problem with input size of n Algorithm divide_and_conquer(s) if (S is small enough to handle) solve it //conquer else split S into two (equally-sized) subproblems S1 and S2 divide_and_conquer(s1) divide_and_conquer(s2) combine solutions to S1 and S2 endif End

General Divide-and-Conquer Recurrence Recursive algorithms are a natural fit for divide-and-conquer Distinguish from Dynamic Programming Recall in Chapter 2, algorithm efficiency analysis for recursive algorithms Key: Recurrence Relation Solve: backward substitution, often cumbersome!

General Divide-and-Conquer Recurrence T(n) = at(n/b) + f (n) where f(n) (n d ), d 0, f(n) accounts for the time spent on dividing the problem into smaller ones and combining their solutions Master Theorem: If a < b d, T(n) (n d ) If a = b d, T(n) (n d log n) If a > b d, T(n) (n log b a ) Note: The same results hold with O instead of. Examples: T(n) = 4T(n/2) + n T(n)? T(n) = 4T(n/2) + n 2 T(n)? T(n) = 4T(n/2) + n 3 T(n)?

Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary tree traversals Binary search (degenerated) Multiplication of large integers Matrix multiplication: Strassen s algorithm Closest-pair and convex-hull algorithms

Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary tree traversals Binary search (degenerated) Multiplication of large integers Matrix multiplication: Strassen s algorithm Closest-pair and convex-hull algorithms

Mergesort Question #1: what makes mergesort distinct from many other sorting algorithms? internal and external algorithm Question #2: How to design mergesort using divide-and-conquer technique?

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 Q: when to stop? 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.

Pseudocode of Mergesort

Pseudocode of Merge

8 3 2 9 7 1 5 4 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 3 8 2 9 1 7 4 5 2 3 8 9 1 4 5 7 1 2 3 4 5 7 8 9

Analysis of Mergesort Time efficiency by recurrence reltation: T(n) = 2T(n/2) + f(n) n-1 comparisons in merge operation for worst case! T(n) = Θ(n log n) Number of comparisons in the worst case is close to theoretical minimum for comparison-based sorting: log 2 n! n log 2 n - 1.44n (Section 11.2) Space requirement: Θ(n) (not in-place) Can be implemented without recursion (bottom-up)

Mergsort: A big picture Problem: Assume you want to sort X terabytes (even perabytes) of data using a cluster of M (even thousands) computers. Solution?

Similar to Search Engines in some aspects: data partitioning!

Applications in My Recent Research Work "Measurement and Analysis of An Online Content Voting Network: A Case Study of Digg", In 19th International World Wide Web Conference (WWW2010) More interesting to you: I applied divide-and-conquer techniques User diggs retrieval (12/2004 4/2009) based on social network graph Use PageRank algorithm (112 lines of Python code) to rank users and thus weigh their votes on content (Matrix manipulations) to defend against vote spam

Exercises #1 a. Write a pseudocode for a divide-and-conquer algorithm for finding the position of the largest element in an array of n numbers. b. Set up and solve a recurrence relation for the number of key comparisons made by your algorithm. c. How does this algorithm compare with the brute-force algorithm for this problem?

Quicksort Select a pivot (partitioning element) here, the first element for simplicity! 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 the pivot (see next slide for an algorithm) 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

Quicksort Basic operation: split/divide Differ from the divide operation in mergesort What is the major difference?

Quicksort Basic operation: split/divide Differ from the divide operation in mergesort What is the major difference? Each split will place the pivot in the right position, and the left sublist < the right sublist No explicit merge

Partitioning Algorithm A[i] > p A[j] <= p

8, 2, 13, 5, 14, 3, 7 Quicksort Example

Best case: T(n) =? Analysis of Quicksort Worst case: T(n) =?

Analysis of Quicksort Best case: split in the middle Θ(n log n) Worst case: sorted array! Θ(n 2 ) Average case: random arrays Θ(n log n) Assume the split can happen in each position with equal probability! See textbook for details! Improvements: better pivot selection: median-of-three partitioning switch to insertion sort on small sublists elimination of recursion These combination makes 20-25% improvement Considered the method of choice for internal sorting of large files (n 10000)

Questions for Quicksort Q1: How to implement the median-of-three rule by reusing the previous implementation of the split operation? Q2: How to implement a non-recursive quicksort?

Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary search (degenerated) Binary tree traversals Multiplication of large integers Matrix multiplication: Strassen s algorithm Closest-pair and convex-hull algorithms

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

Analysis of Binary Search Time efficiency worst-case recurrence: T w (n) = 1 + T w ( n/2 ), T w (1) = 1 solution: T w (n) = log 2 (n+1) This is VERY fast: e.g., T w (10 6 ) = 20 Optimal for searching a sorted array Limitations must be a sorted array direct access to each element (not linked list) Bad (degenerate) example of divide-and-conquer

Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary search (degenerated) Binary tree traversals Multiplication of large integers Matrix multiplication: Strassen s algorithm Closest-pair and convex-hull algorithms

Binary Tree Algorithms Binary tree is a divide-and-conquer ready structure! Ex. 1: Classic traversals (preorder, inorder, postorder) Algorithm Inorder(T) if T a a Inorder(T left ) b c b c print(root of T) d e d e Inorder(T right ) external node: Efficiency: Θ(n) # of : x = n+1

Binary Tree Algorithms (cont.) Ex. 2: Computing the height of a binary tree T L T R

Binary Tree Algorithms (cont.) Ex. 2: Computing the height of a binary tree T L T R h(t) = max{h(t L ), h(t R )} + 1 if T and h( ) = 0 Efficiency: Θ(n)

Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary search (degenerated) Binary tree traversals Multiplication of large integers Matrix multiplication: Strassen s algorithm Closest-pair and convex-hull algorithms

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

Multiplication of Large Integers Consider the problem of multiplying two (large) n-digit integers represented by arrays of their digits such as: A = 12345678901357986429 B = 87654321284820912836 The grade-school algorithm: a 1 a 2 a n b 1 b 2 b n (d 10 ) d 11 d 12 d 1n (d 20 ) d 21 d 22 d 2n (d n0 ) d n1 d n2 d nn Efficiency: n 2 one-digit multiplications

Multiplication of Large Integers Consider the problem of multiplying two (large) n-digit integers represented by arrays of their digits such as: A = 12345678901357986429 B = 87654321284820912836 Discussion: How to apply divide-and-conquer to this problem?

First Cut 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 Recurrence for the number of one-digit multiplications T(n): T(n) = 4T(n/2), T(1) = 1 Solution: T(n) = n 2

Second Cut A B = A 1 B 1 10 n + (A 1 B 2 + A 2 B 1 ) 10 n/2 + A 2 B 2 The idea is to decrease the number of multiplications from 4 to 3: (A 1 + A 2 ) (B 1 + B 2 ) = A 1 B 1 + (A 1 B 2 + A 2 B 1 ) + A 2 B 2, i.e., (A 1 B 2 + A 2 B 1 ) = (A 1 + A 2 ) (B 1 + B 2 ) - A 1 B 1 - A 2 B 2, which requires only 3 multiplications at the expense of (4-1) extra add/sub. Recurrence for the number of multiplications T(n): T(n) = 3T(n/2), T(1) = 1 Solution: T(n) = 3 log 2n = n log 23 n 1.585

Questions: Large-Integer Multiplication What if two large numbers have different number of digits? What if n is an odd number?

Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary search (degenerated) Binary tree traversals Multiplication of large integers Matrix multiplication: Strassen s algorithm Closest-pair and convex-hull algorithms

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 = M 1 + M 4 - M 5 + M 7 M 3 + M 5 M 2 + M 4 M 1 + M 3 - M 2 + M 6 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 ) 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 )

Analysis of Strassen s Algorithm If n is not a power of 2, matrices can be padded with zeros. Number of multiplications: T(n) = 7T(n/2), T(1) = 1 Solution: T(n) = 7 log 2n = n log 27 n 2.807 brute-force alg. vs. n 3 of Algorithms with better asymptotic efficiency are known but they are even more complex.

Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary search (degenerated) Binary tree traversals Multiplication of large integers Matrix multiplication: Strassen s algorithm Closest-pair and convex-hull algorithms

Closest-Pair Problem S is a set of n points P i =(x i, y i ) in the plane For simplicity, n is a power of two Without loss of generality, we assume points are ordered by their x coordinates Discussion: How to apply divide-and-conquer?

Closest-Pair Problem by Divide-and-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.

Closest Pair by Divide-and-Conquer (cont.) 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 )!

Closest Pair by Divide-and-Conquer: Worst Case The worst case scenario is depicted below:

Efficiency of the Closest-Pair Algorithm Running time of the algorithm is described by T(n) = 2T(n/2) + f(n), where f(n) O(n) By the Master Theorem (with a = 2, b = 2, d = 1) T(n) O(n log n)