Divide and Conquer Algorithms CSCI 385 Data Structures

  • Slides: 16
Download presentation
Divide and Conquer Algorithms CSCI 385 Data Structures & Analysis of Algorithms Lecture note

Divide and Conquer Algorithms CSCI 385 Data Structures & Analysis of Algorithms Lecture note Sajedul Talukder

Divide-and-conquer paradigm • Divide-and-conquer § Divide up problem into several subproblems (of the same

Divide-and-conquer paradigm • Divide-and-conquer § Divide up problem into several subproblems (of the same kind). § Solve (conquer) each subproblem recursively. § Combine solutions to subproblems into overall solution. • Most common usage § Divide problem of size n into two subproblems of size n / 2. § Solve (conquer) two subproblems recursively. § Combine two solutions into overall solution. • Consequence § Brute force: Θ(n 2). § Divide-and-conquer: O(n log n).

Divide-and-Conquer Technique a problem of size n (instance) subproblem 1 of size n/2 subproblem

Divide-and-Conquer Technique a problem of size n (instance) subproblem 1 of size n/2 subproblem 2 of size n/2 a solution to subproblem 1 a solution to subproblem 2 a solution to the original problem It general leads to a recursive algorithm!

Recursion. . 1. How to break into smaller identical sub-problem(s)? l Divide/Decomposition 2. Which

Recursion. . 1. How to break into smaller identical sub-problem(s)? l Divide/Decomposition 2. Which is the smallest sub-problem that can be solved easily (without further decomposition)? l Base/Stopping case 3. How are the answers to smaller sub-problems combined to form the answer to the larger problem? l Conquer/Composition 3

Merge Sort • Well-known example of divide and conquer algorithm • Recursive design: •

Merge Sort • Well-known example of divide and conquer algorithm • Recursive design: • Divides array in half and merge sorts the halves (decomposition) • Stops when array has only one element (base case) • Combines two sorted halves (composition) • Harder to implement iteratively 4

Merge Sort MERGESORT(A, l, r) { if (l < r) { m = (l+r)/2;

Merge Sort MERGESORT(A, l, r) { if (l < r) { m = (l+r)/2; MERGESORT(A, l, m); MERGESORT(A , m+1, r); MERGE(A, l, m, r); } }

Execution Trace (divide/decomposition) 3 6 8 2 5 4 7 1 3 6 8

Execution Trace (divide/decomposition) 3 6 8 2 5 4 7 1 3 6 8 2 3 6 5 4 7 1 8 2 5 4 8 5 2 4 MERGESORT(A, l, r) { if (l < r) { m = (l+r)/2; MERGESORT(A, l, m); MERGESORT(A , m+1, r); MERGE(A, l, m, r); } } 7 1 6

Execution Trace (conquer/composition) 1 2 3 4 5 6 7 8 2 3 6

Execution Trace (conquer/composition) 1 2 3 4 5 6 7 8 2 3 6 8 3 6 2 8 8 2 1 4 5 7 4 5 5 4 MERGESORT(A, l, r) { if (l < r) { m = (l+r)/2; MERGESORT(A, l, m); MERGESORT(A , m+1, r); MERGE(A, l, m, r); } } 1 7 7 1 7

Merging Two Sorted Arrays 3 6 2 8 8

Merging Two Sorted Arrays 3 6 2 8 8

Merging Two Sorted Arrays 2 3 6 2 8 9

Merging Two Sorted Arrays 2 3 6 2 8 9

Merging Two Sorted Arrays 2 3 3 6 2 8 10

Merging Two Sorted Arrays 2 3 3 6 2 8 10

Merging Two Sorted Arrays 2 3 6 2 8 11

Merging Two Sorted Arrays 2 3 6 2 8 11

Merging Two Sorted Arrays 2 3 6 8 3 6 2 8 12

Merging Two Sorted Arrays 2 3 6 8 3 6 2 8 12

Merging Two Sorted Arrays MERGE (A, l, m, r) { n 1 = m-l+1

Merging Two Sorted Arrays MERGE (A, l, m, r) { n 1 = m-l+1 //size of left subarray n 2 = r-m //size of right subarray Let, L[n 1] and R[n 2] be two auxiliary arrays for (i = 0 to n 1) L[i] = A[l+i] for (j = 0 to n 2) R[j] = A[m+1+j] i = 0, j = 0, k = l while (i < n 1 && j < n 2) { if (L[i] < R[j]) { A[k] = L[i]; i++; } else { A[k] = R[j]; j++; } k++; } Copy remaining elements of L[ ] or R[ ] to A[ ] if any } L A 36 R 28 3 6 2 8

Merge Sort Complexity: Recursion Tree

Merge Sort Complexity: Recursion Tree

Merge Sort Complexity: Recursion Tree • Proposition. If T (n) satisfies the following recurrence,

Merge Sort Complexity: Recursion Tree • Proposition. If T (n) satisfies the following recurrence, • then T (n) = n log 2 n. T (n) T (n / 2) T (n / 4) n = n 2 (n/2) = n 4 (n/4) = n 8 (n/8) = n log 2 n T (n / 8) T (n / 8) ⋮ ⋮ T(n) = n log 2 n