Skip to content
Part IA Lent Term

Special Graph Classes

Directed Acyclic Graphs (DAGs)

A DAG is a directed graph containing no directed cycles. Many real-world dependency structures are naturally acyclic: course prerequisites, build system targets (make), version histories.

Existence of sources and sinks: Every DAG has at least one source (in-degree 0) and at least one sink (out-degree 0). Proof: pick any vertex and follow incoming edges backward. Since there are no cycles and the graph is finite, this walk must terminate at a source. Symmetrically, following outgoing edges forward reaches a sink. This property is used in the source-removal topological sort algorithm.

Topological ordering: A linear ordering exists such that all edges go left to right iff the graph is a DAG. See topological sort for the full algorithm.

SSSP on DAGs: Relax edges in topological order. Correctness: when vertex uu is processed, all incoming paths to uu have already been considered because all predecessors appear earlier in the topological order. Thus one pass suffices. Running time: Θ(V+E)\Theta(|V| + |E|), even with negative edge weights (provided no cycles exist, which they cannot in a DAG). This is the fastest SSSP algorithm amongst those covered.

Longest path on DAGs: By negating all edge weights, or replacing min\min with max\max during relaxation, the longest path can also be found in Θ(V+E)\Theta(|V| + |E|). For general graphs, the longest path problem is NP-hard; DAGs are a notable exception.

Bipartite Graphs

A graph is bipartite if its vertex set can be partitioned into two disjoint sets LL and RR such that every edge has one endpoint in each set. No edges exist within LL or within RR.

Characterisation: An undirected graph is bipartite iff it contains no cycle of odd length. A graph with a triangle (K3K_3) is never bipartite; a tree is always bipartite.

Testing bipartiteness (BFS-based algorithm):

  1. Pick any unvisited vertex, colour it BLACK.
  2. Run BFS from it. Colour each vertex at distance dd: BLACK if dd is even, RED if dd is odd (alternating by parity).
  3. Scan all edges. If any edge connects two vertices of the same colour, an odd cycle exists — the graph is not bipartite.
  4. Repeat for each connected component until all vertices are processed.

Both colouring and conflict checking run in O(V+E)O(|V|+|E|), so the full test is Θ(V+E)\Theta(|V|+|E|). BFS’s distance-level property guarantees the alternating colour assignment is correct: vertices at the same BFS level cannot be adjacent in a bipartite graph.

Worked example: 4-cycle: V={1,2,3,4}V = \{1,2,3,4\}, E={{1,2},{2,3},{3,4},{4,1}}E = \{\{1,2\}, \{2,3\}, \{3,4\}, \{4,1\}\}. BFS from 1: dist(1)=0 (BLACK), dist(2)=dist(4)=1 (RED), dist(3)=2 (BLACK). No conflict — bipartite with L={1,3}L=\{1,3\}, R={2,4}R=\{2,4\}.

Worked non-example: Triangle K3K_3: V={1,2,3}V=\{1,2,3\}, E={{1,2},{2,3},{3,1}}E=\{\{1,2\},\{2,3\},\{3,1\}\}. BFS from 1: dist(1)=0 (BLACK), dist(2)=dist(3)=1 (RED). Edge {2,3}\{2,3\} connects two RED vertices — conflict, not bipartite.

Applications: Matching problems (assigning students to rooms, workers to tasks) are modelled as bipartite graphs. Maximum bipartite matching reduces to a max-flow problem and can be solved with Ford-Fulkerson or Hopcroft-Karp.

Complete Graphs

The complete graph KnK_n has all (n2)\binom{n}{2} possible edges; every vertex has degree n1n-1. KnK_n is (n1)(n-1)-regular. A clique is any complete induced subgraph.

Trees (Detailed)

For an undirected graph on nn vertices, the following are equivalent definitions of a tree:

  1. Connected and acyclic.
  2. Connected and has exactly n1n-1 edges.
  3. Acyclic and has exactly n1n-1 edges.
  4. Between any two vertices, exactly one simple path exists.
  5. Maximally acyclic: adding any edge creates exactly one cycle.
  6. Minimally connected: removing any edge disconnects the graph.

Every tree with n2n \ge 2 has at least two leaves (vertices of degree 1). A rooted tree designates one vertex as root; parent and child relationships emerge from distances to the root. The depth of a vertex is its distance from the root. A binary tree restricts each vertex to at most two children. Every tree is bipartite (alternate levels).

Transpose, Complement, and Square

  • Transpose GTG^T: reverses all edges. (u,v)E    (v,u)ET(u,v) \in E \iff (v,u) \in E^T. Essential for Kosaraju’s algorithm.
  • Complement G\overline{G}: same vertices, (u,v)E(G)(u,v) \in E(\overline{G}) iff (u,v)E(G)(u,v) \notin E(G). A clique in GG is an independent set in G\overline{G}.
  • Square G2G^2: edge (u,v)(u,v) exists iff a path of length 2\le 2 connects them in GG.

Summary

Graph ClassKey PropertyDetection Time
DAGNo directed cycles; 1\ge 1 source and sinkΘ(V+E)\Theta(V+E) (DFS)
BipartiteNo odd cycles; 2-colourableΘ(V+E)\Theta(V+E) (BFS)
Complete KnK_nAll (n2)\binom{n}{2} edges presentO(1)O(1) (check degrees)
TreeE=V1\lvert E \rvert = \lvert V \rvert - 1, connected and acyclicΘ(V+E)\Theta(V+E) (DFS/BFS)
ForestDisjoint union of treesΘ(V+E)\Theta(V+E)
PropertyDAGGeneral digraph
SSSP timeΘ(V+E)\Theta(V+E)O((V+E)logV)O((V+E)\log V) / O(VE)O(VE)
Longest pathΘ(V+E)\Theta(V+E)NP-hard
Topological sortAlways existsOnly if acyclic

Past paper questions: y2026p1q10 (DAG, topological sort, transitive closure for DAGs), y2025p1q9(b)(i) (connected components labelling).