ada chap 4
TRANSCRIPT
-
8/3/2019 ADA CHAP 4
1/30
-
8/3/2019 ADA CHAP 4
2/30
VANDANA M. LADW ANI
Three Steps ofThe Divide and
Conquer Approach
The most well known algorithm design strategy:
1. Divide the problem into two or more smaller
subproblems.
2. Conquer the subproblems by solving themrecursively.
3. Combine the solutions to the subproblemsinto the solutions for the original problem.
-
8/3/2019 ADA CHAP 4
3/30
VANDANA M. LADWANI 3
ATypical Divide and Conquer Case
subproblem 2
of size n/2
subproblem 1
of size n/2
a solution to
subproblem 1
a solution tothe original problem
a solution to
subproblem 2
a problem of size n
-
8/3/2019 ADA CHAP 4
4/30
VANDANA M. LADW ANI
An Example: Calculating a0
+ a1
+ + an-1
Efficiency: (for n = 2k)
A(n) = 2A(n/2) + 1, n >1 A(1) = 0;
ALGORITHM RecursiveSum(A[0..n-1])//Input: An array A[0..n-1] of orderable elements
//Output: the summation of the array elements
if n > 1
return ( RecursiveSum(A[0.. n/2 1]) + RecursiveSum(A[n/2 .. n 1]) )
-
8/3/2019 ADA CHAP 4
5/30
VANDANA M. LADW ANI
General Divide and Conquer recurrence
The Master Theorem
T(n) = aT(n/b) + f (n), where f (n) (nk)
1. a < bk T(n) (nk)
2. a = bk T(n) (nk lg n )
3. a > bk T(n) (nlog b a)
Where a1,b>1,k 0
the time spent on solving a subproblem of size n/b.
the time spent on dividing the problem
into smaller ones and combining their solutions.
-
8/3/2019 ADA CHAP 4
6/30
VANDANA M. LADW ANI
Divide and Conquer Examples Sorting: mergesort and quicksort
Tree traversals
Binary search
Matrix multiplication-Strassens algorithm
-
8/3/2019 ADA CHAP 4
7/30
VANDANA M. LADW ANI
MergesortAlgorithm: The base case: n = 1, the problem is naturally solved.
The general case: Divide: Divide array A[0..n-1] in two and make copies of eachhalf in
arrays B[0.. n/2 - 1] and C[0.. n/2 - 1]
Conquer: Sort arrays B and C recursively using merge sort
Combine: Merge sorted arrays B and C into array A as follows:(Example: 1 3 7, 245 9 )
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 indexindicating the unprocessed portion of that array
Once one of the arrays is exhausted, copy the remaining unprocessed
elements from the other array into A.
-
8/3/2019 ADA CHAP 4
8/30
VANDANA M. LADW ANI
The MergesortAlgorithmALGORITHM Mergesort(A[0..n-1])//Sorts array A[0..n-1] by recursive mergesort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in nondecreasing order
if n > 1
//divide
copy A[0.. n/2 1] to B[0.. n/2 1]
copy A[ n/2 .. n 1] to C[0.. n/2 1]
//conquer
Mergesort(B[0.. n/2 1)
Mergesort(C[0.. n/2 1)
//combine
Merge(B, C, A)
-
8/3/2019 ADA CHAP 4
9/30
VANDANA M. LADWANI 9
The Merge AlgorithmALGORITHM Merge(B[0..p-1], C[0..q-1], A[0..p+q-1])//Merges two sorted arrays into one sorted array
//Input: Arrays B[0..p-1] and C[0..q-1] both sorted
//Output: Sorted Array A[0..p+q-1] of the elements of B and Ci 0; j 0; k 0
while i < p and j < q do//while both B and C are not exhausted
if B[i]
-
8/3/2019 ADA CHAP 4
10/30
VANDANA M. LADWANI 10
Mergesort Examples 8 3 2 9 7 1 54
72 1 64
Efficiency?
Worst case: if the smaller element comes from alternating arrays.
-
8/3/2019 ADA CHAP 4
11/30
VANDANA M. LADWANI 11
The Divide, Conquer and
Combine Steps in Quicksort Divide: Partition array A[l..r] into 2 subarrays, A[l..s-1]
and A[s+1..r] such that each element of the first arrayis A[s] and each element of the second array is A[s].(computing the index of s is part of partition.) Implication: A[s] will be in its final position in the sorted array.
Conquer: Sort the two subarrays A[l..s-1] and A[s+1..r]by recursive calls to quicksort
Combine: No work is needed, because A[s] is already inits correct place after the partition is done, and the two
subarrays have been sorted.
-
8/3/2019 ADA CHAP 4
12/30
VANDANA M. LADWANI 12
Quicksort Select a pivot w.r.t. whose value we are going to divide the
sublist. (e.g., p = A[l])
Rearrange the list so that it starts with the pivot followed by a
sublist (a sublist whose elements are all smaller than or equal tothe pivot) and a sublist (a sublist whose elements are allgreater than or equal to the pivot ) (See algorithm Partition insection 4.2)
Exchange the pivot with the last element in the first sublist(i.e., sublist) the pivot is now in its final position
Sort the two sublists recursively using quicksort.
p
A[i]p A[i] p
-
8/3/2019 ADA CHAP 4
13/30
VANDANA M. LADWANI 13
The QuicksortAlgorithmALGORITHM Quicksort(A[l..r])//Sorts a subarray by quicksort
//Input: A subarray A[l..r] ofA[0..n-1],defined byits left and right indices l and r//Output: The subarray A[l..r] sorted in
nondecreasing order
if l < rs Partition (A[l..r])// s is a split positionQuicksort(A[l..s-1])Quicksort(A[s+1..r]
-
8/3/2019 ADA CHAP 4
14/30
VANDANA M. LADWANI 14
The Partition AlgorithmALGORITHM Partition (A[l ..r])
//Partitions a subarray by using its first element as a pivot
//Input: A subarray A[l..r] ofA[0..n-1], defined by its left and right indices
l and r (l < r)//Output: A partition ofA[l..r], with the split position returned as this
functions value
P A[l]
i l; j r + 1;
Repeat
repeat i i + 1 until A[i]>=p//left-right scanrepeat j j 1 until A[j] = j //no need to scan
swap(A[l], A[j])
return j
-
8/3/2019 ADA CHAP 4
15/30
VANDANA M. LADWANI 15
Quicksort Example15 22 13 27 12 10 20 25
-
8/3/2019 ADA CHAP 4
16/30
VANDANA M. LADWANI 16
Efficiency of QuicksortBased on whether the partitioning is balanced.
Best case: split in the middle ( n log n)
C(n) = 2C(n/2) + (n) //2 subproblems of size n/2 each
Worst case: sorted array! ( n2)
C(n) = C(n-1) + n+1 //2 subproblems of size 0 and n-1 respectively
Average case: random arrays ( n log n)
-
8/3/2019 ADA CHAP 4
17/30
VANDANA M. LADWANI 17
Binary Search an Iterative Algorithm
ALGORITHM BinarySearch(A[0..n-1], K)//Implements nonrecursive binary search
//Input: An array A[0..n-1] sorted in ascending order and
// a searc h key K//Output: An index of the arrays element that is equal to K
// or 1 if there is no such element
l 0, r n 1
while l e r do //l and r crosses over cant find K.
m -(l + r) / 2if K = A[m] return m //the key is found
else
if K < A[m] r m 1 //the key is on the lefthalf of the array
else l m + 1 // the key is on the righthalf of the array
return -1
-
8/3/2019 ADA CHAP 4
18/30
VANDANA M. LADWANI 18
Binary Search a Recursive Algorithm
ALGORITHM BinarySearchRecur(A[0..n-1], l, r, K)if l > r //base case 1: l and r cross over cant find K
return 1
else
m -(l + r) / 2
if K = A[m] //base case 2: K is found
return m
else //general case: divide the problem.
if K < A[m] //the key is on the lefthalf of the array
return BinarySearchRecur(A[0..n-1], l, m-1, K)
else //the key is on the lefthalf of the array
return BinarySearchRecur(A[0..n-1], m+1, r, K)
-
8/3/2019 ADA CHAP 4
19/30
VANDANA M. LADWANI 19
Binary Search -- Efficiency What is the recurrence relation?
Efficiency
C(n) = C(-n / 2) + 2
C(n) ( log n)
-
8/3/2019 ADA CHAP 4
20/30
VANDANA M. LADW ANI 0
Binary Tree Traversals Definitions
Abinary tree T is defined as a finite set of nodes
that is either empty or consists of a root and twodisjoint binary trees TL and TR called, respectively,the left and right subtree of the root.
Examples.
The
heig
ht of a tree is defined as t
he lengt
hof t
helongest path from the root to a leaf.
Problem: find the height of a binary tree.
-
8/3/2019 ADA CHAP 4
21/30
VANDANA M. LADW ANI 1
Find the Height of a Binary TreeALGORITHM Height(T)//Computes recursively the height of a binary tree
//Input: A binary tree T
//Output: The height ofT
ifT =
return 1
else
return max{Height(TL), Height(TR)} + 1
Base case?
Efficiency? C(n) = n + x =2n + 1
-
8/3/2019 ADA CHAP 4
22/30
VANDANA M. LADW ANI
ExercisesThe following algorithm seeks to compute the
number of leaves in a binary tree. Is it correct?
ALGORITHM LeafCounter(T)//Computes recursively the number of leaves in a binary tree
//Input: A binary tree T
//Output: The number of leaves in T
ifT = return 0
else
return LeafCounter(TL)+ LeafCounter(TR)
-
8/3/2019 ADA CHAP 4
23/30
-
8/3/2019 ADA CHAP 4
24/30
VANDANA M. LADW ANI
Multiplication of Large Integers (1) Multiplication of two n-digit integers.
Multiplication of a m-digit integer and a n-digit integer ( where n >m) can be modeled as the multiplication of2 n-digit integers (by
padding n m 0s before the first digit of the m-digit integer)
# of digitmultiplications
# of additions
23 * 14 (n = 2) 4 3
123 * 456(n = 3) 9 8
Multiplication of2n-bit integers
n2 n2-1
-
8/3/2019 ADA CHAP 4
25/30
VANDANA M. LADW ANI
Multiplication of Large Integers (2)
Do we have to make n2 digit multiplications?
n = 2: we only need 3 digit multiplications.
What about n > 2? Assume n is an even number. (What about the situations where
n is an odd number?)
Recursively compute the product of two integers, with theinteger size (# of digits) reduced by half each time.
Efficiency of the recursive algorithm
Recurrence Relation
-
8/3/2019 ADA CHAP 4
26/30
VANDANA M. LADW ANI
Strassens Matrix Multiplication
Brute-force algorithmc00 c01 a00 a01 b00 b01
= *c10 c11 a10 a11 b10 b11
a00 * b00 + a01 * b10 a00 * b01 + a01 * b11=
a10
* b00
+ a11
* b10
a10
* b01
+ a11
* b11
8 multiplications
4 additions
Efficiency class: 5 (n3)
-
8/3/2019 ADA CHAP 4
27/30
VANDANA M. LADW ANI
Strassens Matrix Multiplication Strassens Algorithm (two 2x2 matrices)
c00 c01 a00 a01 b00 b01= *
c10 c11 a10 a11 b10 b11
m1 + m4 - m5+ m7 m3 + m5=
m2 + m4 m1 + m3 - m2+ m6 m1 = (a00 + a11) * (b00 + b11)
m2 = (a10 + a11) * b00 m3 = a00 * (b01 - b11)
m4 = a11 * (b10 - b00)
m5 = (a00 + a01) * b11 m6 = (a10 - a00) * (b00 + b01)
m7 = (a01 - a11) * (b10 + b11)
7 multiplications
18 additions
-
8/3/2019 ADA CHAP 4
28/30
VANDANA M. LADW ANI
Strassens Matrix Multiplication Two nxn matrices, where n is a power of two (What
about the situations where n is not a power of two?)
C00 C01 A00 A01 B00 B01= *
C10 C11 A10 A11 B10 B11
M1 + M4 - M5+ M7 M3 + M5=
M2 + M4 M1 + M3 - M2+ M6
-
8/3/2019 ADA CHAP 4
29/30
VANDANA M. LADW ANI 9
Submatrices: M1 = (A00 + A11) * (B00 + B11)
M2 = (A10 + A11) * B00
M3 = A00 * (B01 - B11)
M4 = A11 * (B10 - B00)
M5 = (A
00 +A
01) * B11
M6 = (A10 - A00) * (B00 + B01)
M7 = (A01 - A11) * (B10 + B11)
-
8/3/2019 ADA CHAP 4
30/30
VANDANA M. LADWANI 30
Efficiency of Strassens Algorithm
If n is not a power of2, matrices can bepadded with rows and columns with zeros
Number of multiplications
Number of additions
Other algorithms have improved this result,but are even more complex