Divide and Conquer Algorithm Explained

Slide Note
Embed
Share

Divide and Conquer algorithm involves dividing a problem into smaller sub-problems, solving them, and combining the solutions to solve the original problem efficiently. The concept is explained through examples of finding maximum and minimum elements in a set, and a detailed algorithmic approach is provided step by step. Additionally, an algorithm for finding the summation of elements using the divide and conquer technique is illustrated.


Uploaded on Aug 30, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Divide and Conquer (Divide, Conquer, Combine) Maram Bani Younes

  2. Divide and Conquer: (Divide, Conquer, Combine) - The idea is to divide the problem into smaller but similar sub problems (divide), solve it (conquer), and (combine) these solutions to create a solution to the original problem.

  3. Problem Finding the maximum and minimum elements in a set of (n) elements using the straightforward algorithm.

  4. Algorithm: Max and Min Algorithm straightforward (a, n, max, min) Input: array (a) with (n) elements Output: max: max value, min: min value max = min = a(1) for i = 2 to n do begin if (a(i)>max) then max=a(i) if (a(i)<min) then min=a(i) end end best= average =worst= 2(n-1) comparisons

  5. Algorithm: Max and Min If we change the body of the loop as follows: max=min=a(1) for i=2 to n do begin if (a(i)>max) then max=a(i) else if (a(i)<min) then min=a(i) end best case: elements in increasing order No. of comparisons = (n-1) Worst case: elements in decreasing order No. of comparisons = 2(n-1) Average case: a(i) is greater than max half the time No. of comparisons= 3n/2 3/2 ((n-1)+(2n-2))

  6. Problem: Problem: is to find the maximum and minimum items in a set of (n) elements. Algorithm MaxMin(i, j, max, min) input: array of N elements, i lower bound, j upper bound output: max: largest value min: smallest value. if (i=j) then max=min=a(i) else if (i=j-1) then if (a(i)<a(j)) then max= a(j) min= a(i) else max= a(i) min= a(j) else mid= (i+j)/2 maxmin(i, mid, max, min) maxmin(mid+1, j, max1, min1) if (max<max1) then max = max1 if (min>min1) then min = min1 end n n + + 2 2 C C for n 2 2 = = 1 2 Cn n = 0 1 n = k 2 n When n is a power of 2, 2 + = C C n n 2 2 C = + + 2 2 2 2 C n 4 = + + 4 4 2 n 4 1 k i = + 1 k i 2 2 C 2 = 1 = + 1 k k 2 2 2 3 n = 2 2 Note: No algorithm based on comparison can do less than this.

  7. Example Example Given a set of n elements. Write an algorithm using divide and conquer, to find the summation of its elements.

  8. + 2 1 2 C for n n Algorithm 2 = = 1 2 C n n = 0 1 n = + 2 1 C C n n 2 = + + 4 2 1 C n 4 2 k = i = + 1 k i 2 2 C 2 0 n = + 1 k 2 1 2 n n = + 1 2 n 2 = = = 1 ( ) { 2 } O n stopping condition n OR 1 k = i = + k i 2 2 C 1 0 = + = } 1 = 0 1 ( ) { n O n stopping condition n

  9. The Maximum Contiguous Subsequence Sum The Maximum Contiguous Subsequence Sum Given (possibly negative) integers , find (and identify the sequence corresponding to) the maximum value of . The maximum contiguous subsequence sum is zero if all the integers are negative. Suppose we are given the following input {-2, 11, -4, 13, -5, 2} The maximum contiguous subsequence sum is (20), which encompassing items 2 through 4.

  10. Divide and Conquer approach Let consider a divide and conquer algorithm: We divide the input into two halves. The max contiguous subsequence sum can occur in one of 3 ways: Case1: It resides entirely in the 1st half. Case2: It resides entirely in the 2nd half. Case3: It begins in the 1st half but ends in the 2nd half. 1) Recursively compute the max contiguous subsequence sum that resides entirely in the 1st half. 2) Recursively compute the max contiguous subsequence sum that resides entirely in the 2nd half. 3) Compute via two consecutive loops the max contiguous subsequence sum that begins in the 1st but ends in the 2nd. 4) Choose the largest of the 3 sums.

  11. + 2 1 C n for n n = C 2 n = 1 1 n = + 2 C C n n n 2 = + + 4 C n n n 4 k = = + k 2 C n 1 1 i = + ( lg ) O n n n

  12. Quick Sort Algorithm Quick Sort Algorithm (Divide & Conquer) (Divide & Conquer) Given the following set of data: 42 23 74 11 65 58 94 36 99 87 1st pass {11 23 36} 42 {65 58 94 74 99 87} 2nd pass 11 {23 36} 42 {65 58 94 74 99 87} Last pass 11 23 36 42 58 65 74 87 94 99

  13. + 2 2 C n for n n = C 2 n = 0 1 n Cn= Note The best thing that could happen in Quick Sort would be that each partitioning stage divides the file exactly in half. ( lg ) O n n

  14. Quick Sort Discussion Quick Sort uses about (1.38nlgn) comparisons on the average. The precise recurrence formula for the number of comparisons used by Quick Sort for a random permutation of n elements is: ( 1 1 1 = = C C With Worst Case + = 0 n C This is equal to: ( ) ( 2 1 2 k k = There is a simple solution to the problem of poor performance instead of using the 1st element of the array as the partitioning element; we could generate a random number (r), where r is in the range of 1 to the number of elements in the partition. n ) = k = + + + 2 C n C C for n 1 n k n k n 0 1 0 2 C n for n 1 n n = 1 ( 1 ) n n n = = 2 ) n

  15. Quick Sort Discussion Although the worst case is still , it is extremely unlikely that we will encounter it. The probability of encounter the worst case is , where n is the number of elements in the file. Another method is the median-of-three. We choose the median of the left element, the right element, and the one halfway between them. Unless this median is already the left element, we exchange it with left.

  16. Space Usage Space Usage Quick Sort is not working in-place sort. While the algorithm is working on one sublist, the beginning and ending indexes (borders) of all the other sublists must be saved on a stack. The size of the stack depends on the no. of sublists into which the list will be split. The worst case amount of space used by stack is .

  17. Merge Sort (Divide & Conquer) We use the merge procedure as the basis for merge sort. If we have two sets of numbers that are already sorted, we can merge them together with one scan. Let the 1st set be ( a a , , , 2 1 ( ) m b b b , , , 2 1 . Assume that both sets are sorted in increasing order. Scan the 1st set until the right place to insert from that place until the right place to insert and so on. ) , and the 2nd set be n a 1bis found, and insert it, then continue the scan 2b is found

  18. Merge Sort (Divide & Conquer) MergeSort algorithm works as follows: 1)The sequence is divided into two equal or close-to- equal (in case of an odd size) parts. 2)Each part is sorted separately recursively 3)The two sorted parts are merged into one sorted sequence. Complexity: + 2 2 n C n 2 C n for n n = = 1 2 = 0 1 n Cn= ( lg ) O n n It is not working in-place algorithm, since it requires additional storage to copy the merged set.

  19. ex: 36 20 17 13 28 14 2315 20 36 13 17 13 17 20 36 14 28 15 23 14 15 23 28 13 14 15 17 20 23 28 36 Notes: The running time of merge sort does not depend on the arrangement of the input values, thus it does not have poor worst case performance. It has the advantage that it sorts a file of n elements in time proportional to (nlgn) even in the worst case. The disadvantage is the extra space proportional to n.

  20. Binary Search , 1 ai i n Let nondecreasing order. Consider the problem of determining whether a given element x is present in the list. If x is present, we are to determine a value j such that . x aj= If x is not in the list, then j is set to be zero. , be a list of elements that are sorted in

Related


More Related Content