Copyright 2000, Kevin Wayne 1

Similar documents
! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution.

Divide-and-Conquer. Consequence. Brute force: n 2. Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici.

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

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

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

5. DIVIDE AND CONQUER I

5. DIVIDE AND CONQUER I

Matrix Multiplication

Algorithm Design and Analysis

Divide-Conquer-Glue Algorithms

Chapter 5. Divide and Conquer. CS 350 Winter 2018

CSE 421 Algorithms: Divide and Conquer

5. DIVIDE AND CONQUER I

How to Multiply. 5.5 Integer Multiplication. Complex Multiplication. Integer Arithmetic. Complex multiplication. (a + bi) (c + di) = x + yi.

Divide-and-conquer. Curs 2015

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Copyright 2000, Kevin Wayne 1

CPS 616 DIVIDE-AND-CONQUER 6-1

CS 580: Algorithm Design and Analysis

DIVIDE AND CONQUER II

Divide and Conquer. Recurrence Relations

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Linear Time Selection

Design and Analysis of Algorithms

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

1. Basic Algorithms: Bubble Sort

CS483 Design and Analysis of Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

1. Basic Algorithms: Bubble Sort

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CSC373: Algorithm Design, Analysis and Complexity Fall 2017

Divide and Conquer Algorithms

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

Matching Residents to Hospitals

Divide and conquer. Philip II of Macedon

The Divide-and-Conquer Design Paradigm

Introduction to Algorithms 6.046J/18.401J/SMA5503

Reductions, Recursion and Divide and Conquer

Objec&ves. Review. Divide and conquer algorithms

Divide and Conquer Strategy

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Kartsuba s Algorithm and Linear Time Selection

COMP 355 Advanced Algorithms

2. ALGORITHM ANALYSIS

Divide and Conquer Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

CSE 202 Dynamic Programming II

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Asymptotic Analysis and Recurrences

CS483 Design and Analysis of Algorithms

Chapter 4 Divide-and-Conquer

Divide-and-Conquer. a technique for designing algorithms

CSE 421 Algorithms. T(n) = at(n/b) + n c. Closest Pair Problem. Divide and Conquer Algorithms. What you really need to know about recurrences

COMP 382: Reasoning about algorithms

Divide and Conquer Algorithms

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Lecture 2: Divide and conquer and Dynamic programming

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

Divide and Conquer. Andreas Klappenecker

2.1 Computational Tractability. Chapter 2. Basics of Algorithm Analysis. Computational Tractability. Polynomial-Time

CS 580: Algorithm Design and Analysis

Algorithms. Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity divide-and-conquer ROBERT SEDGEWICK KEVIN WAYNE

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

Algorithms And Programming I. Lecture 5 Quicksort

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Data Structures and Algorithms Chapter 3

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

6. DYNAMIC PROGRAMMING I

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

1 Divide and Conquer (September 3)

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Algorithms Design & Analysis. Analysis of Algorithm

The PRIMES 2014 problem set

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

Fall 2017 November 10, Written Homework 5

Discrete Mathematics U. Waterloo ECE 103, Spring 2010 Ashwin Nayak May 17, 2010 Recursion

Divide and Conquer Problem Solving Method

Design and Analysis of Algorithms

Chapter 5 Divide and Conquer

Introduction to Divide and Conquer

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Week 5: Quicksort, Lower bound, Greedy

Lecture 4. Quicksort

shelat divide&conquer closest points matrixmult median

Selection and Adversary Arguments. COMP 215 Lecture 19

Design and Analysis of Algorithms

Quicksort algorithm Average case analysis

Transcription:

Divide-and-Conquer Chapter 5 Divide and Conquer Divide-and-conquer. Break up problem into several parts. Solve each part recursively. Combine solutions to sub-problems into overall solution. Most common usage. Break up problem of size n into two equal parts of size ½n. Solve two parts recursively. Combine two solutions into overall solution in linear time. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Consequence. Brute force: n 2. Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici. - Julius Caesar 2 3 Sorting 5.1 Mergesort Sorting. Given n elements, rearrange in ascending order. Obvious sorting applications. ist files in a directory. Organize a playlist. ist names in address book. Display Google PageRank results. Problems become easier once sorted. Find the median. Greedy algorithms. Find the closest pair. Binary search in a database. Identify statistical outliers. Find duplicates in a mailing list. Non-obvious sorting applications. Data compression. Computer graphics. Interval scheduling. Computational biology. Minimum spanning tree. Supply chain management. Simulate a system of particles. Book recommendations on Amazon. oad balancing on a parallel computer.... 5 Copyright 2000, Kevin Wayne 1

Mergesort Merging Mergesort. Divide array into two halves. Recursively sort each half. Merge two halves to make sorted whole. Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? inear number of comparisons. Use temporary array. Jon von Neumann (1945) A G O R I T H M S A G O R H I M S T A G O R I T H M S divide O(1) A G H I A G O R H I M S T sort 2T(n/2) A G H I M O R S T merge O(n) Challenge for the bored. In-place merge. [Kronrud, 1969] using only a constant amount of extra storage 7 8 A Useful Recurrence Relation Proof by Telescoping Def. T(n) = number of comparisons to mergesort an input of size n. Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. Mergesort recurrence. 0 if n =1 ) T(n) ( T( n/2 ) + T( % n/2& ) +!#" #!#" # % n otherwise ) * solve left half solve right half merging Solution. T(n) = O(n log 2 n). Pf. For n > 1: " 0 if n =1 T(n) = # 2T(n/2) + n!# "# % otherwise % sorting both halves merging T(n) n = = =! = 2T(n /2) n T(n /2) n/2 T(n / 4) n/4 T(n /n) n/n = log 2 n + 1 + 1 + 1 + 1 + 1 +! + 1 " # % log 2 n assumes n is a power of 2 9 Copyright 2000, Kevin Wayne 2

Proof by Induction Analysis of Mergesort Recurrence Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. Claim. If T(n) satisfies the following recurrence, then T(n) n élg nù. " 0 if n =1 T(n) = # 2T(n/2) + n!# "# % otherwise % sorting both halves merging assumes n is a power of 2 T(n) 0 if n =1 ) ( T( n /2 ) + T( % n /2& ) +!#" #!#" # % n otherwise ) * solve left half solve right half merging log 2n Pf. (by induction on n) Base case: n = 1. Inductive hypothesis: T(n) = n log 2 n. Goal: show that T(2n) = 2n log 2 (2n). T(2n) = 2T(n) + 2n = 2nlog 2 n + 2n = 2n( log 2 (2n) 1) + 2n = 2nlog 2 (2n) Pf. (by induction on n) Base case: n = 1. Define n 1 = ën / 2û, n 2 = én / 2ù. Induction step: assume true for 1, 2,..., n 1. T(n) T(n 1 ) + T(n 2 ) + n n 1 # lgn 1 + n 2 # lg n 2 + n n 1 # lgn 2 + n 2 # lg n 2 + n = n # lgn 2 + n n( # lgn 1 ) + n = n# lgn n 2 = " n /2# " 2 " lg n # / 2 # = 2 " lg n # / 2 lgn 2 " lg n# 1 13 14 Counting Inversions 5.3 Counting Inversions Music site tries to match your song preferences with others. You rank n songs. Music site consults database to find people with similar tastes. Similarity metric: number of inversions between two rankings. My rank: 1, 2,, n. Your rank: a 1, a 2,, a n. Songs i and j inverted if i < j, but a i > a j. Me You Songs A B C D E 1 2 3 4 5 1 3 4 2 5 Inversions 3-2, 4-2 Brute force: check all Q(n 2 ) pairs i and j. 16 Copyright 2000, Kevin Wayne 3

Applications Counting Inversions: Divide-and-Conquer Applications. Voting theory. Collaborative filtering. Measuring the "sortedness" of an array. Genomic distance between two gene sequences. Sensitivity analysis of Googles ranking function. Rank aggregation for meta-searching on the Web. Nonparametric statistics (e.g., Kendalls Tau distance). Divide-and-conquer. 17 18 Counting Inversions: Divide-and-Conquer Counting Inversions: Divide-and-Conquer Divide-and-conquer. Divide: separate list into two pieces. Divide-and-conquer. Divide: separate list into two pieces. Conquer: recursively count inversions in each half. Divide: O(1). Divide: O(1). Conquer: 2T(n / 2) 5 blue-blue inversions 8 green-green inversions 5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, -3, -7, -11, 11-3, 11-7 19 20 Copyright 2000, Kevin Wayne 4

Counting Inversions: Divide-and-Conquer Counting Inversions: Combine Divide-and-conquer. Divide: separate list into two pieces. Conquer: recursively count inversions in each half. Combine: count inversions where a i and a j are in different halves, and return sum of three quantities. Combine: count blue-green inversions Assume each half is sorted. Count inversions where a i and a j are in different halves. Merge two sorted halves into sorted whole. to maintain sorted invariant Divide: O(1). 3 7 10 14 18 19 2 11 16 17 23 25 6 3 2 2 0 0 Conquer: 2T(n / 2) 13 blue-green inversions: 6 + 3 + 2 + 2 + 0 + 0 Count: O(n) 5 blue-blue inversions 8 green-green inversions 9 blue-green inversions 5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7 Combine:??? 2 3 7 10 11 14 16 17 18 19 23 25 Merge: O(n) Total = 5 + 8 + 9 = 22. T(n) T( # n/2 ) + T( % n/2& ) + O(n) T(n) = O(nlog n) 22 Counting Inversions: Implementation Pre-condition. [Merge-and-Count] A and B are sorted. Post-condition. [Sort-and-Count] is sorted. 5.4 Sort-and-Count() { if list has one element return 0 and the list Divide the list into two halves A and B (r A, A) Sort-and-Count(A) (r B, B) Sort-and-Count(B) (r B, ) Merge-and-Count(A, B) } return r = r A + r B + r and the sorted list 23 Copyright 2000, Kevin Wayne 5

: First Attempt Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them. Divide. Sub-divide region into 4 quadrants. Fundamental geometric primitive. Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. Special case of nearest neighbor, Euclidean MST, Voronoi. fast closest pair inspired fast algorithms for these problems Brute force. Check all pairs of points p and q with Q(n 2 ) comparisons. 1-D version. O(n log n) easy if points are on a line. Assumption. No two points have same x coordinate. to make presentation cleaner 25 26 : First Attempt Divide. Sub-divide region into 4 quadrants. Obstacle. Impossible to ensure n/4 points in each piece. Algorithm. Divide: draw vertical line so that roughly ½n points on each side. 27 28 Copyright 2000, Kevin Wayne 6

Algorithm. Divide: draw vertical line so that roughly ½n points on each side. Conquer: find closest pair in each side recursively. Algorithm. Divide: draw vertical line 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. seems like Q(n 2 ) 8 29 30 Find closest pair with one point in each side, assuming that distance < d. Find closest pair with one point in each side, assuming that distance < d. Observation: only need to consider points within d of line. d = min(, ) d = min(, ) 31 d 32 Copyright 2000, Kevin Wayne 7

Find closest pair with one point in each side, assuming that distance < d. Observation: only need to consider points within d of line. Sort points in 2d-strip by their y coordinate. Find closest pair with one point in each side, assuming that distance < d. Observation: only need to consider points within d of line. Sort points in 2d-strip by their y coordinate. Only check distances of those within 11 positions in sorted list! 7 7 6 6 4 5 4 5 3 d = min(, ) 3 d = min(, ) 2 2 1 1 d 33 d 34 Closest Pair Algorithm Def. et s i be the point in the 2d-strip, with the i th smallest y-coordinate. Claim. If i j ³, then the distance between s i and s j is at least d. Pf. No two points lie in same ½d-by-½d box. Two points at least 2 rows apart have distance ³ 2(½d). 2 rows 31 29 39 30 j ½d ½d Closest-Pair(p 1,, p n ) { Compute separation line such that half the points are on one side and half on the other side. d 1 = Closest-Pair(left half) d 2 = Closest-Pair(right half) d = min(d 1, d 2 ) Delete all points further than d from separation line Sort remaining points by y-coordinate. O(n log n) 2T(n / 2) O(n) O(n log n) Fact. Still true if we replace with 7. i 27 28 ½d Scan points in y-order and compare distance between each point and next 11 neighbors. If any of these distances is less than d, update d. O(n) 26 25 } return d. d d 35 36 Copyright 2000, Kevin Wayne 8

: Analysis Running time. 5.5 Integer Multiplication T(n) 2T( n/2) + O(n log n) T(n) = O(n log 2 n) Q. Can we achieve O(n log n)? A. Yes. Dont sort points in strip from scratch each time. Each recursive returns two lists: all points sorted by y coordinate, and all points sorted by x coordinate. Sort by merging two pre-sorted lists. T(n) 2T( n/2) + O(n) T(n) = O(n log n) 37 Integer Arithmetic Divide-and-Conquer Multiplication: Warmup Add. Given two n-digit integers a and b, compute a + b. O(n) bit operations. Multiply. Given two n-digit integers a and b, compute a b. Brute force solution: Q(n 2 ) bit operations. * 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 Multiply 1 1 1 1 1 1 0 1 1 1 0 1 + 0 1 1 1 1 0 1 0 1 Add 0 1 0 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 To multiply two n-digit integers: Multiply four ½n-digit integers. Add two ½n-digit integers, and shift to obtain result. x = 2 n /2 x 1 + x 0 y = 2 n /2 y 1 + y 0 ( ) ( 2 n /2 y 1 + y 0 ) = 2 n x 1 y 1 + 2 n /2 x 1 y 0 + x 0 y 1 xy = 2 n /2 x 1 + x 0 T(n) = 4T( n/2 )!# "# +! Θ(n) " T(n) = Θ(n2 ) recursive calls add, shift assumes n is a power of 2 ( ) + x 0 y 0 39 40 Copyright 2000, Kevin Wayne 9

Karatsuba Multiplication Karatsuba: Recursion Tree To multiply two n-digit integers: Add two ½n digit integers. Multiply three ½n-digit integers. Add, subtract, and shift ½n-digit integers to obtain result. " 0 if n =1 T(n) = # 3T(n/2) + n otherwise T(n) log 2 n ( ) k T(n) = n 2 3 = k=0 ( ) 1+log 2 n 1 3 2 3 1 = 3n log2 3 2 2 n x = 2 n /2 x 1 + x 0 y = 2 n /2 y 1 + y 0 xy = 2 n x 1 y 1 + 2 n /2 ( x 1 y 0 + x 0 y 1 ) + x 0 y 0 T(n/2) T(n/2) T(n/2) 3(n/2) = 2 n x 1 y 1 + 2 n /2 ( (x 1 + x 0 )(y 1 + y 0 ) x 1 y 1 x 0 y 0 ) + x 0 y 0 A B A C C Theorem. [Karatsuba-Ofman, 1962] Can multiply two n-digit integers in O(n 1.585 ) bit operations.... 9(n/4)... ( ) + T( n /2 ) + T( 1+ n /2 ) T(n) T n /2 % & % &!####### "####### recursive calls + Θ(n)!# "# add, subtract, shift T(n / 2 k )... 3 k (n / 2 k )... T(n) = O(n log 2 3 ) = O(n 1.585 ) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) 3 lg n (2) 41 42 Matrix Multiplication Matrix Multiplication Matrix multiplication. Given two n-by-n matrices A and B, compute C = AB. n c ij = a ik b kj k=1 " c c! c % 11 1n c c 22! c 2n " " # " # c n1 c n2! c nn & = " a a! a % 11 1n a a 22! a 2n " " # " # a n1 a n2! a nn & " b b! b % 11 1n b b 22! b 2n " " # " # b n1 b n2! b nn & Brute force. Q(n 3 ) arithmetic operations. Fundamental question. Can we improve upon brute force? 44 Copyright 2000, Kevin Wayne 10

Matrix Multiplication: Warmup Matrix Multiplication: Key Idea Divide-and-conquer. Divide: partition A and B into ½n-by-½n blocks. Conquer: multiply 8 ½n-by-½n recursively. Combine: add appropriate products using 4 matrix additions. Key idea. multiply 2-by-2 block matrices with only 7 multiplications. " C 11 C % " A = 11 A % " B 11 B % P 1 = A 11 (B B 22 ) # C C 22 & # A A 22 & # B B 22 & P 2 = ( A 11 + A ) B 22 " C 11 C % " = A 11 A % " B 11 B % # C C 22 & # A A 22 & # B B 22 & C 11 = ( A 11 B 11 ) + ( A B ) C = ( A 11 B ) + ( A B 22 ) C = ( A B 11 ) + ( A 22 B ) C 22 = ( A B ) + ( A 22 B 22 ) C 11 = P 5 + P 4 P 2 + P 6 C = P 1 + P 2 C = P 3 + P 4 C 22 = P 5 + P 1 P 3 P 7 P 3 = ( A + A 22 ) B 11 P 4 = A 22 (B B 11 ) P 5 = ( A 11 + A 22 ) (B 11 + B 22 ) P 6 = ( A A 22 ) (B + B 22 ) P 7 = ( A 11 A ) (B 11 + B ) T(n) = 8T( n/2 )!# "# + Θ(n2 )!##" ## recursive calls add, form submatrices T(n) = Θ(n 3 ) 7 multiplications. 18 = 10 + 8 additions (or subtractions). 45 46 Fast Matrix Multiplication Fast Matrix Multiplication in Practice Fast matrix multiplication. (Strassen, 1969) Divide: partition A and B into ½n-by-½n blocks. Compute: 14 ½n-by-½n matrices via 10 matrix additions. Conquer: multiply 7 ½n-by-½n matrices recursively. Combine: 7 products into 4 terms using 8 matrix additions. Analysis. Assume n is a power of 2. T(n) = # arithmetic operations. T(n) = 7T( n/2 )!# "# + Θ(n2 ) T(n) = Θ(n log 2 7 ) = O(n 2.81 )!#" # recursive calls add, subtract Implementation issues. Sparsity. Caching effects. Numerical stability. Odd matrix dimensions. Crossover to classical algorithm around n = 8. Common misperception: "Strassen is only a theoretical curiosity." Advanced Computation Group at Apple Computer reports 8x speedup on G4 Velocity Engine when n ~ 2,500. Range of instances where its useful is a subject of controversy. Remark. Can "Strassenize" Ax=b, determinant, eigenvalues, and other matrix ops. 47 48 Copyright 2000, Kevin Wayne 11

Fast Matrix Multiplication in Theory Fast Matrix Multiplication in Theory Q. Multiply two 2-by-2 matrices with only 7 scalar multiplications? A. Yes! [Strassen, 1969] Θ(n log 2 7 ) = O(n 2.81 ) Q. Multiply two 2-by-2 matrices with only 6 scalar multiplications? A. Impossible. [Hopcroft and Kerr, 1971] Θ(n log 2 6 ) = O(n 2.59 ) Q. Two 3-by-3 matrices with only scalar multiplications? A. Also impossible. Θ(n log 3 ) = O(n 2.77 ) Best known. O(n 2.3728639 ) [Francois e Gall, 2014] Conjecture. O(n 2+e ) for any e > 0. Caveat. Theoretical improvements to Strassen are progressively less practical. The constant coefficient hidden by the Big O notation is so large that these algorithms are only worthwhile for matrices that are too large to handle on present-day computers. Q. Two 70-by-70 matrices with only 143,640 scalar multiplications? A. Yes! [Pan, 1980] Θ(n log 70 143640 ) = O(n 2.80 ) Decimal wars. December, 1979: O(n 2.5813 ). January, 1980: O(n 2.5801 ). 49 50 Copyright 2000, Kevin Wayne