{ } GraphIntermediate interactive

Graph

Nodes and edges modeling any relationship.

graph_bfs
step 1 / 13
ABCDEF

Start BFS at A. Enqueue it.

frontiercurrentvisited
speed1×

How it works

A graph is a set of vertices connected by edges — the most general data structure. It models social networks, maps, dependencies, and the web. Represented as an adjacency list (sparse) or matrix (dense), it's the substrate for traversal and shortest-path algorithms.

Mental models

  • Adjacency list for sparse graphs; matrix when edges are dense.
  • Directed vs undirected, weighted vs unweighted, cyclic vs acyclic.

Complexity

Adj. list space
O(V + E)
Adj. matrix space
O(V²)
Edge lookup (list)
O(degree)
Edge lookup (matrix)
O(1)

Reach for it when

  • Social & road networks
  • Dependency resolution
  • Recommendation systems