Few topics show up in coding contests as reliably as shortest paths, and knowing which algorithm to reach for under time pressure separates a solved problem from a wasted hour.
Graphs are everywhere in shortest path competitive programming: road networks, state machines, grids, and abstract "cost to reach a goal" puzzles all reduce to finding the cheapest route between nodes. The good news is that a small toolkit of well-understood algorithms covers the overwhelming majority of contest problems. The skill is recognizing the structure of the graph in front of you and matching it to the right tool before you write a single line of code.
The Core Toolkit
Most shortest path problems fall into one of a few categories defined by edge weights and how many sources and destinations you care about. Here is the practical decision tree contestants use.
- Breadth-First Search (BFS) — When every edge has the same weight (an unweighted graph or a uniform grid), plain BFS finds shortest paths in O(V + E). It is the fastest and simplest option, so always check whether weights are really uniform before reaching for anything heavier.
- 0-1 BFS — When edges have only two possible weights, 0 and 1, a double-ended queue (deque) replaces the priority queue. You push 0-weight edges to the front and 1-weight edges to the back, achieving O(V + E) instead of paying for a heap. This pattern is common in grid problems where some moves are "free."
- Dijkstra's algorithm — The workhorse for a single source with non-negative weights. With a binary heap it runs in O(E log V), making it ideal for sparse graphs. It cannot handle negative edges, so confirm weights are non-negative first.
- Bellman-Ford — Slower at O(V · E), but it handles negative edge weights and detects negative cycles. Reach for it only when negatives are present or you specifically need cycle detection.
- Floyd-Warshall — A dynamic programming approach that computes shortest paths between all pairs of vertices in O(V³). It shines on small, dense graphs (roughly a few hundred vertices) and is delightfully short to code.
Quick rule of thumb: uniform weights means BFS, two weights means 0-1 BFS, non-negative weights with one source means Dijkstra, negative edges means Bellman-Ford, and "all pairs on a small graph" means Floyd-Warshall.
Reading the Problem Before You Code
The most common mistake is choosing an algorithm before reading the constraints. The number of vertices and edges, printed right in the problem statement, usually tells you what will pass within the time limit.
- Check the constraints. If V is at most a few hundred and the problem asks about many pairs, Floyd-Warshall's O(V³) is comfortable. If V reaches hundreds of thousands, you need an O(E log V) approach like Dijkstra.
- Look at the edge weights. Any negative number rules out vanilla Dijkstra. All weights equal? BFS wins.
- Count the sources and sinks. One source to all nodes points to Dijkstra or Bellman-Ford; every pair points to Floyd-Warshall or repeated Dijkstra.
- Watch for hidden graphs. Many problems disguise a graph as a grid, a set of states, or a sequence of transformations. Modeling the problem as nodes and edges is often the hardest and most valuable step.
Common Pitfalls
- Using Dijkstra on a graph with negative edges and getting silently wrong answers.
- Forgetting to use a
long-equivalent type when path costs can overflow a 32-bit integer. - Re-adding stale nodes to the priority queue instead of skipping outdated entries.
- Treating an undirected edge as one directed edge, leaving half the graph unreachable.
Where These Skills Pay Off
Graph algorithms are central to algorithmic olympiads. In the USA Computing Olympiad, contestants progress through Bronze, Silver, Gold, and Platinum divisions in timed online rounds, and shortest path techniques appear regularly from Silver upward, growing more intricate at higher levels. Because promotion depends on solving harder graph problems quickly, pattern recognition matters as much as raw coding. Always confirm the current rules, schedule, and division details on the official competition page, since formats can change from season to season.
At BIAA, our competitive programming program trains students to recognize these patterns, reason about constraints, and implement clean, bug-resistant solutions under contest conditions. Structured practice on classic graph problems builds the instinct to choose the right algorithm in seconds rather than minutes.
The fastest path to improvement is deliberate practice on past contest problems, reviewing not just whether you solved them but why your chosen algorithm fit.
Ready to turn algorithmic theory into contest results? Explore BIAA's competitive programming track and start mastering the graph toolkit that top contestants rely on.