

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of topological sort and strongly connected components in directed acyclic graphs (DAGs). It provides an overview of two algorithms for topological sorting, Kahn's Algorithm and Tarjan's Algorithm, and explains how Kosaraju's Algorithm can be used to compute the SCCs of a graph. lecture notes and readings for CIS 121, a course on Data Structures and Algorithms, and is relevant for students studying computer science and related fields.
Typology: Lecture notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Topological Sort / Strongly Connected Components—Monday, October 24 / Tuesday, October 25
A topological sort of a directed acyclic graph (DAG) G = (V, E) is an ordering of the vertices such that for each directed edge (u, v) ∈ E, u appears before v in the ordering. As described in the below algorithms, topologically sorting a DAG only takes O(m + n) time, so given a DAG, it is helpful to topologically sort it, since most graph algorithms take Ω(m + n) time anyway. In other words, topologically sorting a DAG is usually a free step, and if not necessary for your algorithm, it can make reasoning/thinking about the problem easier since it gives you a visual. Below are two algorithms to find a topological sort:
Kahn’s Algorithm
Every DAG has a source node, or a node with no incoming edges. Kahn’s algorithm relies on this intuition — at a high-level, the algorithm operates by repeatedly finding a source node, putting it next in the topological sort, removing the node and all of the edges incident on it from the graph, and repeating this process.
Kahn’s algorithm runs in O(m + n) time. As seen in the pseudocode, the first step to compute the in-degree of each node takes O(m + n) time since for each node, we scan through its neighbors; the second step to populate the queue takes O(n) time since we iterate through all of the vertices. In our while loop, note that we enqueue each node exactly once and scan through each of its neighbors, performing constant work for each, which takes O(m + n) time.
Tarjan’s Algorithm
Tarjan’s algorithm leverages the finishing times of DFS as shown in the pseudocode by just running DFS and then returning the nodes in decreasing order of finishing times. Thus, it also runs in O(m + n) time. This algorithm provides key insight into the following algorithm to find the strongly connected components of a directed graph.
Given a directed graph G = (V, E), a strongly connected component (SCC) is a maximal set S ⊆ V such that for all u, v ∈ S, there exists a path u ⇝ v and a path v ⇝ u. Thus, we can decompose a directed graph G into its SCCs, yielding GSCC^ or our kernel graph. Formally, GSCC^ = (V SCC^ , ESCC^ ). Each vertex vi in GSCC^ represents a single SCC Ci in G, and an edge (vi, vj ) exists in GSCC^ if G contains the directed edge (x, y) where x is in SCC Ci and y is in SCC Cj. Observe that GSCC^ is a DAG, meaning that we can topologically sort it to make the problem easier to think about.
Kosaraju’s algorithm is an algorithm that we can use to compute the SCCs of a graph, and by extension, to obtain GSCC^. It operates by running two DFS traversals, one on G and another on GT^ , the transposed graph obtained by reversing the direction of edges in G; in the latter, we consider vertices in order of decreasing finishing times.
Thus, Kosaraju’s runs in O(m + n) time. As seen in the pseudocode, the first step is just DFS, which takes O(m + n) time; the second step is computing GT^ , but this can be done in O(m + n) time since we just reverse the direction of edges; and the third step is also just DFS, which takes O(m + n) time.
A graph G = (V, E) is “almost strongly connected” if adding a single edge makes the graph strongly connected. Design an O(|V | + |E|) algorithm to determine whether a graph is almost strongly connected.