DIVIDE AND CONQUER. 2 Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer.

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

Divide & Conquer n Divide into two halves n Recursively sort the two n Merge the two sorted lists 1 ALGORITHMS Merge Sort.
1 auxiliary array smallest AGLORHIMST Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary.
AGLOR HIMST Merging Merge. n Keep track of smallest element in each sorted half. n Choose smaller of two elements. n Repeat until done. A.
Divide-and-Conquer The most-well known algorithm design strategy:
Dr. Deshi Ye Divide-and-conquer Dr. Deshi Ye
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
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
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 11.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
1 Divide-and-Conquer Divide-and-conquer. n Break up problem into several parts. n Solve each part recursively. n Combine solutions to sub-problems into.
Theory of Algorithms: Divide and Conquer
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
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.
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.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
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.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
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.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 12.
Divide-and-Conquer 7 2  9 4   2   4   7
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
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.
Project 2 due … Project 2 due … Project 2 Project 2.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURES Divide.
Dale Roberts Mergesort Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
File Organization and Processing Week 13 Divide and Conquer.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
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.
1 Chapter 4-2 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 How to Multiply Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. integers, matrices, and polynomials.
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.
CSCI 256 Data Structures and Algorithm Analysis Lecture 10 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 16.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Dale Roberts Mergesort Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Chapter 2 Divide-and-Conquer algorithms
Chapter 5 Divide and Conquer
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
分治法.
Chapter 2 Divide-and-Conquer algorithms
Chapter 4 Divide-and-Conquer
Chapter 2 Divide-and-Conquer algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
CSCE 411 Design and Analysis of Algorithms
Chapter 5 Divide and Conquer
Topic: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Divide-and-Conquer The most-well known algorithm design strategy:
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSCI 256 Data Structures and Algorithm Analysis Lecture 12
Divide-and-Conquer 7 2  9 4   2   4   7
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

DIVIDE AND CONQUER

2 Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer. Break up a problem into two sub-problems, solve each sub-problem independently, and combine solution to sub-problems to form solution to original problem. Dynamic programming. Break up a problem into a series of overlapping sub-problems, and build up solutions to larger and larger sub-problems.

3 Divide and Conquer Divide and conquer. n Break up problem into several parts. n Solve each part recursively. n Combine solutions to sub-problems into overall solution. Most common usage. n Break up problem of size n into two equal parts of size ½n. n Solve two parts recursively. n Combine two solutions into overall solution in linear time. Consequence. n Brute force: n 2. n Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici. - Julius Caesar

Mergesort

5 Obvious sorting applications. List files in a directory. Organize an MP3 library. List names in a phone book. Display Google PageRank results. Problems become easier once sorted. Find the median. 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. Load balancing on a parallel computer.... Sorting Sorting. Given n elements, rearrange in ascending order.

6 Mergesort Mergesort. n Divide array into two halves. n Recursively sort each half. n Merge two halves to make sorted whole. merge sort divide ALGORITHMS ALGORITHMS AGLORHIMST AGHILMORST Jon von Neumann (1945) O(n) 2T(n/2) O(1)

7 Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? n Linear number of comparisons. n Use temporary array. Challenge for the bored. In-place merge. [Kronrud, 1969] AGLORHIMST AGHI using only a constant amount of extra storage

8 auxiliary array smallest AGLORHIMST Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. A

9 auxiliary array smallest AGLORHIMST A Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. G

10 auxiliary array smallest AGLORHIMST AG Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. H

11 auxiliary array smallest AGLORHIMST AGH Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. I

12 auxiliary array smallest AGLORHIMST AGHI Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. L

13 auxiliary array smallest AGLORHIMST AGHIL Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. M

14 auxiliary array smallest AGLORHIMST AGHILM Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. O

15 auxiliary array smallest AGLORHIMST AGHILMO Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. R

16 auxiliary array first half exhausted smallest AGLORHIMST AGHILMOR Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. S

17 auxiliary array first half exhausted smallest AGLORHIMST AGHILMORS Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. T

18 auxiliary array first half exhausted second half exhausted AGLORHIMST AGHILMORST Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done.

19 A Useful Recurrence Relation Def. T(n) = number of comparisons to mergesort an input of size n. Mergesort recurrence. Solution. T(n) = O(n log 2 n). Assorted proofs. We describe several ways to prove this recurrence. Initially we assume n is a power of 2 and replace  with =.

20 Proof by Recursion Tree T(n) T(n/2) T(n/4) T(2) n T(n / 2 k ) 2(n/2) 4(n/4) 2 k (n / 2 k ) n/2 (2)... log 2 n n log 2 n

21 Proof by Telescoping Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. Pf. For n > 1: assumes n is a power of 2

22 Proof by Induction Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. Pf. (by induction on n) n Base case: n = 1. n Inductive hypothesis: T(n) = n log 2 n. n Goal: show that T(2n) = 2n log 2 (2n). assumes n is a power of 2

23 Analysis of Mergesort Recurrence Claim. If T(n) satisfies the following recurrence, then T(n)  n  lg n . Pf. (by induction on n) n Base case: n = 1. n Define n 1 =  n / 2 , n 2 =  n / 2 . n Induction step: assume true for 1, 2,..., n–1. log 2 n

Integer Multiplication

25 Integer Arithmetic Add. Given two n-digit integers a and b, compute a + b. n O(n) bit operations. Multiply. Given two n-digit integers a and b, compute a × b. n Brute force solution:  (n 2 ) bit operations Add Multiply

26 To multiply two n-digit integers: n Multiply four ½n-digit integers. n Add two ½n-digit integers, and shift to obtain result. Divide-and-Conquer Multiplication: Warmup assumes n is a power of 2

27 To multiply two n-digit integers: n Add two ½n digit integers. n Multiply three ½n-digit integers. n Add, subtract, and shift ½n-digit integers to obtain result. Theorem. [Karatsuba-Ofman, 1962] Can multiply two n-digit integers in O(n ) bit operations. Karatsuba Multiplication AB C A C

28 Karatsuba: Recursion Tree n 3(n/2) 9(n/4) 3 k (n / 2 k ) 3 lg n (2)... T(n) T(n/2) T(n/4) T(2) T(n / 2 k ) T(n/4) T(n/2) T(n/4) T(n/2) T(n/4)...

Matrix Multiplication

30 Matrix multiplication. Given two n-by-n matrices A and B, compute C = AB. Brute force.  (n 3 ) arithmetic operations. Fundamental question. Can we improve upon brute force? Matrix Multiplication

31 Matrix Multiplication: Warmup Divide-and-conquer. n Divide: partition A and B into ½n-by-½n blocks. n Conquer: multiply 8 ½n-by-½n recursively. n Combine: add appropriate products using 4 matrix additions.

32 Matrix Multiplication: Key Idea Key idea. multiply 2-by-2 block matrices with only 7 multiplications. n 7 multiplications. n 18 = additions (or subtractions).

33 Fast Matrix Multiplication Fast matrix multiplication. (Strassen, 1969) n Divide: partition A and B into ½n-by-½n blocks. n Compute: 14 ½n-by-½n matrices via 10 matrix additions. n Conquer: multiply 7 ½n-by-½n matrices recursively. n Combine: 7 products into 4 terms using 8 matrix additions. Analysis. n Assume n is a power of 2. n T(n) = # arithmetic operations.

34 Fast Matrix Multiplication in Practice Implementation issues. n Sparsity. n Caching effects. n Numerical stability. n Odd matrix dimensions. n Crossover to classical algorithm around n = 128. Common misperception: "Strassen is only a theoretical curiosity." n Advanced Computation Group at Apple Computer reports 8x speedup on G4 Velocity Engine when n ~ 2,500. n Range of instances where it's useful is a subject of controversy. Remark. Can "Strassenize" Ax=b, determinant, eigenvalues, and other matrix ops.