CS473 - Algorithms I

Similar documents
Algorithm Design and Analysis

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

Data Structures and Algorithms CSE 465

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

Divide-and-Conquer. Dr. Yingwu Zhu

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

PRAM Divide and Conquer Algorithms

Data Structures and Algorithms CMPSC 465

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

We can use a max-heap to sort data.

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

Divide and Conquer Algorithms

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

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

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

Lecture 4 CS781 February 3, 2011

Multithreaded Programming in. Cilk LECTURE 2. Charles E. Leiserson

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

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

Divide and Conquer 4-0

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

EECS 2011M: Fundamentals of Data Structures

CS Divide and Conquer

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

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

Recurrences and Divide-and-Conquer

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

CS 171: Introduction to Computer Science II. Quicksort

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

Chapter 2 Divid d e-an -Conquer 1

UNIT-2 DIVIDE & CONQUER

Lecture 5: Sorting Part A

Unit-2 Divide and conquer 2016

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

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

Divide and Conquer. Algorithm Fall Semester

n = 1 What problems are interesting when n is just 1?

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Cilk, Matrix Multiplication, and Sorting

Data Structures and Algorithms Week 4

CS Divide and Conquer

7.3 Divide-and-Conquer Algorithm and Recurrence Relations

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

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

CS 303 Design and Analysis of Algorithms

I/O Model. Cache-Oblivious Algorithms : Algorithms in the Real World. Advantages of Cache-Oblivious Algorithms 4/9/13

Divide & Conquer Design Technique

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

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort

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

Lecture 2: Getting Started

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Test 1 Review Questions with Solutions

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

MergeSort. Algorithm : Design & Analysis [5]

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Parallel Algorithms CSE /22/2015. Outline of this lecture: 1 Implementation of cilk for. 2. Parallel Matrix Multiplication

CSCE 411 Design and Analysis of Algorithms

5. DIVIDE AND CONQUER I

Data Structures and Algorithms Chapter 4

Lecture 8: Mergesort / Quicksort Steven Skiena

Algorithms - Ch2 Sorting

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

COMP 3403 Algorithm Analysis Part 2 Chapters 4 5. Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

Lecture 9: Sorting Algorithms

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Lecture 13: Divide and Conquer (1997) Steven Skiena. skiena

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

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

Multithreaded Algorithms Part 2. Dept. of Computer Science & Eng University of Moratuwa

CS483 Analysis of Algorithms Lecture 03 Divide-n-Conquer

Lecture 7: Divide & Conquer 2. Integer Multiplication. & Matrix Multiplication. CS 341: Algorithms. Tuesday, Jan 29 th 2019

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.

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

Analyzing Complexity of Lists

CISC 621 Algorithms, Midterm exam March 24, 2016

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

Outline. CS38 Introduction to Algorithms. Fast Fourier Transform (FFT) Fast Fourier Transform (FFT) Fast Fourier Transform (FFT)

CS 140 : Non-numerical Examples with Cilk++

Back to Sorting More efficient sorting algorithms

CSE 421 Algorithms: Divide and Conquer

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

Lecture 6: Divide-and-Conquer

Lecture 2: Divide and Conquer

Data Structures and Algorithms. Roberto Sebastiani

CS141: Intermediate Data Structures and Algorithms Dynamic Programming

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

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

Parallel Algorithms. 6/12/2012 6:42 PM gdeepak.com 1

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Lecture 3. Recurrences / Heapsort

Ch5. Divide-and-Conquer

Transcription:

CS473 - Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm View in slide-show mode 1

Reminder: Merge Sort Input array A sort this half sort this half Divide Conquer merge two sorted halves Combine 2

The Divide-and-Conquer Design Paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer thesubproblems by solving them recursively. 3. Combine subproblem solutions. CS473 Lecture 4 3

Example: Merge Sort 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear- time merge. T(n) = 2 T(n/2) + Θ(n) # subproblems subproblem size work dividing and combining CS473 Lecture 4 4

Master Theorem: Reminder T(n) = at(n/b) + f(n) Case 1: Case 2: n log b a f (n) = Ω(nε ) T(n) = Θ(n log b a ) f (n) n log b a = Θ(lgk n) T(n) = Θ(n log b a lg k+1 n) Case 3: n log b a f (n) = Ω(nε ) and a f (n/b) c f (n) for c < 1 T(n) = Θ( f (n)) 5

Merge Sort: Solving the Recurrence T(n) = 2 T(n/2) + Θ(n) a = 2, b = 2, f(n) = Θ(n), n log ba = n Case 2: f (n) n log b a = Θ(lgk n) T(n) = Θ(n log b a lg k+1 n) holds for k = 0 T(n) = Θ (nlgn) 6

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 7

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 8

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 9

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 10

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 11

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 12

Recurrence for Binary Search T(n) = 1 T(n/2) + Ө(1) # subproblems subproblem size work dividing and combining CS473 Lecture 4 13

Binary Search: Solving the Recurrence T(n) = T(n/2) + Θ(1) a = 1, b = 2, f(n) = Θ(1), n log ba = n0 = 1 Case 2: f (n) n log b a = Θ(lgk n) T(n) = Θ(n log b a lg k+1 n) holds for k = 0 T(n) = Θ (lgn) 14

Powering a Number Problem: Compute a n, where n is a natural number Naive-Power (a, n) powerval 1 for i 1 to n powerval powerval. a return powerval What is the complexity? T(n) = Θ (n) 15

Powering a Number: Divide & Conquer Basic idea: a n = a n/2. a n/2 even a (n-1)/2. a (n-1)/2. a if n is if n is odd 16

Powering a Number: Divide & Conquer POWER (a, n) if n = 0 then return 1 else if n is even then val POWER (a, n/2) return val * val else if n is odd then val POWER (a, (n-1)/2) return val * val * a 17

Powering a Number: Solving the Recurrence T(n) = T(n/2) + Θ(1) a = 1, b = 2, f(n) = Θ(1), n log ba = n0 = 1 Case 2: f (n) n log b a = Θ(lgk n) T(n) = Θ(n log b a lg k+1 n) holds for k = 0 T(n) = Θ (lgn) 18

Matrix Multiplication Input : A = [a ij ], B = [b ij ]. Output: C = [c ij ] = A. B. i, j = 1, 2,..., n. c 11 c 12... c 1n a 11 a 12... a 1n b 11 b 12... b 1n c 21 c 22... c 2n = a 21 a 22... a 2n. b 21 b 22... b 2n............... c n1 c n2... c nn a n1 a n2... a nn b n1 b n2... b nn............ c ij = 1 k n a ik.b kj CS473 Lecture 4 19

Standard Algorithm for i 1 to n do for j 1 to n do c ij 0 for k 1 to n do c ij c ij + a ik. b kj Running time = Θ(n 3 ) CS473 Lecture 4 20

Matrix Multiplication: Divide & Conquer IDEA: Divide the n x n matrix into 2x2 matrix of (n/2)x(n/2) submatrices C A B c 11 =. c 12 a 11 a 12 b 11 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 11 = a 11 b 11 + a 12 b 21 21

Matrix Multiplication: Divide & Conquer IDEA: Divide the n x n matrix into 2x2 matrix of (n/2)x(n/2) submatrices C A B c 11 b 11 =. c 12 a 11 a 12 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 12 = a 11 b 12 + a 12 b 22 22

Matrix Multiplication: Divide & Conquer IDEA: Divide the n x n matrix into 2x2 matrix of (n/2)x(n/2) submatrices C A B c 11 a 11 a 12 =. c 12 b 11 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 21 = a 21 b 11 + a 22 b 21 23

Matrix Multiplication: Divide & Conquer IDEA: Divide the n x n matrix into 2x2 matrix of (n/2)x(n/2) submatrices C A B c 11 c 12 a 11 b 11 =. a 12 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 22 = a 21 b 12 + a 22 b 22 24

Matrix Multiplication: Divide & Conquer C A B c 11 c 12 a 11 a 12 b =. 11 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 11 = a 11 b 11 + a 12 b 21 c 12 = a 11 b 12 + a 12 b 22 c 21 = a 21 b 11 + a 22 b 21 8 mults of (n/2)x(n/2) submatrices 4 adds of (n/2)x(n/2) submatrices c 22 = a 21 b 12 + a 22 b 22 25

Matrix Multiplication: Divide & Conquer MATRIX-MULTIPLY (A, B) // Assuming that both A and B are nxn matrices if n = 1 then returna * B else partition A, B, and C as shown before c 11 = MATRIX-MULTIPLY (a 11, b 11 ) + MATRIX-MULTIPLY (a 12, b 21 ) c 12 = MATRIX-MULTIPLY (a 11, b 12 ) + MATRIX-MULTIPLY (a 12, b 22 ) c 21 = MATRIX-MULTIPLY (a 21, b 11 ) + MATRIX-MULTIPLY (a 22, b 21 ) c 22 = MATRIX-MULTIPLY (a 21, b 12 ) + MATRIX-MULTIPLY (a 22, b 22 ) return C 26

Matrix Multiplication: Divide & Conquer Analysis T(n) = 8 T(n/2) + Θ(n 2 ) 8 recursive calls each subproblem has size n/2 submatrix addition 27

Matrix Multiplication: Solving the Recurrence T(n) = 8 T(n/2) + Θ(n 2 ) a = 8, b = 2, f(n) = Θ(n 2 ), n log ba = n 3 Case 1: n log b a f (n) = Ω(nε ) T(n) = Θ(n log b a ) T(n) = Θ (n 3 ) No better than the ordinary algorithm! 28

Matrix Multiplication: Strassen s Idea C A B c 11 c 12 a 11 a 12 b =. 11 b 12 c 21 c 22 a 21 a 22 b 21 b 22 Compute c 11, c 12, c 21, and c 22 using 7 recursive multiplications 29

Matrix Multiplication: Strassen s Idea P 1 = a 11 x (b 12 - b 22 ) P 2 = (a 11 + a 12 ) x b 22 P 3 = (a 21 + a 22 ) x b 11 P 4 = a 22 x (b 21 - b 11 ) P 5 = (a 11 + a 22 ) x (b 11 + b 22 ) P 6 = (a 12 - a 22 ) x (b 21 + b 22 ) P 7 = ( a 11 - a 21 ) x (b 11 + b 12 ) Reminder: Each submatrix is of size (n/2)x(n/2) Each add/sub operation takes Θ(n 2 ) time Compute P 1..P 7 using 7 recursive calls to matrix-multiply How to compute c ij using P 1.. P 7? 30

Matrix Multiplication: Strassen s Idea P 1 = a 11 x (b 12 - b 22 ) P 2 = (a 11 + a 12 ) x b 22 P 3 = (a 21 + a 22 ) x b 11 P 4 = a 22 x (b 21 - b 11 ) P 5 = (a 11 + a 22 ) x (b 11 + b 22 ) P 6 = (a 12 - a 22 ) x (b 21 + b 22 ) P 7 = ( a 11 - a 21 ) x (b 11 + b 12 ) c 11 = P 5 + P 4 P 2 + P 6 c 12 = P 1 + P 2 c 21 = P 3 + P 4 c 22 = P 5 + P 1 P 3 P 7 7 recursive multiply calls 18 add/sub operations Does not rely on commutativity of multiplication 31

Matrix Multiplication: Strassen s Idea P 1 = a 11 x (b 12 - b 22 ) P 2 = (a 11 + a 12 ) x b 22 P 3 = (a 21 + a 22 ) x b 11 P 4 = a 22 x (b 21 - b 11 ) P 5 = (a 11 + a 22 ) x (b 11 + b 22 ) P 6 = (a 12 - a 22 ) x (b 21 + b 22 ) P 7 = ( a 11 - a 21 ) x (b 11 + b 12 ) e.g. Show that c 12 = P 1 +P 2 c 12 = P 1 + P 2 = a 11 (b 12 b 22 )+(a 11 +a 12 )b 22 = a 11 b 12 -a 11 b 22 +a 11 b 22 +a 12 b 22 = a 11 b 12 +a 12 b 22 32

Strassen s Algorithm 1. Divide: PartitionA and B into (n/2) x (n/2) submatrices. Form terms to be multiplied using + and. 2. Conquer: Perform 7 multiplications of (n/2) x (n/2) submatrices recursively. 3. Combine: Form C using + and on (n/2) x (n/2) submatrices. Recurrence: T(n) = 7 T(n/2) + Ө(n 2 ) 33

Strassen s Algorithm: Solving the Recurrence T(n) = 7 T(n/2) + Θ(n 2 ) a = 7, b = 2, f(n) = Θ(n 2 ), n log ba = n lg7 Case 1: n log b a f (n) = Ω(nε ) T(n) = Θ(n log b a ) T(n) = Θ (n lg7 ) Note: lg7 2.81 34

Strassen s Algorithm The number 2.81 may not seem much smaller than 3 But, it is significant because the difference is in the exponent. Strassen s algorithm beats the ordinary algorithm on today s machines for n 30 or so. Best to date: Θ(n 2.376... ) (of theoretical interest only) 35

VLSI Layout: Binary Tree Embedding Problem: Embed a complete binary tree with n leaves into a 2D grid with minimum area. Example: 36

Binary Tree Embedding Use divide and conquer root LEFT SUBTREE RIGHT SUBTREE 1. Embed the root node 2. Embed the left subtree 3. Embed the right subtree What is the min-area required for n leaves? 37

Binary Tree Embedding root H(n/2) EMBED LEFT SUBTREE HERE EMBED RIGHT SUBTREE HERE H(n) = H(n/2) + 1 W(n/2) W(n/2) W(n) = 2W(n/2) + 1 38

Binary Tree Embedding Solve the recurrences: W(n) = 2W(n/2) + 1 H(n) = H(n/2) + 1 è W(n) = Ө(n) è H(n) = Ө(lgn) Area(n) = Ө(nlgn) 39

Binary Tree Embedding Example: W(n) H(n) 40

Binary Tree Embedding: H-Tree Use a different divide and conquer method root left subtree 1 subtree 2 subtree 3 right subtree 4 1. Embed root, left, right nodes 2. Embed subtree 1 3. Embed subtree 2 4. Embed subtree 3 5. Embed subtree 4 What is the min-area required for n leaves? 41

Binary Tree Embedding: H-Tree H(n/4) H(n/4) SUBTREE 1 SUBTREE 2 SUBTREE 3 SUBTREE 4 W(n) = 2W(n/4) + 1 H(n) = 2H(n/4) + 1 W(n/4) W(n/4) 42

Binary Tree Embedding: H-Tree Solve the recurrences: W(n) = 2W(n/4) + 1 H(n) = 2H(n/4) + 1 è W(n) = Ө( n ) è H(n) = Ө( n ) Area(n) = Ө(n) 43

Binary Tree Embedding: H-Tree Example: W(n) H(n) 44

Correctness Proofs Proof by induction commonly used for D&C algorithms Base case: Show that the algorithm is correct when the recursion bottoms out (i.e., for sufficiently small n) Inductive hypothesis: Assume the alg. is correct for any recursive call on any smaller subproblem of size k (k < n) General case: Based on the inductive hypothesis, prove that the alg. is correct for any input of size n 45

Example Correctness Proof: Powering a Number POWER (a, n) if n = 0 then return 1 else if n is even then val POWER (a, n/2) return val * val else if n is odd then val POWER (a, (n-1)/2) return val * val * a 46

Example Correctness Proof: Powering a Number Base case: POWER (a, 0) is correct, because it returns 1 Ind. hyp: Assume POWER (a, k) is correct for any k < n General case: In POWER (a, n) function: If n is even: val = a n/2 (due to ind. hyp.) it returns val. val = a n If n is odd: val = a (n-1)/2 (due to ind. hyp.) it returns val. val. a = a n èthe correctness proof is complete 47

Maximum Subarray Problem Input: An array of values Output: The contiguous subarray that has the largest sum of elements Input array: 13-3 -25 20-3 -16-23 18 20-7 12-22 -4 7 the maximum contiguous subarray 48

Maximum Subarray Problem: Divide & Conquer Basic idea: Divide the input array into 2 from the middle Pick the best solution among the following: 1. The max subarray of the left half 2. The max subarray of the right half 3. The max subarray crossing the mid-point Crosses the mid-point A Entirely in the left half Entirely in the right half 49

Maximum Subarray Problem: Divide & Conquer Divide: Trivial (divide the array from the middle) Conquer: Recursively compute the max subarrays of the left and right halves Combine: Compute the max-subarray crossing the midpoint (can be done in Θ(n) time). Return the max among the following: 1. the max subarray of the left subarray 2. the max subarray of the right subarray 3. the max subarray crossing the mid-point See textbook for the detailed solution. 50

Conclusion Divide and conquer is just one of several powerful techniques for algorithm design. Divide-and-conquer algorithms can be analyzed using recurrences and the master method (so practice this math). Can lead to more efficient algorithms CS473 Lecture 4 51