a simple algorithm for stable minimum storage merging pok-son kim kookmin university, department of...
TRANSCRIPT
A Simple Algorithm for Stable Minimum Storage
Merging
Pok-Son KimKookmin University, Department of Mathematics,
Seoul 135-702, Korea
Arne KutznerSeokyeong University, Department of E-Business,
Seoul 136-704, Korea
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 2
Merging
• Make one sorted array out of two consecutive sorted arrays
4 91 3 92
3, 4 91, 92
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 3
Lower Bounds for Merging
• Number of comparisons
– Argumentation over the decision tree (see Knuth)
• Number of assignments
– Each element can change its position in the final sequence
)log(m
nm nm for
nm
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 4
Notions
• An algorithm merges two adjacent sequences with “minimum storage” when it needsbits additional space at most.
• Stability:Merging algorithm preserves the initial ordering of elements with equal value.
))((log2 nmO
We present .....
…a simple, asymptotically optimal (regarding
comparisons), stable, minimum storage merging algorithm
calledSPLITMERGE
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 6
SPLITMERGE: Step 1 - Splitting
• We compute boundaries in our inputs u and v so that we get the following property:
u1 u2 v1 v2
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 7
SPLITMERGE: Step 2 – Rotation
• We rotate u2v1 to v1u2
u1 u2 v1 v2
u1 u2v1 v2
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 8
SPLITMERGE: Step 3 – Recursive Application
• We have:1. u1 u2 and u1 v2
2. v1 u2 and v1 v2
u1 u2v1 v2
left recursion right recursion
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 9
Stability
• Lemma: SPLITMERGE is stable.
• Proof:
u1 u2 v1 v2
u1 u2v1 v2
>
rotation
!!!!
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 10
How do we split?
• Repeated halving of two search spaces until these are reduced to single points (the borders of a rotation).
• For asymmetric inputs one element of the smaller side “freezes” after some time
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 11
Symmetric Splitting – Graphical Description
u21 x u22 v21 y v22
search spaces
x y u22 v21
x y
u21 v22
x yx y
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 12
Splitting Example
98651 655201 8 80
98651 655201 8 80
98651 655201 8 80
98651 655201 8 80
?
?
?
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 13
Symmetric Splitting – Asymmetric Inputs (1)
xu1 u3 v1 v21 y v22 v3
single search space
• After some time one element “freezes” (here x)
• x can “belong” to u1, u3 or both
• x belongs to u1 means u1x v3
• x belongs to u3 means xu3 v1
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 14
Symmetric Splitting – Asymmetric Inputs (2)
xu1 u3 v1 v3
• Rotation – we distinguish two different cases
if x belongs to u3 :
xu1 u3v1 v3
otherwise :
xu1 u3v1 v3
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 15
Symmetric Splitting – Asymmetric Inputs (3)
• Recursion – we distinguish several cases
if x belongs to u1
xu1 u3v1 v3
otherwise
xu1 u3v1 v3
left Recursion right Recursion
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 16
Algorithm in Pseudo code
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 17
Some Remarks …• The “belongs to” information can be followed
from the variables l, r and m
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 18
Worst Case ComplexityLemma: The recursion depth is limited by
min , where
Corollary: Minimum Storage Algorithm
}1,loglog{ mnm
|||,| vnum
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 19
Worst Case Complexity (Comparisons)Special case m→ m/2 (1)
m10
n32m1
2 m22 m3
2 m42
m11 m2
1
n42n2
2n12
n11 n2
1
n10m
m/2 m/2
m/4 m/4 m/4 m/4
mk log1 1 1 1
recursion depth
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 20
Worst Case Complexity (Comparisons)Special case m→ m/2 (2)
m10
n32m1
2 m22 m3
2 m42
m11 m2
1
n42n2
2n12
n11 n2
1
n10 1)log( nm
1)log(1)log( 1
2
1
2
1
1
1
1 nmnm
4
1
2 )log(2j
ij
ij nm
0
1
2
i
j
ij
ij
i nm2
1)log(2
k
jk
j
k
j
k nm21 )log(2
i
k
recursion level
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 21
Worst Case Complexity (Comparisons)Special case m→ m/2 (3)
k
i j
ij
ij
ii
nm0
2
1))log(2(
Overall number of comparisons:
Mathematical Machinery
))1/log(( mnmO
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 22
Worst Case ComplexityHow to treat the other cases? (1)
Example: Maximum spanning case
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 23
Worst Case ComplexityHow to treat other cases? (2)
• Trick: Mapping into a corresponding“m → m/2 case” by the introduction of recursion groups– A recursion group consists of one or several
recursion levels – Grouping of recursion levels into
recursion groups so that each recursion group holds at most 2i-1 recursion levels.
– Altogether k+1 recursion groups
),2,1,0( kii
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 24
Worst Case ComplexityHow to treat other cases? (3)
• Mapping for the maximum spanning case:
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 25
Assignments / Worst Case Complexity
• Theorem:The SPLITMERGE algorithm needs assignments.
Sketch of Proof:At most k+1 recursion groups with O(m+n) many assignments for every group
)log)(( mnmO
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 26
Benchmarks
• n,m : lengths of input sequences (m=n)• i : number of different input elements• te : execution time in ms• #comp : number of comparisons
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 27
Summary
• Simply structured algorithm using a novel binary partition technique
• Doesn’t rely on any third algorithm as subpart
• Short definition in Pseudo Code• Asymptotically optimal regarding the
number of comparisons• Nice Textbook algorithm ?
SOFSEM 2007 A Simple Algorithm for Stable Minimum Storage Merging 28
Thank you very much foryour attention