-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphs.java
More file actions
163 lines (146 loc) · 5.08 KB
/
Graphs.java
File metadata and controls
163 lines (146 loc) · 5.08 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
package dsa;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class Graphs {
private class Node{
private String label;
public Node(String label) {
this.label = label;
}
@Override
public String toString() {
return label;
}
}
private Map <String, Node> nodes = new HashMap<>();
private Map<Node, List<Node>> adjacencyList = new HashMap<>();
public void addNode(String label){
var node = new Node(label);
nodes.putIfAbsent(label, node);
adjacencyList.putIfAbsent(node, new ArrayList<>());
}
public void addEdge(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();
adjacencyList.get(fromNode).add(toNode);
}
public void removeNode(String label){
var node = nodes.get(label);
if(node == null) return;
for(var n : adjacencyList.keySet())
adjacencyList.get(n).remove(node);
adjacencyList.remove(node);
nodes.remove(node);
}
public void removeEdge(String from, String to){
var fromNode = nodes.get(from);
var toNode = nodes.get(to);
if(fromNode == null || toNode == null)
return;
adjacencyList.get(fromNode).remove(toNode);
}
public void traverseDepthFirst(String root){
var node = nodes.get(root);
if(node == null) return;
traverseDepthFirst(node, new HashSet<>());
}
private void traverseDepthFirst(Node root, Set <Node> visited){
System.out.print(root + " ");
visited.add(root);
for (Node node : adjacencyList.get(root))
if(!visited.contains(node))
traverseDepthFirst(node, visited);
}
public void traverseDepthFirstIterative(String root){
var node = nodes.get(root);
if(node == null) return;
Stack <Node> stack = new Stack<>();
Set <Node> visited = new HashSet<>();
stack.push(node);
while(!stack.isEmpty()){
var current = stack.pop();
if(visited.contains(current))
continue;
System.out.print(current + " ");
visited.add(current);
for (var neighbour : adjacencyList.get(current)) {
if(!visited.contains(neighbour))
stack.push(neighbour);
}
}
}
public void traverseBreadthFirst(String root){
var node = nodes.get(root);
if(node == null) return;
Set <Node> visited = new HashSet<>();
Queue <Node> queue = new ArrayDeque<>();
queue.add(node);
while(!queue.isEmpty()){
var current = queue.remove();
if(visited.contains(current))
continue;
System.out.print(current + " ");
visited.add(current);
for(var neighbour : adjacencyList.get(current))
if(!visited.contains(neighbour))
queue.add(neighbour);
}
}
public List<String> topologicalSort(){
Stack <Node> stack = new Stack<>();
Set <Node> visited = new HashSet<>();
for (var node : nodes.values()) {
topologicalSort(node, visited, stack);
}
List <String> sorted = new ArrayList<>();
while (!stack.empty())
sorted.add(stack.pop().label);
return sorted;
}
private void topologicalSort(Node node, Set <Node> visited, Stack <Node> stack){
if(visited.contains(node)) return;
visited.add(node);
for (var neighbour : adjacencyList.get(node))
topologicalSort(neighbour, visited, stack);
stack.push(node);
}
public boolean hasCycle(){
Set <Node> all = new HashSet<>();
all.addAll(nodes.values());
Set <Node> visisting = new HashSet<>();
Set <Node> visisted = new HashSet<>();
while(!all.isEmpty()){
var current = all.iterator().next();
if(hasCycle(current, all, visisting, visisted)) return true;
}
return false;
}
private boolean hasCycle(Node node, Set <Node> all, Set <Node> visiting, Set <Node> visited){
all.remove(node);
visiting.add(node);
for(var neighbour : adjacencyList.get(node)){
if(visited.contains(neighbour)) continue;
if(visiting.contains(neighbour)) return true;
if(hasCycle(neighbour, all, visiting, visited)) return true;
}
visiting.remove(node);
visited.add(node);
return false;
}
public void print(){
for (var source : adjacencyList.keySet()) {
var targets = adjacencyList.get(source);
if(!targets.isEmpty())
System.out.println(source + " ------> " + targets);
}
}
}