Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 47 additions & 29 deletions Graphs/FloydWarshall.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
/*
Source:
https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm

Complexity:
O(|V|^3) where V is the set of vertices
*/

/**
* Computes all-pairs shortest paths using the Floyd-Warshall algorithm.
*
* Source:
* https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
* Complexity:
* O(|V|^3) where V is the set of vertices.
*
* Preconditions and Failure / Empty-Result Behavior:
* - Precondition at Line 24 (`const n = dist.length`): For an empty graph (`dist = []`),
* `n` is 0. The outer relaxation loop condition `k < n` (Line 25) evaluates to false,
* bypassing all relaxation loops and directly executing Line 35 (`return dist`),
* returning the valid empty result `[]` without error.
* - Absent-path sentinel (`Infinity`): Where no path exists between vertices `i` and `j`,
* the condition `dist[i][j] > dist[i][k] + dist[k][j]` at Line 28 is never satisfied
* for any intermediate vertex `k`. Thus, `dist[i][j]` preserves the `Infinity` sentinel,
* representing unreachable vertices / absent shortest paths.
* - In-place mutation: The function updates and returns the input `dist` matrix. Caller-owned
* matrices can be shallow/deep copied prior to invocation if caller persistence is required.
*
* @param {number[][]} dist - 2D square matrix where dist[i][j] represents edge weight b/w i and j.
* @returns {number[][]} Updated 2D matrix where dist[i][j] represents shortest distance b/w i and j.
*
* @example
* // Empty graph (empty-result path, Line 35)
* FloydWarshall([]) // returns []
*
* @example
* // Smallest valid input (1x1 matrix, Line 24)
* FloydWarshall([[0]]) // returns [[0]]
*
* @example
* // Ordinary graph with shortest paths computed
* FloydWarshall([
* [0, 1, 2, Infinity],
* [1, 0, Infinity, Infinity],
* [2, Infinity, 0, 1],
* [Infinity, Infinity, 1, 0]
* ])
* // returns:
* // [
* // [0, 1, 2, 3],
* // [1, 0, 3, 4],
* // [2, 3, 0, 1],
* // [3, 4, 1, 0]
* // ]
*/
const FloydWarshall = (dist) => {
// Input:- dist: 2D Array where dist[i][j] = edge weight b/w i and j
// Output:- dist: 2D Array where dist[i][j] = shortest dist b/w i and j
Expand All @@ -24,24 +63,3 @@ const FloydWarshall = (dist) => {
}

export { FloydWarshall }

// For the following graph (edge weights are shown in brackets)
// 4 1 dist[1][2] = dist[2][1] = 1
// \ (2)/ \ dist[1][3] = dist[3][1] = 2
// \ / \(1) dist[1][4] = dist[4][1] = Infinity
// (1)\ / \ dist[3][4] = dist[4][3] = 1
// 3 2 dist[2][4] = dist[4][2] = Infinity
// dist[2][3] = dist[3][2] = Infinity
// Output should be:
// [ [0, 1, 2, 3],
// [1, 0, 3, 4],
// [2, 3, 0, 1],
// [3, 4, 1, 0] ]

// FloydWarshall(
// [[0, 1, 2, Infinity],
// [1, 0, Infinity, Infinity],
// [2, Infinity, 0, 1],
// [Infinity, Infinity, 1, 0]
// ]
// )
92 changes: 92 additions & 0 deletions Graphs/test/FloydWarshall.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { FloydWarshall } from '../FloydWarshall'

describe('FloydWarshall', () => {
describe('Failure and Empty-Result Paths', () => {
test('handles empty input graph as valid empty-result path (Line 24, 25 -> Line 35)', () => {
// Precondition: dist.length === 0 at Line 24, loop at Line 25 bypassed
const emptyInput = []
const result = FloydWarshall(emptyInput)
expect(result).toEqual([])
})

test('preserves absent-path sentinel Infinity for disconnected components (Line 28 condition false)', () => {
// Disconnected 2-vertex graph: no edge between 0 and 1
const disconnectedGraph = [
[0, Infinity],
[Infinity, 0]
]
const result = FloydWarshall(disconnectedGraph)
expect(result).toEqual([
[0, Infinity],
[Infinity, 0]
])
})

test('caller-owned state remains valid and ordinary call succeeds immediately after empty/failure call', () => {
// 1. Call empty-result path
const emptyCall = FloydWarshall([])
expect(emptyCall).toEqual([])

// 2. Subsequent ordinary call succeeds without corruption
const ordinaryInput = [
[0, 1, 2, Infinity],
[1, 0, Infinity, Infinity],
[2, Infinity, 0, 1],
[Infinity, Infinity, 1, 0]
]
const expectedOrdinary = [
[0, 1, 2, 3],
[1, 0, 3, 4],
[2, 3, 0, 1],
[3, 4, 1, 0]
]
const result = FloydWarshall(ordinaryInput)
expect(result).toEqual(expectedOrdinary)
})
})

describe('Smallest Valid and Ordinary Inputs', () => {
test('smallest valid input: single vertex graph (Line 24: n=1)', () => {
const smallestInput = [[0]]
const expectedSmallest = [[0]]
const result = FloydWarshall(smallestInput)
expect(result).toEqual(expectedSmallest)
})

test('ordinary input: 4-vertex connected graph with indirect paths', () => {
const ordinaryInput = [
[0, 1, 2, Infinity],
[1, 0, Infinity, Infinity],
[2, Infinity, 0, 1],
[Infinity, Infinity, 1, 0]
]
const expectedOrdinary = [
[0, 1, 2, 3],
[1, 0, 3, 4],
[2, 3, 0, 1],
[3, 4, 1, 0]
]
const result = FloydWarshall(ordinaryInput)
expect(result).toEqual(expectedOrdinary)
})

test('ordinary directed graph with weighted edges', () => {
const directedGraph = [
[0, 3, 8, Infinity, -4],
[Infinity, 0, Infinity, 1, 7],
[Infinity, 4, 0, Infinity, Infinity],
[2, Infinity, -5, 0, Infinity],
[Infinity, Infinity, Infinity, 6, 0]
]
const expected = [
[0, 1, -3, 2, -4],
[3, 0, -4, 1, -1],
[7, 4, 0, 5, 3],
[2, -1, -5, 0, -2],
[8, 5, 1, 6, 0]
]
const result = FloydWarshall(directedGraph)
expect(result).toEqual(expected)
})
})
})
Loading