Reference

Data structures & costs

Compare operation costs, ordering, invariants and common algorithm notation.

Scope and conventions

Abstract data structures and conventional implementations. Costs use a unit-cost RAM model unless stated; n is item count, V vertices, E edges and k key/output length. Native containers can have different contracts. Expected, amortized and worst-case bounds are distinguished.

Graphs & traversal

Adjacency-list graph

Operations & costs

Store each vertex’s outgoing neighbors. Space is linear in vertices plus stored edges.

Fragment

Operations & costs · Adjacency-list graph
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Space                     O(V + E)
List neighbors of v       O(deg(v))
Test an edge (u,v)        O(deg(u)) with unsorted lists

Undirected edges commonly appear in both endpoint lists; parallel edges and self-loops need defined handling.

Adjacency-matrix graph

Operations & costs

Store one entry per vertex pair. Dense graphs can benefit from direct edge tests; sparse graphs pay for absent edges.

Fragment

Operations & costs · Adjacency-matrix graph
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Space                     O(V²)
Test/update one edge      O(1)
List neighbors of v       O(V)

The matrix can store booleans, weights or counts; absence needs an unambiguous representation.

Breadth-first search

Operations & costs

BFS uses a queue to explore by edge distance. It finds shortest path lengths in an unweighted graph or when every edge has the same positive cost.

Fragment

Operations & costs · Breadth-first search
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Adjacency-list traversal  O(V + E)
Working space             O(V)
Frontier                  FIFO queue

Mark discovered vertices consistently to avoid duplicate work. Weighted shortest paths need a different algorithm.

Depth-first search

Operations & costs

DFS follows a path before returning to alternatives. Recursive and explicit-stack forms need a visited/active-state policy suitable for the graph.

Fragment

Operations & costs · Depth-first search
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Adjacency-list traversal  O(V + E)
Working space             O(V)
Frontier                  recursion or explicit stack

DFS order is not generally shortest-path order. A recursive path can exhaust the call stack.

Dijkstra shortest paths

Operations & costs

Dijkstra finalizes the smallest tentative distance and relaxes outgoing edges. It requires nonnegative edge weights.

Fragment

Operations & costs · Dijkstra shortest paths
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
With adjacency lists + binary heap:
Time                      O((V+E) log V) conventional bound
Distance/visited storage  O(V)

Lazy duplicate heap entries can use O(E) heap space and alter the precise bound; stale entries must be skipped.

Topological order

Operations & costs

A topological order places each directed edge’s source before its destination. It exists only for a directed acyclic graph.

Fragment

Operations & costs · Topological order
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Kahn queue or DFS-based method:
Time                      O(V + E)
Cycle present             no complete topological order