-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabeledGraph.java
More file actions
123 lines (108 loc) · 3.64 KB
/
Copy pathLabeledGraph.java
File metadata and controls
123 lines (108 loc) · 3.64 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
package graph;
/* See restrictions in Graph.java. */
import java.util.ArrayList;
/** A Graph whose vertices are labeled with type VL and whose edges are
* labeled with type EL.
* @author P. N. Hilfinger
*/
public class LabeledGraph<VL, EL> extends GraphFilter {
/** A labeling of the graph G. Accessors and modifiers of the graph
* act upon G. Attempts to modify the graph structure directly through
* G have undefined effects upon the labeled version created by this
* constructor. */
public LabeledGraph(Graph G) {
super(G);
}
/** Returns the label on vertex V, which must be one of my
* vertices. */
public VL getLabel(int v) {
checkMyVertex(v);
return v < _vlabel.size() ? _vlabel.get(v) : null;
}
/** Returns the label on the edge (U, V), which must be one of
* my edges. */
public EL getLabel(int u, int v) {
int e = edgeId(u, v);
if (e == 0) {
throw new IllegalArgumentException("no such edge");
}
return e < _elabel.size() ? _elabel.get(e) : null;
}
/** Return the successor of vertex U along the edge labeled LAB, if any,
* and otherwise 0. Assumes LAB is not null. If multiple edges have the
* label EL, returns an arbitrary one of them. */
public int getSuccessor(int u, EL lab) {
for (int v : successors(u)) {
if (lab.equals(getLabel(u, v))) {
return v;
}
}
return 0;
}
/** Set getVertexLabel(V) to LAB. V must be one of my vertices. */
public void setLabel(int v, VL lab) {
checkMyVertex(v);
if (lab != null || v < _vlabel.size()) {
expand(_vlabel, v + 1);
_vlabel.set(v, lab);
}
}
/** Set getEdgeLabel(U, V) to LAB. (U, V) must be one of my edges. */
public void setLabel(int u, int v, EL lab) {
int e = edgeId(u, v);
if (e == 0) {
throw new IllegalArgumentException("no such edge");
}
if (lab != null || e < _elabel.size()) {
expand(_elabel, e + 1);
_elabel.set(e, lab);
}
}
/** Returns a new vertex labeled LAB, and adds it to me with no
* incident edges. */
public int add(VL lab) {
int v = add();
setLabel(v, lab);
return v;
}
/** Adds an edge incident on U and V, labeled with LAB and returns
* the same value as for add(u, v). If I am directed,
* the edge is directed (leaves U and enters V). If there is already
* an edge (U, V), sets its label to EL. */
public int add(int u, int v, EL lab) {
int e = add(u, v);
if (lab != null || e < _elabel.size()) {
expand(_elabel, e + 1);
_elabel.set(e, lab);
}
return e;
}
@Override
public void remove(int v) {
super.remove(v);
if (v < _vlabel.size()) {
_vlabel.set(v, null);
}
}
@Override
public void remove(int u, int v) {
int e = edgeId(u, v);
if (e != 0) {
super.remove(u, v);
if (e < _elabel.size()) {
_elabel.set(e, null);
}
}
}
/** If necessary, add nulls to L to make its length N. Has no effect if
* L's length is already at least N. */
static void expand(ArrayList<?> L, int n) {
while (L.size() < n) {
L.add(null);
}
}
/** Mapping of vertex numbers to vertex labels. */
private final ArrayList<VL> _vlabel = new ArrayList<>();
/** Mapping of unique edge ids to edge labels. */
private final ArrayList<EL> _elabel = new ArrayList<>();
}