Algorithm Homework Help
Algorithm Homework Help
Problem 1. Seeing the Forest for the van Emde Boas Trees
Solution:
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
BIB-MIN is the simplest operation, if we are trying to get the global
min: we simply return the value that is already stored. (Here, we use
V.root to represent the structure storing metadata, which is not
recursive and only exists at the top level.)
BIB-MIN(V ) :
1 return V.root.min
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
In the code for BIB-INSERT, we define the base case to be when the
size of the universe is 2; any small constant would suffice here.
The base case Bib Tree structure (the leaves of the tree) is distinct
from that of the upper level ones; rather than having an array of
clusters and a summary Bib Tree, it stores only a bit array which
indicates the presence (or absence) of the elements governed by
it.
BIB-INSERT(V, x)
1 if V.u == 2
2 V.A[x] = 1
3 else
4 BIB-INSERT(V.cluster[high(x)], low(x))
5 BIB-INSERT(V.summary, high(x))
BIB-RECURSIVE-MIN(V ) :
1 if V.u == 2
6 if V.A[0] == 1
7 return 0
8 elseif V.A[1] == 1
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
5 return 1
6 else return NIL
7 else c = BIB-RECURSIVE-MIN(V.summary)
8 if c = = NIL
9 return NIL
10 else i = BIB-RECURSIVE-MIN(V.cluster[c])
11 return c √ u + i
There are a two key cases for BIB-DELETE: if we delete the global
min, we have to find the next min and delete it from the recursive
substructures, as the global min is not stored recursively.
Otherwise, it suffices to simply delete the element from the
appropr √ iate cluster, and delete the cluster from the summary
(which is itself a Bib Tree of size u) if necessary.
BIB-DELETE(V, x) :
1 if x == V.root.min
12 newmin = BIB-RECURSIVE-MIN(V )
3 BIB-DELETE(V, newmin)
4 V.root.min = newmin
5 elseif V.u == 2
6 V.A[x] = 0
7 else
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
8 BIB-DELETE(V.cluster[high(x)], low(x))
9 if BIB-RECURSIVE-MIN(V.cluster[high(x)]) = = NIL // Is the cluster
now empty?
10 BIB-DELETE(V.summary, high(x))
(b) Write the recurrences for the run-times of the operations in the
revised data structure and solve the recurrences.
Solution:
BIB-MIN is not recursive, and so just takes O(1) time.
BIB-INSERT has 2 recursive calls to BIB-INSERT, so has recurrence
T(u) = 2T( √ u)+ O(1). We can solve this recurrence by substitution:
Define m = lg u, so that u = 2m and √ u = 2m/2 , and S(m) = T(2m) =
T(u).
Then S(m) = 2S(m/2) +O(1), so S(m) = Θ(m) by the Master method
and T(u) = Θ(lg u).
BIB-RECURSIVE-MIN likewise has recurrence T(u) = 2T( √ u)+O(1) and
therefore also takes time Θ(lg u).
Fina √ lly, as written above BIB-DELETE has a worst-case running time
of T(u) = 2T( u) + TBIB-RECURS IVE-MIN = 2T( √ u) + Θ(lg u).
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
Using substitution, as above, we get S(m) = 2S(m/2) + Θ(m),
which solves to Θ(m lg m) = Θ(lg u lg lg u) by the Master method.
(c) Ben Bitdiddle decides to use the Bib-Tree data structure in his
implementation of Prim’s algorithm (§23.2). You can assume that
the input graphs of interest all have integer weights in the range
from 1 to n and that edge weights are allowed to repeat. Show
how Ben would make use of his Bib-Tree and provide an analysis
of time complexity for this implementation of Prim.
Solution:
We cannot use the Bib Tree directly for Prim’s algorithm, because
the Bib Tree as described in the previous parts does not have the
capability of storing multiple elements with the same key. In
order to make it possible to use the Bib Tree for Prim’s algorithm,
we make the following modifications:
1. At the leaves of the Bib Tree (the base case, where the
universe size is u = 2), make V.A into an array of pointers
rather than a bit array as it was previously. Each pointer is
NIL if no vertex has that key, and is a pointer to the head of a
linked list containing all vertices with that key otherwise.
2. 2. In the base case of BIB-INSERT, if we are inserting a value v
with key k: rather than setting V.A[k] to be 1, we instead add
v to the end of the linked list at V.A[k], or create a new linked
list with one element if V.A[k] was previously NIL.
Additionally, when we
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
insert (k, v), we store a pointer to its position in that linked list
with the vertex v, which will become useful later for the
modified BIB-DELETE.
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers