Download Data structures 4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data structures
Data structures for disjoint sets
A given set S is divided into disjoint subsets S1, ,Sm.
We want to perform the following operations:
• MakeSet(X) - make new set consisting of single item X
• Find(X) - find set to which item X belongs
• Union(S,T) - merge items of sets S and T into single
set ST
Data structures
Data structures for disjoint sets
S1
a
b
c
d
S2
f
g
h
e
Data structures
Data structures for disjoint sets
S1
a
S2
f
g
h
b
c
d
e
Data structures
Up-Trees
S1
S2
a
b
S1S2
a
b
Data structures
Up-Trees
S
a
b
k
f
d
j
c
h
g
i
e
Data structures
Up-Trees - Summary
MakeSet
(1)
Find
(h)
Union
(1)
Data structures
Up-Trees - Union
S1
S2
a
d
c
e
h
g
s
t
u
z
Data structures
Up-Trees - Union - 1
S1S2
a
d
c
e
h
g
s
t
u
z
Data structures
S1S2
Up-Trees - Union - 2
s
a
d
c
t
u
z
e
h
g
It turns out that it is better to add a tree
with a smaller number of vertices under
a tree with a larger number of vertices.
Data structures
Up-Trees - Height
Theorem
Let T be an Up-Tree of size n. Then the height of T is
at most log n.
Data structures
Up-Trees - Height
|T|=1
|T|=2


Height(T) = 0
Height(T) = 1
Height(T)  h

| T |  2Height(T)
| T | = | T1 | + | T2 | 
h
h+1
 2 | T2 |  2h + 1
T1
T2
h
Data structures
Up-Trees - Path Compression
a
b
T1
T2
c
d
T3
T4
T5
T6
a
b
T1
T7
c
d
T2
T3
T4
T5
T6
T7
Data structures
Up-Trees - Path Compression
Theorem
Let T be an Up-Tree with elements from set {1,,n} for which
Find operations perform also path compression.
Then any sequence of m  n of the operations MakeSet,
Find and Union takes total time O(m log n).
Data structures
Iterated Logarithm
F(i) = 2F(i–1) for all i > 0
F(0) = 1
F(1) = 21 = 2
F(2) = 22 = 4
F(3) =
2
2
2
F(4) =
22
2
2
= 16
2
2
2
F(5) = 2
= 65536
2
= 265536  1019728
All “practical” numbers are smaller than F(5)
Data structures
Iterated Logarithm
log n = the smallest i such that F(i)  n =
= the smallest i such that log loglog n  1.
i times
log n  5 for all “practical” numbers n
Data structures
Up-Trees - Path Compression
O1,,Om - sequence of operations MakeSet,
Find and Union.
T- resulting forest if only MakeSet and Union operations
have been used.
level(v) - height of v in T,
Lemma 1
There are at most n/2l nodes at level l in T.
Data structures
Up-Trees - Path Compression
Lemma 2
If node w is a descendant of v during the execution of
O1,,Om then w is a descendant of v in T, hence
level(w) < level(v).
G(v) = log level(v)
We say that v belongs to group G(v)
Lemma 3
G(v)  log n for each node v.
Data structures
Up-Trees - Path Compression
F - time needed to perform Find operations
F = all Finds Oi |Xi|,
where - Xi - set of all nodes
traversed during the execution of Oi
If v is not root then pi(v) - the parent of v during the execution
of Oi
Data structures
Up-Trees - Path Compression
We split Xi into three subsets:
Yi = {v  Xi: v is a root or a child of a root during Oi}
Zi = {v  Xi: v is moved during Oi and G(v) < G(pi(v))}
Wi = {v  Xi: v is moved during Oi and G(v) = G(pi(v))}
|Yi|  2 and |Zi|  log n (by L3), thus
F = all Finds Oi (|Yi| + |Zi| + |Wi|) = m (2 + log n) + all Finds Oi |Wi|

F
Data structures
Up-Trees - Path Compression
F =
all Finds Oi |Wi|
F =
all nodes v (the number of Oi such that v  Wi)
v - node at level l, then G(v) = log l = g
How many times a node can be moved before its parent is in
higher group than node itself?
(L2) What are the maximum number of different numbers k such
that log l = log k ? Answer: at most F(g)
F 
g = 0
log n
(the number of nodes in group g) · F(g)
Data structures
Up-Trees - Path Compression
F 
g = 0
log n
(the number of nodes in group g) · F(g)
(L1) The number of nodes in group g > 0 is at most
l = F(g–1)+1
F(g)
n / 2l  (n / (2F(g–1)+1)) · (1 + 1/2 + 1/4 + ) =
= n / F(g)
Also the number of nodes in group 0 is at most n / F(0) = n
Data structures
Up-Trees - Path Compression
F 
g = 0
log n
(the number of nodes in group g) · F(g)
The number of nodes in group g is at most n / F(g)
F 
g = 0
log n
(n / F(g)) · F(g) = O(n log n) = O(m log n)
F = F + m (2 + log n) = O(m log n) + m (2 + log n) =
= O(m log n)
Data structures
Ackerman’s Function
A(1, j ) = 2j
for j  1
A(i, 1 ) = A(i – 1, 2 )
for i > 1
A(i, j ) = A(i – 1, A(i,j – 1)) for i,j > 1
The “inverse” of Ackerman’s function (for m  n):
(m,n) = the smallest i  1 such that A(i, m/n ) > log n
(m,n)  4 for all “practical” m and n values
Related documents