Nodes, edges, directed/undirected, weighted, and adjacency representations.
A graph is a set of nodes (vertices) connected by edges. Graphs model networks, maps, dependencies, and social connections. Understanding representation and terminology is the prerequisite for all graph algorithms.
Traversal O(V + E)
Adjacency list O(V + E) · Adjacency matrix O(V²)
Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.
directed edges have a source and destination; undirected go both ways
weighted edges have a cost; unweighted edges are equal
map each node to a list of neighbors; space O(V + E)
V×V boolean matrix; fast edge lookup O(1) but O(V²) space
number of edges connected to a node
a path that starts and ends at the same node
all nodes reachable from any starting node
Directed Acyclic Graph; used for dependency modeling