Graph Theory Basics Cheat Sheet

Graph theory fundamentals — vertices, edges, directed/undirected, weighted graphs, BFS/DFS traversal, Dijkstra shortest path, and graph representations.

Last Updated: July 15, 2025

Graph Terminology

TermDefinition
Vertex (V)Node — a point in the graph
Edge (E)Connection between two vertices
DirectedEdge has direction — (u → v) ≠ (v → u)
UndirectedEdge is bidirectional — {u, v} = {v, u}
WeightedEdge has cost/distance value
DegreeNumber of edges connected to vertex (in-degree + out-degree for directed)
PathSequence of vertices connected by edges
CyclePath that starts and ends at same vertex

Representations

MethodSpaceEdge CheckNeighbors
Adjacency ListO(V + E)O(degree)O(degree)
Adjacency MatrixO(V²)O(1)O(V)
Edge ListO(E)O(E)O(E)

BFS vs DFS

PropertyBFSDFS
Data StructureQueue (FIFO)Stack (LIFO) or recursion
FindsShortest path (unweighted)Any path, cycles, components
TimeO(V + E)O(V + E)
Use CaseSocial distance, web crawlingTopological sort, maze solving

Dijkstra's Algorithm

Input
Weighted graph, source vertex S — non-negative edge weights
Initialize
dist[S] = 0, all others = ∞. Min-heap with (0, S)
While heap not empty
Pop (d, u). For each neighbor v: if d + w(u,v) < dist[v], update and push
Time
O((V+E) log V) with binary heap, O(E + V log V) with Fibonacci heap

Key Graph Types

TypeProperties
TreeConnected, acyclic — |E| = |V| − 1
DAGDirected Acyclic Graph — topological ordering exists
Complete (Kₙ)Every pair connected — |E| = n(n−1)/2
BipartiteVertices can be split into 2 sets, edges only cross sets
Pro Tip: Adjacency list beats adjacency matrix for sparse graphs (most real-world graphs). Space: O(V+E) vs O(V²). Use matrix only for dense graphs or when edge-existence checks dominate.
Part of the Empire Builder Network