DSAVisualiseData Structures & Algorithms Studio
Chapter 07 · graph-traversal, graph-representation, dependency-graph, union-find

Graphs & Connectivity

Graphs model pairwise relationships between entities. Traversal algorithms explore reachability; topological sorting resolves dependency orders; Union-Find tracks connected components under dynamic edge additions; Kruskal algorithm builds Minimum Spanning Trees (MSTs) by greedily uniting non-cyclic edges.

Key Invariants & Rules

  • Breadth-First Search (BFS) discovers vertices in non-decreasing order of edge distance from the source.
  • Depth-First Search (DFS) backtracks only when all adjacent vertices from the current path are explored.
  • Union-Find root compression flattens tree depth during find, achieving near-constant inverse Ackermann time.
  • Kruskal algorithm sorts edges by weight and includes edge (u, v) if and only if find(u) != find(v).
  • Topological sort exists if and only if the directed graph contains no directed cycles (DAG).

Must-Know Algorithms

BFS & Multi-Source BFSO(V + E) time, O(V) space

Computes unweighted shortest paths and multi-source wavefront propagation.

Kruskal MST & Union-FindO(E log E) time, O(V) space

Builds minimum connected spanning forest using greedy cycle-check connectivity.

Erdos Number Collaboration BFSO(V + E) time

Social collaboration network distance tracking using queue BFS.

Learning Path (11 Lessons)

Self-Check & Concept Review

1. What data structure is essential to implement Breadth-First Search (BFS)?

A FIFO queue ensures that all vertices at distance d are visited before any vertex at distance d+1.

2. In Kruskal algorithm for Minimum Spanning Trees, how are cycles detected when considering a candidate edge (u, v)?

If u and v share the same representative root in Union-Find, adding edge (u, v) would create a cycle.

3. What does a multi-source BFS wavefront compute for every vertex in a graph?

Enqueuing all sources initially with distance 0 propagates a simultaneous wavefront finding nearest-source distances.

4. When is a topological sort of a directed graph possible?

If a cycle exists, mutual dependencies prevent any valid linear ordering where every directed edge goes from earlier to later.

5. What is the purpose of path compression in the Union-Find find operation?

Path compression rewires intermediate parent pointers directly to the root during find, giving nearly O(1) amortized cost per operation.