Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms: Divide-n-Conquer Algorithms

Similar presentations


Presentation on theme: "Introduction to Algorithms: Divide-n-Conquer Algorithms"— Presentation transcript:

1 Introduction to Algorithms: Divide-n-Conquer Algorithms

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

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

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

5 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 Analysis of Algorithms

6 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 Analysis of Algorithms

7 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 Analysis of Algorithms

8 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 Analysis of Algorithms

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

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

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

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

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

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

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

16 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 Analysis of Algorithms

17 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 Analysis of Algorithms

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

19 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 Analysis of Algorithms

20 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 Analysis of Algorithms

21 CS 421 - Analysis of Algorithms
Fibonacci Numbers 0 if n = 0 1 if n = 1 Fn–1 + Fn–2 if n  2. Recursive definition: Fn = CS Analysis of Algorithms

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

23 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 Analysis of Algorithms

24 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 Analysis of Algorithms


Download ppt "Introduction to Algorithms: Divide-n-Conquer Algorithms"

Similar presentations


Ads by Google