-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBipartiteGraph.java
More file actions
49 lines (38 loc) · 1.35 KB
/
BipartiteGraph.java
File metadata and controls
49 lines (38 loc) · 1.35 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
package org.psjava;
import org.psjava.util.Assertion;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class BipartiteGraph<V> {
private Set<V> lefts = new HashSet<>();
private Set<V> rights = new HashSet<>();
private List<BipartiteGraphEdge<V>> edges = new ArrayList<>();
public void addLeft(V v) {
Assertion.ensure(!rights.contains(v), () -> "already in right side. vertex=" + v);
lefts.add(v);
}
public void addRight(V v) {
Assertion.ensure(!lefts.contains(v), () -> "already in left size. vertex=" + v);
rights.add(v);
}
public void addEdge(final V left, final V right) {
assertVertexExist(left, lefts);
assertVertexExist(right, rights);
edges.add(new BipartiteGraphEdge<>(left, right));
}
private void assertVertexExist(V v, Set<V> set) {
Assertion.ensure(set.contains(v), () -> "vertex is not in graph. vertex=" + v);
}
Collection<V> getLeftVertices() {
return Collections.unmodifiableCollection(lefts);
}
Collection<V> getRightVertices() {
return Collections.unmodifiableCollection(rights);
}
Collection<BipartiteGraphEdge<V>> getEdges() {
return Collections.unmodifiableCollection(edges);
}
}