This repository was archived by the owner on Jun 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphUtils.java
More file actions
723 lines (656 loc) · 26.2 KB
/
Copy pathGraphUtils.java
File metadata and controls
723 lines (656 loc) · 26.2 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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package gvisual;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.util.*;
/**
* Shared graph traversal and adjacency utilities used by multiple analyzers.
*
* <p>Centralizes common operations that were previously duplicated across
* {@link CommunityDetector}, {@link GraphDiameterAnalyzer},
* {@link NodeCentralityAnalyzer}, {@link PageRankAnalyzer},
* {@link ShortestPathFinder}, {@link LinkPredictionAnalyzer}, and
* {@link GraphIsomorphismAnalyzer}.</p>
*
* <p>All methods are static and stateless — safe for concurrent use.</p>
*
* @author zalenix
*/
public final class GraphUtils {
private GraphUtils() { /* utility class */ }
/**
* Returns the vertex at the other end of an edge from the given vertex.
*
* @param e the edge
* @param current the vertex we're "standing on"
* @return the other endpoint, or {@code null} if {@code current} is not
* an endpoint of the edge
*/
public static String getOtherEnd(edge e, String current) {
String v1 = e.getVertex1();
String v2 = e.getVertex2();
if (current.equals(v1)) return v2;
if (current.equals(v2)) return v1;
return null;
}
/**
* Builds an adjacency set map for all vertices in the graph.
*
* @param graph the JUNG graph
* @return map from each vertex to its set of neighbor vertex IDs
*/
public static Map<String, Set<String>> buildAdjacencyMap(
Graph<String, edge> graph) {
Map<String, Set<String>> adj = new HashMap<String, Set<String>>();
for (String v : graph.getVertices()) {
Set<String> neighbors = new HashSet<String>();
Collection<String> graphNeighbors = graph.getNeighbors(v);
if (graphNeighbors != null) {
neighbors.addAll(graphNeighbors);
}
adj.put(v, neighbors);
}
return adj;
}
/**
* Build an undirected adjacency map restricted to a subset of vertices.
* Only edges where both endpoints are in {@code vertices} are included.
*
* @param graph the JUNG graph
* @param vertices the vertex subset to include
* @return adjacency map (vertex → set of neighbours within the subset)
*/
public static Map<String, Set<String>> buildAdjacencyMap(
Graph<String, edge> graph, Set<String> vertices) {
Map<String, Set<String>> adj = new HashMap<String, Set<String>>();
for (String v : vertices) {
adj.put(v, new HashSet<String>());
}
for (edge e : graph.getEdges()) {
Collection<String> eps = graph.getEndpoints(e);
if (eps == null || eps.size() != 2) continue;
Iterator<String> it = eps.iterator();
String v1 = it.next();
String v2 = it.next();
if (vertices.contains(v1) && vertices.contains(v2)) {
adj.get(v1).add(v2);
adj.get(v2).add(v1);
}
}
return adj;
}
/**
* BFS from a source vertex, returning distances (hop counts) to all
* reachable vertices.
*
* @param graph the JUNG graph
* @param source the starting vertex
* @return map from vertex ID to its BFS distance from source
*/
public static Map<String, Integer> bfsDistances(
Graph<String, edge> graph, String source) {
Map<String, Integer> distances = new HashMap<String, Integer>();
Queue<String> queue = new LinkedList<String>();
distances.put(source, 0);
queue.add(source);
while (!queue.isEmpty()) {
String current = queue.poll();
int currentDist = distances.get(current);
Collection<edge> incidentEdges = graph.getIncidentEdges(current);
if (incidentEdges == null) continue;
for (edge e : incidentEdges) {
String neighbor = getOtherEnd(e, current);
if (neighbor != null && !distances.containsKey(neighbor)) {
distances.put(neighbor, currentDist + 1);
queue.add(neighbor);
}
}
}
return distances;
}
/**
* BFS to discover the connected component containing a source vertex.
*
* @param graph the JUNG graph
* @param source the starting vertex
* @return set of all vertices reachable from source (including source)
*/
public static Set<String> bfsComponent(
Graph<String, edge> graph, String source) {
Set<String> component = new LinkedHashSet<String>();
Queue<String> queue = new LinkedList<String>();
component.add(source);
queue.add(source);
while (!queue.isEmpty()) {
String current = queue.poll();
Collection<edge> incidentEdges = graph.getIncidentEdges(current);
if (incidentEdges == null) continue;
for (edge e : incidentEdges) {
String neighbor = getOtherEnd(e, current);
if (neighbor != null && !component.contains(neighbor)) {
component.add(neighbor);
queue.add(neighbor);
}
}
}
return component;
}
/**
* Finds all connected components of the graph.
*
* @param graph the JUNG graph
* @return list of components, each a set of vertex IDs, sorted largest-first
*/
public static List<Set<String>> findComponents(
Graph<String, edge> graph) {
Set<String> visited = new HashSet<String>();
List<Set<String>> components = new ArrayList<Set<String>>();
for (String vertex : graph.getVertices()) {
if (!visited.contains(vertex)) {
Set<String> component = bfsComponent(graph, vertex);
visited.addAll(component);
components.add(component);
}
}
// Sort largest-first
Collections.sort(components, (Set<String> a, Set<String> b) -> {
return Integer.compare(b.size(), a.size());
});
return components;
}
/**
* Finds the largest connected component.
*
* @param graph the JUNG graph
* @return set of vertices in the largest component, or empty set if graph is empty
*/
public static Set<String> findLargestComponent(
Graph<String, edge> graph) {
List<Set<String>> components = findComponents(graph);
return components.isEmpty() ? Collections.<String>emptySet() : components.get(0);
}
/**
* Returns the set of common neighbors between two vertices.
*
* @param adjacency precomputed adjacency map
* @param u first vertex
* @param v second vertex
* @return set of vertices adjacent to both u and v
*/
public static Set<String> getCommonNeighbors(
Map<String, Set<String>> adjacency, String u, String v) {
Set<String> uNeighbors = adjacency.get(u);
Set<String> vNeighbors = adjacency.get(v);
if (uNeighbors == null || vNeighbors == null) {
return new HashSet<String>();
}
Set<String> common = new HashSet<String>(uNeighbors);
common.retainAll(vNeighbors);
return common;
}
/**
* Checks whether the subgraph induced by the given vertices contains a cycle.
* Works for both directed and undirected graphs.
*
* @param graph the JUNG graph
* @param vertices the vertex subset to check (only edges within this set are considered)
* @param directed whether to treat edges as directed
* @return true if the induced subgraph contains a cycle
*/
public static boolean hasCycleInSubgraph(
Graph<String, edge> graph, Set<String> vertices, boolean directed) {
if (vertices.size() <= 1) return false;
Set<String> visited = new HashSet<String>();
Set<String> inStack = new HashSet<String>();
for (String v : vertices) {
if (!visited.contains(v)) {
if (directed) {
if (hasCycleDFS_directed(graph, v, vertices, visited, inStack)) return true;
} else {
if (hasCycleDFS_undirected(graph, v, null, vertices, visited)) return true;
}
}
}
return false;
}
private static boolean hasCycleDFS_undirected(
Graph<String, edge> graph, String v, String parent,
Set<String> vertices, Set<String> visited) {
visited.add(v);
for (String n : graph.getNeighbors(v)) {
if (!vertices.contains(n)) continue;
if (!visited.contains(n)) {
if (hasCycleDFS_undirected(graph, n, v, vertices, visited)) return true;
} else if (!n.equals(parent)) {
return true;
}
}
return false;
}
private static boolean hasCycleDFS_directed(
Graph<String, edge> graph, String v, Set<String> vertices,
Set<String> visited, Set<String> inStack) {
visited.add(v);
inStack.add(v);
for (String n : graph.getSuccessors(v)) {
if (!vertices.contains(n)) continue;
if (!visited.contains(n)) {
if (hasCycleDFS_directed(graph, n, vertices, visited, inStack)) return true;
} else if (inStack.contains(n)) {
return true;
}
}
inStack.remove(v);
return false;
}
/**
* Counts edges in the subgraph induced by the given vertex set.
*
* @param graph the JUNG graph
* @param vertices the vertex subset
* @return number of edges where both endpoints are in the vertex set
*/
public static int countEdgesInSubgraph(
Graph<String, edge> graph, Set<String> vertices) {
// Single pass over all edges — O(E) with no auxiliary HashSet.
// For sparse subgraphs this is competitive with the vertex-centric
// approach, and for dense subgraphs it avoids the O(Σdeg) edge
// deduplication overhead that the previous seen-set approach had.
int count = 0;
for (edge e : graph.getEdges()) {
String v1 = e.getVertex1();
String v2 = e.getVertex2();
if (vertices.contains(v1) && vertices.contains(v2)) {
count++;
}
}
return count;
}
/**
* Counts connected components in the subgraph induced by the given vertex set.
*
* @param graph the JUNG graph
* @param vertices the vertex subset
* @return number of connected components within the subset
*/
public static int countComponentsInSubgraph(
Graph<String, edge> graph, Set<String> vertices) {
Set<String> visited = new HashSet<String>();
int components = 0;
for (String v : vertices) {
if (!visited.contains(v)) {
components++;
Queue<String> queue = new LinkedList<String>();
queue.add(v);
visited.add(v);
while (!queue.isEmpty()) {
String curr = queue.poll();
for (String n : graph.getNeighbors(curr)) {
if (vertices.contains(n) && !visited.contains(n)) {
visited.add(n);
queue.add(n);
}
}
}
}
}
return components;
}
/**
* Computes the cycle rank (circuit rank) of the subgraph induced by the
* given vertex set: edges − vertices + components.
*
* @param graph the JUNG graph
* @param vertices the vertex subset
* @return the cycle rank of the induced subgraph
*/
public static int cycleRankOfSubgraph(
Graph<String, edge> graph, Set<String> vertices) {
int edges = countEdgesInSubgraph(graph, vertices);
int comps = countComponentsInSubgraph(graph, vertices);
return edges - vertices.size() + comps;
}
// ── Graph copy ──────────────────────────────────────────────────────
/**
* Creates a deep copy of a graph, preserving all vertices, edges, edge
* types, weights, and labels.
*
* @param graph the graph to copy
* @return a new graph with identical structure
*/
public static Graph<String, edge> copyGraph(Graph<String, edge> graph) {
Graph<String, edge> copy = new UndirectedSparseGraph<String, edge>();
for (String v : graph.getVertices()) {
copy.addVertex(v);
}
for (edge e : graph.getEdges()) {
Collection<String> endpoints = graph.getEndpoints(e);
Iterator<String> it = endpoints.iterator();
String v1 = it.next();
String v2 = it.next();
edge newEdge = new edge(e.getType(), v1, v2);
newEdge.setWeight(e.getWeight());
newEdge.setLabel(e.getLabel());
copy.addEdge(newEdge, v1, v2);
}
return copy;
}
// ── Betweenness Centrality (array-based Brandes) ──────────────────
/**
* Computes betweenness centrality for all vertices using Brandes'
* algorithm with array-based storage. Avoids per-source HashMap
* allocation, using indexed arrays for sigma, distance, delta, and
* predecessor lists.
*
* <p>For undirected graphs the raw scores are halved (each shortest
* path is counted from both endpoints).</p>
*
* @param graph the graph
* @return map from vertex ID to betweenness centrality score
*/
public static Map<String, Double> computeBetweenness(Graph<String, edge> graph) {
int n = graph.getVertexCount();
if (n == 0) return Collections.emptyMap();
// Build vertex index for array-based lookups
List<String> vertexList = new ArrayList<String>(graph.getVertices());
Map<String, Integer> vertexIndex = new HashMap<String, Integer>(n * 2);
for (int i = 0; i < n; i++) {
vertexIndex.put(vertexList.get(i), i);
}
// Build adjacency lists using integer indices
int[][] adjLists = new int[n][];
for (int i = 0; i < n; i++) {
String node = vertexList.get(i);
Collection<String> neighbors = graph.getNeighbors(node);
List<Integer> adj = new ArrayList<Integer>();
if (neighbors != null) {
for (String nb : neighbors) {
Integer idx = vertexIndex.get(nb);
if (idx != null) adj.add(idx);
}
}
adjLists[i] = new int[adj.size()];
for (int j = 0; j < adj.size(); j++) {
adjLists[i][j] = adj.get(j);
}
}
double[] bc = new double[n];
// Reusable arrays (allocated once, cleared per source)
double[] sigma = new double[n];
int[] dist = new int[n];
double[] delta = new double[n];
@SuppressWarnings("unchecked")
List<Integer>[] pred = new List[n];
for (int i = 0; i < n; i++) {
pred[i] = new ArrayList<Integer>();
}
int[] stack = new int[n];
int stackTop;
int[] queue = new int[n];
for (int s = 0; s < n; s++) {
// Reset arrays
Arrays.fill(sigma, 0.0);
Arrays.fill(dist, -1);
Arrays.fill(delta, 0.0);
for (int i = 0; i < n; i++) pred[i].clear();
sigma[s] = 1.0;
dist[s] = 0;
stackTop = 0;
int qHead = 0, qTail = 0;
queue[qTail++] = s;
while (qHead < qTail) {
int v = queue[qHead++];
stack[stackTop++] = v;
for (int w : adjLists[v]) {
if (dist[w] < 0) {
queue[qTail++] = w;
dist[w] = dist[v] + 1;
}
if (dist[w] == dist[v] + 1) {
sigma[w] += sigma[v];
pred[w].add(v);
}
}
}
// Back-propagation
while (stackTop > 0) {
int w = stack[--stackTop];
for (int v : pred[w]) {
delta[v] += (sigma[v] / sigma[w]) * (1.0 + delta[w]);
}
if (w != s) {
bc[w] += delta[w];
}
}
}
// Halve for undirected and build result map
Map<String, Double> result = new LinkedHashMap<String, Double>();
for (int i = 0; i < n; i++) {
result.put(vertexList.get(i), bc[i] / 2.0);
}
return result;
}
// ── Global Efficiency (array-based BFS) ───────────────────────────
/**
* Computes the global efficiency of a graph:
* E = (2 / (n*(n-1))) * Σ_{i<j} 1/d(i,j)
*
* <p>Uses array-based BFS instead of HashMap-based to minimize
* allocation overhead, especially when called repeatedly during
* resilience simulations.</p>
*
* @param graph the graph
* @return global efficiency in [0, 1]
*/
public static double globalEfficiency(Graph<String, edge> graph) {
int n = graph.getVertexCount();
if (n <= 1) return 0.0;
// Build vertex index and adjacency lists
List<String> vertexList = new ArrayList<String>(graph.getVertices());
Map<String, Integer> vertexIndex = new HashMap<String, Integer>(n * 2);
for (int i = 0; i < n; i++) {
vertexIndex.put(vertexList.get(i), i);
}
int[][] adjLists = new int[n][];
for (int i = 0; i < n; i++) {
String node = vertexList.get(i);
Collection<String> neighbors = graph.getNeighbors(node);
List<Integer> adj = new ArrayList<Integer>();
if (neighbors != null) {
for (String nb : neighbors) {
Integer idx = vertexIndex.get(nb);
if (idx != null) adj.add(idx);
}
}
adjLists[i] = new int[adj.size()];
for (int j = 0; j < adj.size(); j++) {
adjLists[i][j] = adj.get(j);
}
}
// Reusable BFS arrays
int[] dist = new int[n];
int[] queue = new int[n];
double sum = 0.0;
for (int source = 0; source < n; source++) {
Arrays.fill(dist, -1);
dist[source] = 0;
int qHead = 0, qTail = 0;
queue[qTail++] = source;
while (qHead < qTail) {
int v = queue[qHead++];
for (int w : adjLists[v]) {
if (dist[w] < 0) {
dist[w] = dist[v] + 1;
queue[qTail++] = w;
}
}
}
// Only count j > source to avoid double-counting
for (int j = source + 1; j < n; j++) {
if (dist[j] > 0) {
sum += 1.0 / dist[j];
}
}
}
return (2.0 * sum) / ((long) n * (n - 1));
}
// ── Weighted shortest paths (Dijkstra) ────────────────────────────
/**
* Result of a single-source Dijkstra computation: distances and
* predecessor map for path reconstruction.
*/
public static class DijkstraResult {
/** Shortest distance from source to each reachable vertex. */
public final Map<String, Double> dist;
/** Predecessor on the shortest path (vertex → its predecessor). */
public final Map<String, String> prev;
public DijkstraResult(Map<String, Double> dist, Map<String, String> prev) {
this.dist = dist;
this.prev = prev;
}
}
/**
* Runs Dijkstra's algorithm from a single source vertex, returning
* shortest distances and predecessors for all reachable vertices.
*
* <p>Uses a visited set to avoid re-processing settled nodes, and
* immutable PQ entries to handle Java's non-updatable PriorityQueue.
* Edge weights are taken from {@link edge#getWeight()}; non-positive
* weights default to 1.0.</p>
*
* @param graph the JUNG graph
* @param source the source vertex
* @return distances and predecessors for all reachable vertices
*/
public static DijkstraResult dijkstra(Graph<String, edge> graph, String source) {
Map<String, Double> dist = new HashMap<String, Double>();
Map<String, String> prev = new HashMap<String, String>();
Set<String> visited = new HashSet<String>();
// PQ entries: [distance, vertexIndex]
final List<String> vertexIndex = new ArrayList<String>();
vertexIndex.add(source);
final Map<String, Integer> vertexToIdx = new HashMap<String, Integer>();
vertexToIdx.put(source, 0);
PriorityQueue<double[]> pq = new PriorityQueue<double[]>(11,
(double[] a, double[] b) -> {
return Double.compare(a[0], b[0]);
});
dist.put(source, 0.0);
pq.add(new double[]{0.0, 0});
while (!pq.isEmpty()) {
double[] entry = pq.poll();
double entryDist = entry[0];
String u = vertexIndex.get((int) entry[1]);
if (visited.contains(u)) continue;
visited.add(u);
// Skip stale PQ entries
Double uDist = dist.get(u);
if (uDist == null || entryDist > uDist) continue;
for (edge e : graph.getIncidentEdges(u)) {
String v = getOtherEnd(e, u);
if (v == null || visited.contains(v)) continue;
double w = e.getWeight() > 0 ? e.getWeight() : 1.0;
double newDist = entryDist + w;
Double oldDist = dist.get(v);
if (oldDist == null || newDist < oldDist) {
dist.put(v, newDist);
prev.put(v, u);
Integer idx = vertexToIdx.get(v);
if (idx == null) {
idx = vertexIndex.size();
vertexIndex.add(v);
vertexToIdx.put(v, idx);
}
pq.add(new double[]{newDist, idx});
}
}
}
return new DijkstraResult(dist, prev);
}
/**
* Reconstructs the shortest path from source to target using the
* predecessor map from a Dijkstra result.
*
* @param dr Dijkstra result containing predecessor map
* @param source the source vertex
* @param target the target vertex
* @return ordered list of vertices from source to target, or null if
* target is unreachable
*/
public static List<String> reconstructPath(
DijkstraResult dr, String source, String target) {
if (!dr.dist.containsKey(target)) return null;
List<String> path = new ArrayList<String>();
String cur = target;
while (cur != null && !cur.equals(source)) {
path.add(cur);
cur = dr.prev.get(cur);
}
if (cur == null) return null;
path.add(source);
Collections.reverse(path);
return path;
}
// ── Null-safe neighbor access ───────────────────────────────
/**
* Returns the neighbors of a vertex, never {@code null}.
* Wraps {@code graph.getNeighbors(v)} with a null-safe fallback.
*
* @param graph the JUNG graph
* @param v the vertex
* @return neighbors of v, or an empty collection if null
*/
public static Collection<String> neighborsOf(
Graph<String, edge> graph, String v) {
Collection<String> nbrs = graph.getNeighbors(v);
return nbrs != null ? nbrs : Collections.<String>emptyList();
}
// ── Directed adjacency ──────────────────────────────────────
/**
* Directed adjacency structure: vertices with successor and predecessor
* maps. Extracted from {@link TopologicalSortAnalyzer} for reuse by
* any analyzer that needs directed-edge traversal.
*/
public static final class DirectedAdj {
/** All vertices in the graph. */
public final Set<String> vertices;
/** Vertex → set of outgoing neighbors (vertex1 → vertex2). */
public final Map<String, Set<String>> successors;
/** Vertex → set of incoming neighbors. */
public final Map<String, Set<String>> predecessors;
public DirectedAdj(Set<String> vertices,
Map<String, Set<String>> successors,
Map<String, Set<String>> predecessors) {
this.vertices = vertices;
this.successors = successors;
this.predecessors = predecessors;
}
}
/**
* Builds directed adjacency maps from a graph. Each edge is interpreted
* as vertex1 → vertex2.
*
* @param graph the JUNG graph
* @return a {@link DirectedAdj} with successor and predecessor maps
*/
public static DirectedAdj buildDirectedAdjacencyMap(
Graph<String, edge> graph) {
Map<String, Set<String>> successors = new HashMap<String, Set<String>>();
Map<String, Set<String>> predecessors = new HashMap<String, Set<String>>();
Set<String> allVertices = new HashSet<String>();
for (String v : graph.getVertices()) {
allVertices.add(v);
successors.put(v, new HashSet<String>());
predecessors.put(v, new HashSet<String>());
}
for (edge e : graph.getEdges()) {
String from = e.getVertex1();
String to = e.getVertex2();
if (from != null && to != null
&& allVertices.contains(from) && allVertices.contains(to)) {
successors.get(from).add(to);
predecessors.get(to).add(from);
}
}
return new DirectedAdj(allVertices, successors, predecessors);
}
}