Sei sulla pagina 1di 7

21.

1 Disjoint-set operations
21.1-1
Suppose that CONNECTED-COMPONENTS is run on the undirected graph
where

G=(V,E),

V={a,b,c,d,e,f,g,h,i,j,k} and the edges of E are processed in the order (d,i),(f,k),(g,i),

(b,g),(a,h),(i,j),(d,k),(b,j),(d,f),(g,j),(a,e). List the vertices in each connected component after


each iteration of lines 35.

Edge processed

Collection of disjoint sets

initial sets

{a} {b} {c} {d} {e} {f} {g} {h} {i} {j} {k}

(d,i)

{a} {b} {c} {d,i} {e} {f} {g} {h} {j} {k}

(f,k)

{a} {b} {c} {d,i} {e} {f,k} {g} {h} {j}

(g,i)

{a} {b} {c} {d,i} {e} {f,k} {g} {h} {j}

(b,g)

{a} {b,g} {c} {d,i} {e} {f,k} {h} {j}

(a,h)

{a,h} {b,g} {c} {d,i} {e} {f,k} {j}

(i,j)

{a,h} {b,g} {c} {d,i,j} {e} {f,k}

(d,k)

{a,h} {b,g} {c} {d,f,i,j,k} {e}

(b,j)

{a,h} {b,d,f,g,i,j,k} {c} {e}

(d,f)

{a,h} {b,d,f,g,i,j,k} {c} {e}

(g,j)

{a,h} {b,d,f,g,i,j,k} {c} {e}

(a,e)

{a,e,h} {b,d,f,g,i,j,k} {c}

21.1-3
FIND-SET:

2|E|.

UNION: Initially, there are

|V| components, and each UNION operation decreases the number of connected

components by 1, thus UNION is called

|V|k times.

Write pseudocode for MAKE-SET, FIND-SET, and UNION using the linked-list
representation and the weighted-union heuristic. Make sure to specify the
attributes that you assume for set objects and list objects.
UNION: if > swap & size +=

21.2-2
Show the data structure that results and the answers returned by the FIND-SET
operations in the following program. Use the linked-list representation with the
weighted-union heuristic.
1
2
3
4
5
6

for i = 1 to 16
MAKE-SET(x_{i})
for i = 1 to 15 by 2
UNION(x_{i}, x_{i+1})
for i = 1 to 13 by 4
UNION(x_{i}, x_{i+2})

UNION(x_{1}, x_{5})

UNION(x_{11}, x_{13})

UNION(x_{1}, x_{10})

10 FIND-SET(x_{2})
11 FIND-SET(x_{9})

All same set.

21.2-3
Adapt the aggregate proof of Theorem 21.1 to obtain amortized time bounds
of O(1) for MAKE-SET and FIND-SET and

O(lgn) for UNION using the linked-list

representation and the weighted-union heuristic.

O(nlgn)/n=O(lgn)

21.2-4
Give a tight asymptotic bound on the running time of the sequence of operations in
Figure 21.3 assuming the linked-list representation and the weighted-union
heuristic.

(n)+(n)=(n)

21.2-5
Professor Gompers suspects that it might be possible to keep just one pointer in
each set object, rather than two (head and tail), while keeping the number of
pointers in each list element at two. Show that the professor's suspicion is well
founded by describing how to represent each set by a linked list such that each
operation has the same running time as the operations described in this section.
Describe also how the operations work. Your scheme should allow for the
weighted-union heuristic, with the same effect as described in this section. (Hint:
Use the tail of a linked list as its set's representative.)
If each node has a pointer points to the first node and we use the tail as the
representative, then we can find the head (first node) with the tail's pointer
in

(1).

21.2-6
Suggest a simple change to the UNION procedure for the linked-list representation
that removes the need to keep the tail pointer to the last object in each list.
Whether or not the weighted-union heuristic is used, your change should not
change the asymptotic running time of the UNION procedure. (Hint: Rather than
appending one list to another, splice them together.)
We can splice/zip two lists. Suppose one list is
is

135101, the other

246, then the spliced/zipped list is 123457101. When

the shorter one is used up, we can concatenate the remaining part of the longer
list directly to the tail of merged list, thus it is identical to the weighted-union
heuristic.

Give a sequence of

m MAKE-SET, UNION, and FIND-SET operations, n of which


are MAKE-SET operations, that takes (mlgn) time when we use union by rank
only.

((m2n)lgn)=(mlgn)

21.3-4
Suppose that we wish to add the operation PRINT-SET (x), which is given a
node

x and prints all the members of x's set, in any order. Show how we can add

just a single attribute to each node in a disjoint-set forest so that PRINTSET(x) takes time linear in the number of members of

x's set and the asymptotic

running times of the other operations are unchanged. Assume that we can print
each member of the set in

O(1) time.

Each member has a pointer points to the next element in the set, which forms a
circular linked list. When union two sets

x and y, swap x.next and y.next to

merged the two linked lists.

21.4-2
Prove that every node has rank at most
The rank increases by 1 only when

lgn.

x.rank=y.rank, thus each time we need

the twice number of elements to increase the rank by 1, therefore the rank is at
most

lgn.

21.4-3
In light of Exercise 21.4-2, how many bits are necessary to store
node

x?

lgn.

x.rank for each

21.4-4
Using Exercise 21.4-2, give a simple proof that operations on a disjoint-set forest
with union by rank but without path compression run in

O(mlgn) time.

O((mx)lgn)=O(mlgn).

21.4-5
Professor Dante reasons that because node ranks increase strictly along a simple
path to the root, node levels must monotonically increase along the path. In other
words, if

x.rank>0 and x.p is not a root, then level( x) level(x.p). Is the

professor correct?
No. Think about an extreme condition

100991, since level(99)=0,

level(1)1, we have level(x) > level(x.p).

22.2.3
During the BFS procedure, color of each vertex is initialized as WHITE,
changed to GRAY oncethe node is visited and enqueued, and, finally, set
to BLACK when its dequeued and every neighbor vertex is visited. Than
being said, color of a node stays GRAY during a period, whichlasts from
the point when it is enqueued to the point when it is dequeued, followed
by the step tovisit every neighboring vertex. Therefore the meaning of
GRAY color is roughly equivalent toexistence in queue. Line 5 and 14,
which set vertex color to GRAY, should be removed, in orderto use a single
bit to store each vertex color.Or simply, the entire BFS procedure does not
have any line that distinguishes BLACK fromGRAY. It only checks either it is
WHITE or non-WHITE, which shows that it is meaningless to have two
colors to represent visited vertices.
22.2.4
If the graph is represented by adjancency matrix, then the time of iterating all the
edge becomes O(V^2), so the running time is O(V+V^2).
22.3.5
a.) Show that edge (u, v) is a tree edge or forward edge if and only if d[u]
< d[v] < f[v] < f[u] Proof: : Assume that edge (u, v) is a tree or forward

edge. If (u, v) is a tree edge, then by the definition of tree edge v is first
discovered by exploring edge (u, v). If (u, v) is a forward edge, then by the
definition of forward edge v is an ancestor of u. In either case, d[u] < d[v].
Since v was discovered after u, then must be finished prior to u being
finished, hence f[v] < f[u]. Every vertex must be discovered before it can
be finished, so d[v] < f[v]. Putting all three inequalities together yields
d[u] < d[v] < f[v] < f[u]. : Assume that for vertices u and v, d[u] < d[v]
< f[v] < f[u]. d[v] < f[v] can be established trivially. d[u] < d[v] and f[v] <
f[u] imply that v was discovered after u, and finished before u. Therefore,
edge (u, v) is a tree edge if v was discovered by traversing edge (u, v).
Otherwise, (u, v) is a forward edge.
b.) Show that edge (u, v) is a back edge if and only if d[v] < d[u] < f[u] <
f[v] Proof: : Assume that edge (u, v) is a back edge. Then by definition, v
is an ancestor of u, so d[v] < d[u]. Since v was discovered before u, u will
finish before v finishes. Hence, f[u] < f[v]. d[u] < f[u] can be established
trivially. Therefore d[v] < d[u] < f[u] < f[v]. : Assume d[v] < d[u] < f[u] <
f[v]. f[u] < f[v] and d[v] < d[u] imply that v was discovered before u and
finished after u. Therefore v is an ancestor of u. Therefore (u, v) is a back
edge.
c.) Show that edge (u, v) is a cross edge if and only if d[v] < f[v] < d[u] <
f[u] Proof: : Assume that edge (u, v) is a cross edge. Therefore, there is
no parental or ancestral relationship between u and v. d[u] < f[u] and d[v]
< f[v] can be established trivially. If d[u] < d[v], then edge (u, v) would
indicate a parental relationship between u and v in the depth-first tree,
which cannot happen by the definition of cross edge. Hence, d[u] > d[v].
Similarly, we cannot have d[u] < f[v], because this would indicate that v
was finished (but not discovered) after u was discovered, but before u was
finished, which cannot happen. Therefore, d[v] < f[v] < d[u] < f[u] :
Assume d[v] < f[v] < d[u] < f[u]. Since f[v] < d[u]], there is no parental or
ancestral relationship between u and v. Therefore edge (u, v) is a cross
edge.
22.4.1
22.4-2 (pg. 552) Give a linear-time algorithm that takes as input a directed
acyclic graph G = (V, E) and two vertices s and t, and returns the number
of paths from s to t in G. The basic idea here is to start at vertex t, and use
depth-first search in reverse direction until we reach vertex s. Each and
maintains a counter which indicates the number of unique reverse paths
found from vertex t. 1. Initialize counters to 0 for all vertices. 2. Start
depth-first-search in reverse direction using vertex t as a root. 3. For each
edge (u, v) examined in the breadth-first search. Counter(v) =
max{ Counter(v) + 1, Counter(v) + Counter(u) } 4. Return Counter(s)
22.4.3
An undirected graph is acyclic (i.e., a forest) if and only if a DFS yields no
back edges. If theres a back edge, theres a cycle. If theres no back

edge, then by Theorem 22.10, there are only tree edges. Hence, the graph
is acyclic. Thus, we can run DFS: if we find a back edge, theres a cycle.
Time: O.V /. (Not O.V C E/!) If we ever see jV j distinct edges, we must
have seen a back edge because (by Theorem B.2 on p. 1174) in an acyclic
(undirected) forest, jEj jV j 1.