Chapter 21 Answers

powered by FreeFind

Modified: 

Question

  1. What is G=(V, E)? G is the pair of V and E.
  2. What is V[ G ]? {a,b,c,d,e,f,g,h,i,j}
    1. Is b Î V[ G ]? Yes.
    2. Is z Î V[ G ]? No.
  3. What is E[ G ]? {(a,b),(b,a),(a,c),(c,a),(b,d),(d,b),(b,c),(c,b),(e,f),(f,e),(e,g),(g,e),(h,i), (i,h)}
    1. Is (b, d) Î E[ G ]? Yes.
    2. Is (b, e) Î E[ G ]? No.
  4. What does MAKE-SET do? Create singleton sets for each vertex: {a}, {b}, {c}, ..., {j}.
  5. Step through the execution of CONNECT-COMPONENTS for the graph G at right.
    1. What is FIND-SET( b ) ¹ FIND-SET( d ) for sets {b} and {d}? True.
    2. What is UNION(b, d ) for sets {b} and {d}? {b,d}
    3. What is FIND-SET( b ) ¹ FIND-SET( d ) for set {b, d}? True.
  6. After running CONNECT-COMPONENT(G), what is the result of SAME-COMPONENT(a, b)? TRUE. SAME-COMPONENT( a, e)? FALSE.

 

 

FIND-SET - Assumes the representative points to itself.

-- return the key of the representative of set x
FIND-SET (x)
   if p[ x ] ¹ x then return FIND-SET ( p[ x ] )
   return x
Question
UNION - Assumes the representative points to itself. Note that we do not maintain that the representative is the minimum of the union.
-- join two sets containing items x and y together
UNION (x, y)
   a  FIND-SET (x)       -- a is x's representative
   b ¬ FIND-SET (y)    -- b is y's representative
   p[ a ] ¬ b                -- now b is a's parent, and FIND-SET (a) would return b
Question 

Improvements

FIND-SET path compression

Question - For the set at right:

 

UNION by size, weighted-union heuristic

Each UNION pushes some tree down a level. This means that the next find done on a member of this subtree will take a little longer than it would have before the UNION. Although the path is compressed during the FIND-SET, the damage is already done with the time spent doing the FIND-SET.

Example - Consider the cost of FIND-SET( 7 ) on the following uncompressed path:

The cost would be less if the larger set was the parent.

Question

i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
p[ i ] 1 2 2 4 2 1 2 0 15 2 4 2 2 6 11

Union-by-size

Examples
a) Initially 6 singleton sets

    i  1  2  3  4  5  6
p[i]  1  2  3  4  5  6
c[i]  1  1  1  1  1  1

b) Union (3, 5)

    i  1  2  3  4  5  6
p[i]  1  2  5  4  5  6
c[i]  1  1  1  1  2  1

c) Union (4, 2)

    i  1  2  3  4  5  6
p[i]  1  2  5  2  5  6
c[i]  1  2  1  1  2  1

d) Union (2, 6)

    i  1  2  3  4  5  6
p[i]  1  2  5  2  5  2
c[i]  1  3  1   1  2  1

e) Union (1, 4)

    i  1  2  3  4  5  6
p[i]  2  2  5  2  5  2
c[i]  1  4   1  1  2  1

f) Union (3, 6)

    i  1  2  3  4  5  6
p[i]  2  2  5  2  2  2
c[i]  1   6  1  1  2  1

Question

i 1 2 3 4 5 6
p[ i ] 2 2 2 2 2 2