Skip to content

Commit c7d9170

Browse files
committed
Completed Bellman Ford and Dijkstra's Algorithm
1 parent 2146219 commit c7d9170

File tree

1 file changed

+108
-0
lines changed
  • src/main/java/com/thealgorithms/datastructures/graphs

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import java.util.*;
4+
5+
public class Johnson {
6+
7+
private static final int INFINITY = Integer.MAX_VALUE;
8+
9+
static class Vertex {
10+
public String label;
11+
public int weight = INFINITY;
12+
public Vertex predecessor;
13+
public final HashMap<String, Integer> edges = new HashMap<>();
14+
15+
public Vertex(String label) {
16+
this.label = label;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
StringBuilder stringOfEdges = new StringBuilder();
22+
stringOfEdges.append(predecessor == null ? "NONE" : predecessor.label).append("\n\tEdges:");
23+
edges.forEach((v, cost) -> stringOfEdges.append("\n\t\t").append(cost).append(" ")
24+
.append(label).append(" -> ").append(v));
25+
return "Vertex " + label +
26+
":\n\tWeight: " + weight +
27+
"\n\tPredecessor: " +
28+
stringOfEdges;
29+
}
30+
}
31+
32+
static class Graph {
33+
private final HashMap<String, Vertex> adj = new HashMap<>();
34+
private final String source;
35+
36+
public Graph(String source, String[] next, Integer[] weights) {
37+
addEdge((this.source = source), next, weights);
38+
adj.get(source).weight = 0;
39+
}
40+
41+
public void addEdge(String label, String[] next, Integer[] weights) {
42+
adj.put(label, new Vertex(label));
43+
for (int i = 0; i < next.length; i++) {
44+
adj.get(label).edges.put(next[i], weights[i]);
45+
}
46+
}
47+
48+
public boolean bellmanFord() {
49+
for (int i = 1; i < adj.size() - 1; i++) {
50+
adj.forEach((uLabel, u) -> relax(uLabel));
51+
}
52+
return adj.values().stream().anyMatch(u -> u.edges.entrySet().stream().noneMatch(entry ->
53+
adj.get(entry.getKey()).weight >
54+
(u.weight == INFINITY ? INFINITY : u.weight + entry.getValue())
55+
));
56+
}
57+
58+
public LinkedHashSet<Vertex> djikstra() {
59+
reset();
60+
LinkedHashSet<Vertex> shortestPath = new LinkedHashSet<>();
61+
PriorityQueue<Vertex> queue = new PriorityQueue<>(Comparator.comparingInt(v -> v.weight));
62+
queue.addAll(adj.values());
63+
while (!queue.isEmpty()) {
64+
Vertex u = queue.poll();
65+
shortestPath.add(u);
66+
relax(u.label);
67+
}
68+
return shortestPath;
69+
}
70+
71+
private void relax(String u) {
72+
adj.get(u).edges.forEach((vLabel, vWeight) -> {
73+
if (adj.get(vLabel).weight >
74+
(adj.get(u).weight == INFINITY ? INFINITY : adj.get(u).weight + vWeight)) {
75+
adj.get(vLabel).weight = adj.get(u).weight + vWeight;
76+
adj.get(vLabel).predecessor = adj.get(u);
77+
}
78+
});
79+
}
80+
81+
public void reset() {
82+
adj.forEach((label, vertex) -> {
83+
vertex.weight = vertex.label.equals(source) ? 0 : INFINITY;
84+
vertex.predecessor = null;
85+
});
86+
}
87+
88+
public void print() {
89+
adj.values().forEach(System.out::println);
90+
}
91+
}
92+
93+
public static void main(String[] args) {
94+
Graph graph = new Graph("s", new String[]{"t", "y"}, new Integer[]{6, 7});
95+
graph.addEdge("t", new String[]{"x", "y", "z"}, new Integer[]{5, 8, -4});
96+
graph.addEdge("x", new String[]{"t"}, new Integer[]{-2});
97+
graph.addEdge("y", new String[]{"x", "z"}, new Integer[]{-3, 9});
98+
graph.addEdge("z", new String[]{"s", "x"}, new Integer[]{2, 7});
99+
System.out.println(graph.bellmanFord());
100+
graph.print();
101+
graph = new Graph("s", new String[]{"t", "y"}, new Integer[]{10, 5});
102+
graph.addEdge("t", new String[]{"x", "y"}, new Integer[]{1, 2});
103+
graph.addEdge("x", new String[]{"z"}, new Integer[]{4});
104+
graph.addEdge("y", new String[]{"t","x", "z"}, new Integer[]{3, 9, 2});
105+
graph.addEdge("z", new String[]{"s", "x"}, new Integer[]{7, 6});
106+
graph.djikstra().forEach(System.out::println);
107+
}
108+
}

0 commit comments

Comments
 (0)