Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Graphs

Bellman-Ford

O(V · E)

Relax every edge V−1 times; the k-stops variant just caps the number of relaxation rounds at k+1.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
Learn firstDijkstra (Weighted Shortest Path)UnlocksMinimum-Cost Flow (and the assignment problem)
0%
mastery
0/5 solved
IdeaPracticeTemplatePitfalls

Recognise it

The signal

Shortest path with negative edge weights, detect a negative cycle, or cheapest path using at most K edges / stops.

  1. 1Brute force

    Enumerate paths.

  2. 2The observation

    Exponential path enumeration: dynamic programming over number of edges.

  3. 3Why it works

    After round i, dist[v] <= the shortest path to v using at most i edges.

Reach for it when

  • +detect a negative (or positive, for longest path) cycle
  • +at most K edges / stops

Not when

  • ×Large graph, all weights non-negativeuse Dijkstra, O(E log V)
  • ×All-pairs distances on small Vuse Floyd-Warshall

Practice

Next · Foundation

Network Delay Time

V-1 rounds of relaxing all edges; compare with Dijkstra.

0/5 solved
  • Network Delay TimeFoundationFoundationAmazon, UberMedium
  • Cheapest Flights Within K StopsApply it · Limit on edges used.Apply itAmazon, UberMedium
  • Cycle FindingTwists · Find and print a negative cycle.TwistsCSESMedium
  • High ScoreMastery checkMastery checkCSESMedium
  • Subarray Sum ConstraintsCSESHard

Pitfalls

Classic bugs

  • !K-stops: relax from a copy of last round's dist, or one round chains several edges and breaks the limit.
  • !A negative cycle only ruins the answer if it's reachable from the source and can reach the target; check, don't reject all.

Looks like it, isn't

  • Path with Maximum Probability needs Dijkstra (Weighted Shortest Path)
    Why
    Multiplying probabilities <= 1 never increases a path's value, so Dijkstra with a max-heap is valid. source ↗
Go deepervariants and techniques · resources

Variants and techniques

  • System of Difference ConstraintsCP 2000Each constraint x_v - x_u <= w becomes an edge u -> v of weight w. A solution exists iff there is no negative cycle, and shortest distances from a virtual source give one.

Resources

  • Graph Theory playlistWilliam Fiset · video
  • Graph TraversalUSACO Guide · course
  • Disjoint Set Unioncp-algorithms · article
  • Dijkstracp-algorithms · article
  • Topological sortingcp-algorithms · article
  • Shortest paths visualizerVisuAlgo · tool
  • Graph cheatsheetTech Interview Handbook · article
Dijkstra (Weighted Shortest Path)Floyd-Warshall
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial