-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbellmanford.java
87 lines (71 loc) · 1.9 KB
/
bellmanford.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package graphs;
import java.util.ArrayList;
import java.util.HashMap;
import graphs.makegraph.Graph;
public class bellmanford {
public static class edgepair {
String v1;
String v2;
int cost;
edgepair(String v1, String v2, int cost) {
this.v1 = v1;
this.v2 = v2;
this.cost = cost;
}
}
public static ArrayList<edgepair> alledges(HashMap<String, HashMap<String, Integer>> vtces) {
ArrayList<edgepair> ans = new ArrayList<>();
for (String v : vtces.keySet()) {
HashMap<String, Integer> nbr = vtces.get(v);
for (String nbrv : nbr.keySet()) {
ans.add(new edgepair(v, nbrv, nbr.get(nbrv)));
}
}
return ans;
}
public static void bellmanford(HashMap<String, HashMap<String, Integer>> vtces, String src) {
ArrayList<edgepair> edges = alledges(vtces);
HashMap<String, Integer> map = new HashMap<>();
// fill map with every vname and max value
for (String vname : vtces.keySet()) {
// not putting max value as we will add and that can go beyound range
map.put(vname, 1000000);
if (vname == src) {
map.put(vname, 0);
}
}
// now relax every vertex
int V = vtces.size();
for (int i = 1; i <= V; i++) {
for (edgepair e : edges) {
int oc = map.get(e.v2);
int nc = map.get(e.v1) + e.cost;
if (oc > nc) {
if (i <= V - 1)
map.put(e.v2, nc);
else
System.out.println("-ve weight cycle present");
}
}
}
System.out.println(map);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Graph graph = new Graph();
graph.addvertex("a");
graph.addvertex("b");
graph.addvertex("c");
graph.addvertex("d");
graph.addvertex("e");
graph.addedge("a", "b", 8);
graph.addedge("a", "c", 4);
graph.addedge("a", "d", 5);
graph.addedge("c", "d", -3);
graph.addedge("d", "e", 4);
graph.addedge("b", "e", 2);
graph.addedge("e", "b", 1);
graph.diplay();
bellmanford(graph.vtces, "a");
}
}