-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
LinkedDirectedGraph.java
595 lines (513 loc) · 16.5 KB
/
LinkedDirectedGraph.java
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
* Copyright 2008 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.graph;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A directed graph using linked list within nodes to store edge information.
* <p>
* This implementation favors directed graph operations inherited from <code>
* DirectedGraph</code>.
* Operations from <code>Graph</code> would tends to be slower.
*
*
* @param <N> Value type that the graph node stores.
* @param <E> Value type that the graph edge stores.
*/
public class LinkedDirectedGraph<N, E>
extends DiGraph<N, E> implements GraphvizGraph {
protected final Map<N, LinkedDirectedGraphNode<N, E>> nodes = new HashMap<>();
@Override
public SubGraph<N, E> newSubGraph() {
return new SimpleSubGraph<>(this);
}
public static <N, E> LinkedDirectedGraph<N, E> createWithoutAnnotations() {
return new LinkedDirectedGraph<>(false, false);
}
public static <N, E> LinkedDirectedGraph<N, E> create() {
return new LinkedDirectedGraph<>(true, true);
}
private final boolean useNodeAnnotations;
private final boolean useEdgeAnnotations;
protected LinkedDirectedGraph(
boolean useNodeAnnotations, boolean useEdgeAnnotations) {
this.useNodeAnnotations = useNodeAnnotations;
this.useEdgeAnnotations = useEdgeAnnotations;
}
@Override
public void connect(N srcValue, E edgeValue, N destValue) {
LinkedDirectedGraphNode<N, E> src = getNodeOrFail(srcValue);
LinkedDirectedGraphNode<N, E> dest = getNodeOrFail(destValue);
LinkedDirectedGraphEdge<N, E> edge =
useEdgeAnnotations
? new AnnotatedLinkedDirectedGraphEdge<>(src, edgeValue, dest)
: new LinkedDirectedGraphEdge<>(src, edgeValue, dest);
src.getOutEdges().add(edge);
dest.getInEdges().add(edge);
}
// TODO(johnlenz): make this part of the general graph interface.
/**
* DiGraphNode look ups can be expensive for a large graph operation, prefer this
* method if you have the DiGraphNode available.
*/
public void connect(
DiGraphNode<N, E> src,
E edgeValue,
DiGraphNode<N, E> dest) {
LinkedDirectedGraphEdge<N, E> edge =
useEdgeAnnotations
? new AnnotatedLinkedDirectedGraphEdge<>(src, edgeValue, dest)
: new LinkedDirectedGraphEdge<>(src, edgeValue, dest);
src.getOutEdges().add(edge);
dest.getInEdges().add(edge);
}
// TODO(johnlenz): make this part of the general graph interface.
/**
* DiGraphNode look ups can be expensive for a large graph operation, prefer this
* method if you have the DiGraphNode available.
*/
public void connectIfNotConnectedInDirection(N srcValue, E edgeValue, N destValue) {
LinkedDirectedGraphNode<N, E> src = createDirectedGraphNode(srcValue);
LinkedDirectedGraphNode<N, E> dest = createDirectedGraphNode(destValue);
if (!this.isConnectedInDirection(src, Predicates.equalTo(edgeValue), dest)) {
this.connect(src, edgeValue, dest);
}
}
@Override
public void disconnect(N n1, N n2) {
disconnectInDirection(n1, n2);
disconnectInDirection(n2, n1);
}
@Override
public void disconnectInDirection(N srcValue, N destValue) {
LinkedDirectedGraphNode<N, E> src = getNodeOrFail(srcValue);
LinkedDirectedGraphNode<N, E> dest = getNodeOrFail(destValue);
for (DiGraphEdge<?, E> edge : getDirectedGraphEdges(srcValue, destValue)) {
src.getOutEdges().remove(edge);
dest.getInEdges().remove(edge);
}
}
@Override
public Iterable<DiGraphNode<N, E>> getDirectedGraphNodes() {
return Collections.<DiGraphNode<N, E>>unmodifiableCollection(
nodes.values());
}
@Override
public DiGraphNode<N, E> getDirectedGraphNode(N nodeValue) {
return nodes.get(nodeValue);
}
@Override
public GraphNode<N, E> getNode(N nodeValue) {
return getDirectedGraphNode(nodeValue);
}
@Override
public List<DiGraphEdge<N, E>> getInEdges(N nodeValue) {
LinkedDirectedGraphNode<N, E> node = getNodeOrFail(nodeValue);
return Collections.unmodifiableList(node.getInEdges());
}
@Override
public List<DiGraphEdge<N, E>> getOutEdges(N nodeValue) {
LinkedDirectedGraphNode<N, E> node = getNodeOrFail(nodeValue);
return Collections.unmodifiableList(node.getOutEdges());
}
@Override
public LinkedDirectedGraphNode<N, E> createDirectedGraphNode(N nodeValue) {
LinkedDirectedGraphNode<N, E> node = nodes.get(nodeValue);
if (node == null) {
node = useNodeAnnotations
? new AnnotatedLinkedDirectedGraphNode<N, E>(nodeValue)
: new LinkedDirectedGraphNode<N, E>(nodeValue);
nodes.put(nodeValue, node);
}
return node;
}
@Override
public List<DiGraphEdge<N, E>> getEdges(N n1, N n2) {
// Since this is a method from a generic graph, edges from both
// directions must be added to the returning list.
List<DiGraphEdge<N, E>> forwardEdges = getDirectedGraphEdges(n1, n2);
List<DiGraphEdge<N, E>> backwardEdges = getDirectedGraphEdges(n2, n1);
int totalSize = forwardEdges.size() + backwardEdges.size();
List<DiGraphEdge<N, E>> edges = new ArrayList<>(totalSize);
edges.addAll(forwardEdges);
edges.addAll(backwardEdges);
return edges;
}
@Override
public GraphEdge<N, E> getFirstEdge(N n1, N n2) {
DiGraphNode<N, E> dNode1 = getNodeOrFail(n1);
DiGraphNode<N, E> dNode2 = getNodeOrFail(n2);
for (DiGraphEdge<N, E> outEdge : dNode1.getOutEdges()) {
if (outEdge.getDestination() == dNode2) {
return outEdge;
}
}
for (DiGraphEdge<N, E> outEdge : dNode2.getOutEdges()) {
if (outEdge.getDestination() == dNode1) {
return outEdge;
}
}
return null;
}
@Override
public DiGraphNode<N, E> createNode(N value) {
return createDirectedGraphNode(value);
}
@Override
public List<DiGraphEdge<N, E>> getDirectedGraphEdges(N n1, N n2) {
DiGraphNode<N, E> dNode1 = getNodeOrFail(n1);
DiGraphNode<N, E> dNode2 = getNodeOrFail(n2);
List<DiGraphEdge<N, E>> edges = new ArrayList<>();
for (DiGraphEdge<N, E> outEdge : dNode1.getOutEdges()) {
if (outEdge.getDestination() == dNode2) {
edges.add(outEdge);
}
}
return edges;
}
/**
* DiGraphNode look ups can be expensive for a large graph operation, prefer the
* version below that takes DiGraphNodes, if you have them available.
*/
@Override
public boolean isConnectedInDirection(N n1, N n2) {
return isConnectedInDirection(n1, Predicates.<E>alwaysTrue(), n2);
}
/**
* DiGraphNode look ups can be expensive for a large graph operation, prefer the
* version below that takes DiGraphNodes, if you have them available.
*/
@Override
public boolean isConnectedInDirection(N n1, E edgeValue, N n2) {
return isConnectedInDirection(n1, Predicates.equalTo(edgeValue), n2);
}
/**
* DiGraphNode look ups can be expensive for a large graph operation, prefer this
* method if you have the DiGraphNodes available.
*/
public boolean isConnectedInDirection(
DiGraphNode<N, E> dNode1,
Predicate<E> edgeMatcher,
DiGraphNode<N, E> dNode2) {
// Verify the nodes.
List<DiGraphEdge<N, E>> outEdges = dNode1.getOutEdges();
int outEdgesLen = outEdges.size();
List<DiGraphEdge<N, E>> inEdges = dNode2.getInEdges();
int inEdgesLen = inEdges.size();
// It is possible that there is a large assymmetry between the nodes, so pick the direction
// to search based on the shorter list since the edge lists should be symmetric.
if (outEdgesLen < inEdgesLen) {
for (int i = 0; i < outEdgesLen; i++) {
DiGraphEdge<N, E> outEdge = outEdges.get(i);
if (outEdge.getDestination() == dNode2
&& edgeMatcher.apply(outEdge.getValue())) {
return true;
}
}
} else {
for (int i = 0; i < inEdgesLen; i++) {
DiGraphEdge<N, E> inEdge = inEdges.get(i);
if (inEdge.getSource() == dNode1
&& edgeMatcher.apply(inEdge.getValue())) {
return true;
}
}
}
return false;
}
private boolean isConnectedInDirection(N n1, Predicate<E> edgeMatcher, N n2) {
// Verify the nodes.
DiGraphNode<N, E> dNode1 = getNodeOrFail(n1);
DiGraphNode<N, E> dNode2 = getNodeOrFail(n2);
return isConnectedInDirection(dNode1, edgeMatcher, dNode2);
}
@Override
public List<DiGraphNode<N, E>> getDirectedPredNodes(N nodeValue) {
return getDirectedPredNodes(nodes.get(nodeValue));
}
@Override
public List<DiGraphNode<N, E>> getDirectedSuccNodes(N nodeValue) {
return getDirectedSuccNodes(nodes.get(nodeValue));
}
@Override
public List<DiGraphNode<N, E>> getDirectedPredNodes(
DiGraphNode<N, E> dNode) {
Preconditions.checkNotNull(dNode);
List<DiGraphNode<N, E>> nodeList =
new ArrayList<>(dNode.getInEdges().size());
for (DiGraphEdge<N, E> edge : dNode.getInEdges()) {
nodeList.add(edge.getSource());
}
return nodeList;
}
@Override
public List<DiGraphNode<N, E>> getDirectedSuccNodes(
DiGraphNode<N, E> dNode) {
Preconditions.checkNotNull(dNode);
List<DiGraphNode<N, E>> nodeList =
new ArrayList<>(dNode.getOutEdges().size());
for (DiGraphEdge<N, E> edge : dNode.getOutEdges()) {
nodeList.add(edge.getDestination());
}
return nodeList;
}
@Override
public List<GraphvizEdge> getGraphvizEdges() {
List<GraphvizEdge> edgeList = new ArrayList<>();
for (LinkedDirectedGraphNode<N, E> node : nodes.values()) {
for (DiGraphEdge<N, E> edge : node.getOutEdges()) {
edgeList.add((LinkedDirectedGraphEdge<N, E>) edge);
}
}
return edgeList;
}
@Override
public List<GraphvizNode> getGraphvizNodes() {
List<GraphvizNode> nodeList = new ArrayList<>(nodes.size());
for (LinkedDirectedGraphNode<N, E> node : nodes.values()) {
nodeList.add(node);
}
return nodeList;
}
@Override
public String getName() {
return "LinkedGraph";
}
@Override
public boolean isDirected() {
return true;
}
@Override
public Collection<DiGraphNode<N, E>> getNodes() {
return Collections.<DiGraphNode<N, E>>unmodifiableCollection(nodes.values());
}
@Override
public List<GraphNode<N, E>> getNeighborNodes(N value) {
DiGraphNode<N, E> node = getDirectedGraphNode(value);
List<GraphNode<N, E>> result = new ArrayList<>(
node.getInEdges().size() + node.getOutEdges().size());
for (DiGraphEdge<N, E> inEdge : node.getInEdges()) {
result.add(inEdge.getSource());
}
for (DiGraphEdge<N, E> outEdge : node.getOutEdges()) {
result.add(outEdge.getDestination());
}
return result;
}
@Override
public List<DiGraphEdge<N, E>> getEdges() {
List<DiGraphEdge<N, E>> result = new ArrayList<>();
for (DiGraphNode<N, E> node : nodes.values()) {
result.addAll(node.getOutEdges());
}
return Collections.unmodifiableList(result);
}
@Override
public int getNodeDegree(N value) {
DiGraphNode<N, E> node = getNodeOrFail(value);
return node.getInEdges().size() + node.getOutEdges().size();
}
/**
* A directed graph node that stores outgoing edges and incoming edges as an
* list within the node itself.
*/
public static class LinkedDirectedGraphNode<N, E> implements DiGraphNode<N, E>,
GraphvizNode {
List<DiGraphEdge<N, E>> inEdgeList = new ArrayList<>();
List<DiGraphEdge<N, E>> outEdgeList = new ArrayList<>();
protected final N value;
/**
* Constructor
*
* @param nodeValue Node's value.
*/
LinkedDirectedGraphNode(N nodeValue) {
this.value = nodeValue;
}
@Override
public N getValue() {
return value;
}
@Override
public <A extends Annotation> A getAnnotation() {
throw new UnsupportedOperationException(
"Graph initialized with node annotations turned off");
}
@Override
public void setAnnotation(Annotation data) {
throw new UnsupportedOperationException(
"Graph initialized with node annotations turned off");
}
@Override
public String getColor() {
return "white";
}
@Override
public String getId() {
return "LDN" + hashCode();
}
@Override
public String getLabel() {
return String.valueOf(value);
}
@Override
public String toString() {
return getLabel();
}
@Override
public List<DiGraphEdge<N, E>> getInEdges() {
return inEdgeList;
}
@Override
public List<DiGraphEdge<N, E>> getOutEdges() {
return outEdgeList;
}
}
/**
* A directed graph node with annotations.
*/
static class AnnotatedLinkedDirectedGraphNode<N, E>
extends LinkedDirectedGraphNode<N, E> {
protected Annotation annotation;
/**
* @param nodeValue Node's value.
*/
AnnotatedLinkedDirectedGraphNode(N nodeValue) {
super(nodeValue);
}
@SuppressWarnings("unchecked")
@Override
public <A extends Annotation> A getAnnotation() {
return (A) annotation;
}
@Override
public void setAnnotation(Annotation data) {
annotation = data;
}
}
/**
* A directed graph edge that stores the source and destination nodes at each
* edge.
*/
static class LinkedDirectedGraphEdge<N, E> implements DiGraphEdge<N, E>,
GraphvizEdge {
private DiGraphNode<N, E> sourceNode;
private DiGraphNode<N, E> destNode;
protected final E value;
/**
* Constructor.
*
* @param edgeValue Edge Value.
*/
LinkedDirectedGraphEdge(DiGraphNode<N, E> sourceNode,
E edgeValue, DiGraphNode<N, E> destNode) {
this.value = edgeValue;
this.sourceNode = sourceNode;
this.destNode = destNode;
}
@Override
public DiGraphNode<N, E> getSource() {
return sourceNode;
}
@Override
public DiGraphNode<N, E> getDestination() {
return destNode;
}
@Override
public void setDestination(DiGraphNode<N, E> node) {
destNode = node;
}
@Override
public void setSource(DiGraphNode<N, E> node) {
sourceNode = node;
}
@Override
public E getValue() {
return value;
}
@Override
public <A extends Annotation> A getAnnotation() {
throw new UnsupportedOperationException(
"Graph initialized with edge annotations turned off");
}
@Override
public void setAnnotation(Annotation data) {
throw new UnsupportedOperationException(
"Graph initialized with edge annotations turned off");
}
@Override
public String getColor() {
return "black";
}
@Override
public String getLabel() {
return String.valueOf(value);
}
@Override
public String getNode1Id() {
return ((LinkedDirectedGraphNode<N, E>) sourceNode).getId();
}
@Override
public String getNode2Id() {
return ((LinkedDirectedGraphNode<N, E>) destNode).getId();
}
@Override
public String toString() {
return sourceNode + " -> " + destNode;
}
@Override
public GraphNode<N, E> getNodeA() {
return sourceNode;
}
@Override
public GraphNode<N, E> getNodeB() {
return destNode;
}
}
/**
* A directed graph edge that stores the source and destination nodes at each
* edge.
*/
static class AnnotatedLinkedDirectedGraphEdge<N, E>
extends LinkedDirectedGraphEdge<N, E> {
protected Annotation annotation;
/**
* Constructor.
*
* @param edgeValue Edge Value.
*/
AnnotatedLinkedDirectedGraphEdge(DiGraphNode<N, E> sourceNode,
E edgeValue, DiGraphNode<N, E> destNode) {
super(sourceNode, edgeValue, destNode);
}
@SuppressWarnings("unchecked")
@Override
public <A extends Annotation> A getAnnotation() {
return (A) annotation;
}
@Override
public void setAnnotation(Annotation data) {
annotation = data;
}
}
}