


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!



03/06/2009 2
03/06/2009 3
Solve problem by transforming into:
ย presorting ย Gaussian elimination
ย balanced search trees ย heaps and heapsort ย polynomial evaluation by Hornerโs rule ย Fast Fourier Transform
ย reductions to graph problems ย linear programming 03/06/2009 4
03/06/2009 5
ย minimum: k = 1 ย maximum: k =n ย median: k = โก n/2โค ย Presorting-based algorithm ย sort list (e.g., using mergesort) ย return A[k] ย Partition-based algorithm (Variable-size-decrease and conquer): ย pivot/split at A[s] using partitioning algorithm from quicksort ย ifs=k return A[s] ย else ifs<k repeat with sublist A[s+1],โฆA[n]. ย else if s>k repeat with sublist A[1],โฆA[s-1].
03/06/2009 6
ย Presorting-based algorithm: ฮฉ(n logn) + ฮ(1) = ฮฉ(n logn)
ย Conclusion: Presorting doesnot help in this case.
03/06/2009 7
ย use mergesort (optimal): ฮ(n logn)
elements: ฮ(n)
ฮ ฮ((n lognlognn))
03/06/2009 8
ย Amode is the value that occurs most often in a given
distinct elements. If a matching is found, its frequency is incremented by 1. Otherwise, current element is added to the list as a distinct element
distinctdistinct
03/06/2009 9
modevalue
i i runlength
modefrequency runlengthmodevalue runvalue
runlength modefrequency
runlength runlength
i runlength n Ai runlength runvalue
runlength runvalue Ai
i n
modfrequency
i
PresortModeA n
return
if
while and
while do
0 highestfrequencyseensofar
Sort thearray
03/06/2009 10
03/06/2009 11
ย Brute-force search: worst case ฮ(n).
ย Presorted search T(n)=Tsort(n)+ Tsearch (n)
= ฮ(n logn)+ ฮ(logn) = ฮ(n logn)
03/06/2009 12
ย sequential search ย binary search ย binary tree search
ย AVL trees ย red-black trees ย multiway balanced trees (2-3 trees, 2-3-4 trees, B trees)
ย separate chaining ย open addressing
03/06/2009 19
03/06/2009 20
ย Height (h) of an AVL tree withn nodes is bounded by lgn โค h โค 1.4404 lg (n + 2) - 1. average: 1.01 lgn + 0.1 for largen
tree take ฮ(logn) in the worst case
03/06/2009 21
5
6
0
5
0 5
6
8
0
6
0
8
0 5
0
L (5)
6
1
5
1
3
0
8
0
6
2
5
2
3
1
2
0
8
0 >
R (5)
6
1
3
0
2
0
8
0
5
0
03/06/2009 22
6
2
3
2
0 5
1
4
0
8
0 >
LR (6)
5
0
3
0
2
0 4
0
6
8
0
5
3
0
2
0 4
0
6
8
1
7 0
>
RL (6)
5
0
3
0
2
0 4
0
7
0
8
0 6
0