Advertisement
Guest User

BellmanFord TestGraphs

a guest
Nov 17th, 2017
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package bellmanford;
  2.  
  3. import java.util.Iterator;
  4. import java.util.PriorityQueue;
  5.  
  6. public class TestGraphs {
  7.  
  8.     public static void main(String[] args) {
  9.         //------------------------------------------------------------------------------
  10.         System.out.println("Same graph as with Dijkstra!");
  11.         Graph g = new Graph(5);
  12.  
  13.         System.out.println("Graph:");
  14.         // add Edges
  15.         g.addEdge(0, 1, 5.2f);
  16.         g.addEdge(0, 3, 12.8f);
  17.         g.addEdge(0, 2, 10.3f);
  18.         g.addEdge(1, 3, 5.9f);
  19.         g.addEdge(1, 4, 15.2f);
  20.         g.addEdge(2, 1, 1.5f);
  21.         g.addEdge(2, 3, 2.3f);
  22.         g.addEdge(3, 4, 8.5f);
  23.         g.addEdge(4, 2, 2.7f);
  24.  
  25.         // print Graph
  26.         g.printGraph();
  27.  
  28.         // Bellman-Ford Shortest Path Algorithm
  29.         System.out.println("Bellman-Ford Shortest Path:");
  30.         BellmanFord(g, 0); 
  31.        
  32.         //------------------------------------------------------------------------------
  33.         System.out.println("\nNew graph with negative weight circle!");
  34.        
  35.         Graph g2 = new Graph(8);
  36.  
  37.         System.out.println("Graph:");
  38.         // add Edges
  39.         g2.addEdge(0, 1, 4);
  40.         g2.addEdge(0, 2, 4);
  41.         // 1 nowhere
  42.         g2.addEdge(2, 4, 4);
  43.         g2.addEdge(2, 5, -2);
  44.         g2.addEdge(3, 0, 3);
  45.         g2.addEdge(3, 2, 2);
  46.         g2.addEdge(4, 3, 1);
  47.         g2.addEdge(4, 6, -2); // *
  48.         g2.addEdge(5, 1, 3);
  49.         g2.addEdge(5, 4, -3);
  50.         g2.addEdge(6, 5, 2);
  51.         g2.addEdge(6, 7, 2); // *
  52.         g2.addEdge(7, 4, -2); //*
  53.         // *part of a negative-weight circle -2 + 2 - 2 = -2 < 0
  54.  
  55.         // print Graph
  56.         g2.printGraph();
  57.  
  58.         // Bellman-Ford Shortest Path Algorithm
  59.         System.out.println("Bellman-Ford Shortest Path:");
  60.         BellmanFord(g2, 0);    
  61.     }
  62.  
  63.     public static void BellmanFord(Graph g, int startVertex) {
  64.         // for storing distances
  65.         float[] distances = new float[g.getvCount()];
  66.         // for storing predecessors
  67.         int[] predecessors = new int[g.getvCount()];
  68.  
  69.         // initialize arrays
  70.         for (int i = 0; i < distances.length; i++) {
  71.             distances[i] = Float.MAX_VALUE;
  72.             predecessors[i] = -1;
  73.         }
  74.         distances[startVertex] = 0;
  75.  
  76.         // relax all edges v - 1 times repeatedly
  77.         for (int i = 1; i < g.getvCount() - 1; i++) {
  78.             for (int j = 0; j < g.getvCount() - 1; j++) {
  79.                 Iterator<Edge> it = g.neighbours(j).iterator();
  80.                 while (it.hasNext()) {
  81.                     Edge e = it.next(); // edge (u, v)
  82.                     // if dist(u) + w(u, v) < dist(v) then
  83.                     // dist(v) = dist(u) + w(u, v)
  84.                     // pred(v) = u
  85.                     if (distances[e.getStartPoint()] + e.getWeight() < distances[e.getEndPoint()]) {
  86.                         distances[e.getEndPoint()] = distances[e.getStartPoint()] + e.getWeight();
  87.                         predecessors[e.getEndPoint()] = e.getStartPoint();
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.  
  93.         // check for negative-weight circles
  94.         for (int i = 0; i < g.getvCount() - 1; i++) {
  95.             Iterator<Edge> it = g.neighbours(i).iterator();
  96.             while (it.hasNext()) {
  97.                 Edge e = it.next(); // edge (u, v)
  98.                 // if dist(u) + w(u, v) < dist(v) then
  99.                 // graph contains negative-weight circle
  100.                 if (distances[e.getStartPoint()] + e.getWeight() < distances[e.getEndPoint()]) {
  101.                     System.out.println("Graph contains negative-weight circle!");
  102.                     return;
  103.                 }
  104.             }
  105.         }
  106.  
  107.         // print final shortest paths
  108.         System.out.println("Vertex\tDistance\tPredecessor");
  109.         for (int i = 0; i < g.getvCount(); i++) {
  110.             System.out.println(i + "\t" + distances[i] + "\t\t" + predecessors[i]);
  111.         }
  112.  
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement