Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer Approach

Similar presentations


Presentation on theme: "Divide and Conquer Approach"— Presentation transcript:

1 Divide and Conquer Approach

2 Divide and Conquer Approach
Divide the problems into a number of sub problems. Conquer the sub problems by solving them recursively. If the sub-problem sizes are small enough, just solve the problems in a straight forward manner. Combine the solutions to the sub problems into the solution for the original problem.

3 Merge Sort Divide the n element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences to produce the sorted answer. Combine: Merge the two sorted sub sequences to produce the sorted answer.

4 Merge Sort Base Case: When the sequences to be sorted has length 1. 14
34 89 56 66 108 12 Sorted 108 56 12 14 89 34 66 Unsorted 108 56 14 66 Divide 56 66 108 14 Merge 34 12 89 Merge 12 89 34 Divide 108 66 Divide 14 56 Merge 56 14 Divide 12 89 Divide 89 12 Merge 34 Merge 34 Divide 34 BCase 66 108 Merge 89 Divide 89 BCase 89 Merge 12 BCase 12 Merge 12 Divide 66 BCase 66 Divide 66 Merge 108 Merge 108 Divide 108 BCase 56 Merge 56 Divide 56 BCase 14 BCase 14 Divide 14 Merge

5 Merge Sort Algorithm MergeSort(A, i, j) if j > i then
mid ← (i + j)/2 MergeSort(A, i, mid ) MergeSort(A, mid + 1, j ) Merge(A, i, mid, j )

6 Merge Algorithm //LeftPos = start of left half;
//RightPos = start of right half void Merge(int A[ ], int LeftPos, int RightPos, int RightEnd) { int LeftEnd = RightPos – 1; int TmpPos = 1 int NumElements = RightEnd – LeftPos + 1; int TempArray[NumElements]; while(leftPos <= LeftEnd && RightPos <= RightEnd) if(A[LeftPos] <= A[RightPos]) TmpArray[TmpPos++] = A[LeftPos++]; else TmpArray[TmpPos++] = A[RightPos++]; while(LeftPos <= LeftEnd) //Copy rest of first half while(RightPos <= RightEnd) //Copy rest of second half TmpArray[TmpPos++] = A[RightPos++]; for(int i = 1; i <= NumElements; i++) //Copy TmpArray back A[LeftPos++] = TmpArray[i]; }

7 Merge Algorithm The basic merging algorithms takes
Two input arrays, A[] and B[], An output array C[] And three counters aptr, bptr and cptr. (initially set to the beginning of their respective arrays) The smaller of A[aptr] and B[bptr] is copied to the next entry in C i.e. C[cptr]. The appropriate counters are then advanced. When either of input list is exhausted, the remainder of the other list is copied to C.

8 Merge Algorithm 34 12 89 B[] bptr 56 66 108 14 A[] aptr
14 > 12 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 12 12 C[] cptr

9 Merge Algorithm 34 12 89 B[] 56 66 108 14 A[] aptr bptr
14 > 12 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 12 12 cptr++ cptr C[]

10 Merge Algorithm 34 12 89 B[] 56 66 108 14 A[] aptr bptr bptr++
14 > 12 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 12 12 cptr++ C[] cptr

11 Merge Algorithm 56 66 108 14 A[] aptr 34 12 89 B[] bptr
14 < 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 14 14 C[] 12 cptr

12 Merge Algorithm 34 12 89 B[] 56 66 108 14 A[] aptr bptr
14 < 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 14 14 cptr++ C[] 12 cptr

13 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr aptr++ bptr
14 < 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 14 14 cptr++ C[] 12 cptr

14 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr
56 > 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 34 34 C[] 12 14 cptr

15 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr
56 > 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 34 14 cptr++ C[] 12 34 cptr

16 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr bptr++ aptr
56 > 34 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 34 14 cptr++ C[] 12 34 cptr

17 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr
56 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 56 56 C[] 12 14 34 cptr

18 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr
56 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 56 56 cptr++ cptr C[] 12 14 34

19 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr aptr++ bptr
56 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 56 56 cptr++ C[] 12 14 34 cptr

20 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr
66 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 66 66 C[] 12 14 34 56 cptr

21 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr
66 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 66 cptr++ C[] 12 14 34 56 66 cptr

22 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr aptr++ bptr
66 < 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 66 cptr++ C[] 12 14 34 56 66 cptr

23 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr
108 > 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 89 89 C[] 12 14 34 56 66 cptr

24 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr
108 > 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 89 89 cptr++ C[] 12 14 34 56 66 cptr

25 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] bptr aptr bptr++
108 > 89 therefore If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[cptr] = 89 89 cptr++ C[] 12 14 34 56 66 cptr

26 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr
Array B is now finished, copy remaining elements of array A in array C If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[] 12 14 34 56 66 89 cptr

27 Merge Algorithm 56 66 108 14 A[] 34 12 89 B[] aptr bptr
Array B is now finished, copy remaining elements of array A in array C If A[aptr] < B[bptr] C[cptr++] = A[aptr++] Else C[cptr++] = B[bptr++] C[] 12 14 34 56 66 89 108 cptr

28 Merge Algorithm Analysis
Suppose total number of elements to be merged is n Running time is O(n)

29 Merge Sort Analysis MergeSort computation tree has depth log n
Associate some running time with each node in computation tree Sum up times at each node to find total running time

30 Computation Tree N = 7 lg 7  = 3 Tree Depth = 3 108 56 12 14 89 34
66 108 56 14 66 12 89 34 108 66 56 14 12 89 34 66 108 56 14 89 12

31 Computation of Time What is included in running time of a node:
Time Spent at a node What is included in running time of a node: time to divide up input: O(1) time to make recursive calls: O(1) time to merge results of recursive calls: O(n) Total Time : O(n)

32 Merge Sort Analysis Consider one level at a time every element appears once on each level (except perhaps lowest level) n elements on each level O(n) running time per level O(log n) levels Total running time: O(n log n)

33 Problem with Merge Sort
Merging two sorted lists requires linear extra memory. Additional work spent copying to the temporary array and back through out the algorithm. This problem slows down the sort considerably.


Download ppt "Divide and Conquer Approach"

Similar presentations


Ads by Google