Link¨opings universitet Institutionen f¨or datavetenskap / Matematiska institutionen

advertisement
Linköpings universitet
Institutionen för datavetenskap / Matematiska institutionen
Christoph Kessler / Kaj Holmberg
Tentamen
TDDB56 DALGOPT-D Algoritmer och Optimering
TTIT33 DALGOPT-IT Algoritmer och Optimering
9 april 2002, kl 8-13
Preliminary solution proposal, DALG part
DALG assignment #1:
1. SkipListDeleteMin ( SkipList
) : SkipListItem
Header( ) // the artificial header element
.next[0]; // first element has a minimum key
do
for from 0 to height .next .next return
2. , which is supposed to be 3.
next-pointer updates (# iterations of the for loop)
4. An item has height
updates.
with probability
!#"%$ .
Deleting an item of height
'& !#")(+*,! & !#"%-*/...%0 352 1476 8!#" 3 09!
2;: <BA ?)C D ? - and ? A C ! we have 2 : 8!#" 3 09! .
because with < 476>=@?
35476
E
Hence, the average case time complexity of SkipListDeleteMin is .
requires
next-pointer
hence expected # pointer updates =
5. The easiest way is to maintain an additional pointer last to the last element in the skip list, which
contains a maximum key because the skip list is sorted in increasing order of keys. FindMin then just
returns that pointer, which obviously takes constant time.
The operation Insert(i) must be modified such that last is updated to point to if happens to end up
as the last (w.r.t. next[0]-linkage) element in the skip list. Note that this also covers the case where
the list was empty.
Delete must update last if the element being deleted is the one pointed to by last. In that case, last is
set to its predecessor wrt. next[0] linkage, which is one of the flagged items in the deletion process.
DeleteMin needs modify last only if the list contained only a single element.
LookUp needs not be modified as it does not change the list.
1
DALG assignment #2:
2
3
7
11
5
13
17
(a) array representation: [ 2, 3, 5, 7, 11, 13, 17, 19 ], tree representation 19
(b) after DeleteMin: [ 3, 7, 5, 19, 11, 13, 17 ] plus tree representation
(c) after Insert(1): [ 1, 2, 5, 3, 11, 13, 17, 19, 7 ] plus tree representation
DALG assignment #3:
The hash table contains the entries [0, 1, 12, 11, 14, 22, 29, 7, empty, 9 ],
Lookup(29) probes 8 times.
F H G F G
F HG F
I F7J
K
L
F
0
F
I G F)J & K
LF LM K
F L& F M A K
F . NO
F is as big as possible if NO
F '&
(b) We have to find a function NO
F such that NO
F
M
P
M
if NO
F
G F MRQ C F , i.e.,
F C F . Hence, NO
F F C G LF is such a solution, and we have
F F F because LF grows asymptotically slower than F .
DALG assignment #4:
(a) We apply heap sort. As the first
elements are already sorted, we can interpret these first
table entries as a heap. Now we insert the last
elements in the heap one by another, each of which
as the heap has
elements in all cases. Hence, the total worst-case time complexity
takes time
is
.
DALG assignment #5:
(a) no. There are two unary nodes.
(b) no. Level 3 is not filled completely, neither is the last level filled properly from left to right.
S[Z TVEUX\ WY!!@W [Z W E\ WYT!]W Z_^ \ W`U Z]\ W`UaZ TXW ZXZ ^ W Z ^!@^aW b ^^ab
inorder: S W WY!!@WYT!@WYTUXWcU#T@W`U W W !@W W
E\ [Z
Z ZX \ ^_^ Z_^ b
postorder: S W WYT!@WY!!@WcUaT]W !@W W`U W W WdTVU
(c) no. The node with key 56 is out of balance (-2).
(d) preorder:
2
Download