Beruflich Dokumente
Kultur Dokumente
Conquer the subproblems by solving them recursively. Combine the solutions to the subproblems into the solutions for the original problem.
VANDANA M. LADWANI 2
3.
a solution to subproblem 1
a solution to subproblem 2
An Example: Calculating a0 + a1 +
ALGORITHM RecursiveSum(A[0..n-1])
+ an-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]) )
k Efficiency: (for n = 2 )
T(n) = aT(n/b) + f (n), where f (n) (nk) k 1. a < b T(n) (nk) k 2. a = b T(n) (nk lg n ) k 3. a > b T(n) (nlog b a) Where a 1,b>1,k the0 spent on dividing the problem time
into smaller ones and combining their solutions.
VANDANA M. LADWANI 5
Sorting: mergesort and quicksort Tree traversals Binary search Matrix multiplication-Strassen s algorithm
VANDANA M. LADWANI
Mergesort
Algorithm:
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 each half 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, 2 4 5 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 index indicating the unprocessed portion of that array Once one of the arrays is exhausted, copy the remaining unprocessed elements from the other array into A.
VANDANA M. LADWANI 7
Mergesort Examples
83297154 72164
Efficiency?
Worst case: if the smaller element comes from alternating arrays.
VANDANA M. LADWANI
10
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 array is A[s] and each element of the second array is A[s]. (computing the index of s is part of partition.)
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 in its correct place after the partition is done, and the two subarrays have been sorted.
VANDANA M. LADWANI
11
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 to the pivot) and a sublist (a sublist whose elements are all greater than or equal to the pivot ) (See algorithm Partition in section 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
VANDANA M. LADWANI
A[i] p
12
Quicksort Example
15 22 13 27 12 10 20 25
VANDANA M. LADWANI
15
Efficiency of Quicksort
Based on whether the partitioning is balanced. Best case: split in the middle ( n log n)
C(n) = 2C(n/2) +
(n)
( n2) ( n log n)
VANDANA M. LADWANI
16
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 search key K //Output: An index of the array s element that is equal to K // or 1 if there is no such element
//l and r crosses over can t find K. //the key is found //the key is on the left half of the array // the key is on the right half of the array
17
VANDANA M. LADWANI
Binary Search
a Recursive Algorithm
ALGORITHM BinarySearchRecur(A[0..n-1], l, r, K)
//base case 1: l and r cross over can t find K if l > r return 1 else m (l + r) / 2 //base case 2: K is found if K = A[m] return m //general case: divide the problem. else if K < A[m] //the key is on the left half of the array return BinarySearchRecur(A[0..n-1], l, m-1, K) else //the key is on the left half of the array return BinarySearchRecur(A[0..n-1], m+1, r, K)
VANDANA M. LADWANI 18
VANDANA M. LADWANI
19
Definitions
A binary tree T is defined as a finite set of nodes that is either empty or consists of a root and two disjoint binary trees TL and TR called, respectively, the left and right subtree of the root.
Examples.
The height of a tree is defined as the length of the longest path from the root to a leaf.
C(n) = n + x =2n + 1
VANDANA M. LADWANI 21
Exercises
The 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 if T = return 0 else return LeafCounter(TL)+ LeafCounter(TR)
VANDANA M. LADWANI 22
Binary tee traversal: visit all nodes of a binary tree recursively. Write a recursive preorder binary tree traversal algorithm.
Algorithm Preorder(T) //Implement the preorder traversal of a binary tree //Input: Binary tree T (with labeled vertices) //Output: Node labels listed in preorder if T write label of T s root Preorder(TL) Preorder(TR)
23
Multiplication of two n-digit integers. # of digit multiplications 23 * 14 (n = 2) 123 * 456(n = 3) Multiplication of 2 n-bit integers 4 9 n2 # of additions 3 8 n2-1
Multiplication of a m-digit integer and a n-digit integer ( where n > m) can be modeled as the multiplication of 2 n-digit integers (by padding n m 0s before the first digit of the m-digit integer)
VANDANA M. LADWANI
24
Assume n is an even number. (What about the situations where n is an odd number?) Recursively compute the product of two integers, with the integer size (# of digits) reduced by half each time.
Recurrence Relation
VANDANA M. LADWANI
25
Brute-force algorithm
c00 c10 c01 = c11 a10 a11 a00 a01 * b10 b11 a00 * b01 + a01 * b11 a10 * b01 + a11 * b11 b00 b01
8 multiplications 4 additions
VANDANA M. LADWANI
26
m1 + m4 - m5 + m7 = m2 + m4
m1 = m2 = m3 = m4 = m5 = m6 =
(a00 + a11) * (b00 + b11) (a10 + a11) * b00 a00 * (b01 - b11) a11 * (b10 - b00) (a00 + a01) * b11 (a10 - a00) * (b00 + b01)
7 multiplications 18 additions
27
Two nxn matrices, where n is a power of two (What about the situations where n is not a power of two?)
C00 C10 C01 = C11 A10 A11 A00 A01 * B10 B11 M3 + M5 M1 + M3 - M2 + M6 B00 B01
M1 + M4 - M5 + M7 = M2 + M4
VANDANA M. LADWANI
28
Submatrices:
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)
VANDANA M. LADWANI 29
If n is not a power of 2, matrices can be padded with rows and columns with zeros Number of multiplications Number of additions Other algorithms have improved this result, but are even more complex
VANDANA M. LADWANI 30