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

Floyd-Warshall

Update an all-pairs distance matrix by allowing one additional intermediate vertex per round.

Core idea

Dynamic programming expands the set of allowed intermediate vertices. For each pivot k, every ordered pair i and j asks whether traveling through k beats its current best distance.

Read the visualization

The highlighted pivot row and column provide the two candidate legs. The active matrix cell either accepts their sum or keeps its previous distance.

ABCD
A036∞
B∞024
C∞∞01
D5∞∞0

Initialize direct edge distances, zero diagonals, and infinity for unknown routes.

1function floyd(adj: number[][], n: number): number[][] {
2 const dist = adj.map((row) => [...row]);
3 for (let k = 0; k < n; k++) {
4 for (let i = 0; i < n; i++) {
5 for (let j = 0; j < n; j++) {
6 if (dist[i][k] + dist[k][j] < dist[i][j]) {
7 dist[i][j] = dist[i][k] + dist[k][j];
8 }
9 }
10 }
11 }
12 return dist;
13}
n4
intermediate k-
already update0
1 / 19

Complexity and tradeoffs

Time: O(V^3). Space: O(V^2). Handles negative edges but not negative cycles; the matrix is practical for dense, moderate graphs.

Invariant: After pivot k, every matrix entry is the shortest path whose intermediate vertices come only from the processed pivot set.

Where it fits

Floyd-Warshall is concise for dense graphs, transitive closure, and moderate all-pairs queries. Sparse large graphs usually favor repeated single-source algorithms.