-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph.java
More file actions
342 lines (282 loc) · 8.72 KB
/
Copy pathGraph.java
File metadata and controls
342 lines (282 loc) · 8.72 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.List;
public class Graph<T extends Comparable<T>> {
private List<Vertex<T>> verticies = new CopyOnWriteArrayList<Vertex<T>>();
private List<Edge<T>> edges = new CopyOnWriteArrayList<Edge<T>>();
public enum TYPE {
DIRECTED, UNDIRECTED
};
private TYPE type = TYPE.UNDIRECTED;
public Graph(){
}
public Graph(Graph<T> g) {
// Deep copies
// Copy the vertices (which copies the edges)
for (Vertex<T> v : g.getVerticies()) {
this.verticies.add(new Vertex<T>(v));
}
// Update the object references
for (Vertex<T> v : this.verticies) {
for (Edge<T> e : v.getEdges()) {
Vertex<T> fromVertex = e.getFromVertex();
Vertex<T> toVertex = e.getToVertex();
int indexOfFrom = this.verticies.indexOf(fromVertex);
e.from = this.verticies.get(indexOfFrom);
int indexOfTo = this.verticies.indexOf(toVertex);
e.to = this.verticies.get(indexOfTo);
this.edges.add(e);
}
}
type = g.getType();
}
public Graph(TYPE type) {
this();
this.type = type;
}
public Graph(List<Vertex<T>> verticies, List<Edge<T>> edges) {
this(TYPE.UNDIRECTED, verticies, edges);
}
public Graph(TYPE type, List<Vertex<T>> verticies, List<Edge<T>> edges) {
this(type);
this.verticies.addAll(verticies);
this.edges.addAll(edges);
for (Edge<T> e : edges) {
Vertex<T> from = e.from;
Vertex<T> to = e.to;
if (!this.verticies.contains(from) || !this.verticies.contains(to))
continue;
int index = this.verticies.indexOf(from);
Vertex<T> fromVertex = this.verticies.get(index);
index = this.verticies.indexOf(to);
Vertex<T> toVertex = this.verticies.get(index);
fromVertex.addEdge(e);
if (this.type == TYPE.UNDIRECTED) {
Edge<T> reciprical = new Edge<T>(e.cost, toVertex, fromVertex);
toVertex.addEdge(reciprical);
this.edges.add(reciprical);
}
}
}
public TYPE getType() {
return type;
}
public List<Vertex<T>> getVerticies() {
return verticies;
}
public List<Edge<T>> getEdges() {
return edges;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (Vertex<T> v : verticies) {
builder.append(v.toString());
}
return builder.toString();
}
// 정점
public static class Vertex<T extends Comparable<T>> implements Comparable<Vertex<T>>{
private T value = null;
private int weight = 0;
private List<Edge<T>> edges = new ArrayList<Edge<T>>();
public Vertex(T value){
this.value = value;
}
public Vertex(T value, int weight) {
this(value);
this.weight = weight;
}
public Vertex(Vertex<T> vertex) {
this(vertex.value, vertex.weight);
this.edges = new ArrayList<Edge<T>>();
for (Edge<T> e : vertex.edges) {
this.edges.add(new Edge<T>(e));
}
}
public T getValue(){
return value;
}
public int getWeight(){
return weight;
}
public void setWeight(int weight){
this.weight = weight;
}
public void addEdge(Edge<T> e){
edges.add(e);
}
public List<Edge<T>> getEdges(){
return edges;
}
public boolean pathTo(Vertex<T> v) {
for (Edge<T> e : edges) {
if (e.to.equals(v))
return true;
}
return false;
}
@Override
public int compareTo(Vertex<T> v) {
if (this.value == null || v.value == null)
return -1;
return this.value.compareTo(v.value);
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object v1) {
if (!(v1 instanceof Vertex))
return false;
Vertex<T> v = (Vertex<T>) v1;
boolean values = this.value.equals(v.value);
if (!values)
return false;
boolean weight = this.weight == v.weight;
if (!weight)
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("vertex:").append(" value=").append(value).append(" weight=").append(weight).append("\n");
for (Edge<T> e : edges) {
builder.append("\t").append(e.toString());
}
return builder.toString();
}
}
// 간선
public static class Edge<T extends Comparable<T>> implements Comparable<Edge<T>> {
private Vertex<T> from = null;
private Vertex<T> to = null;
private int cost = 0;
public Edge(int cost, Vertex<T> from, Vertex<T> to) {
if (from == null || to == null)
throw (new NullPointerException("Both 'to' and 'from' Verticies need to be non-NULL."));
this.cost = cost;
this.from = from;
this.to = to;
}
public Edge(Edge<T> e) {
this(e.cost, e.from, e.to);
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
;
}
public Vertex<T> getFromVertex() {
return from;
}
public Vertex<T> getToVertex() {
return to;
}
@Override
public int compareTo(Edge<T> e) {
if (this.cost < e.cost)
return -1;
if (this.cost > e.cost)
return 1;
return 0;
}
@Override
public int hashCode() {
return this.cost * (this.getFromVertex().value.hashCode() * this.getToVertex().value.hashCode());
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object e1) {
if (!(e1 instanceof Edge))
return false;
Edge<T> e = (Edge<T>) e1;
boolean costs = this.cost == e.cost;
if (!costs)
return false;
boolean froms = this.from.equals(e.from);
if (!froms)
return false;
boolean tos = this.to.equals(e.to);
if (!tos)
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("edge:").append(" [").append(from.value).append("]").append(" -> ").append("[")
.append(to.value).append("]").append(" = ").append(cost).append("\n");
return builder.toString();
}
}
// 경로 테이블 만들때 Vertex 와 Cost 쌍
public static class CostVertexPair<T extends Comparable<T>> implements Comparable<CostVertexPair<T>> {
private int cost = Integer.MAX_VALUE;
private Vertex<T> vertex = null;
public CostVertexPair(int cost, Vertex<T> vertex) {
if (vertex == null)
throw (new NullPointerException("vertex cannot be NULL."));
this.cost = cost;
this.vertex = vertex;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public Vertex<T> getVertex() {
return vertex;
}
@Override
public int compareTo(CostVertexPair<T> p) {
if (p == null)
throw new NullPointerException("CostVertexPair 'p' must be non-NULL.");
if (this.cost < p.cost)
return -1;
if (this.cost > p.cost)
return 1;
return 0;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Vertex=").append(vertex.getValue()).append(" cost=").append(cost).append("\n");
return builder.toString();
}
}
// 경로 path pair
public static class CostPathPair<T extends Comparable<T>> {
private int cost = 0;
private Set<Edge<T>> path = null;
public CostPathPair(int cost, Set<Edge<T>> path) {
if (path == null)
throw (new NullPointerException("path cannot be NULL."));
this.cost = cost;
this.path = path;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public Set<Edge<T>> getPath() {
return path;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Cost = ").append(cost).append("\n");
for (Edge<T> e : path) {
builder.append("\t").append(e);
}
return builder.toString();
}
}
}