


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



University of South Carolina
02/23/2009 2
Please download it from class website
Have your answers neatly typed
Explain your answer clearly and adequately by
showing the steps
02/23/2009 3
L
R
T L (^) T R
02/23/2009 4
For analysis convenience, we can extend binary trees by
replacing empty subtrees by special nodes
Internal and external nodes:
#external_node = #internal_node+
: internal
: external
02/23/2009 5
Recurrence
Æ If the basic operation is the line to check whether a tree
elsereturnmax{ ( ), ( )} 1
if return 1
L R HeightT HeightT
HeightT
φ
A nT AnT AnT nT L r for
02/23/2009 6
List all the nodes
Preorder traversal : root Æ left subtree Æ right subtree
Inorder traversal : left subtree Æ root Æ right subtree
Postorder traversal : left subtree Æ right subtree Æ root
What is the efficiency?
output
if
R
L
root
preorderT
preorderT
preorderT
≠ φ
02/23/2009 7
02/23/2009 8
Some applications, notably modern cryptology, require
manipulation of integers over 100 decimal digits long
Such integers are too long to fit a single word of a
computer; therefore, they require special treatment
Consider the multiplication of two such long integers
If we use the classic pen-and-pencil algorithm to
2 digit
multiplications
Can we design a divide-and-conquer algorithm to solve
this problem with better efficiency?
02/23/2009 9
We want to calculate 23 x 14
Since
We have
2 )
But
Therefore, we only need three digit multiplications
1 0 1 0 23 = 2 ⋅ 10 + 3 ⋅ 10 and 14 = 1 ⋅ 10 + 4 ⋅ 10
2 1 0
1 0 1 0
02/23/2009 10
1
0
1
0
We have
That means only three digit multiplications are needed
to multiply two 2-digit integers
1 1 0 1 0 2 0
0 0 0
2 1 1
0 0
1 1
2 2
c a a b b c c
c a b
c a b
c ab c c c
where
02/23/2009 11
Then
To calculate the involved three multiplications –
0
/ 2 0 1
/ 2 1 a a 10 a b b 10 b
n n = + and = +
1 1 0 1 0 2 0
0 0 0
2 1 1
0 0
/ 2 2 1
c a a b b c c
c a b
c a b
c ab c c c
n n
where
02/23/2009 12
The recurrence relation is
k yields
Therefore,
T ( n )= 3 T ( n / 2 ) for n > 1 , T ( 1 )= 1
k kk k
k k k
1 2 2
−
− −
log 2 log 23 1. 585 2
n
02/23/2009 19
Closest Pair by Divide-and-
Conquer (cont.)
Step 2 Find recursively the closest pairs for the left and right
subsets.
Step 3 Setd = min{d 1 ,d 2 }
We can limit our attention to the points in the symmetric vertical strip of width 2d as possible closest pair. Let C 1 and C 2 be the subsets of points in the left subset S 1 and of the right subset S 2 , respectively, that lie in this vertical strip. The points in C 1 and C 2 are stored in increasing order of theiry coordinates, which is maintained by merging during the execution of the next step.
Step 4 For every pointP(x,y) in C 1 , we inspect points in
C 2 that may be closer toP thand. There can be no more than 6 such points (becaused ≤ d 2 )!
02/23/2009 20
Closest Pair by Divide-and-
Conquer: Worst Case
The worst case scenario is depicted
below:
02/23/2009 21
Efficiency of Closest-Pair Algorithm
Running time of the algorithm is described by
T(n) = 2T(n/2) + M(n), where M(n) ∈ O(n)
By the Master Theorem (witha = 2,b = 2,d = 1)
T(n) ∈ O(n logn)