-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
278 lines (224 loc) · 4.95 KB
/
Graph.java
File metadata and controls
278 lines (224 loc) · 4.95 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.util.*;
public class Graph{
public static class UnionFind{
public static class UNode{
private Integer value;
public UNode(int v){
value = v;
}
}
private HashMap<UNode,UNode> parents;
private HashMap<Integer,UNode> uNodes;
private HashMap<UNode,Integer> sizeMap;
public UnionFind(int[] arr){
parents = new HashMap<>();
sizeMap = new HashMap<>();
uNodes = new HashMap<>();
for (int i=0; i<arr.length;i++ ) {
UNode u=new UNode(arr[i]);
parents.put(u,u);
sizeMap.put(u,1);
uNodes.put(arr[i],u);
}
}
public UNode find(int a){
Stack<UNode> stack = new Stack<>();
UNode cur = uNodes.get(a);
while(cur != parents.get(cur)){
stack.push(cur);
cur = parents.get(cur);
}
while(!stack.isEmpty()){
parents.put(stack.pop(),cur);
}
return cur;
}
public boolean isSameSet(int a,int b){
return find(a) == find(b);
}
public void union(int a,int b){
UNode fa = find(a);
UNode fb = find(b);
if(fa!=fb){
int sa = sizeMap.get(fa);
int sb = sizeMap.get(fb);
UNode big = sa>=sb ? fa : fb;
UNode small = big==fa ? fb : fa;
parents.put(small,big);
sizeMap.put(big,sa+sb);
sizeMap.remove(small);
}
}
}
public static class Node{
public int value;
public int in;
public int to;
public List<Edge> edges;
public List<Node> nexts;
public Node(int v,int i,int t,List<Edge> e,List<Node> n){
value=v;
in = i;
to =t;
edges=e;
nexts=n;
}
}
public static class Edge{
private int weight;
private Node from ;
private Node to;
public Edge(int w,Node f,Node t){
weight=w;
from=f;
to=t;
}
}
private HashMap<Integer,Node> nodes;
private HashSet<Edge> edges;
public Graph(){
nodes = new HashMap<>();
edges = new HashSet<>();
}
public void add(int[][] arr){
for (int i=0; i<arr.length; i++) {
Node f = nodes.get(arr[i][1]);
Node t = nodes.get(arr[i][2]);
if(f==null){
List<Node> nList = new ArrayList<>();
List<Edge> eList = new ArrayList<>();
f = new Node(arr[i][1],0,1,eList,nList);
nodes.put(arr[i][1],f);
}else{
f.to++;
}
if(t == null){
List<Edge> eList = new ArrayList<>();
List<Node> nList = new ArrayList<>();
t = new Node(arr[i][2],1,0,eList,nList);
nodes.put(arr[i][2],t);
}else{
t.in++;
}
Edge e = new Edge(arr[i][0],f,t);
edges.add(e);
f.edges.add(e);
f.nexts.add(t);
}
}
public void BFS(){
Queue<Node> queue = new LinkedList<>();
HashSet<Node> exist=new HashSet<>();
for (Node node : nodes.values() ) {
queue.offer(node);
while(!queue.isEmpty()){
Node cur = queue.poll();
if(!exist.contains(cur)){
exist.add(cur);
System.out.print(cur.value+",");
for (Node n : cur.nexts) {
queue.offer(n);
}
}
}
System.out.println();
}
}
public void DFS(){
Stack<Node> stack = new Stack<>();
HashSet<Node> exist = new HashSet<>();
for (Node node : nodes.values()) {
if(!exist.contains(node)){
stack.push(node);
exist.add(node);
System.out.print(node.value+",");
while(!stack.isEmpty()){
Node cur = stack.pop();
for (Node next: cur.nexts) {
if(!exist.contains(next)){
stack.push(cur);
stack.push(next);
exist.add(next);
System.out.print(next.value+",");
break;
}
}
}
}
}
}
public static class WeightCompar implements Comparator<Edge>{
@Override
public int compare(Edge e1,Edge e2){
return e1.weight - e2.weight;
}
}
public List<Edge> Kruskal(){
int[] arr=new int[nodes.values().size()];
int index=0;
for (Node node : nodes.values()) {
arr[index++] = node.value;
}
UnionFind union=new UnionFind(arr);
PriorityQueue<Edge> queue = new PriorityQueue<>(new WeightCompar());
for (Edge e : edges) {
queue.add(e);
}
List<Edge> result = new ArrayList<>();
while(!queue.isEmpty()){
Edge curr = queue.poll();
if(!union.isSameSet(curr.from.value,curr.to.value)){
result.add(curr);
union.union(curr.from.value,curr.to.value);
}
}
return result;
}
public List<Node> topologicalSort(){
List<Node> result = new ArrayList<>();
Stack<Node> stack = new Stack<>();
for (Node node : nodes.values() ) {
if(node.in ==0){
stack.push(node);
}
}
while(!stack.isEmpty()){
Node curr = stack.pop();
result.add(curr);
System.out.print(curr.value+",");
for (Node next : curr.nexts) {
--next.in;
if(next.in ==0){
stack.push(next);
}
}
}
return result;
}
public static void main(String[] args){
Graph g = new Graph();
int[][] arr=new int[][]{
{4,1,4},
{2,4,2},
{40,3,4},
{9,4,5},
{2,1,3},
{1,12,17},
{11,12,13},
{1,3,7},
{7,5,9},
{5,2,8},
{100,8,17},
{5,2,7},
{1,17,13}
};
g.add(arr);
g.DFS();
System.out.println();
List<Edge> eArray = g.Kruskal();
for (Edge e:eArray) {
System.out.println(e.from.value+"--->"+e.to.value);
}
g.topologicalSort();
}
}