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 pathBipartiteAnalyzer.java
More file actions
716 lines (635 loc) · 25.9 KB
/
Copy pathBipartiteAnalyzer.java
File metadata and controls
716 lines (635 loc) · 25.9 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
package gvisual;
import edu.uci.ics.jung.graph.Graph;
import java.util.*;
/**
* Bipartite graph analysis — detects bipartiteness via 2-coloring (BFS),
* computes maximum matching (Hopcroft–Karp algorithm), minimum vertex cover
* (König's theorem), and maximum independent set.
*
* <h3>Capabilities</h3>
* <ul>
* <li><b>Bipartiteness test</b> — BFS 2-coloring across all components</li>
* <li><b>Two-coloring</b> — assigns vertices to left/right partitions</li>
* <li><b>Maximum matching</b> — Hopcroft–Karp O(E√V) algorithm</li>
* <li><b>Minimum vertex cover</b> — via König's theorem (complement of
* max independent set in bipartite graphs)</li>
* <li><b>Maximum independent set</b> — vertices not in min vertex cover</li>
* <li><b>Odd cycle detection</b> — finds an odd cycle witness when the
* graph is not bipartite</li>
* </ul>
*
* <p>Applications include task assignment, scheduling, network routing,
* and social network analysis (e.g., two-mode networks).</p>
*
* @author zalenix
*/
public class BipartiteAnalyzer {
private static final int UNCOLORED = -1;
private static final int LEFT = 0;
private static final int RIGHT = 1;
private static final String NIL = "__NIL__";
private static final int INF = Integer.MAX_VALUE;
private final Graph<String, Edge> graph;
private Map<String, Integer> coloring;
private boolean bipartite;
private boolean computed;
private List<String> oddCycle;
/**
* Creates a new BipartiteAnalyzer for the given graph.
*
* @param graph the JUNG graph to analyze
* @throws IllegalArgumentException if graph is null
*/
public BipartiteAnalyzer(Graph<String, Edge> graph) {
if (graph == null) {
throw new IllegalArgumentException("Graph must not be null");
}
this.graph = graph;
this.coloring = new LinkedHashMap<String, Integer>();
this.bipartite = false;
this.computed = false;
this.oddCycle = null;
}
// ── Bipartiteness test (BFS 2-coloring) ────────────────────────
/**
* Runs the bipartiteness test. Idempotent — repeated calls are no-ops.
*
* @return this analyzer for chaining
*/
public BipartiteAnalyzer compute() {
if (computed) return this;
Collection<String> vertices = graph.getVertices();
if (vertices.isEmpty()) {
bipartite = true;
computed = true;
return this;
}
Map<String, Integer> color = new LinkedHashMap<String, Integer>();
Map<String, String> parent = new HashMap<String, String>();
for (String v : vertices) {
color.put(v, UNCOLORED);
}
bipartite = true;
for (String start : vertices) {
if (color.get(start) != UNCOLORED) continue;
color.put(start, LEFT);
parent.put(start, null);
Queue<String> queue = new LinkedList<String>();
queue.add(start);
while (!queue.isEmpty() && bipartite) {
String v = queue.poll();
int vColor = color.get(v);
for (String u : graph.getNeighbors(v)) {
if (color.get(u) == UNCOLORED) {
color.put(u, 1 - vColor);
parent.put(u, v);
queue.add(u);
} else if (color.get(u) == vColor) {
bipartite = false;
// Build odd cycle witness
oddCycle = buildOddCycle(v, u, parent);
}
}
}
if (!bipartite) break;
}
this.coloring = color;
this.computed = true;
return this;
}
private List<String> buildOddCycle(String v, String u, Map<String, String> parent) {
List<String> pathV = new ArrayList<String>();
List<String> pathU = new ArrayList<String>();
String a = v;
while (a != null) {
pathV.add(a);
a = parent.get(a);
}
String b = u;
while (b != null) {
pathU.add(b);
b = parent.get(b);
}
// Find lowest common ancestor
Set<String> ancestorsV = new HashSet<String>(pathV);
String lca = null;
for (String x : pathU) {
if (ancestorsV.contains(x)) {
lca = x;
break;
}
}
List<String> cycle = new ArrayList<String>();
// Path from v to LCA
for (String x : pathV) {
cycle.add(x);
if (x.equals(lca)) break;
}
// Path from u to LCA (reversed, excluding LCA)
List<String> uToLca = new ArrayList<String>();
for (String x : pathU) {
if (x.equals(lca)) break;
uToLca.add(x);
}
Collections.reverse(uToLca);
cycle.addAll(uToLca);
return cycle;
}
// ── Accessors ──────────────────────────────────────────────────
/**
* Returns whether the graph is bipartite.
*
* @return true if bipartite
*/
public boolean isBipartite() {
ensureComputed();
return bipartite;
}
/**
* Returns the 2-coloring: vertex → 0 (left) or 1 (right).
* Only meaningful if the graph is bipartite.
*
* @return unmodifiable coloring map
*/
public Map<String, Integer> getColoring() {
ensureComputed();
return Collections.unmodifiableMap(coloring);
}
/**
* Returns the left partition (color 0).
*
* @return sorted list of left-partition vertices
*/
public List<String> getLeftPartition() {
ensureComputed();
return getPartition(LEFT);
}
/**
* Returns the right partition (color 1).
*
* @return sorted list of right-partition vertices
*/
public List<String> getRightPartition() {
ensureComputed();
return getPartition(RIGHT);
}
private List<String> getPartition(int side) {
List<String> result = new ArrayList<String>();
for (Map.Entry<String, Integer> e : coloring.entrySet()) {
if (e.getValue() == side) {
result.add(e.getKey());
}
}
Collections.sort(result);
return result;
}
/**
* Returns an odd cycle witness if the graph is NOT bipartite.
*
* @return list of vertices forming an odd cycle, or null if bipartite
*/
public List<String> getOddCycle() {
ensureComputed();
return oddCycle != null ? Collections.unmodifiableList(oddCycle) : null;
}
// ── Maximum Matching (Hopcroft–Karp) ───────────────────────────
/**
* Represents a matching edge between two vertices.
*/
public static class MatchingEdge {
private final String left;
private final String right;
public MatchingEdge(String left, String right) {
this.left = left;
this.right = right;
}
public String getLeft() { return left; }
public String getRight() { return right; }
@Override
public String toString() {
return left + " — " + right;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MatchingEdge)) return false;
MatchingEdge that = (MatchingEdge) o;
return left.equals(that.left) && right.equals(that.right);
}
@Override
public int hashCode() {
return 31 * left.hashCode() + right.hashCode();
}
}
/**
* Computes the maximum matching using Hopcroft–Karp algorithm.
* Only valid for bipartite graphs.
*
* @return list of matching edges
* @throws IllegalStateException if the graph is not bipartite
*/
public List<MatchingEdge> getMaximumMatching() {
ensureComputed();
if (!bipartite) {
throw new IllegalStateException(
"Maximum matching (Hopcroft-Karp) requires a bipartite graph");
}
List<String> leftVerts = getLeftPartition();
List<String> rightVerts = getRightPartition();
// Build adjacency for left vertices to right vertices
Map<String, List<String>> adj = new HashMap<String, List<String>>();
for (String l : leftVerts) {
List<String> neighbors = new ArrayList<String>();
for (String n : graph.getNeighbors(l)) {
if (coloring.get(n) == RIGHT) {
neighbors.add(n);
}
}
adj.put(l, neighbors);
}
Map<String, String> matchL = new HashMap<String, String>();
Map<String, String> matchR = new HashMap<String, String>();
Map<String, Integer> dist = new HashMap<String, Integer>();
for (String l : leftVerts) matchL.put(l, NIL);
for (String r : rightVerts) matchR.put(r, NIL);
// Hopcroft-Karp main loop
while (bfs(leftVerts, adj, matchL, matchR, dist)) {
for (String l : leftVerts) {
if (matchL.get(l).equals(NIL)) {
dfs(l, adj, matchL, matchR, dist);
}
}
}
// Collect matching edges
List<MatchingEdge> matching = new ArrayList<MatchingEdge>();
for (String l : leftVerts) {
String r = matchL.get(l);
if (!r.equals(NIL)) {
matching.add(new MatchingEdge(l, r));
}
}
return matching;
}
private boolean bfs(List<String> leftVerts, Map<String, List<String>> adj,
Map<String, String> matchL, Map<String, String> matchR,
Map<String, Integer> dist) {
Queue<String> queue = new LinkedList<String>();
for (String l : leftVerts) {
if (matchL.get(l).equals(NIL)) {
dist.put(l, 0);
queue.add(l);
} else {
dist.put(l, INF);
}
}
dist.put(NIL, INF);
while (!queue.isEmpty()) {
String l = queue.poll();
if (dist.get(l) < dist.get(NIL)) {
List<String> neighbors = adj.get(l);
if (neighbors != null) {
for (String r : neighbors) {
String pairR = matchR.get(r);
if (pairR == null) pairR = NIL;
Integer pairDist = dist.get(pairR);
if (pairDist == null || pairDist == INF) {
dist.put(pairR, dist.get(l) + 1);
if (!pairR.equals(NIL)) {
queue.add(pairR);
}
}
}
}
}
}
return dist.get(NIL) != INF;
}
private boolean dfs(String l, Map<String, List<String>> adj,
Map<String, String> matchL, Map<String, String> matchR,
Map<String, Integer> dist) {
if (!l.equals(NIL)) {
List<String> neighbors = adj.get(l);
if (neighbors != null) {
for (String r : neighbors) {
String pairR = matchR.get(r);
if (pairR == null) pairR = NIL;
Integer pairDist = dist.get(pairR);
if (pairDist != null && pairDist == dist.get(l) + 1) {
if (dfs(pairR, adj, matchL, matchR, dist)) {
matchR.put(r, l);
matchL.put(l, r);
return true;
}
}
}
}
dist.put(l, INF);
return false;
}
return true;
}
/**
* Returns the size of the maximum matching.
*
* @return number of matched edges
* @throws IllegalStateException if the graph is not bipartite
*/
public int getMatchingSize() {
return getMaximumMatching().size();
}
// ── Minimum Vertex Cover (König's theorem) ─────────────────────
/**
* Computes the minimum vertex cover using König's theorem:
* In a bipartite graph, |min vertex cover| = |max matching|.
*
* <p>Uses alternating path BFS from unmatched left vertices to
* identify the cover set.</p>
*
* @return sorted list of vertices in the minimum vertex cover
* @throws IllegalStateException if the graph is not bipartite
*/
public List<String> getMinimumVertexCover() {
ensureComputed();
if (!bipartite) {
throw new IllegalStateException(
"Minimum vertex cover (König) requires a bipartite graph");
}
List<MatchingEdge> matching = getMaximumMatching();
List<String> leftVerts = getLeftPartition();
List<String> rightVerts = getRightPartition();
// Build matched-partner maps
Map<String, String> matchL = new HashMap<String, String>();
Map<String, String> matchR = new HashMap<String, String>();
for (MatchingEdge me : matching) {
matchL.put(me.getLeft(), me.getRight());
matchR.put(me.getRight(), me.getLeft());
}
// Find unmatched left vertices
Set<String> unmatchedLeft = new LinkedHashSet<String>();
for (String l : leftVerts) {
if (!matchL.containsKey(l)) {
unmatchedLeft.add(l);
}
}
// BFS alternating paths from unmatched left vertices
// Alternate: unmatched edge to right, matched edge back to left
Set<String> visitedL = new LinkedHashSet<String>(unmatchedLeft);
Set<String> visitedR = new LinkedHashSet<String>();
Queue<String> queue = new LinkedList<String>(unmatchedLeft);
while (!queue.isEmpty()) {
String l = queue.poll();
// Follow unmatched edges to right side
for (String n : graph.getNeighbors(l)) {
if (coloring.get(n) == RIGHT && !visitedR.contains(n)) {
// Only follow if this edge is NOT in the matching
if (!n.equals(matchL.get(l))) {
visitedR.add(n);
// Follow matched edge back to left
String partner = matchR.get(n);
if (partner != null && !visitedL.contains(partner)) {
visitedL.add(partner);
queue.add(partner);
}
}
}
}
}
// König's theorem: cover = (L \ visitedL) ∪ (R ∩ visitedR)
Set<String> cover = new LinkedHashSet<String>();
for (String l : leftVerts) {
if (!visitedL.contains(l)) {
cover.add(l);
}
}
for (String r : rightVerts) {
if (visitedR.contains(r)) {
cover.add(r);
}
}
List<String> result = new ArrayList<String>(cover);
Collections.sort(result);
return result;
}
// ── Maximum Independent Set ────────────────────────────────────
/**
* Computes the maximum independent set as the complement of the
* minimum vertex cover.
*
* @return sorted list of vertices in the maximum independent set
* @throws IllegalStateException if the graph is not bipartite
*/
public List<String> getMaximumIndependentSet() {
ensureComputed();
if (!bipartite) {
throw new IllegalStateException(
"Maximum independent set requires a bipartite graph");
}
Set<String> cover = new HashSet<String>(getMinimumVertexCover());
List<String> independent = new ArrayList<String>();
for (String v : graph.getVertices()) {
if (!cover.contains(v)) {
independent.add(v);
}
}
Collections.sort(independent);
return independent;
}
// ── Analytics ──────────────────────────────────────────────────
/**
* Computes the balance ratio of the two partitions.
* A perfectly balanced bipartite graph has ratio 1.0.
*
* @return ratio of smaller partition to larger partition, or 0 for empty graphs
* @throws IllegalStateException if the graph is not bipartite
*/
public double getPartitionBalance() {
ensureComputed();
if (!bipartite) {
throw new IllegalStateException("Partition balance requires a bipartite graph");
}
int leftSize = getLeftPartition().size();
int rightSize = getRightPartition().size();
if (leftSize == 0 && rightSize == 0) return 0.0;
int maxSize = Math.max(leftSize, rightSize);
int minSize = Math.min(leftSize, rightSize);
return (double) minSize / maxSize;
}
/**
* Computes edge density of the bipartite graph.
* For bipartite graphs, max edges = |L| × |R|, so density = E / (|L| × |R|).
*
* @return density in [0, 1], or 0 for trivial cases
* @throws IllegalStateException if the graph is not bipartite
*/
public double getBipartiteDensity() {
ensureComputed();
if (!bipartite) {
throw new IllegalStateException("Bipartite density requires a bipartite graph");
}
int leftSize = getLeftPartition().size();
int rightSize = getRightPartition().size();
if (leftSize == 0 || rightSize == 0) return 0.0;
return (double) graph.getEdgeCount() / ((long) leftSize * rightSize);
}
/**
* Computes matching coverage: fraction of vertices that are matched.
*
* @return coverage in [0, 1]
* @throws IllegalStateException if the graph is not bipartite
*/
public double getMatchingCoverage() {
ensureComputed();
if (!bipartite) {
throw new IllegalStateException("Matching coverage requires a bipartite graph");
}
int n = graph.getVertexCount();
if (n == 0) return 0.0;
return (2.0 * getMatchingSize()) / n;
}
/**
* Checks if the graph has a perfect matching (every vertex is matched).
*
* @return true if a perfect matching exists
* @throws IllegalStateException if the graph is not bipartite
*/
public boolean hasPerfectMatching() {
ensureComputed();
if (!bipartite) {
throw new IllegalStateException("Perfect matching check requires a bipartite graph");
}
int leftSize = getLeftPartition().size();
int rightSize = getRightPartition().size();
if (leftSize != rightSize) return false;
return getMatchingSize() == leftSize;
}
// ── Result object ──────────────────────────────────────────────
/**
* Comprehensive result of the bipartite analysis.
*/
public static class BipartiteResult {
private final boolean bipartite;
private final int vertexCount;
private final int edgeCount;
private final int leftSize;
private final int rightSize;
private final double partitionBalance;
private final double bipartiteDensity;
private final int matchingSize;
private final double matchingCoverage;
private final boolean perfectMatching;
private final int vertexCoverSize;
private final int independentSetSize;
private final List<MatchingEdge> matching;
private final List<String> vertexCover;
private final List<String> independentSet;
private final List<String> oddCycle;
public BipartiteResult(boolean bipartite, int vertexCount, int edgeCount,
int leftSize, int rightSize, double partitionBalance,
double bipartiteDensity, int matchingSize,
double matchingCoverage, boolean perfectMatching,
int vertexCoverSize, int independentSetSize,
List<MatchingEdge> matching, List<String> vertexCover,
List<String> independentSet, List<String> oddCycle) {
this.bipartite = bipartite;
this.vertexCount = vertexCount;
this.edgeCount = edgeCount;
this.leftSize = leftSize;
this.rightSize = rightSize;
this.partitionBalance = partitionBalance;
this.bipartiteDensity = bipartiteDensity;
this.matchingSize = matchingSize;
this.matchingCoverage = matchingCoverage;
this.perfectMatching = perfectMatching;
this.vertexCoverSize = vertexCoverSize;
this.independentSetSize = independentSetSize;
this.matching = matching;
this.vertexCover = vertexCover;
this.independentSet = independentSet;
this.oddCycle = oddCycle;
}
public boolean isBipartite() { return bipartite; }
public int getVertexCount() { return vertexCount; }
public int getEdgeCount() { return edgeCount; }
public int getLeftSize() { return leftSize; }
public int getRightSize() { return rightSize; }
public double getPartitionBalance() { return partitionBalance; }
public double getBipartiteDensity() { return bipartiteDensity; }
public int getMatchingSize() { return matchingSize; }
public double getMatchingCoverage() { return matchingCoverage; }
public boolean hasPerfectMatching() { return perfectMatching; }
public int getVertexCoverSize() { return vertexCoverSize; }
public int getIndependentSetSize() { return independentSetSize; }
public List<MatchingEdge> getMatching() { return matching; }
public List<String> getVertexCover() { return vertexCover; }
public List<String> getIndependentSet() { return independentSet; }
public List<String> getOddCycle() { return oddCycle; }
}
/**
* Returns a comprehensive result object with all bipartite analysis data.
*
* @return BipartiteResult with all metrics
*/
public BipartiteResult getResult() {
ensureComputed();
if (!bipartite) {
return new BipartiteResult(false, graph.getVertexCount(),
graph.getEdgeCount(), 0, 0, 0, 0, 0, 0, false,
0, 0, null, null, null, oddCycle);
}
List<MatchingEdge> matching = getMaximumMatching();
List<String> cover = getMinimumVertexCover();
List<String> independent = getMaximumIndependentSet();
return new BipartiteResult(true, graph.getVertexCount(),
graph.getEdgeCount(), getLeftPartition().size(),
getRightPartition().size(), getPartitionBalance(),
getBipartiteDensity(), matching.size(),
getMatchingCoverage(), hasPerfectMatching(),
cover.size(), independent.size(),
matching, cover, independent, null);
}
// ── Summary ────────────────────────────────────────────────────
/**
* Returns a formatted multi-line summary of the bipartite analysis.
*
* @return human-readable summary string
*/
public String getSummary() {
ensureComputed();
StringBuilder sb = new StringBuilder();
sb.append("=== Bipartite Graph Analysis ===\n");
sb.append(String.format("Vertices: %d | Edges: %d\n",
graph.getVertexCount(), graph.getEdgeCount()));
sb.append(String.format("Bipartite: %s\n", bipartite ? "YES" : "NO"));
if (!bipartite) {
if (oddCycle != null) {
sb.append(String.format("Odd cycle witness: %s\n", oddCycle));
}
return sb.toString();
}
List<String> left = getLeftPartition();
List<String> right = getRightPartition();
sb.append(String.format("\n--- Partitions ---\n"));
sb.append(String.format(" Left (%d): %s\n", left.size(), left));
sb.append(String.format(" Right (%d): %s\n", right.size(), right));
sb.append(String.format(" Balance: %.4f\n", getPartitionBalance()));
sb.append(String.format(" Bipartite density: %.4f\n", getBipartiteDensity()));
List<MatchingEdge> matching = getMaximumMatching();
sb.append(String.format("\n--- Maximum Matching (%d edges) ---\n", matching.size()));
for (MatchingEdge me : matching) {
sb.append(String.format(" %s\n", me));
}
sb.append(String.format(" Coverage: %.1f%%\n", getMatchingCoverage() * 100));
sb.append(String.format(" Perfect matching: %s\n", hasPerfectMatching() ? "YES" : "NO"));
List<String> cover = getMinimumVertexCover();
sb.append(String.format("\n--- Minimum Vertex Cover (%d vertices) ---\n", cover.size()));
sb.append(String.format(" %s\n", cover));
List<String> independent = getMaximumIndependentSet();
sb.append(String.format("\n--- Maximum Independent Set (%d vertices) ---\n", independent.size()));
sb.append(String.format(" %s\n", independent));
return sb.toString();
}
// ── Internals ──────────────────────────────────────────────────
private void ensureComputed() {
if (!computed) compute();
}
}