-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathGraphX.java
More file actions
209 lines (166 loc) · 4.47 KB
/
GraphX.java
File metadata and controls
209 lines (166 loc) · 4.47 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
import java.util.*;
//****************BLACKBOX START*****************
//START COPYING FROM HERE
class Hash {
private Map<Triple, Integer> hashTable;
public Hash() {
hashTable = new HashMap<>();
}
public int hash(int x) {
return hash(new Triple(x, 0, 0));
}
public int hash(Pair x) {
return hash(new Triple(x.first, x.second, 0));
}
public int hash(Triple x) {
if (hashTable.containsKey(x)) {
return hashTable.get(x);
}
int newHash = hashTable.size();
hashTable.put(x, newHash);
return newHash;
}
}
/* ---------- Helper Classes ---------- */
class Pair {
int first, second;
Pair(int f, int s) {
first = f;
second = s;
}
}
class Triple {
int first, second, third;
Triple(int f, int s, int t) {
first = f;
second = s;
third = t;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Triple)) return false;
Triple t = (Triple) o;
return first == t.first && second == t.second && third == t.third;
}
@Override
public int hashCode() {
return Objects.hash(first, second, third);
}
}
/* ---------- Graph Class ---------- */
class Graph {
boolean isDirected;
ArrayList<ArrayList<Pair>> adj;
int n;
int N = 5_000_000;
Hash h;
Graph(int n_, boolean isDirected_) {
n = n_;
isDirected = isDirected_;
h = new Hash();
adj = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
adj.add(new ArrayList<>());
}
}
int hash(int u, int v) {
return h.hash(new Pair(u, v));
}
int hash(int u, int v, int k) {
return h.hash(new Triple(u, v, k));
}
void addEdge(int uR, int vR, int c) {
int u = h.hash(uR);
int v = h.hash(vR);
addEdgeInternal(u, v, c);
}
void addEdge(Pair uR, Pair vR, int c) {
int u = h.hash(uR);
int v = h.hash(vR);
addEdgeInternal(u, v, c);
}
void addEdge(Triple uR, Triple vR, int c) {
int u = h.hash(uR);
int v = h.hash(vR);
addEdgeInternal(u, v, c);
}
private void addEdgeInternal(int u, int v, int c) {
addEdgeWeighted(u, v, c);
if (!isDirected) {
addEdgeWeighted(v, u, c);
}
}
private void addEdgeWeighted(int u, int v, int c) {
adj.get(u).add(new Pair(v, c));
}
}
/* ---------- BFS Class ---------- */
class BFS {
ArrayList<Integer> minDistFromSource;
boolean[] visited;
Graph g;
BFS(Graph g_) {
g = g_;
clear();
}
void clear() {
minDistFromSource = new ArrayList<>(Collections.nCopies(g.N, -1));
visited = new boolean[g.N];
}
void run(int sourceR) {
int source = g.h.hash(sourceR);
runInternal(source);
}
void run(Pair sourceR) {
int source = g.h.hash(sourceR);
runInternal(source);
}
void run(Triple sourceR) {
int source = g.h.hash(sourceR);
runInternal(source);
}
int minDist(int targetR) {
int target = g.h.hash(targetR);
return minDistFromSource.get(target);
}
int minDist(Pair targetR) {
int target = g.h.hash(targetR);
return minDistFromSource.get(target);
}
int minDist(Triple targetR) {
int target = g.h.hash(targetR);
return minDistFromSource.get(target);
}
boolean isVisited(int targetR) {
int target = g.h.hash(targetR);
return visited[target];
}
boolean isVisited(Pair targetR) {
int target = g.h.hash(targetR);
return visited[target];
}
boolean isVisited(Triple targetR) {
int target = g.h.hash(targetR);
return visited[target];
}
private void runInternal(int source) {
Queue<Integer> q = new LinkedList<>();
q.add(source);
visited[source] = true;
minDistFromSource.set(source, 0);
while (!q.isEmpty()) {
int cur = q.poll();
for (Pair p : g.adj.get(cur)) {
int adjNode = p.first;
if (!visited[adjNode]) {
visited[adjNode] = true;
minDistFromSource.set(adjNode, minDistFromSource.get(cur) + 1);
q.add(adjNode);
}
}
}
}
}
//END COPYING HERE
//****************BLACKBOX END******************