|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.mycompany.bellmanford; |
| 7 | + |
| 8 | +/** |
| 9 | + * |
| 10 | + * @author asadp |
| 11 | + */ |
| 12 | +class CreateGraph { |
| 13 | + // CreateGraph - it consists of edges |
| 14 | + class CreateEdge { |
| 15 | + int s, d, w; |
| 16 | + |
| 17 | + CreateEdge() { |
| 18 | + s = d = w = 0; |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + int V, E; |
| 23 | + CreateEdge edge[]; |
| 24 | + |
| 25 | + // Creates a graph with V vertices and E edges |
| 26 | + CreateGraph(int v, int e) { |
| 27 | + V = v; |
| 28 | + E = e; |
| 29 | + edge = new CreateEdge[e]; |
| 30 | + for (int i = 0; i < e; ++i) |
| 31 | + edge[i] = new CreateEdge(); |
| 32 | + } |
| 33 | + |
| 34 | + void BellmanFord(CreateGraph graph, int s) { |
| 35 | + int V = graph.V, E = graph.E; |
| 36 | + int dist[] = new int[V]; |
| 37 | + |
| 38 | + // Step 1: fill the distance array and predecessor array |
| 39 | + for (int i = 0; i < V; ++i) |
| 40 | + dist[i] = Integer.MAX_VALUE; |
| 41 | + |
| 42 | + // Mark the source vertex |
| 43 | + dist[s] = 0; |
| 44 | + |
| 45 | + // Step 2: relax edges |V| - 1 times |
| 46 | + for (int i = 1; i < V; ++i) { |
| 47 | + for (int j = 0; j < E; ++j) { |
| 48 | + // Get the edge data |
| 49 | + int u = graph.edge[j].s; |
| 50 | + int v = graph.edge[j].d; |
| 51 | + int w = graph.edge[j].w; |
| 52 | + if (dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]) |
| 53 | + dist[v] = dist[u] + w; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Step 3: detect negative cycle |
| 58 | + // if value changes then we have a negative cycle in the graph |
| 59 | + // and we cannot find the shortest distances |
| 60 | + for (int j = 0; j < E; ++j) { |
| 61 | + int u = graph.edge[j].s; |
| 62 | + int v = graph.edge[j].d; |
| 63 | + int w = graph.edge[j].w; |
| 64 | + if (dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]) { |
| 65 | + System.out.println("CreateGraph contains negative w cycle"); |
| 66 | + return; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // No negative w cycle found! |
| 71 | + // Print the distance and predecessor array |
| 72 | + printSolution(dist, V); |
| 73 | + } |
| 74 | + |
| 75 | + // Print the solution |
| 76 | + void printSolution(int dist[], int V) { |
| 77 | + System.out.println("Vertex Distance from Source"); |
| 78 | + for (int i = 0; i < V; ++i) |
| 79 | + System.out.println(i + "\t\t" + dist[i]); |
| 80 | + } |
| 81 | + |
| 82 | + public static void main(String[] args) { |
| 83 | + int V = 5; // Total vertices |
| 84 | + int E = 8; // Total Edges |
| 85 | + |
| 86 | + CreateGraph graph = new CreateGraph(V, E); |
| 87 | + |
| 88 | + // edge 0 --> 1 |
| 89 | + graph.edge[0].s = 0; |
| 90 | + graph.edge[0].d = 1; |
| 91 | + graph.edge[0].w = 5; |
| 92 | + |
| 93 | + // edge 0 --> 2 |
| 94 | + graph.edge[1].s = 0; |
| 95 | + graph.edge[1].d = 2; |
| 96 | + graph.edge[1].w = 4; |
| 97 | + |
| 98 | + // edge 1 --> 3 |
| 99 | + graph.edge[2].s = 1; |
| 100 | + graph.edge[2].d = 3; |
| 101 | + graph.edge[2].w = 3; |
| 102 | + |
| 103 | + // edge 2 --> 1 |
| 104 | + graph.edge[3].s = 2; |
| 105 | + graph.edge[3].d = 1; |
| 106 | + graph.edge[3].w = 6; |
| 107 | + |
| 108 | + // edge 3 --> 2 |
| 109 | + graph.edge[4].s = 3; |
| 110 | + graph.edge[4].d = 2; |
| 111 | + graph.edge[4].w = 2; |
| 112 | + |
| 113 | + graph.BellmanFord(graph, 0); // 0 is the source vertex |
| 114 | + } |
| 115 | +} |
| 116 | + |
0 commit comments