Lecture 15 - Directed graphs, DFS, DAGs, TopSort

Date Pre-lecture slides Post-lecture scribbles Async video Lecture recording
October 17 2023 Lecture 15 - Directed graphs, DFS, DAGs, TopSort Lecture 15 - Directed graphs, DFS, DAGs, TopSort Lecture 15 - Directed graphs, DFS, DAGs, TopSort Lecture 15 - Directed graphs, DFS, DAGs, TopSort
 

Notes

Directed Acyclic Graph

Definition

A directed graph G is a directed acyclic graph (DAG) if there is no directed cycle in G.

Concatenation

Properties

Topological Ordering/Sorting

Definition

A topological ordering/topological sorting of G = (V, E) is an ordering $<$ on V such that if $(u → v) \in E $ then $u < v$ .

Informal Definition : One can order the vertices of the graph along a line (say the x-axis) such that all edges are from left to right.

Concatenation

Depth First Search

DFS with pre-post numbering in directed graphs

The Pre-visit number indicates when the node enters the DFS recursion stack, and the Post-visit number indicates when the node exits the DFS recursion stack. Pre and Post numbers can be used to determine whether a particular node is in the sub-tree of another node.

DFS(G)
    Mark all nodes u as unvisited
    T is set to ∅
    time = 0
    while there is an unvisited node u do
        DFS(u)
    Output T
DFS(u)
    Mark u as visited
    pre(u) = ++time
    for each edge (u, v) in Out(u) do
        if v is not visited
            add edge (u, v) to T
            DFS(v)
    post(u) = ++time
Properties
Example

Concatenation

Edges of G can be classified with respect to the DFS tree T as:

Concatenation

Cycle detection in directed graph using topological sorting

Given a graph G, if it is a Directed Acyclic graph then compute a topological sort. If it failes, then output the cycle C.

The algorithm will be as follows:

The above algorithm runs in $O(n + m)$ time.

Graph of strong connected components

Concatenation

Let $S_1, S_2, . . . S_k$ be the strong connected components (i.e.,SCCs) of G. The graph of SCCs is $G^{SCC}$. It is created by collapsing every strong connected component to a single vertex.

For a directed graph G, its meta-graph $G^{SCC}$ is a DAG.

The straightforward algorithm(discussed in Lecture 15) to find all SCCs of a given directed graph has a running time of $O(n(n + m))$.

The Linear time Algorithm for SCCs will be as follows:

If v is the vertex with maximum post numbering in $DFS(G^{rev})$. Then v is in a SCC S, such that S is a sink of $G^{SCC}$. So, we can find a vertex in a sink SCC of $G^{SCC}$ for the linear time algorithm. Let us assume $G1=G^{rev}$.

do DFS(G1) and output vertices in decreasing post order.
Mark all nodes as unvisited
for each u in the computed order do
    if u is not visited then
        DFS(u)
        Let S1 be the nodes reached by u
        Output S1 as a strong connected component
        Remove S1 from G

The above algorithm runs in time $O(m + n)$ and correctly outputs all the SCCs of G.

Additional Resources

Contributors

Sindhu Vydana