Skip to content

Commit c35040d

Browse files
committed
S49
1 parent b4ecca3 commit c35040d

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
/dFS/target/
1919
/bFS/target/
2020
/dijkstra/target/
21+
/bellmanFord/target/

bellmanFord/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.mycompany</groupId>
5+
<artifactId>bellmanFord</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>11</maven.compiler.source>
11+
<maven.compiler.target>11</maven.compiler.target>
12+
</properties>
13+
</project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)