Adjacency-list graph
#Store each vertex’s outgoing neighbors. Space is linear in vertices plus stored edges.
Fragment
Undirected edges commonly appear in both endpoint lists; parallel edges and self-loops need defined handling.
Reference
Compare operation costs, ordering, invariants and common algorithm notation.
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.
Store each vertex’s outgoing neighbors. Space is linear in vertices plus stored edges.
Fragment
Undirected edges commonly appear in both endpoint lists; parallel edges and self-loops need defined handling.
Store one entry per vertex pair. Dense graphs can benefit from direct edge tests; sparse graphs pay for absent edges.
Fragment
The matrix can store booleans, weights or counts; absence needs an unambiguous representation.
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
Mark discovered vertices consistently to avoid duplicate work. Weighted shortest paths need a different algorithm.
DFS follows a path before returning to alternatives. Recursive and explicit-stack forms need a visited/active-state policy suitable for the graph.
Fragment
DFS order is not generally shortest-path order. A recursive path can exhaust the call stack.
Dijkstra finalizes the smallest tentative distance and relaxes outgoing edges. It requires nonnegative edge weights.
Fragment
Lazy duplicate heap entries can use O(E) heap space and alter the precise bound; stale entries must be skipped.
A topological order places each directed edge’s source before its destination. It exists only for a directed acyclic graph.
Fragment