Searching and Sorting Graphs
Overview
Kent Quanrud develops graph-search techniques from topological sorting through depth-first search (DFS) to strongly connected components (SCCs). He shows how DFS finishing order yields a second linear-time topological sort for directed acyclic graphs, then derives the two-pass Kosaraju algorithm, which finds every SCC in O(n + m) time by searching the reversed graph first and the original graph next.
Key takeaways
- For a DAG, repeatedly removing zero-indegree sources computes a topological order in O(n + m) time when indegrees and newly available sources are maintained incrementally.
- DFS requires visited marks because recursion alone does not shrink a graph containing cycles; with marks, each vertex and edge is processed at most once.
- In a DAG, DFS postorder is reverse topological order: reversing the list gives a topological ordering in O(n + m) time.
- A vertex’s SCC is the intersection of vertices reachable from it in the original graph and vertices reachable from it in the reversed graph.
- Kosaraju’s algorithm finds all SCCs in O(n + m) time by computing DFS postorder in the reversed graph, then searching the original graph in decreasing finishing-time order.
- Contracting SCCs always yields a DAG, because any cycle among contracted components would make those components mutually reachable and therefore part of one SCC.
Chapters
- The lecture uses n for the number of vertices and m for the number of edges.
- Directed edges have an orientation; undirected edges represent mutual connections.
- Adjacency lists store each vertex’s outgoing neighbors, while adjacency matrices use an n × n table.
- A directed acyclic graph (DAG) has no directed cycle; vertices with no incoming edges are sources, and vertices with no outgoing edges are sinks.
- Boolean circuits form DAGs: inputs flow through gates such as AND, OR, and NOT toward outputs.
- A topological ordering puts each vertex before the vertices it points to, allowing circuit gates to be evaluated in order.
- Every nonempty DAG has a source: following incoming edges indefinitely would repeat a vertex and create a cycle.
- Repeatedly output and remove a source; update the indegrees of its neighbors and add any newly zero-indegree vertices to a queue.
- With indegrees maintained as bookkeeping, source removal computes a topological ordering in O(n + m) time.
- The maze asks whether a path connects the starting cat to the goal while alternating between cats facing toward and away from the viewer.
- Modeling locations and permitted moves as vertices and directed edges turns the puzzle into a reachability question.
- A second example asks whether a directed graph’s start vertex S can reach every other vertex; a cycle with no incoming edge demonstrates why the answer can be no.
- A natural recursive procedure tests each outgoing neighbor V to see whether it can reach the target T.
- On a cycle, recursive calls can revisit the same vertices indefinitely because the graph does not shrink between calls.
- The missing ingredient is memory of visited vertices, which prevents repeated work and breaks recursive cycles.
- DFS marks a vertex when first visited, explores its neighbors, and returns immediately if it encounters a marked vertex.
- The marks act like breadcrumbs in a maze, preventing the search from cycling forever.
- Each vertex is marked once and each outgoing edge is examined at most once, so a search takes O(n + m) time.
- Assume a vertex W is reachable from the DFS start but remains unmarked; a path from the start to W must cross from marked vertices to unmarked ones.
- At that crossing, DFS has marked a vertex A with an edge to an unmarked vertex B.
- DFS examines A’s outgoing edges, so it must visit B, a contradiction that proves all reachable vertices are marked.
- DFS finishes a vertex only after its recursive exploration of reachable unmarked vertices returns.
- If V can reach W but W cannot reach V, any DFS that visits both finishes W before V.
- The claim follows by considering whether DFS marks V or W first: either W is explored inside V’s call, or W’s search cannot reach V.
- A DFS driver calls DFS from every still-unmarked vertex so disconnected portions of the graph are included.
- Appending each vertex when its DFS call finishes produces postorder; in a DAG, this is reverse topological order.
- Reversing the postorder list gives another O(n + m) topological sort, using visited marks and finishing order.
- Two vertices are strongly connected when each can reach the other; mutual reachability is transitive and therefore partitions vertices into equivalence classes.
- Each class is a strongly connected component (SCC), so vertices in one SCC have identical pairwise reachability relationships to vertices outside it.
- Contracting each SCC into one vertex produces a DAG: a cycle among contracted components would mean they belong to one larger SCC.
- A forward DFS from vertex V finds all vertices reachable from V; a DFS from V in the reversed graph finds all vertices that can reach V in the original graph.
- The intersection of those two reachable sets is exactly V’s SCC.
- Building the reversed adjacency lists and running both searches takes O(n + m) time, unlike checking reachability separately from every vertex.
- Running the one-component procedure repeatedly can still revisit large parts of the graph and take quadratic time.
- If an SCC is a sink in the condensation DAG, a forward DFS from any vertex in it cannot leave the component.
- Removing sink components one at a time would avoid redundant searches, so the challenge is to identify a sink SCC efficiently.
- The DFS finishing-time relation orders vertices across SCCs: if one component can reach another, its vertices finish later when DFS searches the original graph.
- Running DFS on the reversed graph makes original-graph sink components appear at the end of the postorder list.
- After removing a sink component, the same ordering principle applies to the remaining graph, allowing the process to continue.
- First run DFS on the reversed graph and record vertices in postorder.
- Process the list from last to first, running DFS in the original graph from each still-unassigned vertex; each search returns one SCC.
- Mark or remove each discovered component so every vertex and edge is processed only a constant number of times, yielding O(n + m) total time.
Transcript excerpt
0:04 All right. Uh let's do um let's let's do some computer science. Okay. So, searching and sorting uh graphs today. So, okay. We're going to talk about a bunch of topics. You might have seen something called depth first search before in discrete math or something, but probably my perspective will be a little different. So, hopefully you've seen grass before. From here going forward, we're going to do a lot of stuff in grass. We had a couple of problems on your homework dealing with graphs. But you know, you know, you have vertices, you have edges. We'll often use n for the number of vertices, m for the number of edges. Uh there's two kinds of edges in this class. One is an undirected edge with like a mutual connection between two points. The other is a directed edge. There's some asymmetry. Okay? And depending on the context.
0:52 Now, uh you guys have probably been exposed to this before. What are some ways to maybe represent uh a graph in your computer? Yeah, >> you can do like a list. >> Yeah, a list. So, just for every vertex, I'm just going to list off like uh all the edges uh that leave it. Sometimes we call that an adjacency list. What else? Yeah, >> I forgot what it's called, but it's the matrix where >> Yeah, it's called a matrix. So an adjacy matrix where you you just make like a full n byn table like this. Anyway, it's not I don't think that uh these subtleties will come up really in any of our problems, but it's just good to be aware that you are probably representing on your computer somehow. Okay, so uh three main topics uh for today. We're going to talk about
1:42 something called directed asyclic graphs
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Kent Quanrud.