DAA Notes Unit II-divide and Conquer (V+ Edition)
DAA Notes Unit II-divide and Conquer (V+ Edition)
com
Divide-and-Conquer
The most-
most-well known algorithm design strategy:
1. Divide instance of problem into two or more smaller
instances
.
Design and Analysis of Algorithms Unit III
a problem of size n
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
.
Design and Analysis of Algorithms Unit III
www.Vidyarthiplus.com
1
www.Vidyarthiplus.com
Design and Analysis of Algorithms Unit 3
Divide-and-Conquer Examples
I Sorting: mergesort and quicksort
I Closest-
Closest-pair and convex-
convex-hull algorithms
.
Design and Analysis of Algorithms Unit III
4T(n/2) + n ⇒ T(n) ∈ ?
Examples: T(n) = 4T
4T(n/2) + n2 ⇒ T(n) ∈ ?
T(n) = 4T
4T(n/2) + n3 ⇒ T(n) ∈ ?
T(n) = 4T
.
Design and Analysis of Algorithms Unit III
www.Vidyarthiplus.com
2
www.Vidyarthiplus.com
Design and Analysis of Algorithms Unit 3
Mergesort
I Split array A[0..n
A[0..n-1] in two about equal halves and make
copies of each half in arrays B and C
I Sort arrays B and C recursively
I Merge sorted arrays B and C into array A as follows:
• Repeat the following until no elements remain in one of
the arrays:
– compare the first elements in the remaining
unprocessed portions of the arrays
– copy the smaller of the two into A, while
incrementing the index indicating the unprocessed
portion of that array
• Once all elements in one of the arrays are processed,
copy the remaining unprocessed elements from the other
array into A.
.
Design and Analysis of Algorithms Unit III
Pseudocode of Mergesort
.
Design and Analysis of Algorithms Unit III
www.Vidyarthiplus.com
3
www.Vidyarthiplus.com
Design and Analysis of Algorithms Unit 3
Pseudocode of Merge
.
Design and Analysis of Algorithms Unit III
Mergesort Example
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
.
Design and Analysis of Algorithms Unit III
www.Vidyarthiplus.com
4
www.Vidyarthiplus.com
Design and Analysis of Algorithms Unit 3
Analysis of Mergesort
.
Design and Analysis of Algorithms Unit III
Quicksort
A[i]≤p A[i]≥p
I Exchange the pivot with the last element in the first (i.e., ≤)
subarray — the pivot is now in its final position
I Sort the two subarrays recursively
.
Design and Analysis of Algorithms Unit III
www.Vidyarthiplus.com
5
www.Vidyarthiplus.com
Design and Analysis of Algorithms Unit 3
Partitioning Algorithm
.
Design and Analysis of Algorithms Unit III
Quicksort Example
5 3 1 9 8 2 4 7
.
Design and Analysis of Algorithms Unit III
www.Vidyarthiplus.com
6
www.Vidyarthiplus.com
Design and Analysis of Algorithms Unit 3
Analysis of Quicksort
I Best case: split in the middle — Θ(n log n)
I Worst case: sorted array! — Θ(n2)
I Average case: random arrays — Θ(n log n)
I Improvements:
• better pivot selection: median of three partitioning
• switch to insertion sort on small subfiles
• elimination of recursion
These combine to 20-
20-25% improvement
Binary Search
Very efficient algorithm for searching in sorted array:
array:
K
vs
A[0] . . . A[m
A[m] . . . A[n
A[n-1]
If K = A[m
A[m], stop (successful search); otherwise, continue
searching by the same method in A[0..m
A[0..m-1] if K < A[m
A[m]
and in A[m
A[m+1..n
+1..n-1] if K > A[m
A[m]
l ← 0; r ← n-1
while l ≤ r do
m ← ⎣( )/2⎦
⎣(l+r)/2⎦
if K = A[m
A[m] return m
A[m] r ← m-1
else if K < A[m
else l ← m+1
return -1
.
Design and Analysis of Algorithms Unit III
www.Vidyarthiplus.com
7
www.Vidyarthiplus.com
Design and Analysis of Algorithms Unit 3
.
Design and Analysis of Algorithms Unit III
Efficiency: Θ(n)
.
Design and Analysis of Algorithms Unit III
www.Vidyarthiplus.com
8