The Divide-and-Conquer Design Paradigm

Similar documents
Introduction to Algorithms 6.046J/18.401J/SMA5503

Divide-and-conquer algorithm

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

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

Chapter 4 Divide-and-Conquer

Introduction to Algorithms 6.046J/18.401J LECTURE 3 Divide and conquer Binary search Powering a number Fibonacci numbers Matrix multiplication

Algorithms Design & Analysis. Divide & Conquer

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

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

Matrix Multiplication

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

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

Algorithm Design and Analysis

CPS 616 DIVIDE-AND-CONQUER 6-1

1 Substitution method

Algorithms and Data Structures Strassen s Algorithm. ADS (2017/18) Lecture 4 slide 1

Data Structures and Algorithms Chapter 3

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

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

Divide and Conquer algorithms

Asymptotic Analysis and Recurrences

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Tutorials. Algorithms and Data Structures Strassen s Algorithm. The Master Theorem for solving recurrences. The Master Theorem (cont d)

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

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

The Master Theorem for solving recurrences. Algorithms and Data Structures Strassen s Algorithm. Tutorials. The Master Theorem (cont d)

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

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

Divide and Conquer. Andreas Klappenecker

Data Structures and Algorithms CSE 465

Algorithms Chapter 4 Recurrences

Fast Convolution; Strassen s Method

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

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

Divide and Conquer. Recurrence Relations

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

Analysis of Algorithms - Using Asymptotic Bounds -

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Algorithms And Programming I. Lecture 5 Quicksort

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

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

CSE 613: Parallel Programming. Lectures ( Analyzing Divide-and-Conquer Algorithms )

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

Design and Analysis of Algorithms

Analysis of Multithreaded Algorithms

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

Divide and Conquer Algorithms

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

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

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

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

Data Structures and Algorithms CSE 465

Copyright 2000, Kevin Wayne 1

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Design and Analysis of Algorithms Recurrence. Prof. Chuhua Xian School of Computer Science and Engineering

6.046 Recitation 2: Recurrences Bill Thies, Fall 2004 Outline

Divide and Conquer Strategy

Chapter 2. Divide-and-conquer. 2.1 Strassen s algorithm

Review Of Topics. Review: Induction

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

Recurrence Relations

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

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

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

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

CSC373: Algorithm Design, Analysis and Complexity Fall 2017

Objective. - mathematical induction, recursive definitions - arithmetic manipulations, series, products

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

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Algorithms Design & Analysis. Analysis of Algorithm

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

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

COMP 382: Reasoning about algorithms

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Divide-and-conquer. Curs 2015

ITEC2620 Introduction to Data Structures

Data Structures and Algorithms Chapter 3

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Data Structures and Algorithms

Lecture 6: Recurrent Algorithms:

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: October 11. In-Class Midterm. ( 7:05 PM 8:20 PM : 75 Minutes )

CS325: Analysis of Algorithms, Fall Midterm

Lecture 8 - Algebraic Methods for Matching 1

Methods for solving recurrences

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

ENGR-1100 Introduction to Engineering Analysis. Lecture 21. Lecture outline

Algorithm Analysis Divide and Conquer. Chung-Ang University, Jaesung Lee

Practical Session #3 - Recursions

Answer the following questions: Q1: ( 15 points) : A) Choose the correct answer of the following questions: نموذج اإلجابة

Algorithms. Adnan YAZICI Dept. of Computer Engineering Middle East Technical Univ. Ankara - TURKEY. Algorihms, A.Yazici, Fall 2007 CEng 315

Mergesort and Recurrences (CLRS 2.3, 4.4)

ENGR-1100 Introduction to Engineering Analysis. Lecture 21

Today s Outline. CS 362, Lecture 13. Matrix Chain Multiplication. Paranthesizing Matrices. Matrix Multiplication. Jared Saia University of New Mexico

CS483 Design and Analysis of Algorithms

Kartsuba s Algorithm and Linear Time Selection

Transcription:

CS473- Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm CS473 Lecture 4 1

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

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

Master Theorem (reprise) T(n) = at(n/b) + f(n) a CASE 1: f(n) = O(n - ) => T(n) = (n log a ). b b CASE 2: f(n) = (n log a lg k n) => T(n) = (n log a lg k+1 b b n). CASE 3: f(n) = Ω(n log ε log => T(n) = (f(n)). Merge Sort: a = 2, b = 2 => n a b ε + ) and af(n/b) cf(n) a log b = n => Case 2 ( k = 0 ) => T(n) = ( n lg n ). CS473 Lecture 4 4

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 5

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 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

Recurrence for Binary Search T(n) = 1 T(n/2) + Ө(1) # subproblems subproblem size work dividing and combining n log a = n log 1 = n 0 =1 => CASE 2 (k = 0) b 2 => T(n) = (lgn). CS473 Lecture 4 11

Powering a Number Problem: Compute a n, where n is in N. Naive algorithm: (n) Divide-and-conquer algorithm: a n = a n/2. a n/2 a (n-1)/2. a (n-1)/2. a if n is even if n is odd T(n) = T(n/2) + (1) => T(n) = (lgn). CS473 Lecture 4 12

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 13

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 14

Divide-and-Conquer Algorithm IDEA: n x n matrix = 2 x 2 matrix of (n/2) x (n/2) submatrices 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 = A. B 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 21 + a 22 b 22 CS473 Lecture 4 15

Analysis of D&C Algorithm T(n) = 8 T(n/2) + (n 2 ) # submatrices submatrix size work dividing submatrices a log b log 2 8 n = n = n 3 => CASE 1 => T(n) = (n 3 ). No better than the ordinary algorithm! CS473 Lecture 4 16

Strassen s Idea Multiply 2x2 matrices with only 7 recursive mults P 1 = a 11 x (b 12 - b 22 ) c 11 = P 5 + P 4 P 2 + P 6 P 2 = (a 11 + a 12 ) x b 22 c 12 = P 1 + P 2 P 3 = (a 21 + a 22 ) x b 11 c 21 = P 3 + P 4 P 4 = a 22 x (b 21 - b 11 ) c 22 = P 5 + P 1 P 3 P 7 P 5 = (a 11 + a 22 ) x (b 11 + b 22 ) P 6 = (a 11 - a 22 ) x (b 21 + b 22 ) P 7 = ( a 11 - a 21 ) x (b 11 + b 12 ) 7 mults 18 adds/subs. Does not rely on commutativity of mult. CS473 Lecture 4 17

Strassen s Idea Multiply 2x2 matrices with only 7 recursive mults P 1 = a 11 x (b 12 - b 22 ) P 2 = (a 11 + a 12 ) x b 22 c 12 = P 1 + P 2 P 3 = (a 21 + a 22 ) x b 11 = a 11 (b 12 b 22 )+(a 11 +a 12 )b 22 P 4 = a 22 x (b 21 - b 11 ) = a 11 b 12 -a 11 b 22 +a 11 b 22 +a 12 b 22 P 5 = (a 11 + a 22 ) x (b 11 + b 22 ) = a 11 b 12 +a 12 b 22 P 6 = (a 11 - a 22 ) x (b 21 + b 22 ) P 7 = ( a 11 - a 21 ) x (b 11 + b 12 ) CS473 Lecture 4 18

Strassen s Algorithm 1. Divide: Partition A 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. T(n) = 7 T(n/2) + Ө(n 2 ) CS473 Lecture 4 19

Analysis of Strassen T(n) = 7 T(n/2) + (n 2 ) n log b a = n log 2 7 = n 2.81 => CASE 1 => T(n) = (n lg7 ). The number 2.81 may not seemmuch smaller than 3, but because the difference is in the exponent, the impact on running time is significant. In fact, Strassen s algorithm beats the ordinary algorithmon today s machines for n 30 or so. Best to date (of theoretical interest only) : (n 2.376... ). CS473 Lecture 4 20

VLSI Layout Problem: Embed a complete binary tree with n leavesin a grid using minimal area. W(n) H(n) H(n) = H(n/2) + Ө(1) W(n) = 2 W(n/2) + Ө(1) = Ө(lg n) = Ө(n) Area = Ө(nlg(n)) CS473 Lecture 4 21

H-tree Embedding L(n) L(n) L(n) = 2 L(n/4)+ (1) = ( n) Area = (n) CS473 Lecture 4 22

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 23