CS 411 Fall 2025  >  Outline & Supplemental Notes for September 19, 2025


CS 411 Fall 2025
Outline & Supplemental Notes
for September 19, 2025

Outline

Topological Sort [L 4.2]

Supplemental Notes

Topological Sort via Find-Source

The time-consuming part of the find-source method is finding a source. An idea of A.B. Kahn [1962] is to keep track of the set of all sources and update it when we remove a vertex from the digraph. When we remove a vertex \(s\), the only possible new sources are its out-neighbors: vertices that are heads of arcs whose tail is \(s\).

An efficient way to handle updating the set of sources is to keep track of each vertex’s in-degree: the number of arcs pointing to that vertex. Having found a source \(s\), we remove it from the digraph by:

  1. removing \(s\) from the set of sources, and
  2. decrementing the in-degree of each out-neighbor of \(s\).

Whenever a vertex’s in-degree reaches zero, we add it to the set of sources.

Here is our algorithm in more detail. We are given a digraph with \(V\) vertices.

If the above algorithm does not exit early, then it constructs a topological sort for the given digraph.

See topological_sort.cpp for an implementation of the find-source topological sort algorithm in iterative form.

We conclude by analyzing the above algorithm. Our input size will be written in terms of the number of vertices (\(V\)) and the number of arcs (\(E\)). Our basic operations will all single operations on vertices or arcs. We will assume that all single-item operations on the set of sources are constant-time (which is true if the set is implemented using, for example, a linked-list-based queue).

If the digraph is represented using an ajacency matrix, then we have two sets of nested loops. In each case, both loops execute at most \(V\) times. Thus, \(\Theta(V^2)\) basic operations are performed in the worst case.

If the digraph is represented using adjacency lists, then the inner loops go through all edges at most once, over all iterations of the outer loops. The outer loops both execute at most \(V\) times. Thus, \(\Theta(V + E)\) basic operations are performed in the worst case.

Note that in both cases, the order of this algorithm is the same as that of our DFS and BFS algorithms.