-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab8.java
130 lines (114 loc) 路 5.1 KB
/
Lab8.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
import java.util.HashSet;
import java.util.Set;
public class Lab8 {
public static void main(String[] args) {
BipartiteGraph graph = BipartiteGraph.example8();
System.out.println(graph.maximumMatching());
}
private static class BipartiteGraph {
private final int[][] adjacencyMatrix;
private final Set<Integer> inDegreeVertices;
private final Set<Integer> outDegreeVertices;
private BipartiteGraph(int[][] adjacencyMatrix, Set<Integer> inDegreeVertices, Set<Integer> outDegreeVertices) {
this.adjacencyMatrix = adjacencyMatrix;
this.inDegreeVertices = inDegreeVertices;
this.outDegreeVertices = outDegreeVertices;
}
public static BipartiteGraph from(int[][] adjacencyMatrix) {
assertMatrixIsSquare(adjacencyMatrix);
assertEdgesAreBinary(adjacencyMatrix);
BipartiteInfo info = assertGraphIsBipartite(adjacencyMatrix);
return new BipartiteGraph(adjacencyMatrix, info.inDegreeVertices, info.outDegreeVertices);
}
private static void assertMatrixIsSquare(int[][] matrix) {
int degree = matrix.length;
for (int[] row : matrix) {
if (row.length != degree) {
throw new RuntimeException("Invalid: The matrix is not a square");
}
}
}
private static void assertEdgesAreBinary(int[][] adjacencyMatrix) {
for (int[] row : adjacencyMatrix) {
for (int element : row) {
if (element != 0 && element != 1) {
throw new RuntimeException("Invalid: Bipartite Graph must be unweighted and all edge weights " +
"must be 0 or 1");
}
}
}
}
private static class BipartiteInfo {
private final Set<Integer> outDegreeVertices;
private final Set<Integer> inDegreeVertices;
BipartiteInfo(Set<Integer> inDegreeVertices, Set<Integer> outDegreeVertices) {
this.inDegreeVertices = inDegreeVertices;
this.outDegreeVertices = outDegreeVertices;
}
}
private static BipartiteInfo assertGraphIsBipartite(int[][] adjacencyMatrix) {
Set<Integer> inDegreeVertices = new HashSet<>();
Set<Integer> outDegreeVertices = new HashSet<>();
for (int row = 0 ; row < adjacencyMatrix.length ; row++) {
for (int column = 0 ; column < adjacencyMatrix.length ; column++) {
if (adjacencyMatrix[row][column] > 0) {
assertVertexHasNoIndegree(row, inDegreeVertices);
assertVertexHashNoOutdegree(column, outDegreeVertices);
inDegreeVertices.add(column);
outDegreeVertices.add(row);
}
}
}
return new BipartiteInfo(inDegreeVertices, outDegreeVertices);
}
private static void assertVertexHasNoIndegree(int vertex, Set<Integer> vertices) {
assertVertexNotIn(vertex, vertices);
}
private static void assertVertexHashNoOutdegree(int vertex, Set<Integer> vertices) {
assertVertexNotIn(vertex, vertices);
}
private static void assertVertexNotIn(int vertex, Set<Integer> set) {
if (set.contains(vertex)) {
throw new RuntimeException("vertex: " + vertex + " should not be present here. This doesn't " +
"satisfy the condition for a Bipartite graph");
}
}
public static BipartiteGraph example3() {
// maximum matching 1
return BipartiteGraph.from(new int[][] {
{0, 1},
{0, 0}
});
}
public static BipartiteGraph example8() {
return BipartiteGraph.from(new int[][] {
{0, 0, 0, 1, 1},
{0, 0, 0, 1, 1},
{0, 0, 0, 0, 1},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
});
}
public int maximumMatching() {
int[][] flowNetworkedMatrix = getFlowMatrix();
FordFulkerson graph = FordFulkerson.from(flowNetworkedMatrix);
return graph.maxFlow(0, adjacencyMatrix.length + 1);
}
@SuppressWarnings("ManualArrayCopy")
private int[][] getFlowMatrix() {
int[][] matrix = new int[adjacencyMatrix.length + 2][adjacencyMatrix.length + 2];
for (int row = 0 ; row < adjacencyMatrix.length ; row++) {
for (int column = 0 ; column < adjacencyMatrix.length ; column++) {
matrix[row + 1][column + 1] = adjacencyMatrix[row][column];
}
}
for (int vertex : inDegreeVertices) {
matrix[vertex + 1][matrix.length - 1] = 1;
}
for (int vertex : outDegreeVertices) {
matrix[0][vertex + 1] = 1;
}
return matrix;
}
}
}