Skip to content
  • Overview
  • Curriculum
    • FLUMental maths and numerical fluency
    • MKTMarkets and products
    • CSData structures and algorithms
      • 1Complexity

        • Complexity: reading it off, and deriving it
      • 2Linear structures

        • Linear structures: arrays, hash maps and monotonic stacks
      • 3Trees and heaps

        • Trees and heaps: BSTs, priority queues and range queries
      • 4Graphs

        • Graphs: traversal, shortest paths and union–find
      • 5Core techniques

        • Core techniques: binary search on the answer, two pointers, sliding windows
      • 6Dynamic programming

        • Dynamic programming, and why it is the same as an EV recursion
      • 7Bit manipulation and number theory

        • Bit manipulation and modular arithmetic
    • PYPython and data for quants
    • NUMNumerical methods
    • SYSSystems and low latency

Practise

  • Question bank
  • Mental arithmetic
  • Market simulator
  • Arbitrage trees
  • Horse racing
  • Bid book
  • Screening tests
  • Mock papers

Reference

  • Formula reference
  • Search

Your record

  • Review queue
  • Progress
  • Leaderboard
  • Profile
  • Invite friends
AccountSend feedback
  1. Curriculum
  2. /Quantitative development
  3. /Data structures and algorithms
  4. /Graphs

Graphs: traversal, shortest paths and union–find

CS · Chapter 4·12 min read·Asked at Hudson River Trading, Jump, Citadel Securities, Optiver

Assumes Trees and heaps: BSTs, priority queues and range queries.

After this lesson you should be able to

  • Choose between BFS and DFS from the question.
  • Pick the right shortest-path algorithm for the edge weights.
  • Recognise the problems union–find solves.

Graph questions are usually easy once the graph is identified, and identifying it is the difficulty: currencies and exchange rates, instruments and dependencies, states and transitions. The algorithms themselves are a short list chosen by two properties — are the edges weighted, and can the weights be negative.

AspectBFSDFS
ExploresLevel by levelAs deep as possible first
UsesA queueA stack, or recursion
FindsShortest path in an unweighted graphAny path; cycles; topological order
MemoryThe whole frontier — can be wideThe current depth
Good forMinimum hops, level structureConnectivity, ordering, backtracking
Table 4.1 · BFS against DFS. BFS finds the shortest path only when every edge has the same weight — which is exactly when it is Dijkstra with a queue instead of a heap.
AlgorithmHandlesComplexity
BFSUnweightedO(V+E)O(V + E)O(V+E)
DijkstraNon-negative weightsO((V+E)log⁡V)O((V+E)\log V)O((V+E)logV) with a heap
Bellman–FordNegative weights; detects negative cyclesO(VE)O(VE)O(VE)
Floyd–WarshallAll pairsO(V3)O(V^3)O(V3)
A*Single target, with a heuristicDepends on the heuristic
Table 4.2 · Shortest paths. The Bellman–Ford row is the one with a financial use: taking negative logs of exchange rates turns a triangular arbitrage into a negative cycle, which Bellman–Ford detects directly.

Why Dijkstra breaks on negative edges. Dijkstra finalises a node as soon as it is the closest unvisited one, on the reasoning that no later path could be shorter — every remaining edge only adds distance. A negative edge destroys that reasoning: a longer detour can come back cheaper, so a node finalised early may be wrong and nothing ever revisits it. Bellman–Ford makes no such commitment and simply relaxes every edge V−1V-1V−1 times, which is slower and correct. If a further relaxation still improves something, there is a negative cycle.

cost 2cost 5cost −4StartAB
Figure 4.3 · The three-edge counterexample to Dijkstra. Dijkstra would finalise A at distance 2 before visiting B at distance 5. But the route Start → B → A costs 5 − 4 = 1. A later negative edge invalidates the assumption that a finalised distance cannot improve.
105001000020000004000000Dijkstra, (V+E) log VBellman–Ford, VEVerticesOperations
Figure 4.4 · Why negative edges are expensive. On a sparse graph with four edges a vertex. Bellman–Ford is the price of admitting negative weights — and you pay it deliberately, because detecting a negative cycle is how a triangular arbitrage is found in a currency graph.

The rest of this lesson is in Premium

You have read the opening. 11 more sections follow, including 4 worked examples and 3 quick checks.

Start the free 7-day trialSign in

Nothing is charged for 7 days, and you can cancel before then. Or read Complexity: reading it off, and deriving it in full, free.

← Trees and heaps: BSTs, priority queues and range queriesCore techniques: binary search on the answer, two pointers, sliding windows →
On this page
  • BFS against DFS
  • Shortest paths
  • The three-edge counterexample to Dijkstra
  • Why negative edges are expensive

QuantMax · 141 lessons · 1342 questions · c5c0caa

  • Premium
  • Arbitrage trees
  • Horse racing
  • Invite friends
  • Account
  • About QuantMax
  • Terms
  • Privacy

Firm names identify publicly reported question patterns and nothing more. QuantMax is not affiliated with, endorsed by, or recruiting for any firm named in the curriculum. Everything you do in lessons and the question bank is kept to your account.