-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedGraphs.java
More file actions
167 lines (152 loc) · 5.36 KB
/
WeightedGraphs.java
File metadata and controls
167 lines (152 loc) · 5.36 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package dsa;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.Stack;
public class WeightedGraphs {
private class Node{
private String label;
private List<Edge> edges = new ArrayList<>();
public Node(String label) {
this.label = label;
}
@Override
public String toString() {
return label;
}
public void addEdge(Node to, int weight){
edges.add(new Edge(this, to, weight));
}
public List<Edge> getEdges(){
return edges;
}
}
private class Edge{
private Node from;
private Node to;
private int weight;
public Edge(Node from, Node to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
@Override
public String toString() {
return from + " --> " + to;
}
}
private class NodeEntry{
private Node node;
private int priority;
public NodeEntry(Node node, int priority) {
this.node = node;
this.priority = priority;
}
}
private Map <String, Node> nodes = new HashMap<>();
public void addNode(String label){
nodes.putIfAbsent(label, new Node(label));
}
public void addEdge(String from, String to, int weight){
var fromNode = nodes.get(from);
if(fromNode == null) throw new IllegalArgumentException();
var toNode = nodes.get(to);
if(toNode == null) throw new IllegalArgumentException();
fromNode.addEdge(toNode, weight);
toNode.addEdge(fromNode, weight);
}
public Path dijkistrasShortestPath(String from, String to){
var fromNode = nodes.get(from);
if(fromNode == null) throw new IllegalArgumentException();
var toNode = nodes.get(to);
if(toNode == null) throw new IllegalArgumentException();
Map<Node, Integer> distances = new HashMap<>();
for (var node : nodes.values())
distances.put(node, Integer.MAX_VALUE);
distances.replace(fromNode, 0);
Map <Node, Node> previousNodes = new HashMap<>();
Set<Node> visited = new HashSet<>();
PriorityQueue <NodeEntry> queue = new PriorityQueue<>(Comparator.comparingInt(ne -> ne.priority));
queue.add(new NodeEntry(fromNode, 0));
while(!queue.isEmpty()){
var current = queue.remove().node;
visited.add(current);
for(var edge : current.getEdges()){
if(visited.contains(edge.to)) continue;
var newDistance = distances.get(current) + edge.weight;
if(newDistance < distances.get(edge.to)){
distances.replace(edge.to, newDistance);
previousNodes.put(edge.to, current);
queue.add(new NodeEntry(edge.to, newDistance));
}
}
}
return buildPath(previousNodes, toNode);
}
private Path buildPath(Map <Node, Node> previousNodes, Node toNode){
Stack <Node> stack = new Stack<>();
stack.push(toNode);
var previous = previousNodes.get(toNode);
while(previous != null){
stack.push(previous);
previous = previousNodes.get(previous);
}
var path = new Path();
while(!stack.isEmpty()){
path.add(stack.pop().label);
}
return path;
}
public boolean hasCycle(){
Set <Node> visited = new HashSet<>();
for (var node : nodes.values()) {
if(!visited.contains(node) && hasCycle(node, null, visited))
return true;
}
return false;
}
private boolean hasCycle(Node node, Node parent, Set <Node> visited){
visited.add(node);
for (var edge : node.getEdges()) {
if(edge.to == parent) continue;
if(visited.contains(edge.to) || hasCycle(edge.to, node, visited))
return true;
}
return false;
}
public WeightedGraphs getMinimumSpanningTree(){
var tree = new WeightedGraphs();
if(nodes.isEmpty()) return tree;
PriorityQueue <Edge> edges = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));
var startNode = nodes.values().iterator().next();
edges.addAll(startNode.getEdges());
tree.addNode(startNode.label);
if(edges.isEmpty()) return tree;
while (tree.nodes.size() < nodes.size()) {
var minEdge = edges.remove();
var nextNode = minEdge.to;
if(tree.containsNode(nextNode.label)) continue;
tree.addNode(nextNode.label);
tree.addEdge(minEdge.from.label, nextNode.label, minEdge.weight);
for (var edge : nextNode.getEdges())
if(!tree.containsNode(edge.to.label))
edges.add(edge);
}
return tree;
}
public boolean containsNode(String label){
return nodes.containsKey(label);
}
public void print(){
for (var node : nodes.values()) {
var edges = node.getEdges();
if(!edges.isEmpty())
System.out.println(node + " -----> " + edges);
}
}
}