Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer Neil Tang 4/24/2008

Similar presentations


Presentation on theme: "Divide and Conquer Neil Tang 4/24/2008"— Presentation transcript:

1 Divide and Conquer Neil Tang 4/24/2008
CS223 Advanced Data Structures and Algorithms

2 CS223 Advanced Data Structures and Algorithms
Course Survey Please complete the course survey by May 3 (Sat) at: CS223 Advanced Data Structures and Algorithms

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

4 CS223 Advanced Data Structures and Algorithms
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. CS223 Advanced Data Structures and Algorithms

5 CS223 Advanced Data Structures and Algorithms
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. The time complexity is O(NlogN) CS223 Advanced Data Structures and Algorithms

6 CS223 Advanced Data Structures and Algorithms
Merge Sort CS223 Advanced Data Structures and Algorithms

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

8 CS223 Advanced Data Structures and Algorithms
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. CS223 Advanced Data Structures and Algorithms

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

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

11 Integer Multiplication
Compute XY, where X and Y are N-digit integers. Divide: X=XL104+XR , Y=YL104+YR Conquer and combine: XY = XLYL108 + (XLYR+ XRYL)104+XRYR CS223 Advanced Data Structures and Algorithms

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

13 CS223 Advanced Data Structures and Algorithms
A Better Algorithm CS223 Advanced Data Structures and Algorithms

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

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

16 Matrix Multiplication
CS223 Advanced Data Structures and Algorithms

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

18 CS223 Advanced Data Structures and Algorithms
A Better Algorithm CS223 Advanced Data Structures and Algorithms

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


Download ppt "Divide and Conquer Neil Tang 4/24/2008"

Similar presentations


Ads by Google