Divide-and-conquer algorithm

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

Introduction to Algorithms 6.046J/18.401J/SMA5503

The Divide-and-Conquer Design Paradigm

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

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

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

Algorithms Design & Analysis. Divide & Conquer

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

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

Matrix Multiplication

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

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

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

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

Fast Convolution; Strassen s Method

CPS 616 DIVIDE-AND-CONQUER 6-1

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

Divide and Conquer Algorithms

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

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

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

Chapter 4. Recurrences

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

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

Data Structures and Algorithms CSE 465

Analysis of Multithreaded Algorithms

Chapter 4 Divide-and-Conquer

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

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

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

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Algorithms And Programming I. Lecture 5 Quicksort

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

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Data Structures and Algorithms CMPSC 465

Divide-and-conquer. Curs 2015

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

Data Structures and Algorithms CSE 465

Chapter. Divide-and-Conquer. Contents

Divide and Conquer algorithms

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

Design and Analysis of Algorithms

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

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

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Introduction to Algorithms

COMP 382: Reasoning about algorithms

shelat divide&conquer closest points matrixmult median

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

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Discrete Structures, Final Exam

Design and Analysis of Algorithms

The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem

Math Exam 1 Solutions October 12, 2010

Copyright 2000, Kevin Wayne 1

Lecture 8 - Algebraic Methods for Matching 1

MATRICES The numbers or letters in any given matrix are called its entries or elements

Divide-and-Conquer. a technique for designing algorithms

Algorithms Design & Analysis. Analysis of Algorithm

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

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

8/13/16. Data analysis and modeling: the tools of the trade. Ø Set of numbers. Ø Binary representation of numbers. Ø Floating points.

ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

Recurrence Relations

Divide and Conquer. Arash Rafiey. 27 October, 2016

Divide and Conquer. Andreas Klappenecker

Introduction to Algorithms 6.046J/18.401J

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

Algorithm Design and Analysis

Mergesort and Recurrences (CLRS 2.3, 4.4)

Kartsuba s Algorithm and Linear Time Selection

Asymptotic Analysis and Recurrences

Objec&ves. Review. Divide and conquer algorithms

feb abhi shelat Matrix, FFT

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

1 4 3 A Scalar Multiplication

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

Analysis of Algorithms

Dynamic Programming (CLRS )

Asymptotic Algorithm Analysis & Sorting

Algorithm efficiency analysis

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Practice Final Solutions. 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) { 2 j = 0 3 if (n = 0) { 4 return j

Divide and Conquer. Recurrence Relations

CSI2101-W08- Recurrence Relations

Speedy Maths. David McQuillan

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

CSCI Honor seminar in algorithms Homework 2 Solution

DIVIDE AND CONQUER II

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

Algorithms Design & Analysis. Dynamic Programming

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

Data Structures and Algorithms

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

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

Determinants Chapter 3 of Lay

Lecture 6: Recurrent Algorithms:

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

Searching. Sorting. Lambdas

Transcription:

Divide-and-conquer algorithm IDEA: n n matrix = 2 2 matrix of (n/2) (n/2) submatrices: r=ae+bg s=af+bh t =ce+dh u=cf+dg r t s u = a c e g September 15, 2004 Introduction to Algorithms L3.31 b d C = A B recursive 8 mults of (n/2) (n/2) submatrices ^ 4 adds of (n/2) (n/2) submatrices f h

Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n 2 ) # submatrices submatrix size work adding submatrices September 15, 2004 Introduction to Algorithms L3.32

Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n 2 ) # submatrices submatrix size work adding submatrices n log ba = n log 2 8 = n 3 CASE 1 T(n) = Θ(n 3 ). September 15, 2004 Introduction to Algorithms L3.33

Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n 2 ) # submatrices submatrix size work adding submatrices n log ba = n log 2 8 = n 3 CASE 1 T(n) = Θ(n 3 ). No better than the ordinary algorithm. September 15, 2004 Introduction to Algorithms L3.34

Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. September 15, 2004 Introduction to Algorithms L3.35

Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) September 15, 2004 Introduction to Algorithms L3.36

Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) r = P 5 + P 4 P 2 + P 6 s = P 1 + P 2 t = P 3 + P 4 u = P 5 + P 1 P 3 P 7 September 15, 2004 Introduction to Algorithms L3.37

Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) r = P 5 + P 4 P 2 + P 6 s = P 1 + P 2 t = P 3 + P 4 u = P 5 + P 1 P 3 P 7 7 mults, 18 adds/subs. Note: No reliance on commutativity of mult! September 15, 2004 Introduction to Algorithms L3.38

Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) r = P 5 + P 4 P 2 + P 6 =(a + d)(e + h) + d (g e) (a + b) h + (b d)(g + h) = ae + ah + de + dh + dg de ah bh + bg + bh dg dh = ae + bg September 15, 2004 Introduction to Algorithms L3.39

Strassen s algorithm 1. Divide: Partition A and B into (n/2) (n/2) submatrices. Form terms to be multiplied using + and. 2. Conquer: Perform 7 multiplications of (n/2) (n/2) submatrices recursively. 3. Combine: Form C using + and on (n/2) (n/2) submatrices. September 15, 2004 Introduction to Algorithms L3.40

Strassen s algorithm 1. Divide: Partition A and B into (n/2) (n/2) submatrices. Form terms to be multiplied using + and. 2. Conquer: Perform 7 multiplications of (n/2) (n/2) submatrices recursively. 3. Combine: Form C using + and on (n/2) (n/2) submatrices. T(n) = 7 T(n/2) + Θ(n 2 ) September 15, 2004 Introduction to Algorithms L3.41

Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) September 15, 2004 Introduction to Algorithms L3.42

Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) n log ba = n log 2 7 n 2.81 CASE 1 T(n) = Θ(n lg 7 ). September 15, 2004 Introduction to Algorithms L3.43

Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) n log ba = n log 2 7 n 2.81 CASE 1 T(n) = Θ(n lg 7 ). The number 2.81 may not seem much 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 algorithm on today s machines for n 32 or so. September 15, 2004 Introduction to Algorithms L3.44

Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) n log ba = n log 2 7 n 2.81 CASE 1 T(n) = Θ(n lg 7 ). The number 2.81 may not seem much 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 algorithm on today s machines for n 32 or so. Best to date (of theoretical interest only): Θ(n 2.376L ). September 15, 2004 Introduction to Algorithms L3.45

VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. September 15, 2004 Introduction to Algorithms L3.46

VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) September 15, 2004 Introduction to Algorithms L3.47

VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) H(n) = H(n/2) + Θ(1) = Θ(lg n) September 15, 2004 Introduction to Algorithms L3.48

VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) H(n) = H(n/2) + Θ(1) = Θ(lg n) W(n) = 2W(n/2) + Θ(1) = Θ(n) September 15, 2004 Introduction to Algorithms L3.49

VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) H(n) = H(n/2) + Θ(1) W(n) = 2W(n/2) + Θ(1) = Θ(lg n) = Θ(n) Area = Θ(n lg n) September 15, 2004 Introduction to Algorithms L3.50

H-tree embedding L(n) L(n) September 15, 2004 Introduction to Algorithms L3.51

H-tree embedding L(n) L(n) L(n/4) Θ(1) L(n/4) September 15, 2004 Introduction to Algorithms L3.52

H-tree embedding L(n) L(n) L(n) = 2L(n/4) + Θ(1) = Θ( n) Area = Θ(n) L(n/4) Θ(1) L(n/4) September 15, 2004 Introduction to Algorithms L3.53

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). The divide-and-conquer strategy often leads to efficient algorithms. September 15, 2004 Introduction to Algorithms L3.54