Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.

Similar presentations


Presentation on theme: "CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010."— Presentation transcript:

1 CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010

2 CS223 Advanced Data Structures and Algorithms 2 Class Overview  Basic idea  Quick sort  Merge sort  Integer multiplication  Matrix multiplication

3 CS223 Advanced Data Structures and Algorithms 3 Basic Idea  Divide: divide the original problem to several sub-problems.  Conquer: Solve the sub-problems recursively.  Combine: Combine the solutions to sub-problems to form a solution for the original problem.

4 CS223 Advanced Data Structures and Algorithms 4 Merge Sort  Divide: Divide the N-element sequence into 2 subsequences of N/2 each.  Conquer: Sort each subsequence recursively using merge sort.  Combine: Merge two sorted subsequences to produce a single sorted sequence.

5 CS223 Advanced Data Structures and Algorithms 5 Merge Sort

6 CS223 Advanced Data Structures and Algorithms 6 Merge Sort  T(N) = 2T(N/2) + N  a=2, b=2, f(N)=N  log 2 2 = 1  So we have case 2, T(N) =  (NlogN).

7 CS223 Advanced Data Structures and Algorithms 7 Quick Sort  Divide: Divide the sequence into 2 subsequences, s.t. each element in the 1st subsequence is less than or equal to each element in the 2nd subsequence.  Conquer: Sort each subsequence recursively using quick sort.  Combine: no work is needed.

8 CS223 Advanced Data Structures and Algorithms 8 Quick Sort age 25 people age  25 people age  25 age 23 people age < 23 age 30 people age  30 people age  23 people age < 30

9 CS223 Advanced Data Structures and Algorithms 9 Quick Sort  T(N) = T(N-1) + N  The Master method does not work.  T(N) = O(N 2 )

10 CS223 Advanced Data Structures and Algorithms 10 Integer Multiplication  Compute XY, where X and Y are N-digit integers.  Divide: X=X L 10 4 +X R, Y=Y L 10 4 +Y R  Conquer and combine: XY = X L Y L 10 8 + (X L Y R + X R Y L )10 4 +X R Y R

11 CS223 Advanced Data Structures and Algorithms 11 Integer Multiplication  T(N) = 4T(N/2) +  (N)  a=4, b=2, f(N)=  (N)  log 2 4 = 2   So we have case 1, T(N) =  (N 2 ).

12 CS223 Advanced Data Structures and Algorithms 12 A Better Algorithm

13 CS223 Advanced Data Structures and Algorithms 13 Integer Multiplication  T(N) = 3T(N/2) +  (N)  a=3, b=2, f(N)=  (N)  log 2 3 = 1.59   So we have case 1, T(N) =  (N 1.59 ).

14 CS223 Advanced Data Structures and Algorithms 14 Matrix Multiplication  Compute AB, where A and B are N  N matrix.  Divide: Divide each matrix to 4 quadrants.  Conquer and combine:

15 CS223 Advanced Data Structures and Algorithms 15 Matrix Multiplication

16 CS223 Advanced Data Structures and Algorithms 16 Matrix Multiplication  T(N) = 8T(N/2) +  (N 2 )  a=8, b=2, f(N)=  (N 2 )  log 2 8 = 3   So we have case 1, T(N) =  (N 3 ).

17 CS223 Advanced Data Structures and Algorithms 17 A Better Algorithm

18 CS223 Advanced Data Structures and Algorithms 18 Matrix Multiplication  T(N) = 7T(N/2) +  (N 2 )  a=7, b=2, f(N)=  (N 2 )  log 2 7 = 2.81   So we have case 1, T(N) =  (N 2.81 ).


Download ppt "CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010."

Similar presentations


Ads by Google