Lectures On Recursive Algorithms: COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski
Lectures On Recursive Algorithms: COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski
Lectures On Recursive Algorithms: COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski
Recursive Algorithms
COMP 523: Advanced Algorithmic Techniques
Lecturer: Dariusz Kowalski
Solve the problem for half of the input q times, and spend
at most f(n) time for partitioning and/or merging
Lectures on Recursive Algorithms 5
Example 1:
1 2 3 4 5 6 7 8
8 leaves
root
2
odd, no majority
2 2 1 height = 2
2 2 2 2 1 2 1 1
8 leaves
T(n) T(n/2) + cn
(T(n/4) + cn/2) + cn = T(n/4) + (3/2)cn
(T(n/8) + cn/4) + (3/2)cn = T(n/8) + (7/4)cn
…
(T(n/2i) + cn/2i-1) + ((2i-1-1)/2i-2) . cn
= T(n/2i) + ((2i-1)/2i-1) . cn
…
T(n/2log n) + ((2log n-1)/2log n-1) . cn
= T(1) + (n – 1)/(n/2) . cn c + 2cn – 2c 2cn
Lectures on Recursive Algorithms 19
Case q = 1, f(n) = c n
formal analysis
T(n) T(n/2) + c n
T(1) c
Solution: T(n) 2cn
Proof: by induction.
– For n = 1 straightforward: T(1) c 2c 1
– Suppose T(n/2) 2c(n/2); then
T(n) T(n/2) + cn 2c(n/2) + c n
= 2cn
Lectures on Recursive Algorithms 20
Textbook and exercises
READING: Chapter 5, up to Section 5.2
EXERCISES:
• Solve the following recursive formulas the best you can:
– T(n) T(n/2) + 4
– T(n) T(n/2) + 5n
– T(n) T(n/2) + 3n2
– T(n) (3/2)T(n/2) + 1
Compare and sort the obtained (asymptotic) formulas according to the
asymptotic order. For each of them try to find an algorithmic example
with performance giving by this formula.
• Sort the following formulas according to big/small Oh order:
log (n1/2), log (9n), log (n3), 2log n, 23log n, 2log (9n), n2, n log n
• Propose and analyse an algorithm for checking if there is a majority in a
given sorted array A.
T(n) 2T(n/2) + cn
2(2T(n/4) + cn/2) + cn = 4T(n/4) + 2cn
4(2T(n/8) + cn/4) + 2cn = 8T(n/8) + 3cn
…
2i-1 (2T(n/2i) + cn/2i-1) + (i-1) . cn
= 2i T(n/2i) + i . cn
…
2log n - 1 T(2) + (log n - 1) . cn cn log n
Lectures on Recursive Algorithms 24
Solution for time
formal analysis
T(n) 2 T(n/2) + cn
T(2) c
Solution: T(n) cnlog n
Proof: by induction.
– For n = 2 straightforward: T(2) c c2log 2
– Suppose T(n/2) c(n/2)log (n/2); then
T(n) 2T(n/2) + cn 2c(n/2)log (n/2) + cn
= 2c(n/2)log n – 2cn/2 + cn = cnlog n
Lectures on Recursive Algorithms 25
Example - sorting by merging
• Input: list L of n numbers
• Problem: Sort list L
• Algorithm MergeSort(L):
If L has at most two elements then sort them by comparison and exit;
Else:
– Split L into two lists prefix and suffix, each of size n/2
– Sort, by merging, the prefix and the suffix separately:
MergeSort(prefix) and MergeSort(suffix)
– Merge sorted prefix with sorted suffix as follows:
• Initialize final list as empty
• Repeat until either prefix or suffix is empty:
– Compare the first elements on the lists prefix and suffix and then move the smaller one to the
end of the final list
• Concatenate final list with the remaining non-empty list (prefix or suffix)
1 2 4 7 8
3 5 6 9 10
1 2 3 4 5 6 7 8 9 10
merging operations
height = 3
2 4 1 7 8 3 6 5
8 leaves
In other words: divide and conquer algorithm must store the binary
tree of pointers during the computation
Lectures on Recursive Algorithms 29
Quick sort - algorithmic scheme
Generic Quick Sort:
• Select one element x from the input
• Partition the input into part containing elements not
greater than x and part containing elements bigger than x
• Sort each part separately
• Concatenate these two sorted parts
Randomized:
• Select separation element x uniformly at random
• Time (expected): O(n log n), additional memory O(n)
Lectures on Recursive Algorithms 32
Randomized approach - analysis
Let T(n) denote the expected time: sum of all possible
values of time weighted by the probabilities of these values
T(n) 1/n ([T(n-1)+T(1)] + [T(n-2)+T(2)] + … +[T(0)+T(n)]) + cn
T(0) = T(1) = 1, T(2) c
Solution: T(n) d n log n, for some constant d 8c
Proof: by induction.
– For n = 2 straightforward: T(2) c d . 2 . log 2
– Suppose T(m) d m log m, for every m < n; then
(1-1/n)T(n) (2/n)(T(0) + … + T(n-1)) + c n
(2d/n)(1 + 1 + 2log 2 + … + (n-1)log(n-1)) + c n
(2d/n)((n2/2) log n – n2/8) + c n
d n log n - d (n/4) + c n d n log n - d n/2
5 7
height = 5
3
2 4
1 2 3 4 5 6 7 8
8 leaves
Lectures on Recursive Algorithms 34
Textbook and Exercises
READING: Section 5.3
EXERCISES:
• Solved exercises 1, 2 from the textbook,
chapter 5 “Divide and Conquer”
• Prove that
1 + 1 + 2 log 2 + … + (n-1)log(n-1) n(n/2) log(4n) , and
(n/(n-1))(d n log n - d n/2) d n log n , for n > 16
Remarks:
• Exhaustive search algorithm gives time O(n2)
• Similar approach gives ALL pairs of closest points
Lectures on Recursive Algorithms 37
Solution
Preprocessing:
• Sort points according to the first coordinate (obtain horizontal
list H) and according to the second coordinate (obtain vertical
list V)
Main Algorithm:
• Partition input list H into halves (H1,H2) in linear time (draw vertical line L
containing medium point); split list V accordingly into V1,V2, where Vi
contains the same elements as Hi and inherits the initial order from V (for i
= 1,2)
• Solve the problem for each half of the input separately, obtaining two pairs
of closest points; let be the smallest distance from the obtained ones
• Find if there is a pair which has distance smaller than - if yes then find
the smallest distance pair
• Naïve Algorithm:
– Multiply each bit i of x by y, add (i-1) zeroes at the end
– Add the obtained at most n numbers
• Time: (n2) of bit operations
Lectures on Recursive Algorithms 44
Divide and Conquer approach
Let x = x1 + 2n/2 x2 and y = y1 + 2n/2 y2
Let p = x1 + x2 and q = y1 + y2. Then
x y = (x1 + 2n/2 x2) (y1 + 2n/2 y2)
= x1 y1 + 2n/2 (x2 y1+ x1 y2) + 2n x2 y2
= x1 y1 + 2n/2 (pq - x1 y1- x2 y2) + 2n x2 y2
Algorithm:
– Compute p and q (in linear time)
– Compute recursively: x1 y1 , x2 y2 , pq
– Perform x1 y1 + 2n/2 (pq - x1 y1- x2 y2) + 2n x2 y2 in linear time