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 pathGraphCompressor.java
More file actions
559 lines (482 loc) · 22.2 KB
/
Copy pathGraphCompressor.java
File metadata and controls
559 lines (482 loc) · 22.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
package gvisual;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.util.*;
/**
* Compresses a graph by merging groups of nodes into supernodes,
* producing a smaller quotient graph that preserves the macro-structure
* of the original network.
*
* <h3>Compression Strategies</h3>
* <ul>
* <li><b>Structural Equivalence</b> — merges nodes with identical
* neighbor sets. Two nodes are structurally equivalent if they
* connect to exactly the same set of other nodes.</li>
* <li><b>Neighborhood Similarity</b> — merges nodes whose neighbor
* sets have Jaccard similarity above a threshold. A relaxed
* version of structural equivalence.</li>
* <li><b>Degree-Based</b> — groups nodes by degree (or degree ranges),
* collapsing each group into a supernode.</li>
* <li><b>Attribute-Based</b> — groups nodes by a user-supplied
* attribute function (e.g., community label, type).</li>
* <li><b>K-Hop Locality</b> — merges each seed node with its
* k-hop neighborhood into a supernode.</li>
* </ul>
*
* <h3>Usage</h3>
* <pre>
* GraphCompressor compressor = new GraphCompressor(graph);
*
* // Structural equivalence compression
* CompressionResult result = compressor.byStructuralEquivalence();
* System.out.println(result.getSummary());
*
* // Neighborhood similarity with 0.5 threshold
* CompressionResult result2 = compressor.byNeighborhoodSimilarity(0.5);
*
* // Degree-based with bin size 5
* CompressionResult result3 = compressor.byDegree(5);
*
* // Attribute-based
* Map<String, String> communities = ...;
* CompressionResult result4 = compressor.byAttribute(communities::get);
* </pre>
*
* @author zalenix
*/
public class GraphCompressor {
private final Graph<String, edge> graph;
/**
* Creates a compressor for the given graph.
*
* @param graph the graph to compress (must not be null)
* @throws IllegalArgumentException if graph is null
*/
public GraphCompressor(Graph<String, edge> graph) {
if (graph == null) {
throw new IllegalArgumentException("Graph must not be null");
}
this.graph = graph;
}
// ── Compression Strategies ──────────────────────────────────────
/**
* Compresses by structural equivalence: nodes with identical
* neighbor sets are merged into a single supernode.
*
* @return compression result with quotient graph and mappings
*/
public CompressionResult byStructuralEquivalence() {
Map<String, Set<String>> neighborSets = new HashMap<>();
for (String v : graph.getVertices()) {
neighborSets.put(v, new TreeSet<>(graph.getNeighbors(v)));
}
// Group nodes by their sorted neighbor set
Map<String, List<String>> groups = new LinkedHashMap<>();
for (String v : graph.getVertices()) {
String key = neighborSets.get(v).toString();
groups.computeIfAbsent(key, k -> new ArrayList<>()).add(v);
}
return buildQuotientGraph(new ArrayList<>(groups.values()), "structural_equivalence");
}
/**
* Compresses by neighborhood similarity: nodes whose neighbor sets
* have Jaccard similarity ≥ threshold are merged greedily.
*
* @param threshold Jaccard similarity threshold in [0.0, 1.0]
* @return compression result
* @throws IllegalArgumentException if threshold is out of range
*/
public CompressionResult byNeighborhoodSimilarity(double threshold) {
if (threshold < 0.0 || threshold > 1.0) {
throw new IllegalArgumentException("Threshold must be in [0.0, 1.0], got: " + threshold);
}
Map<String, Set<String>> neighborSets = new HashMap<>();
for (String v : graph.getVertices()) {
neighborSets.put(v, new HashSet<>(graph.getNeighbors(v)));
}
// Sort vertices by degree to improve pruning effectiveness.
// Vertices with similar degrees are more likely to have high Jaccard
// similarity, so sorting brings candidate pairs closer together.
List<String> vertices = new ArrayList<>(graph.getVertices());
vertices.sort((a, b) -> Integer.compare(neighborSets.get(a).size(), neighborSets.get(b).size()));
boolean[] merged = new boolean[vertices.size()];
List<List<String>> groups = new ArrayList<>();
for (int i = 0; i < vertices.size(); i++) {
if (merged[i]) continue;
List<String> group = new ArrayList<>();
group.add(vertices.get(i));
merged[i] = true;
Set<String> refNeighbors = neighborSets.get(vertices.get(i));
int refSize = refNeighbors.size();
for (int j = i + 1; j < vertices.size(); j++) {
if (merged[j]) continue;
Set<String> otherNeighbors = neighborSets.get(vertices.get(j));
int otherSize = otherNeighbors.size();
// Degree-based upper bound pruning: the maximum possible
// Jaccard similarity between two sets is min(|A|,|B|)/max(|A|,|B|).
// Since vertices are sorted by degree, refSize <= otherSize.
// If this upper bound < threshold, no later vertex can match either
// (their degrees only increase), so break early.
if (otherSize > 0 && (double) refSize / otherSize < threshold) {
break;
}
// Compute Jaccard without allocating new sets: count intersection
// by iterating the smaller set and checking the larger.
double jaccard = jaccardFast(refNeighbors, refSize, otherNeighbors, otherSize);
if (jaccard >= threshold) {
group.add(vertices.get(j));
merged[j] = true;
}
}
groups.add(group);
}
return buildQuotientGraph(groups, "neighborhood_similarity(threshold=" + threshold + ")");
}
/**
* Computes Jaccard similarity without allocating intermediate HashSets.
* Iterates the smaller set, counting members present in the larger set.
* Union size is derived as |A| + |B| - |intersection|.
*
* @return Jaccard similarity in [0.0, 1.0]
*/
private static double jaccardFast(Set<String> a, int aSize, Set<String> b, int bSize) {
if (aSize == 0 && bSize == 0) return 1.0;
if (aSize == 0 || bSize == 0) return 0.0;
// Iterate the smaller set for fewer hash lookups
Set<String> smaller = aSize <= bSize ? a : b;
Set<String> larger = aSize <= bSize ? b : a;
int intersection = 0;
for (String v : smaller) {
if (larger.contains(v)) {
intersection++;
}
}
int union = aSize + bSize - intersection;
return union == 0 ? 1.0 : (double) intersection / union;
}
/**
* Compresses by degree: nodes are grouped into bins by their degree.
*
* @param binSize size of each degree bin (nodes with degree in
* [k*binSize, (k+1)*binSize) are grouped together)
* @return compression result
* @throws IllegalArgumentException if binSize < 1
*/
public CompressionResult byDegree(int binSize) {
if (binSize < 1) {
throw new IllegalArgumentException("Bin size must be >= 1, got: " + binSize);
}
Map<Integer, List<String>> bins = new TreeMap<>();
for (String v : graph.getVertices()) {
int degree = graph.degree(v);
int bin = degree / binSize;
bins.computeIfAbsent(bin, k -> new ArrayList<>()).add(v);
}
return buildQuotientGraph(new ArrayList<>(bins.values()), "degree(binSize=" + binSize + ")");
}
/**
* Compresses by exact degree: nodes with the same degree are merged.
*
* @return compression result
*/
public CompressionResult byExactDegree() {
return byDegree(1);
}
/**
* Compresses by a user-supplied attribute function: nodes that map
* to the same attribute value are merged into a supernode.
*
* @param attributeFunction maps each node ID to its group label;
* nodes returning null are placed in an
* "unassigned" group
* @return compression result
* @throws IllegalArgumentException if attributeFunction is null
*/
public CompressionResult byAttribute(java.util.function.Function<String, String> attributeFunction) {
if (attributeFunction == null) {
throw new IllegalArgumentException("Attribute function must not be null");
}
Map<String, List<String>> groups = new LinkedHashMap<>();
for (String v : graph.getVertices()) {
String attr = attributeFunction.apply(v);
if (attr == null) attr = "__unassigned__";
groups.computeIfAbsent(attr, k -> new ArrayList<>()).add(v);
}
return buildQuotientGraph(new ArrayList<>(groups.values()), "attribute");
}
/**
* Compresses by k-hop locality: starting from seed nodes, each seed
* absorbs all nodes within k hops into its supernode. Unclaimed
* nodes form singleton supernodes.
*
* @param seeds seed node IDs
* @param k number of hops (must be >= 1)
* @return compression result
* @throws IllegalArgumentException if seeds is null/empty or k < 1
*/
public CompressionResult byKHopLocality(Collection<String> seeds, int k) {
if (seeds == null || seeds.isEmpty()) {
throw new IllegalArgumentException("Seeds must not be null or empty");
}
if (k < 1) {
throw new IllegalArgumentException("k must be >= 1, got: " + k);
}
Set<String> claimed = new HashSet<>();
List<List<String>> groups = new ArrayList<>();
for (String seed : seeds) {
if (!graph.containsVertex(seed) || claimed.contains(seed)) continue;
Set<String> neighborhood = new HashSet<>();
Set<String> frontier = new HashSet<>();
frontier.add(seed);
for (int hop = 0; hop <= k; hop++) {
Set<String> nextFrontier = new HashSet<>();
for (String v : frontier) {
if (claimed.contains(v)) continue;
neighborhood.add(v);
if (hop < k) {
for (String n : graph.getNeighbors(v)) {
if (!neighborhood.contains(n) && !claimed.contains(n)) {
nextFrontier.add(n);
}
}
}
}
frontier = nextFrontier;
}
if (!neighborhood.isEmpty()) {
groups.add(new ArrayList<>(neighborhood));
claimed.addAll(neighborhood);
}
}
// Add unclaimed nodes as singletons
for (String v : graph.getVertices()) {
if (!claimed.contains(v)) {
groups.add(Collections.singletonList(v));
}
}
return buildQuotientGraph(groups, "k_hop_locality(k=" + k + ",seeds=" + seeds.size() + ")");
}
/**
* Analyzes the compressibility of the graph across all strategies
* and returns a summary report.
*
* @return multi-line compressibility report
*/
public String compressibilityReport() {
StringBuilder sb = new StringBuilder();
sb.append("=== Graph Compressibility Report ===\n");
sb.append(String.format("Original: %d nodes, %d edges\n\n",
graph.getVertexCount(), graph.getEdgeCount()));
CompressionResult structural = byStructuralEquivalence();
sb.append(formatReportLine("Structural Equivalence", structural));
double[] thresholds = {0.9, 0.7, 0.5, 0.3};
for (double t : thresholds) {
CompressionResult sim = byNeighborhoodSimilarity(t);
sb.append(formatReportLine("Neighborhood Sim (t=" + t + ")", sim));
}
CompressionResult exactDeg = byExactDegree();
sb.append(formatReportLine("Exact Degree", exactDeg));
int[] binSizes = {2, 5, 10};
for (int bs : binSizes) {
CompressionResult deg = byDegree(bs);
sb.append(formatReportLine("Degree (bin=" + bs + ")", deg));
}
return sb.toString();
}
// ── Quotient Graph Builder ──────────────────────────────────────
private CompressionResult buildQuotientGraph(List<List<String>> groups, String strategy) {
Graph<String, edge> quotient = new UndirectedSparseGraph<>();
Map<String, List<String>> supernodeMembers = new LinkedHashMap<>();
Map<String, String> nodeToSupernode = new HashMap<>();
// Create supernodes
for (int i = 0; i < groups.size(); i++) {
List<String> group = groups.get(i);
String supernodeName;
if (group.size() == 1) {
supernodeName = group.get(0);
} else {
supernodeName = "S" + i + "{" + group.size() + "}";
}
quotient.addVertex(supernodeName);
supernodeMembers.put(supernodeName, new ArrayList<>(group));
for (String member : group) {
nodeToSupernode.put(member, supernodeName);
}
}
// Create superedges (aggregate edges between groups)
Set<String> addedEdges = new HashSet<>();
int edgeCounter = 0;
Map<String, SuperEdgeInfo> superEdgeInfos = new HashMap<>();
for (edge e : graph.getEdges()) {
String v1 = graph.getEndpoints(e).getFirst();
String v2 = graph.getEndpoints(e).getSecond();
String s1 = nodeToSupernode.get(v1);
String s2 = nodeToSupernode.get(v2);
if (s1 == null || s2 == null || s1.equals(s2)) continue;
String edgeKey = s1.compareTo(s2) < 0 ? s1 + "||" + s2 : s2 + "||" + s1;
SuperEdgeInfo info = superEdgeInfos.get(edgeKey);
if (info == null) {
info = new SuperEdgeInfo();
superEdgeInfos.put(edgeKey, info);
edge superEdge = new edge("super", s1, s2);
superEdge.setLabel("compressed");
quotient.addEdge(superEdge, s1, s2);
info.edge = superEdge;
}
info.count++;
info.totalWeight += e.getWeight();
}
// Set aggregated weights
for (SuperEdgeInfo info : superEdgeInfos.values()) {
info.edge.setWeight(info.totalWeight);
}
return new CompressionResult(
graph, quotient, supernodeMembers, nodeToSupernode, strategy);
}
// ── Helpers ─────────────────────────────────────────────────────
private static double jaccardSimilarity(Set<String> a, Set<String> b) {
if (a.isEmpty() && b.isEmpty()) return 1.0;
Set<String> union = new HashSet<>(a);
union.addAll(b);
if (union.isEmpty()) return 1.0;
Set<String> intersection = new HashSet<>(a);
intersection.retainAll(b);
return (double) intersection.size() / union.size();
}
private String formatReportLine(String label, CompressionResult result) {
return String.format(" %-30s → %d supernodes, %d edges (%.1f%% node reduction, %.1f%% edge reduction)\n",
label,
result.getCompressedNodeCount(),
result.getCompressedEdgeCount(),
result.getNodeReductionPercent(),
result.getEdgeReductionPercent());
}
private static class SuperEdgeInfo {
edge edge;
int count;
float totalWeight;
}
// ── Result Class ────────────────────────────────────────────────
/**
* Holds the result of a graph compression operation: the quotient
* graph, supernode membership mappings, and compression statistics.
*/
public static class CompressionResult {
private final Graph<String, edge> original;
private final Graph<String, edge> compressed;
private final Map<String, List<String>> supernodeMembers;
private final Map<String, String> nodeToSupernode;
private final String strategy;
CompressionResult(Graph<String, edge> original,
Graph<String, edge> compressed,
Map<String, List<String>> supernodeMembers,
Map<String, String> nodeToSupernode,
String strategy) {
this.original = original;
this.compressed = compressed;
this.supernodeMembers = Collections.unmodifiableMap(supernodeMembers);
this.nodeToSupernode = Collections.unmodifiableMap(nodeToSupernode);
this.strategy = strategy;
}
/** Returns the compressed quotient graph. */
public Graph<String, edge> getCompressedGraph() { return compressed; }
/** Returns a map from supernode ID to its member node IDs. */
public Map<String, List<String>> getSupernodeMembers() { return supernodeMembers; }
/** Returns a map from original node ID to its supernode ID. */
public Map<String, String> getNodeToSupernode() { return nodeToSupernode; }
/** Returns the compression strategy name. */
public String getStrategy() { return strategy; }
/** Original node count. */
public int getOriginalNodeCount() { return original.getVertexCount(); }
/** Original edge count. */
public int getOriginalEdgeCount() { return original.getEdgeCount(); }
/** Compressed node count. */
public int getCompressedNodeCount() { return compressed.getVertexCount(); }
/** Compressed edge count. */
public int getCompressedEdgeCount() { return compressed.getEdgeCount(); }
/** Compression ratio (compressed/original nodes). */
public double getCompressionRatio() {
if (original.getVertexCount() == 0) return 1.0;
return (double) compressed.getVertexCount() / original.getVertexCount();
}
/** Node reduction percentage. */
public double getNodeReductionPercent() {
return (1.0 - getCompressionRatio()) * 100.0;
}
/** Edge reduction percentage. */
public double getEdgeReductionPercent() {
if (original.getEdgeCount() == 0) return 0.0;
return (1.0 - (double) compressed.getEdgeCount() / original.getEdgeCount()) * 100.0;
}
/** Number of supernodes that contain more than one original node. */
public int getMergedGroupCount() {
return (int) supernodeMembers.values().stream()
.filter(members -> members.size() > 1)
.count();
}
/** Size of the largest supernode (number of merged nodes). */
public int getLargestSupernodeSize() {
return supernodeMembers.values().stream()
.mapToInt(List::size)
.max()
.orElse(0);
}
/** Average supernode size. */
public double getAverageSupernodeSize() {
if (supernodeMembers.isEmpty()) return 0.0;
return (double) nodeToSupernode.size() / supernodeMembers.size();
}
/**
* Returns the members of a specific supernode.
*
* @param supernodeId the supernode ID
* @return list of member node IDs, or empty list if not found
*/
public List<String> getMembersOf(String supernodeId) {
return supernodeMembers.getOrDefault(supernodeId, Collections.emptyList());
}
/**
* Returns which supernode a given original node belongs to.
*
* @param nodeId the original node ID
* @return supernode ID, or null if node not found
*/
public String getSupernodeOf(String nodeId) {
return nodeToSupernode.get(nodeId);
}
/** Returns a human-readable summary of the compression. */
public String getSummary() {
StringBuilder sb = new StringBuilder();
sb.append("=== Graph Compression Result ===\n");
sb.append(String.format("Strategy: %s\n", strategy));
sb.append(String.format("Original: %d nodes, %d edges\n",
getOriginalNodeCount(), getOriginalEdgeCount()));
sb.append(String.format("Compressed: %d nodes, %d edges\n",
getCompressedNodeCount(), getCompressedEdgeCount()));
sb.append(String.format("Node reduction: %.1f%%\n", getNodeReductionPercent()));
sb.append(String.format("Edge reduction: %.1f%%\n", getEdgeReductionPercent()));
sb.append(String.format("Compression ratio: %.3f\n", getCompressionRatio()));
sb.append(String.format("Merged groups: %d\n", getMergedGroupCount()));
sb.append(String.format("Largest supernode: %d members\n", getLargestSupernodeSize()));
sb.append(String.format("Avg supernode size: %.1f\n", getAverageSupernodeSize()));
return sb.toString();
}
/**
* Exports the compression mapping as CSV text.
*
* @return CSV with columns: original_node,supernode,group_size
*/
public String toCSV() {
StringBuilder sb = new StringBuilder();
sb.append("original_node,supernode,group_size\n");
List<String> sortedNodes = new ArrayList<>(nodeToSupernode.keySet());
Collections.sort(sortedNodes);
for (String node : sortedNodes) {
String sn = nodeToSupernode.get(node);
int size = supernodeMembers.getOrDefault(sn, Collections.emptyList()).size();
sb.append(String.format("%s,%s,%d\n", node, sn, size));
}
return sb.toString();
}
}
}