Introduction to Algorithms
May 6, 2005
Massachusetts Institute of Technology
6.046J/18.410J
Professors Charles E. Leiserson and Ronald L. Rivest
Quiz 2 Solutions
Quiz 2 Solutions
Problem 1.
Static Graph Representation
Let
G
= (
V, E
)
be a sparse undirected graph, where
V
=
{
1
,
2
, . . . , n
}
. For a vertex
v
∈
V
and
for
i
= 1
,
2
, . . . ,
outdegree(
v
)
, define
v
’s
i
th neighbor
to be the
i
th smallest vertex
u
such that
(
v, u
)
∈
E
, that is, if you sort the vertices adjacent to
v
, then
u
is the
i
th smallest.
Construct a representation of the graph
G
to support the following queries:
•
D
EGREE
(
v
)
: returns the degree of vertex
v
.
•
L
INKED
(
u, v
)
: output
TRUE
if an edge connects vertices
u
and
v
, and
FALSE
otherwise.
•
N
EIGHBOR
(
v, i
)
: returns
v
’s
i
th neighbor.
Your data structure should use asymptotically as little space as possible, and the operations should
run asymptotically as fast as possible, but space is more important than time. Analyze your data
structure in terms of both space and time.
Solution:
We give a solution that uses
Θ(
E
)
space for the data structure and takes
Θ(1)
time for
each of the three operations. Create a hash table that contains key
(
u, v
)
if
(
u, v
)
∈
E
or key
(
u, i
)
if vertex
u
has an
i
th neighbor. It is possible that for some
u
and
v
=
i
, vertex
u
has both an
adjacent vertex
v
and an
i
th neighbor. This is handled by storing satellite data with each record
of the hash table. For the record with key
(
u, i
)
in the hash table, if
u
has a neighbor
v
=
i
, then
indicate so using a bit in the record; if
u
has an
i
th neighbor, then store the corresponding neighbor
vertex index in the record. Also, for every vertex
u
that is connected to some other vertex, store its
degree in the record for key
(
u,
1)
.
Thus, for each vertex
u
in the first coordinate of the key, the hash table has at most
degree(
u
) +
degree(
u
) = 2 degree(
u
)
entries. The total number of entries is thus at most
∑
n
u
=1
2 degree(
u
) =
4

E

. By using
perfect hashing
, and choosing a suitable hash function through a small number
of random samplings (during data structure construction), we can make the lookup time
Θ(1)
and
space requirement linear in the number of entries stored, i.e.,
Θ(
E
)
(see CLRS, page 249, Corollary
11.12). We can use the same family of hash functions as in CLRS by converting each
2
tuple
(
u, v
)
into a distinct number
(
u

1)
n
+
v
in the range of
[1
. . . n
2
]
. The total space requirement of the
degree array and hash table is thus
Θ(
V
+
E
)
.
Then, D
EGREE
(
v
)
looks up the key
(
v,
1)
in the hash table. If found, it returns the degree value
stored in the record. Otherwise, it returns
0
, since
v
is not adjacent to any vertex. This takes
Θ(1)
time. L
INKED
(
u, v
)
looks up the key
(
u, v
)
in the hash table and returns
TRUE
if it exists and the
associated bit in the record is set. This is
Θ(1)
time. N
EIGHBOR
(
v, i
)
looks up the key
(
v, i
)
in
the hash table – if it exists and a neighbor vertex is stored in the record, it returns its index. This is
also
Θ(1)
time.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
6.046J/18.410J Quiz 2 Solutions
2
+1
+4
3
10
Entrance
Exit
Figure 1
: An example of a
1
admissible maze.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 ErikDemaine
 Algorithms, Graph Theory, Data Structures, NSERTE DGE

Click to edit the document details