|
| 1 | +class Edge { |
| 2 | + constructor(source, destination, weight) { |
| 3 | + this.source = source; |
| 4 | + this.destination = destination; |
| 5 | + this.weight = weight; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +function findShortestPaths(edges, V, source) { |
| 10 | + const distances = new Array(V); |
| 11 | + for (let i = 0; i < V; i++) { |
| 12 | + distances[i] = Number.MAX_SAFE_INTEGER; |
| 13 | + } |
| 14 | + distances[source] = 0; |
| 15 | + |
| 16 | + // Relax all edges V-1 times |
| 17 | + for (let i = 1; i < V; i++) { |
| 18 | + for (const edge of edges) { |
| 19 | + const u = edge.source; |
| 20 | + const v = edge.destination; |
| 21 | + const weight = edge.weight; |
| 22 | + |
| 23 | + if (distances[u] !== Number.MAX_SAFE_INTEGER && |
| 24 | + distances[u] + weight < distances[v]) { |
| 25 | + distances[v] = distances[u] + weight; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Check for negative weight cycle |
| 31 | + for (const edge of edges) { |
| 32 | + const u = edge.source; |
| 33 | + const v = edge.destination; |
| 34 | + const weight = edge.weight; |
| 35 | + |
| 36 | + if (distances[u] !== Number.MAX_SAFE_INTEGER && |
| 37 | + distances[u] + weight < distances[v]) { |
| 38 | + // Negative weight cycle detected |
| 39 | + return null; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return distances; |
| 44 | +} |
| 45 | + |
| 46 | +function printShortestPaths(distances) { |
| 47 | + if (distances === null) { |
| 48 | + console.log("Graph contains negative weight cycle!"); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + console.log("Vertex \t Distance from Source"); |
| 53 | + for (let i = 0; i < distances.length; i++) { |
| 54 | + if (distances[i] === Number.MAX_SAFE_INTEGER) { |
| 55 | + console.log(i + "\t\t INFINITY"); |
| 56 | + } else { |
| 57 | + console.log(i + "\t\t " + distances[i]); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Test Case 1: Graph without negative cycle |
| 63 | +console.log("Test Case 1: Graph without negative cycle"); |
| 64 | +const edges1 = [ |
| 65 | + new Edge(0, 1, 4), |
| 66 | + new Edge(0, 2, 2), |
| 67 | + new Edge(1, 2, -1), |
| 68 | + new Edge(1, 3, 3), |
| 69 | + new Edge(2, 3, 1) |
| 70 | +]; |
| 71 | +const V1 = 4; |
| 72 | +const result1 = findShortestPaths(edges1, V1, 0); |
| 73 | +printShortestPaths(result1); |
| 74 | +console.log(); |
| 75 | + |
| 76 | +// Test Case 2: Graph with negative cycle |
| 77 | +console.log("Test Case 2: Graph with negative cycle"); |
| 78 | +const edges2 = [ |
| 79 | + new Edge(0, 1, 1), |
| 80 | + new Edge(1, 2, 2), |
| 81 | + new Edge(2, 3, 3), |
| 82 | + new Edge(3, 1, -7) |
| 83 | +]; |
| 84 | +const V2 = 4; |
| 85 | +const result2 = findShortestPaths(edges2, V2, 0); |
| 86 | +printShortestPaths(result2); |
| 87 | +console.log(); |
| 88 | + |
| 89 | +// Test Case 3: Graph with unreachable vertices |
| 90 | +console.log("Test Case 3: Graph with unreachable vertices"); |
| 91 | +const edges3 = [ |
| 92 | + new Edge(0, 1, 5), |
| 93 | + new Edge(1, 2, 2), |
| 94 | + new Edge(3, 4, 1) |
| 95 | +]; |
| 96 | +const V3 = 5; |
| 97 | +const result3 = findShortestPaths(edges3, V3, 0); |
| 98 | +printShortestPaths(result3); |
| 99 | + |
| 100 | +module.exports = { Edge, findShortestPaths, printShortestPaths }; |
0 commit comments