V

Algorithm Visualizer

ZHEN
Share on WeiboShare on XGitHub repositoryAuthor website
Learning ToolsAlgorithm Complexity ReferenceAlgorithm Learning Paths
Data StructuresArrayLinked ListStackQueue and DequeBinary Search TreeBinary HeapHash TableGraphTrieDisjoint Set UnionLRU CacheSkip ListSegment TreeB+ TreeBloom FilterFenwick Tree
SortingBubble SortCocktail Shaker SortBitonic SortSelection SortInsertion SortBinary Insertion SortShell SortMerge SortTop-Down Merge SortQuick SortThree-Way Quick SortDual-Pivot Quick SortHeap SortCounting SortRadix SortBucket Sort
Graph AlgorithmsDijkstra's Shortest PathKruskal's Minimum Spanning TreePrim's Minimum Spanning TreeBellman-Ford Shortest PathsTopological SortFloyd-WarshallStrongly Connected Components2-SATMaximum FlowBipartite MatchingLowest Common AncestorEulerian Path
Dynamic ProgrammingEdit Distance0/1 KnapsackUnbounded KnapsackLongest Common SubsequenceLongest Increasing SubsequenceCoin ChangeStone MergingTraveling Salesperson DPTree Dynamic ProgrammingDigit DPRerooting DP
Backtracking and SearchN-QueensSubsetsPermutationsCombination SumMaze Solving with DFSNumber of IslandsWord SearchSudoku SolverA* Search
StringsKMP String MatchingRabin-Karp String MatchingBoyer-Moore String MatchingManacher's Longest Palindromic SubstringSuffix ArrayLCP ArrayAho-Corasick AutomatonZ Function
Math and Number TheorySieve of EratosthenesLinear SieveEuclidean AlgorithmBinary ExponentiationExtended Euclidean AlgorithmChinese Remainder TheoremEuler's Totient FunctionMiller-Rabin Primality TestFast Fourier TransformPollard's Rho Factorization
Computational GeometryConvex HullRotating CalipersClosest Pair of PointsLine Segment IntersectionBentley-Ottmann Sweep Line
SearchingBinary SearchLower and Upper BoundSearch in a Rotated Sorted ArrayBinary Search on the AnswerTernary Search

Bellman-Ford Shortest Paths

Single-source shortest paths with negative edge weights

Relax edges instead of finalizing vertices

A negative edge can improve a route after another vertex already looked settled, so the greedy guarantee used by Dijkstra no longer applies. Bellman-Ford repeatedly scans every directed edge and relaxes u -> v whenever dist[u] + w < dist[v].

Why V - 1 rounds are enough

A simple shortest path visits at most V - 1 edges. After round k, all shortest paths using at most k edges can be represented by the distance labels. The player highlights each scanned edge and updates the destination badge only when a shorter candidate is found.

-224-3645A0B∞C∞D∞E∞

Set source A to distance 0 and every other vertex to infinity.

1function bellmanFord(edges: [number, number, number][], n: number, source: number): number[] {
2 const dist = Array(n).fill(Infinity);
3 dist[source] = 0;
4 for (let k = 0; k < n - 1; k++) {
5 for (const [u, v, w] of edges) {
6 if (dist[u] + w < dist[v]) {
7 dist[v] = dist[u] + w;
8 }
9 }
10 }
11 return dist;
12}
Round k-
Total rounds V-14
Current edge-
Updates this round0
dist[A]0
dist[B]∞
dist[C]∞
dist[D]∞
dist[E]∞
1 / 34

Cost and negative-cycle detection

Scanning every edge for V - 1 rounds costs O(VE) time and O(V) distance storage. Run one additional round to detect a reachable negative cycle: if any distance still improves, no finite shortest path exists for vertices reachable from that cycle.

Use Bellman-Ford when negative weights are possible or negative-cycle detection matters. On a graph with non-negative weights, Dijkstra's algorithm is usually faster.