dsaGraphsOverview

Graphs

Basics

  • A graph is a set of nodes (vertices, VV) and edges (EE) connecting them. A node can point to itself, and E≤V2E \le V^2.
  • Types:
    • Directed: edges have a direction (A to B is not B to A). Undirected: edges go both ways, so store each edge twice.
    • Weighted: edges carry a cost. Unweighted: all edges cost the same.
    • Cyclic or acyclic. A directed acyclic graph is a DAG.
    • Connected or disconnected: loop over all nodes when starting a traversal, since one start may not reach everything.
  • A tree is a connected, undirected graph with no cycles and exactly V−1V - 1 edges.

Representations

RepresentationSpaceEdge lookupList neighborsUse when
Matrix (grid)O(n⋅m)O(n \cdot m)O(1)O(1)O(1)O(1) (4 or 8 directions)The input is a 2D grid; the grid is the graph
Adjacency matrixO(V2)O(V^2)O(1)O(1)O(V)O(V)Dense graphs, frequent “is there an edge” queries
Adjacency listO(V+E)O(V + E)O(degree)O(\text{degree})O(degree)O(\text{degree})Default for most problems, especially sparse graphs
  • Matrix (grid): each cell is a node and its neighbors are the adjacent cells. Values such as 0 or 1 usually mean open or blocked. Use a directions array ([0,1], [0,-1], [1,0], [-1,0]) and bounds checks. See Matrix DFS and Matrix BFS.
  • Adjacency matrix: a V×VV \times V array where m[i][j] is 1 (or the weight) if there is an edge from i to j. For undirected graphs it is symmetric (m[j][i] is set too). It takes the same space even for sparse graphs.
  • Adjacency list: a hash map from each node to the list of its neighbors, built from an edge list. In the worst case a graph has about V2/2V^2/2 edges, which makes it O(V2)O(V^2) too. See Adjacency List.
  • Inputs arrive as an edge list ([[a, b], ...]); convert it to an adjacency list first. Remember to add the reverse edge for undirected graphs.

BFS vs DFS

BFSDFS
UsesQueueStack or recursion
TimeO(V+E)O(V + E)O(V+E)O(V + E) with a visited set
SpaceO(V)O(V)O(V)O(V) worst case (path depth)
Best forShortest path in an unweighted graph, minimum steps, level by level, multi-source spreadExistence of a path, connected components, counting paths (backtracking), cycle detection, topological sort
  • BFS layer by layer: loop len(queue) times per level and increment the distance after each level. Mark nodes visited when they are pushed, not when popped, to avoid duplicates in the queue.
  • Multi-source BFS: push all sources first (for example all rotten oranges) and run a single BFS.
  • Counting all paths with backtracking requires removing the node from visited after exploring it. This is exponential: up to O(4n⋅m)O(4^{n \cdot m}) on a grid, versus O(n⋅m)O(n \cdot m) for plain DFS with a permanent visited set.
  • Plain adjacency list BFS is O(V+E)O(V + E) time and O(V)O(V) space.
  • Do not reset visited inside the recursive call; it must be shared across calls (create it once).

Cycle detection

  • Undirected graph: DFS with a visited set; reaching a visited node that is not the parent means a cycle. Union-Find also works: an edge whose endpoints already share a root closes a cycle (see Union-Find).
  • Directed graph: DFS with two sets.
    • Add the node to the path (in-progress) set before recursing into its neighbors and remove it afterwards.
    • Meeting a node that is currently in path means a cycle.
    • After finishing a node, add it to a completed set (or keep it in visited) so that it is not explored again.
  • Alternatively run Topological Sort (Kahn’s algorithm): if fewer than VV nodes are output, there is a cycle.

Shortest path

  • Unweighted: BFS, O(V+E)O(V + E).
  • Non-negative weights: Dijkstra’s, single source to all nodes. BFS is not enough on weighted graphs.
    • Greedy: always expand the unvisited node with the smallest known distance (min heap of (distance, node)). The first time a node is popped, its distance is final.
    • Skip nodes already finalized when popping (stale heap entries).
    • Time O(Elog⁡V)O(E \log V) (O(Elog⁡E)O(E \log E) is equivalent), space O(V+E)O(V + E).
    • Does not work with negative edge weights; use Bellman-Ford instead (O(V⋅E)O(V \cdot E)).
    • Unreachable nodes remain without a distance; decide on a sentinel such as -1 or infinity.
  • Variants: the “weight” can be a different cost, for example the maximum height along a path (Swim in Rising Water) or a probability multiplied along edges (Path with Maximum Probability, using a max heap).

Minimum spanning tree (MST)

A spanning tree connects all VV vertices of a connected, undirected, weighted graph with V−1V - 1 edges and no cycle; the MST has the minimum total weight.

Prim’sKruskal’s
IdeaGrow one tree from a start node, always adding the cheapest edge to a new nodeSort all edges, add the cheapest edge that does not form a cycle
StructuresMin heap, visited setSorted edge list, Union-Find
TimeO(Elog⁡E)O(E \log E)O(Elog⁡E)O(E \log E) (sorting dominates)
Better forDense graphs, adjacency list already builtSparse graphs, edge list input
  • Both produce an MST with V−1V - 1 edges for a connected graph; the tree may not be unique when weights tie.
  • Kruskal: stop once V−1V - 1 edges have been added. If the graph is disconnected, the result is a minimum spanning forest.
  • Prim: a node popped that is already visited is skipped, which is what avoids cycles.

Union-Find (disjoint set)

  • Tracks connected components with find(x) (root of the component) and union(a, b) (merge two components, returning false if already connected).
  • With path compression and union by rank or size, each operation is nearly O(1)O(1) (inverse Ackermann).
  • Use it for: dynamic connectivity, counting components, detecting the edge that creates a cycle, grouping (accounts merge), Kruskal’s. See Union-Find.

Topological sort

  • A linear ordering of a DAG’s nodes such that for every edge A to B, A comes before B. Only possible if the graph has no cycle. Time O(V+E)O(V + E).
  • DFS version: run DFS from every unvisited node, append a node to the result after all its neighbors are done (postorder), then reverse the result. A node that finishes last goes first.
  • Kahn’s (BFS) version: compute in-degrees, start from nodes with in-degree 0, and when removing a node decrement its neighbors’ in-degrees, queueing those that reach 0. If the output has fewer than VV nodes, the graph has a cycle.
  • Recognition: “prerequisites”, “dependencies”, “order of tasks”, “build order”, “alien dictionary”.

See Topological Sort.

Problem signal to algorithm

Problem signalAlgorithm
Grid, count regions or islands, flood fill, areaMatrix DFS (or BFS)
Grid, minimum steps, shortest path with unit cost, spreading over timeMatrix BFS
Count or list all paths in a grid or graphDFS with backtracking
Shortest path, unweightedBFS
Shortest path, weighted with non-negative weightsDijkstra’s
Connect all points or nodes at minimum total costPrim’s or Kruskal’s
Are two nodes connected, number of components, redundant edgeUnion-Find (or DFS)
Prerequisites, dependency order, detect impossible orderingTopological Sort
Clone or copy a graph, build from edges and traverseAdjacency List with DFS or BFS and a map of visited nodes
Cycle in a directed graphDFS with a path set, or topological sort
Cycle in an undirected graphDFS with parent tracking, or Union-Find

Templates

  • Matrix DFS: grid DFS, counting paths with backtracking.
  • Matrix BFS: grid BFS for the shortest path.
  • Adjacency List: building an adjacency list, then DFS path counting and BFS shortest path.
  • Dijkstra’s: shortest paths from a source with a min heap.
  • Prim’s: minimum spanning tree grown from a start node.
  • Kruskal’s: minimum spanning tree by sorted edges and Union-Find.
  • Union-Find: disjoint set with path compression and union by rank.
  • Topological Sort: ordering of a DAG.

Problems

ProblemPattern / template
Number of IslandsMatrix DFS
Max Area of IslandMatrix DFS
Shortest Path in Binary MatrixMatrix BFS
Rotting OrangesMatrix BFS, multi-source
Clone GraphAdjacency List
Course ScheduleAdjacency List, Topological Sort, cycle detection
Course Schedule IITopological Sort
Course Schedule IVTopological Sort
Sort Items by Groups Respecting DependenciesTopological Sort
Alien DictionaryTopological Sort
Network Delay TimeDijkstra’s
Swim in Rising WaterDijkstra’s
Path with Maximum ProbabilityDijkstra’s, max heap
Min Cost to Connect All PointsPrim’s or Kruskal’s
Find Critical and Pseudo-Critical Edges in Minimum Spanning TreePrim’s or Kruskal’s
Redundant ConnectionUnion-Find
Accounts MergeUnion-Find
Longest Consecutive SequenceUnion-Find
Number of Connected Components in an Undirected GraphUnion-Find