Applications of DFS

Applications of DFS

(b) acyclic (tree) iff a DFS yeilds no Back edges 2.A directed graph is acyclic iff a DFS yields no back edges, i.e., DAG (directed acyclic graph) , no back edges 3. Topological sort of a DAG { next 4. Connected components of a undirected graph (see Homework 6) 5. Strongly connected components of a drected graph (see Sec.22.5 of [CLRS,3rd ed.]) Applications of DFS 1. For a undirected graph, (a) a DFS produces only Tree and Back edges 1 / 7 2.A directed graph is acyclic iff a DFS yields no back edges, i.e., DAG (directed acyclic graph) , no back edges 3. Topological sort of a DAG { next 4. Connected components of a undirected graph (see Homework 6) 5. Strongly connected components of a drected graph (see Sec.22.5 of [CLRS,3rd ed.]) Applications of DFS 1. For a undirected graph, (a) a DFS produces only Tree and Back edges (b) acyclic (tree) iff a DFS yeilds no Back edges 1 / 7 3. Topological sort of a DAG { next 4. Connected components of a undirected graph (see Homework 6) 5. Strongly connected components of a drected graph (see Sec.22.5 of [CLRS,3rd ed.]) Applications of DFS 1. For a undirected graph, (a) a DFS produces only Tree and Back edges (b) acyclic (tree) iff a DFS yeilds no Back edges 2.A directed graph is acyclic iff a DFS yields no back edges, i.e., DAG (directed acyclic graph) , no back edges 1 / 7 4. Connected components of a undirected graph (see Homework 6) 5. Strongly connected components of a drected graph (see Sec.22.5 of [CLRS,3rd ed.]) Applications of DFS 1. For a undirected graph, (a) a DFS produces only Tree and Back edges (b) acyclic (tree) iff a DFS yeilds no Back edges 2.A directed graph is acyclic iff a DFS yields no back edges, i.e., DAG (directed acyclic graph) , no back edges 3. Topological sort of a DAG { next 1 / 7 5. Strongly connected components of a drected graph (see Sec.22.5 of [CLRS,3rd ed.]) Applications of DFS 1. For a undirected graph, (a) a DFS produces only Tree and Back edges (b) acyclic (tree) iff a DFS yeilds no Back edges 2.A directed graph is acyclic iff a DFS yields no back edges, i.e., DAG (directed acyclic graph) , no back edges 3. Topological sort of a DAG { next 4. Connected components of a undirected graph (see Homework 6) 1 / 7 Applications of DFS 1. For a undirected graph, (a) a DFS produces only Tree and Back edges (b) acyclic (tree) iff a DFS yeilds no Back edges 2.A directed graph is acyclic iff a DFS yields no back edges, i.e., DAG (directed acyclic graph) , no back edges 3. Topological sort of a DAG { next 4. Connected components of a undirected graph (see Homework 6) 5. Strongly connected components of a drected graph (see Sec.22.5 of [CLRS,3rd ed.]) 1 / 7 Example: given a DAG Linear ordering: I A TS is not possible if G has a cycle. I The ordering is not necessarily unique. Topological sort I A topological sort (TS) of a DAG G = (V; E) is a linear ordering of all its vertices such that if (u; v) 2 E, then u appears before v. 2 / 7 Linear ordering: I A TS is not possible if G has a cycle. I The ordering is not necessarily unique. Topological sort I A topological sort (TS) of a DAG G = (V; E) is a linear ordering of all its vertices such that if (u; v) 2 E, then u appears before v. Example: given a DAG 2 / 7 I A TS is not possible if G has a cycle. I The ordering is not necessarily unique. Topological sort I A topological sort (TS) of a DAG G = (V; E) is a linear ordering of all its vertices such that if (u; v) 2 E, then u appears before v. Example: given a DAG Linear ordering: 2 / 7 I The ordering is not necessarily unique. Topological sort I A topological sort (TS) of a DAG G = (V; E) is a linear ordering of all its vertices such that if (u; v) 2 E, then u appears before v. Example: given a DAG Linear ordering: I A TS is not possible if G has a cycle. 2 / 7 Topological sort I A topological sort (TS) of a DAG G = (V; E) is a linear ordering of all its vertices such that if (u; v) 2 E, then u appears before v. Example: given a DAG Linear ordering: I A TS is not possible if G has a cycle. I The ordering is not necessarily unique. 2 / 7 Topological sort Applications: call-graph 3 / 7 Topological sort Applications: call-graph EXPLANATION Redding Element Lake Major River Secondary River Canal Sacramento San Francisco Fresno Bakersfield 4 / 7 I Running time: Θ(jV j + jEj) Topological sort I TS algorithm 1. run DFS(G) to compute finishing times f[v] for all v 2 V 2. output vertices in order of decreasing times 5 / 7 Topological sort I TS algorithm 1. run DFS(G) to compute finishing times f[v] for all v 2 V 2. output vertices in order of decreasing times I Running time: Θ(jV j + jEj) 5 / 7 22.4 Topological sort 613 11/16 undershorts socks 17/18 watch 9/10 12/15 pants shoes 13/14 shirt 1/8 (a) 6/7 belt tie 2/5 jacket 3/4 Topologically sorted (b) socks undershorts pants shoes watch shirt belt tie jacket 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4 Figure 22.7 (a) Professor Bumstead topologically sorts his clothing when getting dressed. Each directed edge .u; / means that garment u must be put on before garment . The discovery and finishing times from a depth-first search are shown next to each vertex. (b) Thesamegraphshown topologically sorted, with its vertices arranged from left to right in order of decreasing finishing time. All directed edges go from left to right. pants). A directed edge .u; / in the dag of Figure 22.7(a) indicates that garment u must be donned before garment . A topological sort of this dag therefore gives an order for getting dressed. Figure 22.7(b) shows the topologically sorted dag as an ordering of vertices along a horizontal line such that all directed edges go from left to right. The following simple algorithm topologically sorts a dag: TOPOLOGICAL-SORT.G/ 1 call DFS.G/ to compute finishing times :f for each vertex 2 as each vertex is finished, insert it onto the front of a linked list 3 return the linked list of vertices Figure 22.7(b) shows how the topologically sorted vertices appear in reverse order of their finishing times. We can perform a topological sort in time ‚.V C E/, since depth-first search takes ‚.V C E/ time and it takes O.1/ time to insert each of the jV j vertices onto the front of the linked list. We prove the correctness of this algorithm using the following key lemma char- acterizing directed acyclic graphs. Topological sort Example:22.4 Topological\Getting-dressed-graph" sort and DFS 613 11/16 undershorts socks 17/18 watch 9/10 12/15 pants shoes 13/14 shirt 1/8 (a) 6/7 belt tie 2/5 jacket 3/4 (b) socks undershorts pants shoes watch shirt belt tie jacket 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4 Figure 22.7 (a) Professor Bumstead topologically sorts his clothing when getting dressed. Each directed edge .u; / means that garment u must be put on before garment . The discovery and finishing times from a depth-first search are shown next to each vertex. (b) Thesamegraphshown topologically sorted, with its vertices arranged from left to right in order of decreasing finishing time. All directed edges go from left to right. 6 / 7 pants). A directed edge .u; / in the dag of Figure 22.7(a) indicates that garment u must be donned before garment . A topological sort of this dag therefore gives an order for getting dressed. Figure 22.7(b) shows the topologically sorted dag as an ordering of vertices along a horizontal line such that all directed edges go from left to right. The following simple algorithm topologically sorts a dag: TOPOLOGICAL-SORT.G/ 1 call DFS.G/ to compute finishing times :f for each vertex 2 as each vertex is finished, insert it onto the front of a linked list 3 return the linked list of vertices Figure 22.7(b) shows how the topologically sorted vertices appear in reverse order of their finishing times. We can perform a topological sort in time ‚.V C E/, since depth-first search takes ‚.V C E/ time and it takes O.1/ time to insert each of the jV j vertices onto the front of the linked list. We prove the correctness of this algorithm using the following key lemma char- acterizing directed acyclic graphs. 22.4 Topological sort 613 11/16 undershorts socks 17/18 watch 9/10 12/15 pants shoes 13/14 shirt 1/8 (a) 6/7 belt tie 2/5 jacket 3/4 (b) socks undershorts pants shoes watch shirt belt tie jacket 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4 Figure 22.7 (a) Professor Bumstead topologically sorts his clothing when getting dressed.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    19 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us