|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * This class implements the Floyd-Warshall algorithm for finding the shortest |
| 7 | + * distances between all pairs of vertices in a weighted graph. |
| 8 | + * It works for both positive and negative edge weights but no negative cycles. |
| 9 | + * |
| 10 | + * Time Complexity: O(V^3), where V is the number of vertices. |
| 11 | + * Space Complexity: O(V^2) |
| 12 | + * |
| 13 | + * Example usage: |
| 14 | + * <pre> |
| 15 | + * FloydWarshall.Graph graph = new FloydWarshall.Graph(4); |
| 16 | + * graph.addEdge(0, 1, 5); |
| 17 | + * graph.addEdge(0, 3, 10); |
| 18 | + * graph.addEdge(1, 2, 3); |
| 19 | + * graph.addEdge(2, 3, 1); |
| 20 | + * FloydWarshall fw = new FloydWarshall(graph); |
| 21 | + * int[][] distances = fw.solve(); |
| 22 | + * </pre> |
| 23 | + * |
| 24 | + * Author: <a href="https://github.com/YourGitHubUsername">Your Name</a> |
| 25 | + */ |
| 26 | +public class FloydWarshall { |
| 27 | + |
| 28 | + /** Graph representation using adjacency matrix */ |
| 29 | + public static class Graph { |
| 30 | + private final int numVertices; |
| 31 | + private final int[][] adjacencyMatrix; |
| 32 | + private static final int INF = Integer.MAX_VALUE / 2; // avoid overflow |
| 33 | + |
| 34 | + public Graph(int numVertices) { |
| 35 | + this.numVertices = numVertices; |
| 36 | + adjacencyMatrix = new int[numVertices][numVertices]; |
| 37 | + for (int i = 0; i < numVertices; i++) { |
| 38 | + Arrays.fill(adjacencyMatrix[i], INF); |
| 39 | + adjacencyMatrix[i][i] = 0; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Adds a directed edge from 'from' to 'to' with given weight. |
| 45 | + * |
| 46 | + * @param from starting vertex |
| 47 | + * @param to ending vertex |
| 48 | + * @param weight edge weight |
| 49 | + */ |
| 50 | + public void addEdge(int from, int to, int weight) { |
| 51 | + adjacencyMatrix[from][to] = weight; |
| 52 | + } |
| 53 | + |
| 54 | + public int getNumVertices() { |
| 55 | + return numVertices; |
| 56 | + } |
| 57 | + |
| 58 | + public int[][] getAdjacencyMatrix() { |
| 59 | + return adjacencyMatrix; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private final Graph graph; |
| 64 | + |
| 65 | + public FloydWarshall(Graph graph) { |
| 66 | + this.graph = graph; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Solves the Floyd-Warshall algorithm to compute shortest paths |
| 71 | + * between all pairs of vertices. |
| 72 | + * |
| 73 | + * @return distance matrix where dist[i][j] is the shortest distance from i to j |
| 74 | + */ |
| 75 | + public int[][] solve() { |
| 76 | + int V = graph.getNumVertices(); |
| 77 | + int[][] dist = new int[V][V]; |
| 78 | + |
| 79 | + // Initialize distances with adjacency matrix |
| 80 | + for (int i = 0; i < V; i++) { |
| 81 | + dist[i] = Arrays.copyOf(graph.getAdjacencyMatrix()[i], V); |
| 82 | + } |
| 83 | + |
| 84 | + // Main Floyd-Warshall loop |
| 85 | + for (int k = 0; k < V; k++) { |
| 86 | + for (int i = 0; i < V; i++) { |
| 87 | + for (int j = 0; j < V; j++) { |
| 88 | + if (dist[i][k] + dist[k][j] < dist[i][j]) { |
| 89 | + dist[i][j] = dist[i][k] + dist[k][j]; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return dist; |
| 96 | + } |
| 97 | + |
| 98 | + /** Simple main method for testing */ |
| 99 | + public static void main(String[] args) { |
| 100 | + Graph graph = new Graph(4); |
| 101 | + graph.addEdge(0, 1, 5); |
| 102 | + graph.addEdge(0, 3, 10); |
| 103 | + graph.addEdge(1, 2, 3); |
| 104 | + graph.addEdge(2, 3, 1); |
| 105 | + |
| 106 | + FloydWarshall fw = new FloydWarshall(graph); |
| 107 | + int[][] distances = fw.solve(); |
| 108 | + |
| 109 | + System.out.println("All-pairs shortest distances:"); |
| 110 | + for (int[] row : distances) { |
| 111 | + System.out.println(Arrays.toString(row)); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments