Chapter 22 Answers


Document last modified: 

Graph Basics

Question 22.1 for the directed graph at right

Question 22.2 for the directed graph at right

Question 22.3 for the directed graph at right

Question 22.4 - 5 × 5 matrix

Question 22.5

BFS

Question 22.6 - Would it be possible for breadth first search starting at Arad to reach Neamt on the way to Fagaras? No. What about depth first? Yes.

Question 22.7 After executing BFS,

  1. What does x.d mean? The distance in edges from vertex s to vertex x.
  2. What does x.p mean? Equals u, the last edge (u, x) from s to vertex x.
  3. What is the maximum distance to source s? 3
  4. Trace the path starting at y.p to the source s. y"x"w"s
    w x y
    s w x
BFS (G, s)
1 for each vertex u Î G.V - {s} do
2    u.color ¬ WHITE
3    u.d ¬ ¥
4    u.p ¬ NIL
5 s.color ¬ GRAY
6 s.d ¬ 0
7 s.p ¬ NIL
8 Q ¬ Æ
9 Enqueue (Q, s)
10 while Q ¹ Æ do
11    u ¬ Dequeue (Q)
12    for each v Î G.Adj[u] do
13        if v.color = WHITE then
14           v.color ¬ GRAY
15           v.d ¬ u.d + 1
16           v.p ¬ u
17           Enqueue (Q, v)
18    u.color ¬ BLACK

Question 22.8

  • Execute the BFS on source vertex 5, visiting adjacent vertices at line 12 in numerical order.
      1 2 3 4 5
    p NIL NIL NIL NIL NIL
    d ¥ ¥ ¥ ¥ 0
    color WHITE WHITE WHITE WHITE GRAY

    Q=<5>

      1 2 3 4 5
    p 5 5 NIL 5 NIL
    d 1 1 ¥ 1 0
    color GRAY GRAY WHITE GRAY BLACK

    Q=<1,2,4>

      1 2 3 4 5
    p 5 5 NIL 5 NIL
    d 1 1 ¥ 1 0
    color BLACK GRAY WHITE GRAY BLACK

    Q=<2,4>

      1 2 3 4 5
    p 5 5 2 5 NIL
    d 1 1 2 1 0
    color BLACK BLACK GRAY GRAY BLACK

    Q=<4,3>

      1 2 3 4 5
    p 5 5 2 5 NIL
    d 1 1 2 1 0
    color BLACK BLACK GRAY BLACK BLACK

    Q=<3>

      1 2 3 4 5
    p 5 5 2 5 NIL
    d 1 1 2 1 0
    color BLACK BLACK BLACK BLACK BLACK

    Q=Æ

     

  • Count the number of times Line 12 is executed. 14 Did it agree with |V|+|E|+1? Not quite. |V|+|E|+1=5+7+1=13?
    Vertex Count
    1 2
    2 4
    3 2
    4 3
    5 3
    Total 14

  • Draw BFS tree.

        5
      / | \
     1 2 4
        |
        3

Analysis - BFS is O(V+E) due to line 12.

Note that this is for an adjacency list representation.

Question 22.9

  1. Is every vertex visited for an undirected graph? Yes, if all vertices are reachable, x y  for all x, y Î V. a directed graph? No. See the graph at right.
  2. Give an example that contradicts the statement "every edge is examined in an undirected graph". If the graph is not strongly connected.
  3. What is the purpose of the queue? Maintain the reachable vertices that have not been explored. Think about what happens in Lines 12-17.
  4. What would be the running time for an adjacency matrix representation? Hint: Consider how edges are represented at right. O(V2)
  5. Run BFS on the graph at right starting at D.
      A B C D E
    p NIL NIL NIL NIL NIL
    d ¥ ¥ ¥ 0 ¥
    color WHITE WHITE WHITE GRAY WHITE

     Q={D}

      A B C D E
    p NIL D NIL NIL D
    d ¥ 1 ¥ 0 1
    color WHITE GRAY WHITE BLACK GRAY

    Q={B,E}

      A B C D E
    p NIL D B NIL D
    d ¥ 1 2 0 1
    color WHITE BLACK GRAY BLACK GRAY

    Q={E,C}

      A B C D E
    p NIL D B NIL D
    d ¥ 1 2 0 1
    color WHITE BLACK GRAY BLACK BLACK

    Q={C}

      A B C D E
    p NIL D B NIL D
    d ¥ 1 2 0 1
    color WHITE BLACK BLACK BLACK BLACK

    Q={}

     

  6. Draw the resulting breadth-first tree(s).

           D
         /    \
       B      E
       |
       C

  7. We stated that is O(V+E). From Question e, is it also W(V+E)? No, if we had started at E, W(1).

 

Shortest paths

Question 22.10.1 - For the graph below.

  1. Suppose we reran BFS with a different source, would the results be the same? No
  2. What is the distance to s from y? 3
  3. What is the shortest path to s from y? s-w-x-y
  4. Give an undirected graph that has the shortest path length |V|, the path includes every vertex. Simplest is when no vertex is adjacent to no more than two vertices.
  5. Give an algorithm to print the vertices in the shortest path from any vertex to the source.
    --  pre: BFS has been run on G with vertex s, vÎG.V
    -- post: the shortest path from s to v is printed if a path exists

    Print-Path( v )

    while v ¹ NIL do

    output v

    v ¬ v.p

  r s t u v w x y
p s NIL w t r s w x
d 1 0 2 3 2 1 2 3

 

Print-Path (G, s, v)
--  pre: BFS has been run on G with vertex s
-- post: the shortest path from s to v is printed if it exists
1 if v = s then
2    print s
3    else if v.p = NIL then
4       print "no path from " s " to " v " exists"
        else
5          Print-Path (G, s, v.p)
6          print v
 

Question 22.10.2 - Execute Print-Path( G, s, u ) for the following:

  r s t u v w x y
p s NIL w t r s w x
d 1 0 2 3 2 1 2 3

Prints: s w t u

DFS

Question 22.11

  1. Where is the DFS? Line 7, DFS-Visit (G, v), explores the most recently discovered vertex.
  2. Suppose G.V taken in order of {u,v,w,x,y,z}. What is the next value of u of DFS used in calling DFS-Visit(u) at line 7? w
  3. Finish DFS.
  4. Where is the FIFO stack that maintains the discovered but not finished vertices? The stack is maintained by the recursion on DFS-Visit at line 7.
DFS (G)
1  for each vertex u Î G.V do
2    u.color ¬ WHITE
3    u.p ¬ NIL
4  time ¬ 0
5  for each vertex u Î G.V do
6     if u.color = WHITE then
7        DFS-Visit (G, u)
DFS-Visit (G, u)
1       u.color ¬ GRAY
2 time ¬ time + 1
3 u.d ¬ time
4 for each vertex v Î G.Adj[u] do
5    if v.color = WHITE then
6       v.p ¬ u
7       DFS-Visit (G, v)
8 u.color ¬ BLACK
9 u.f ¬ time ¬ time + 1

  u v w x y z
p NIL u NIL y v w
d 1 2 9 4 3 0
f 9 7   5 6  
color BLACK BLACK GRAY BLACK BLACK WHITE

  u v w x y z
p NIL u NIL y v w
d 1 2 9 4 3 10
f 9 7 12 5 6 11
color BLACK BLACK BLACK BLACK BLACK BLACK

 

DFS (G)
1  for each vertex u Î G.V do
2    u.color ¬ WHITE
3    u.p ¬ NIL
4  time ¬ 0
5  for each vertex u Î G.V do
6     if u.color = WHITE then
7        DFS-Visit (G, u)
DFS-Visit (G, u)
1       u.color ¬ GRAY
2 time ¬ time + 1
3 u.d ¬ time
4 for each vertex v Î G.Adj[u] do
5    if v.color = WHITE then
6       v.p ¬ u
7       DFS-Visit (G, v)
8 u.color ¬ BLACK
9 u.f ¬ time ¬ time + 1

Question 22.12 - Use graph at right.

  1. Give the DFS solution visiting vertices in alphabetical order.

    A at DFS line 5.  B, C, B at DFS-Visit line 4.

      A B C D E
    p NIL A B NIL NIL
    d 1 2 3    
    f 6 5 4    
    color BLACK BLACK BLACK WHITE WHITE

    D at DFS line 5. B, E at DFS-Visit line 4.

      A B C D E
    p NIL A B NIL D
    d 1 2 3 7 8
    f 6 5 4 10 9
    color BLACK BLACK BLACK BLACK BLACK
  2. Is every vertex visited for an undirected graph? Yes. a directed graph? Yes. Line 5 iterates over all vertices.
  3. Is every path explored? Yes. Explain using graph. Line 5 iterates over all vertices and line 4 iterates over all adjacent vertices (i.e. paths).
  4. BFS requires a queue to maintain a list of those vertices discovered but not yet visited, DFS does not. Why not? BFS discovers all new (WHITE) adjacent vertices first before exploring, queuing newly discovered vertices for later exploration. DFS discovers a new vertex and then recursively fully explores the vertex; recursion at line 7 of DFS-Visit maintains a LIFO list of adjacent vertices to discover and explore.

Predecessor subgraph p

Gp=(V, Ep)

Ep={(p[v], v) : v Î V  and p[v] ¹ NIL }

Example: (p[x], x) = (y, x)

Ep={(u, v), (v, y), (y, x), (w, z)} for above figure

  u v w x y z
p NIL u NIL y v w
d 1 1 9 4 3 10
f 8 7 12 5 6 11

Question 22.13 - For above graph.

  1. Trace the path using the predecessor subgraph p starting at x. {(x,y), (y,v), (v,u)}
  2. Draw the resulting depth-first tree(s).

         u             w
         |              |
         v             z
         |
         y
         |
         x  

  3. With 10 vertices in a graph, what is the maximum finish time? 20
  4. Do you agree or disagree with the statement "Depth first search is a good way of establishing the nature of the connectivity in a graph. You can build a table out of the results of a search showing which vertices are reachable from which other vertices."? Disagree for our implementation. DFS is guaranteed to fully explore all vertices in a graph but creates subgraphs when multiple paths to a vertex; the path connects to only one of the ancestors.

Classification of edges

Question 22.14 - Classify each edge of the graph below left for the DFS results of below right.

  
  A B C D E
p NIL A B NIL D
d 1 2 3 7 8
f 6 5 4 10 9

Tree - (A,B), (B,C), (D,E)

Back - (C,B)

Forward - none

Cross - (D,B)

 

Parenthesis theorem

For all u and v, exactly one of the following holds:
  1. u and v are in different branches or trees, neither of u and v is a descendant of the other:

    u.d < u.f < v.d < v.f

    or

    v.d < v.f < u.d < u.f

  2. v is a descendant of u:

    u.d < v.d < v.f < u.f

  3. u is a descendant of v:

    v.d < u.d < u.f < v.f

So u.d < v.d < u.f < v.f cannot happen

Like parentheses:

  • OK: ( ) [ ] ( [ ] ) [ ( ) ]
  • Not OK: ( [ ) ] [ ( ] )

Question 22.15 - What is implied by:

  • s.d < w.d < w.f < s.f         w is a descendant of s, (b) at left.
  • x.f < w.d                           x and w are in different branches of DFS tree,
                                             not ancestor or descendant.

 

 

Topological sort

Question 22.16

 

Question 22.17 - Which of the following are invalid orderings and why?

  1. ADEBC Valid. No (A,D) or (D,A) path so either AD or DA order valid.
  2. CBAED Invalid. (B,C) and (E,C) path requires C come after both.
  3. ABCDE Invalid. (E,C) path requires C come after E.
  4. DECAB Invalid. (B,C) path requires C come after B.

Question 22.18

  • List the items in topologically sorted order. Using f value from (b) is: socks, ..., jacket.
  • What can you say about the relationship between finish time and topological ordering? The last finished is the first in the list.
  • Must the professor put on his socks and undershorts in that order? That is could the order be switched? Yes, there is no connecting path.
  • When can our professor (according to the sort) put on his pants? After putting on the undershorts.
  • When can our professor (according to the sort) put on his watch? Anytime.
  • What is the complexity of the Topological-Sort algorithm? That of DFS: Q(V + E).
  • Compute the DFS of the DAG at right.
      C1 C2 C3 C4 C5
    p NIL NIL C1 C3 C4
    d 1 9 2 3 4
    f 8 10 7 6 5
  • Give a topological sort for the DAG at right. Are other sorts possible? C2, C1, C3, C4, C5. Yes: C1, C2, C3, C4, C5.
  • From the following DFS table, what is the topological order? w, z, u, v, y, x
  u v w x y z
p NIL u NIL y v w
d 1 1 9 4 3 10
f 8 7 12 5 6 11

SCC

Example: Figures at right

    (a) G.    DFS trees:  

b        c
|       /   \
e     d     g
|           /  \
a         f     h

b.d < e.d < a.d < a.f < e.f < b.f

  (b    (e     (a       a)      e)      b)

Question 22.19 -  Give the parenthesization for DFS tree rooted at c.

(c         (g        (f        f)     (h       h)        g)     (d        d)      c)

c.d <    g.d  <   f.d  < f.f < h.d < h.f  <   g.f <  d.d < d.f <  c.f

 

Question 22.20 - Is f(gf) > f(cd)? yes.

                Can you tell from the DFS GT table below? Yes, all finish times of cd < finish times of gf.

                What does that mean?  That cd and gf form SCC's.