Math for coding interviews/Vertices, edges, and adjacency

Graph Complexity: Vertices, Edges, and O(V+E)

Graph complexity uses two variables: V (vertices = nodes) and E (edges = connections). A graph with V nodes can have anywhere from 0 edges (isolated nodes) to V×(V-1)/2 edges (every node connected to every other). This is why graph complexity is written as O(V+E).

See it for yourself

Two graphs side by side. Sparse graph: 10 nodes, few connections, looks like a sparse constellation. Dense graph: 10 nodes, almost every pair connected, looks like a tangled web.

Vertices, edges, and adjacency

Graph complexity uses two variables: V (vertices = nodes) and E (edges = connections). A graph with V nodes can have anywhere from 0 edges (isolated nodes) to V×(V-1)/2 edges (every node connected to every other). This is why graph complexity is written as O(V+E).

Visual: Two graphs side by side. Sparse graph: 10 nodes, few connections, looks like a sparse constellation. Dense graph: 10 nodes, almost every pair connected, looks like a tangled web.

Think of it this way: A road network. V = number of cities. E = number of roads connecting them. Driving from city to city, you might visit V cities and travel E roads total.

For algorithms: BFS and DFS visit every node once and check every edge once, giving O(V+E). The edge count E determines whether this is closer to O(V) or O(V²) in practice.

Real-world analogy

A road network. V = number of cities. E = number of roads connecting them. Driving from city to city, you might visit V cities and travel E roads total.

Why it matters in interviews

BFS and DFS visit every node once and check every edge once, giving O(V+E). The edge count E determines whether this is closer to O(V) or O(V²) in practice.

Where it shows up on the learning path