4-solutions-clrs-22

8
Page 1 of 8 PSU CMPSC 465 Spring 2013 Homework Solutions – Unit 4: Chapter 22 CMPSC 465 Disclaimer: This is a draft of solutions that has been prepared by the TAs and the instructor makes no guarantees that solutions presented here contain the level of detail that would be expected on an exam. Any errors or explanations you find unclear should be reported to either of the TAs for corrections first. Exercise 22.1-1 Given an adjacency-list representation of a directed graph, how long does it take to compute the out-degree of every vertex? How long does it take to compute the in-degrees? Solution: Given an adjacency-list representation Adj of a directed graph, the out-degree of a vertex u is equal to the length of Adj[u], and the sum of the lengths of all the adjacency lists in Adj is |E|. Thus the time to compute the out-degree of every vertex is Θ (|V| + |E|). The in-degree of a vertex u is equal to the number of times it appears in all the lists in Adj. If we search all the lists for each vertex, the time to compute the in-degree of every vertex is Θ (|V||E|). (Alternatively, we can allocate an array T of size |V| and initialize its entries to zero. Then we only need to scan the lists in Adj once, incrementing T [u] when we see u in the lists. The values in T will be the in-degrees of every vertex. This can be done in Θ (|V| + |E|) time with Θ (|V|) additional storage.)

Upload: abdullah-simpson

Post on 12-Jan-2016

12 views

Category:

Documents


2 download

DESCRIPTION

solutions

TRANSCRIPT

Page 1: 4-solutions-clrs-22

Page 1 of 8 PSU CMPSC 465 Spring 2013

Homework Solutions – Unit 4: Chapter 22 CMPSC 465 Disclaimer: This is a draft of solutions that has been prepared by the TAs and the instructor makes no guarantees that solutions presented here contain the level of detail that would be expected on an exam. Any errors or explanations you find unclear should be reported to either of the TAs for corrections first. Exercise 22.1-1 Given an adjacency-list representation of a directed graph, how long does it take to compute the out-degree of every vertex? How long does it take to compute the in-degrees? Solution: Given an adjacency-list representation Adj of a directed graph, the out-degree of a vertex u is equal to the length of Adj[u], and the sum of the lengths of all the adjacency lists in Adj is |E|. Thus the time to compute the out-degree of every vertex is Θ (|V| + |E|). The in-degree of a vertex u is equal to the number of times it appears in all the lists in Adj. If we search all the lists for each vertex, the time to compute the in-degree of every vertex is Θ (|V||E|). (Alternatively, we can allocate an array T of size |V| and initialize its entries to zero. Then we only need to scan the lists in Adj once, incrementing T [u] when we see u in the lists. The values in T will be the in-degrees of every vertex. This can be done in Θ (|V| + |E|) time with Θ (|V|) additional storage.)

Page 2: 4-solutions-clrs-22

Page 2 of 8 PSU CMPSC 465 Spring 2013

Exercise 22.1-2 Give an adjacency-list representation for a complete binary tree on 7 vertices. Give an equivalent adjacency-matrix representation. Assume that vertices are numbered from 1 to 7 as in a binary heap. Solution: A complete binary tree looks like:

An adjacency-list representation of this tree is shown below:

An equivalent adjacency-matrix representation of this tree is shown below:

1 2 3 4 5 6 7 1 0 1 1 0 0 0 0 2 1 0 0 1 1 0 0 3 1 0 0 0 0 1 1 4 0 1 0 0 0 0 0 5 0 1 0 0 0 0 0 6 0 0 1 0 0 0 0 7 0 0 1 0 0 0 0

 

 

1

3  2

 5  4  7  6

2 1

2

3

4

5

6

7

3

1 4 5

1 6 7

2

2

3

3

Page 3: 4-solutions-clrs-22

Page 3 of 8 PSU CMPSC 465 Spring 2013

Exercise 22.2-1 Show the dist and pred values that result from running breadth-first search on the directed graph below, using vertex 3 as the source. Solution:

The procedure of the breadth-first search is shown above. From the result, we can see that:

Node Pred dist 3 NIL 0 5 3 1 6 3 1 4 5 2 2 4 3 1 NIL

1 2 3

 

     

   

5 4 6

1 2 3.pred:NIL

 

     

   0

5 4 6

Q 3 0

1 2 3.pred:NIL

 

 1    1

   0

4

Q 5 1

6 1

5.pred:3

6.pred:3

1 2 3.pred:NIL

 

 1  2  1

   0 Q

5.pred:3

6.pred:3

6 1

4 2

4.pred:5

1 2 3.pred:NIL

 

 1  2  1

   0 Q

5.pred:3

6.pred:3

4 2

4.pred:5

3.pred:NIL

 

 1  2  1

 3  0 Q

5.pred:3

6.pred:3

2 3

4.pred:5

1 2.pred

:4

3.pred:NIL

 

 1  2  1

 3  0 Q

5.pred:3

6.pred:3

4.pred:5

1 2.pred

:4

(a)

(b)

(c)

(d)

(e)

(f)

Page 4: 4-solutions-clrs-22

Page 4 of 8 PSU CMPSC 465 Spring 2013

Exercise 22.2-2 Show the dist and pred values that result from running breadth-first search on the undirected graph below, using vertex u as the source. Solution:

r s t u

   

               

         

w v y x

   

               

         0

w v y x

Q u 0

r s t u.pred :NIL

   

         1  1

     1  0

w v

Q t 1

r s t.pred

:u

x 1

y 1

u.pred :NIL

x.pred:u

y.pred :u

   

 2      1  1

     1  0

v

Q x 1

r s t.pred

:u

y 1

w 2

u.pred :NIL

x.pred:u

y.pred :u

w.pred:t

   

 2      1  1

     1  0

v

Q y 1

r s t.pred

:u

w 2

u.pred :NIL

x.pred:u

y.pred :u

w.pred:t

   

 2      1  1

     1  0

v

Q

r s t.pred

:u

w 2

u.pred :NIL

x.pred:u

y.pred :u

w.pred:t

u.pred :NIL

   

 2      1  1

 3  1  0

v

Q

r t.pred

:u

s 3

x.pred:u

y.pred :u

w.pred:t

s.pred:w

u.pred :NIL

 4

 2      1  1

 3  1  0

v

Q

t.pred:u

r 4

x.pred:u

y.pred :u

w.pred:t

s.pred:w

r.pred:s

 4

 2  5  1  1

 3  1  0

Q

t.pred:u

v 5

u.pred :NIL

x.pred:u

y.pred :u

w.pred:t

s.pred:w

r.pred:s

v.pred:r

 4

 2  5  1  1

 3  1  0

Q

t.pred:u

u.pred :NIL

x.pred:u

y.pred :u

w.pred:t

s.pred:w

r.pred:s

v.pred:r

(a) (f)

(b) (g)

(c) (h)

(d) (i)

(e)

Page 5: 4-solutions-clrs-22

Page 5 of 8 PSU CMPSC 465 Spring 2013

The procedure of the breadth-first search is shown above. From the result, we can see that:

Node Pred dist u NIL 0 t u 1 x u 1 y u 1 w t 2 s w 3 r s 4 v r 5

Exercise 22.3-1 Make a 3-by-3 chart with row and column labels WHITE, GRAY, and BLACK. In each cell (i, j), indicate whether, at any point during a depth-first search of directed graph, there can be an edge from a vertex of color i to a vertex of color j. For each possible edge, indicate what edge types it can be. Make a second such chart for depth-first search of an undirected graph. Solution: Directed graph:

WHITE GRAY BLACK WHITE tree, back, forward, and cross back and cross cross GRAY tree and forward tree, forward, back tree, forward, and cross

BLACK × back and cross tree, forward, back and cross Undirected graph:

WHITE GRAY BLACK WHITE tree and back tree and back × GRAY tree and back tree and back tree and back

BLACK × tree and back tree and back Exercise 22.3-2 Show how depth-first search works on the graph of Figure 22.6. Assume that the for loop of lines 5–7 of the DFS procedure considers the vertices in alphabetical order, and assume that each adjacency list is ordered alphabetically. Show the discovery and finishing times for each vertex, and show the classification of each edge.

Page 6: 4-solutions-clrs-22

Page 6 of 8 PSU CMPSC 465 Spring 2013

Solution: The discovery and finishing times for each vertex are shown in the figure below:

Tree edges: (q, s), (s, v), (v, w), (q, t), (t, x), (x, z), (t, y), (r, u) Back edges: (w, s), (z, x), (y, q) Forward edges: (q, w) Cross edges: (r, y), (u, y) Exercise 22.3-3 Show the parenthesis structure of the depth-first search of Figure 22.4.

Figure 22.4 (p) Solution:

Exercise 22.3-7 Rewrite the procedure DFS, using a stack to eliminate recursion. Solution: Assume that the stack has following operations:

PUSH(S, v) – pushes v into the stack; POP(S) – returns the top of the stack and removes it; TOP(S) – returns the top of the stack without removing it.

1,16

2,7

3,6 4,5

8,15

9,12

10,11

13,14

17,20

18,19

Page 7: 4-solutions-clrs-22

Page 7 of 8 PSU CMPSC 465 Spring 2013

Denote an empty stack by EMPTY. Then the pseudocode of this algorithm becomes as follows:

DFS_STACK (G, s) for each vertex u V(G) – {s} {

u.color = WHITE u.pred = NIL

} time = 0 S = EMPTY time = time + 1 s.td = time s.color = GRAY PUSH(S, s) while S != EMPTY {

t = TOP(S) if v V(G).Adj[t] s.t. v.color == WHITE // v’s adjacency list hasn’t been fully examined {

v.pred = t time = time + 1 v.td = time v.color = GRAY PUSH(S, v)

} else // v’s adjacency list has been fully examined {

t = POP(S) time = time + 1 v.tf = time v.color = BLACK

} }

Exercise 22.4-1 Show the ordering of vertices produced by TOPOLGICAL-SORT when it is run on the dag of Figure 22.8, under the assumption of Exercise 22.3-2.

Solution: According to the assumption of Exercise 22.3-2, the for loop of lines 5–7 of the DFS procedure

Page 8: 4-solutions-clrs-22

Page 8 of 8 PSU CMPSC 465 Spring 2013

considers the vertices in alphabetical order, and that each adjacency list is ordered alphabetically. DFS on Figure 22.8:

Ordering of vertices:

Exercise 22.4-2 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 simple paths from s to t in G. For example, the directed acyclic graph of Figure 22.8 contains exactly four simple paths from vertex p to vertex v: pov, poryv, posryv, and psryv. (Your algorithm needs only to count the simple paths, not list them.)

Solution: Add a field to the vertex representation to hold an integer count. Initially, set vertex t’s count to 1 and other vertices’ count to 0. Start running DFS with s as the start vertex. When t is discovered, it should be immediately marked as finished (BLACK), without further processing starting from it. Subsequently, each time DFS finishes a vertex v, set v’s count to the sum of the counts of all vertices adjacent to v. When DFS finishes vertex s, stop and return the count computed for s.

1/20

3/4

6/19

7/8

9/18

10/17 11/14

12/13 15/16

21/26

2/5

22/25

23/24

27/28