Presentation is loading. Please wait.

Presentation is loading. Please wait.

Suggested Solutions to Part of Homework 1

Similar presentations


Presentation on theme: "Suggested Solutions to Part of Homework 1"— Presentation transcript:

1 Suggested Solutions to Part of Homework 1
22.3-6: Eliminate recursion of DFS 22.4-2: Compute the number of st-paths in DAGs in linear time (c) 2011 SDU

2 22.3-6 (c) 2011 SDU

3 Eliminate recursion of DFS (1)
DFS(G) Input: A graph G represented by adjacent list. Output: Vertex list of G in DFS order. 1 for each vertex v  V(G) do 2 color[v]  WHITE /* WHITE means that the corresponding vertex has not been visited. */ 3 endfor 4 Stack ST   5 for each vertex v  V(G) do 6 if color[v] = BLACK then loop /* BLACK means the corresponding vertex has been visited */ (c) 2011 SDU

4 Eliminate recursion of DFS (1)
7 Push(ST, v) 8 while ST   do u  Pop(ST) if color[u] = BLACK then loop Visit(u) /* to say, print u */ color[u]  BLACK for each vertex a  Adj[u] do if color[a] = WHITE then Push(ST, a) endfor endwhile 17 endfor 18 return (c) 2011 SDU

5 Eliminate recursion of DFS (2)
To faithfully implement the DFS algorithm in the textbook without recursion. Vertex color: WHITE – not visited; GRAY – visiting; BLACK – visited. Time: d[u] – the beginning time of visiting vertex u f[u] – the end time of visiting vertex u. Predecessor: [u] – the predecessor of vertex u in the DFS forest. (c) 2011 SDU

6 Eliminate recursion of DFS (2)
DFS(G) Input: A graph G represented by adjacent list. Output: Visit beginning time d[u], visit end time f[u], and a DFS forest . 1 for each vertex v  V(G) do 2 color[v]  WHITE 3 [v]  NIL 4 endfor 5 time  0 6 Stack ST   7 for each vertex v  V(G) do 8 if color[v]  WHITE then loop (c) 2011 SDU

7 Eliminate recursion of DFS (2)
9 Push(ST, (v, HEAD)) /* Each unit of stack ST contains a pair (v, a), which means the vertex v and its an adjacent vertex a The adjacent vertex is gotten from the linked list of vertex v in the adjacent list of G. The adjacent vertex being HEAD means that we are currently at the dummy head node of the linked list (of vertex v). */ while ST   do (u, a)  Pop(ST) if a = HEAD then color[u]  GRAY d[u]  time  time + 1 (c) 2011 SDU

8 Eliminate recursion of DFS (2)
endif a  NextAdjVertex(u, a) /* NextAdjVertex(u, a) – getting the adjacent vertex of u which is after vertex a from u’s linked list If a is the last adjacent vertex in the linked list, the function would return NIL. */ if a  NIL then Push(ST, (u, a)) if color[a] = WHITE then [a]  u Push(ST, (a, HEAD)) loop /* control returns back to while */ (c) 2011 SDU

9 Eliminate recursion of DFS (2)
endif else /* a = NIL. We have considered all the adjacent vertices of u. */ color[u]  BLACK f[u]  time  time + 1 endif endwhile 29 endfor 30 return (c) 2011 SDU

10 22.4-2 (c) 2011 SDU

11 Compute number of st-paths in DAG (1)
Transmit-Alg(G) 1 Compute a topological order L of V(G) 2 for each vertex v from s to t in order of L do 3 count[v]  0 4 endfor 5 count[s]  1 6 for each vertex u from s to the vertex just preceding t do 7 for each vertex v  Adj[u] do count[v]  count[v] + count[u] 9 endfor 10 endfor 11 return count[t] (c) 2011 SDU

12 Compute number of st-paths in DAG (2)
Transmit(G) 1 for each vertex v  V(G) do 2 color[v]  WHITE 3 count[v]  0 4 endfor 5 count[t]  1 6 DFS-Visit(s) 7 return count[s] (c) 2011 SDU

13 Compute number of st-paths in DAG (2)
DFS-Visit(u) 1 if u = t then color[t]  BLACK and return 2 color[u]  GRAY 3 for each vertex v  Adj[u] do 4 if color[v] = WHITE then DFS-Visit(v) 5 count[u]  count[u] + count[v] 6 endfor 7 color[u]  BLACK 8 return (c) 2011 SDU

14 Thanks for attention! (c) 2011 SDU


Download ppt "Suggested Solutions to Part of Homework 1"

Similar presentations


Ads by Google