Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 3 Prof. Erik Demaine.

Slides:



Advertisements
Similar presentations
Divide-and-Conquer CIS 606 Spring 2010.
Advertisements

A simple example finding the maximum of a set S of n numbers.
More on Divide and Conquer. The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide and Conquer Strategy
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Introduction to Algorithms Jiafen Liu Sept
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
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
Theory of Algorithms: Divide and Conquer
Introduction to Algorithms 6.046J/18.401J L ECTURE 3 Divide and Conquer Binary search Powering a number Fibonacci numbers Matrix multiplication Strassen’s.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Foundations of Algorithms, Fourth Edition
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
Arithmetic.
Divide-and-Conquer 7 2  9 4   2   4   7
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
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.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
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.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
1 Chapter 4 Divide-and-Conquer. 2 About this lecture Recall the divide-and-conquer paradigm, which we used for merge sort: – Divide the problem into a.
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.
Lecture 28 CSE 331 Nov 9, Mini project report due WED.
ADA: 4.5. Matrix Mult.1 Objective o an extra divide and conquer example, based on a question in class Algorithm Design and Analysis (ADA) ,
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Divide and Conquer Strategy
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
CMPT 438 Algorithms.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Introduction to Algorithms
Divide-and-Conquer 6/30/2018 9:16 AM
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
CSCE 411 Design and Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 6 More Divide-Conquer and Paradigm #4 Data Structure.
Divide-and-Conquer The most-well known algorithm design strategy:
Introduction to Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer 7 2  9 4   2   4   7
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 3 Prof. Erik Demaine

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.

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

Master theorem (reprise) T(n) = aT(n/b) + f (n) CASE 1: f (n) = O( n logba – ε) ⇒ T(n) = Θ( n logba). CASE 2: f (n) = Θ( n logba lg k n ) ⇒ T(n) = Θ( n logba lg k+1 n ). CASE 3: f (n) = Ω( n logba + ε) and a f (n/b) ≤ c f (n) ⇒ T(n) = Θ( f (n)). Merge sort: a = 2, b = 2 ⇒ n logba = n ⇒ CASE 2 (k = 0) ⇒ T(n) = Θ(n lg n).

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

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

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

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

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

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

Recurrence for binary search T(n) = 1 T(n/2) + Θ (n) n logba = nlog21 = n 0 = 1 ⇒ CASE 2 (k = 0) ⇒ T(n) = Θ(lg n). # subproblems subproblem size work dividing and combining

Powering a number Problem: Compute a n, where n ∈ N. Naive algorithm: Θ(n). Divide-and-conquer algorithm:

Fibonacci numbers Recursive definition: … Naive recursive algorithm: Ω( φn) (exponential time), where is the golden ratio.

Computing Fibonacci numbers Naive recursive squaring: rounded to the nearest integer. 5 Recursive squaring: Θ(lg n) time. This method is unreliable, since floating-point arithmetic is prone to round-off errors. Bottom-up: Compute F 0, F 1, F2, …, F n in order, forming each number by summing the two previous. Running time: Θ(n).

Recursive squaring Theorem: Algorithm: Recursive squaring. Time = Θ(lg n). Proof of theorem. (Induction on n.) Base (n = 1):

Recursive squaring Inductive step (n ≥ 2):

Matrix multiplication Input: Output:

Standard algorithm for i ← 1 to n do for j ← 1 to n do cij ← 0 for k ← 1 to n do cij ← cij + aik ⋅ bkj Running time = Θ( n 3)

Divide-and-conquer algorithm IDEA: n×n matrix = 2×2 matrix of (n/2)×(n/2) submatrices: 8 mults of (n/2)×(n/2) submatrices 4 adds of (n/2)×(n/2) submatrices

Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ ( n 2) n logba = nlog 2 8 = n3 ⇒ CASE 1 (k = 0) ⇒ T(n) = Θ(lg n). No better than the ordinary algorithm. # subproblems subproblem size work dividing and combining

Strassen ’ s idea Multiply 2×2 matrices with only 7 recursive P1 = a ⋅ ( f – h) r = P5 + P4 – P2 + P6 P2 = (a + b) ⋅ h s = P1 + P2 P3 = (c + d) ⋅ e t = P3 + P4 P4 = d ⋅ (g – e) u = P5 + P1 – P3 – P7 P5 = (a + d) ⋅ (e + h) 7 mults, 18 adds/subs. P6 = (b – d) ⋅ (g + h) Note: No reliance on P7 = (a – c) ⋅ (e + f ) commutativity of mult!

Strassen ’ s idea Multiply 2×2 matrices with only 7 recursive P1 = a ⋅ ( f – h) r = P5 + P4 – P2 + P6 P2 = (a + b) ⋅ h = (a+d)(e+h) P3 = (c + d) ⋅ e + d(g-e)-(a+b)h P4 = d ⋅ (g – e) +(b-d)(g+h) P5 = (a + d) ⋅ (e + h) = ae+ah+de+dh P6 = (b – d) ⋅ (g + h) +dg-de-ah-bh P7 = (a – c) ⋅ (e + f ) +bg+bh-dg-dh =ae + bg

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)

Analysis of Strassen T(n) = 7 T(n/2) + Θ(n2) n logba = n log27 ≈ 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 ≥ 30 or so. Best to date (of theoretical interest only): Θ( n … ).

VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. H(n) = H(n/2) + Θ(1) W(n) = 2W(n/2) + Θ(1) = Θ(lg n) = Θ(n) Area = Θ(n lg n)

H-tree embedding L(n) = 2L(n/4) + Θ(1) = Θ(√n ) n Area = Θ(n)

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