davegimo

BF

Aug 25th, 2023
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3.  
  4. public class Ford {
  5.     private int[][] adjacencyMatrix;
  6.  
  7.     public Ford(int totalNodes) {
  8.         adjacencyMatrix = new int[totalNodes][totalNodes];
  9.     }
  10.  
  11.     public void addConnection(int node1, int node2, int weight) {
  12.         if (node1 >= 0 && node1 < adjacencyMatrix.length &&
  13.             node2 >= 0 && node2 < adjacencyMatrix.length) {
  14.             adjacencyMatrix[node1][node2] = weight;
  15.             adjacencyMatrix[node2][node1] = weight;
  16.         } else {
  17.             System.out.println("Invalid nodes");
  18.         }
  19.     }
  20.  
  21.     public void printAdjacencyMatrix() {
  22.         for (int i = 0; i < adjacencyMatrix.length; i++) {
  23.             for (int j = 0; j < adjacencyMatrix[i].length; j++) {
  24.                 System.out.print(adjacencyMatrix[i][j] + " ");
  25.             }
  26.             System.out.println();
  27.         }
  28.     }
  29.  
  30.     public void bellmanFord() {
  31.         for (int sourceNode = 0; sourceNode < adjacencyMatrix.length; sourceNode++) {
  32.             int[] distances = new int[adjacencyMatrix.length];
  33.             Arrays.fill(distances, Integer.MAX_VALUE);
  34.             distances[sourceNode] = 0;
  35.  
  36.             ArrayList<ArrayList<Integer>> shortestPaths = new ArrayList<>();
  37.             for (int i = 0; i < adjacencyMatrix.length; i++) {
  38.                 shortestPaths.add(new ArrayList<>());
  39.             }
  40.  
  41.             for (int i = 0; i < adjacencyMatrix.length - 1; i++) {
  42.                 for (int u = 0; u < adjacencyMatrix.length; u++) {
  43.                     for (int v = 0; v < adjacencyMatrix.length; v++) {
  44.                         if (adjacencyMatrix[u][v] != 0) {
  45.                             if (distances[u] != Integer.MAX_VALUE && distances[u] + adjacencyMatrix[u][v] < distances[v]) {
  46.                                 distances[v] = distances[u] + adjacencyMatrix[u][v];
  47.                                 shortestPaths.get(v).clear();
  48.                                 shortestPaths.get(v).addAll(shortestPaths.get(u));
  49.                                 shortestPaths.get(v).add(v);
  50.                             }
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.  
  56.             System.out.println("Shortest paths from node " + sourceNode + ":");
  57.             for (int i = 0; i < distances.length; i++) {
  58.                 System.out.println("Node " + i + ": Distance = " + distances[i] + ", Path = " + shortestPaths.get(i));
  59.             }
  60.             System.out.println();
  61.         }
  62.     }
  63.  
  64.     public void bellmanFord_() {
  65.         for (int sourceNode = 0; sourceNode < adjacencyMatrix.length; sourceNode++) {
  66.             int[] distances = new int[adjacencyMatrix.length];
  67.             Arrays.fill(distances, Integer.MAX_VALUE);
  68.             distances[sourceNode] = 0;
  69.    
  70.             for (int i = 0; i < adjacencyMatrix.length - 1; i++) {
  71.                 for (int u = 0; u < adjacencyMatrix.length; u++) {
  72.                     for (int v = 0; v < adjacencyMatrix.length; v++) {
  73.                         if (adjacencyMatrix[u][v] != 0) {
  74.                             if (distances[u] != Integer.MAX_VALUE && distances[u] + adjacencyMatrix[u][v] < distances[v]) {
  75.                                 distances[v] = distances[u] + adjacencyMatrix[u][v];
  76.                             }
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.    
  82.             System.out.println("Shortest distances from node " + sourceNode + ":");
  83.             for (int i = 0; i < distances.length; i++) {
  84.                 System.out.println("Node " + i + ": " + distances[i]);
  85.             }
  86.             System.out.println();
  87.         }
  88.     }
  89.  
  90.     public void printMatrix(int[][] matrix) {
  91.         for (int i = 0; i < matrix.length; i++) {
  92.             for (int j = 0; j < matrix[i].length; j++) {
  93.                 System.out.print(matrix[i][j] + " ");
  94.             }
  95.             System.out.println();
  96.         }
  97.     }
  98.  
  99.     public static void main(String[] args) {
  100.  
  101.         Ford graph = new Ford(12);
  102.  
  103.         graph.addConnection(0, 1, 1);
  104.  
  105.         graph.addConnection(1, 11, 1);
  106.         graph.addConnection(1, 5, 4);
  107.         graph.addConnection(1, 4, 1);
  108.  
  109.         graph.addConnection(2, 8, 1);
  110.         graph.addConnection(2, 5, 1);
  111.  
  112.         graph.addConnection(5, 6, 1);
  113.  
  114.         graph.addConnection(11, 8, 1);
  115.  
  116.         graph.addConnection(6, 3, 1);
  117.         graph.addConnection(6, 4, 1);
  118.  
  119.         graph.addConnection(4, 7, 1);
  120.  
  121.         graph.addConnection(7, 9, 1);
  122.  
  123.         graph.addConnection(9, 10, 1);
  124.         graph.addConnection(9, 3, 1);
  125.  
  126.         graph.addConnection(10, 3, 1);
  127.  
  128.        
  129.         System.out.println();
  130.  
  131.         graph.bellmanFord();
  132.  
  133.  
  134.     }
  135.  
  136. }
  137.  
  138.  
  139.  
Advertisement
Add Comment
Please, Sign In to add comment