Introduction to Algorithms: Divide-n-Conquer Algorithms

Slides:



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

More on Divide and Conquer. The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving.
Divide and Conquer Strategy
Introduction to Algorithms Jiafen Liu Sept
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.
Introduction to Algorithms 6.046J/18.401J L ECTURE 3 Divide and Conquer Binary search Powering a number Fibonacci numbers Matrix multiplication Strassen’s.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
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.
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.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
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.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
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.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 3 Prof. Erik Demaine.
CS223 Advanced Data Structures and Algorithms 1 Sorting and Master Method Neil Tang 01/21/2009.
Project 2 due … Project 2 due … Project 2 Project 2.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
INTRODUCTION. What is an algorithm? What is a Problem?
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
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 Andreas Klappenecker [based on slides by Prof. Welch]
CMPT 438 Algorithms.
Introduction to Algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Chapter 4 Divide-and-Conquer
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
Algorithm Design & Analysis
Introduction to the Design and Analysis of Algorithms
Lecture 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer.
CS 3343: Analysis of Algorithms
Intro to Recursion.
Divide-and-Conquer The most-well known algorithm design strategy:
CSCE 411 Design and Analysis of Algorithms
Growth Functions Algorithms Lecture 8
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Data Structures Review Session
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 6 More Divide-Conquer and Paradigm #4 Data Structure.
CSE 373 Data Structures and Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
CSE 2010: Algorithms and Data Structures
Introduction to Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer (Merge Sort)
Divide and Conquer Neil Tang 4/24/2008
Trevor Brown CS 341: Algorithms Trevor Brown
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Algorithms Recurrences.
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-conquer approach
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Introduction to Algorithms: Divide-n-Conquer Algorithms

Introduction to Algorithms Divide and Conquer Binary search Powering a number Fibonacci numbers Matrix multiplication Strassen’s algorithm CS 421 - Analysis of Algorithms

Divide-and-Conquer Design Divide the problem (instance) into sub-problems. Conquer the sub-problems by solving them recursively. Combine sub-problem solutions. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Merge Sort Divide: Trivial. Conquer: Recursively sort 2 sub-arrays. Combine: Linear-time merge. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Merge Sort Divide: Trivial. Conquer: Recursively sort 2 subarrays. Combine: Linear-time merge. T(n) = 2 T(n/2) + (n) # sub-problems sub-problem size work dividing and combining CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Master Theorem T(n) = a T(n/b) + f (n) Case 1: 𝑎 <𝑏 𝑑 Solution: T(n) ∈ ( 𝑛 𝑑 ) . Case 2: 𝑎 =𝑏 𝑑 Solution: T(n) = ( 𝑛 𝑑 log n) . Case 3: 𝑎 >𝑏 𝑑 Solution: T(n) ∈ ( 𝑛 𝑙𝑜𝑔𝑏𝑎 ) . CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Master Theorem T(n) = a T(n/b) + f (n) Case 1: 𝑎 <𝑏 𝑑 Solution: T(n) ∈ ( 𝑛 𝑑 ) . Case 2: 𝑎 =𝑏 𝑑 Solution: T(n) = ( 𝑛 𝑑 log n) . Case 3: 𝑎 >𝑏 𝑑 Solution: T(n) ∈ ( 𝑛 𝑙𝑜𝑔𝑏𝑎 ) . Merge sort: T(n) = 2 T(n/2) + (n) a = 2, b = 2 , f(n) ∈ (n), d = 1 CASE 2  T(n) = (n1 log n) = (n log n). CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Binary Search Find an element in a sorted array: Divide: Check middle element. Conquer: Recursively search 1 sub-array. Combine: Trivial. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Binary Search Find an element in a sorted array: Divide: Check middle element. Conquer: Recursively search 1 sub-array. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Binary Search Find an element in a sorted array: Divide: Check middle element. Conquer: Recursively search 1 sub-array. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Binary Search Find an element in a sorted array: Divide: Check middle element. Conquer: Recursively search 1 sub-array. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Binary Search Find an element in a sorted array: Divide: Check middle element. Conquer: Recursively search 1 sub-array. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Binary Search Find an element in a sorted array: Divide: Check middle element. Conquer: Recursively search 1 sub-array. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Binary Search Find an element in a sorted array: Divide: Check middle element. Conquer: Recursively search 1 sub-array. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS 421 - Analysis of Algorithms

Recurrence for Binary Search T(n) = 1 T(n/2) + (1) # sub-problems sub-problem size work dividing and combining CS 421 - Analysis of Algorithms

Recurrence for Binary Search T(n) = 1 T(n/2) + (1) # sub-problems sub-problem size work dividing and combining nlogba = nlog21 = n0 = 1  CASE 2 (k = 0)  T(n) = (lg n) . CS 421 - Analysis of Algorithms

Recurrence for Binary Search T(n) = 1 T(n/2) + (1) # subproblems subproblem size work dividing and combining a = 1, b = 2 , f(n) ∈ (1), d = 0 CASE 2  T(n) = (n0 log n) = (log n). CS 421 - Analysis of Algorithms

Calculating Powers of a Number Problem: Compute an, where n  N. Naive algorithm: (n). CS 421 - Analysis of Algorithms

Calculating Powers of a Number Problem: Compute an, where n  N. Naive algorithm: (n). Divide-and-conquer algorithm: a n/2  a n/2 a (n–1)/2  a (n–1)/2  a if n is even; if n is odd. an = CS 421 - Analysis of Algorithms

Calculating Powers of a Number Problem: Compute an, where n  N. Naive algorithm: (n). Divide-and-conquer algorithm: a n/2  a n/2 a (n–1)/2  a (n–1)/2  a if n is even; if n is odd. an = T(n) = T(n/2) + (1)  T(n) = (lg n) . CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Fibonacci Numbers 0 if n = 0 1 if n = 1 Fn–1 + Fn–2 if n  2. 0 1 1 2 3 5 8 13 21 34 … Recursive definition: Fn = CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms Fibonacci Numbers 0 if n = 0 1 if n = 1 Fn–1 + Fn–2 if n  2. 0 1 1 2 3 5 8 13 21 34 … Recursive definition: Fn = Naive recursive algorithm: ( n) (exp. time) where  = (1 is the golden ratio. 5)/ 2 CS 421 - Analysis of Algorithms

Computing Fibonacci Numbers Bottom-up: Compute F0, F1, F2, …, Fn in order, forming each number by summing the two previous. Running time: (n). Naive recursive squaring: Fn =  n/ 5 rounded to the nearest integer. Recursive squaring: (lg n) time. This method is unreliable, since floating-point arithmetic is prone to round-off errors. CS 421 - Analysis of Algorithms

CS 421 - Analysis of Algorithms 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. CS 421 - Analysis of Algorithms