(b) acyclic () iff a DFS yeilds no Back edges

2.A 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 .

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) ∈ 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) ∈ 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) ∈ 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) ∈ 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) ∈ 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: Θ(|V | + |E|)

Topological sort

I TS 1. run DFS(G) to compute finishing times f[v] for all v ∈ 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 ∈ V 2. output vertices in order of decreasing times

I Running time: Θ(|V | + |E|)

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 . (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 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. Topological sort

Example:22.4 Topological“Getting-dressed-graph” sort and DFS 613

22.4 Topological sort 613 11/16 undershorts socks 17/18 watch 9/10 11/1612/15undershortspants socksshoes 17/1813/14 shirt 1/8 watch 9/10 (a) 12/156/7 pantsbelt shoes 13/14 shirttie 1/82/5 (a) 6/7 belt jackettie 2/53/4

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 (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 Figure 22.7 (a) Professor Bumstead topologically sorts his clothing when getting dressed. Each topologically sorted, with its vertices arranged from left to right in order of decreasing finishing time. directed edge .u; / means that garment u must be put on before garment . The discovery and All directed edges go from left to right. finishing times from a depth-first search are shown next to each vertex. (b) Thesamegraphshown 6 / 7 topologically sorted, with its vertices arranged from left to right in order of decreasing finishing time. Allpants). directed A directed edges go from edge left.u; to / right.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 pants).order for A getting directed dressed. edge .u; Figure / in the 22.7(b) dag of shows Figure the 22.7(a) topologically indicates sorted that garment dag as anu mustordering be donned of vertices before along garment a horizontal. A topological line such that sort all of directed this dag edges therefore go from gives left an orderto right. for getting dressed. Figure 22.7(b) shows the topologically sorted dag as an orderingThe following of vertices simple along algorithm a horizontal topologically line such that sorts all a directed dag: edges go from left to right. TOPOLOGICALThe following-S simpleORT.G/ algorithm topologically sorts a dag: 1 call DFS.G/ to compute finishing times :f for each vertex 2TOPOLOGICAL as each vertex-SORT is finished,.G/ insert it onto the front of a linked list 31 callreturn DFSthe.G/ linkedto compute list of vertices finishing times :f for each vertex 2 as each vertex is finished, insert it onto the front of a linked list Figure3 return 22.7(b)the linkedshows listhow of the vertices topologically sorted vertices appear in reverse order of their finishing times. FigureWe can 22.7(b) perform shows a topological how the topologically sort in time sorted‚.V verticesC E/, since appear depth-first in reverse search order takesof their‚.V finishingC E/ time times. and it takes O.1/ time to insert each of the jV j vertices onto theWe front can of perform the linked a topological list. sort in time ‚.V C E/, since depth-first search takesWe‚.V proveC theE/ correctnesstime and it of takes thisO.1/ algorithmtime to using insert the each following of the j keyV j vertices lemma char- onto acterizingthe front of directed the linked acyclic list. graphs. We prove the correctness of this algorithm using the following key lemma char- acterizing directed acyclic graphs. Proof: Just need to show that if (u, v) ∈ E, then f[v] < f[u] . When we explore edge (u, v), u is gray, what’s the color of v?

I Is v gray too? no, because then v would be ancestor of u, edge (u, v) is a back edge, a contradiction of a DAG.

I Is v white? yes, then v is descendant of u, by DFS, d[u] < d[v] < f[v] < f[u]

I Is v black? yes, then v is already finished. Since we’re exploring (u, v), we have not yet finished u, therefore f[v] < f[u]

Topological sort Theorem (correctness of the algorithm): TS(G) produces a toplogical sort of a DAG G.

7 / 7 Topological sort Theorem (correctness of the algorithm): TS(G) produces a toplogical sort of a DAG G. Proof: Just need to show that if (u, v) ∈ E, then f[v] < f[u] . When we explore edge (u, v), u is gray, what’s the color of v?

I Is v gray too? no, because then v would be ancestor of u, edge (u, v) is a back edge, a contradiction of a DAG.

I Is v white? yes, then v is descendant of u, by DFS, d[u] < d[v] < f[v] < f[u]

I Is v black? yes, then v is already finished. Since we’re exploring (u, v), we have not yet finished u, therefore f[v] < f[u]

7 / 7