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
|
package bjc.utils.graph;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.InputMismatchException;
import java.util.Scanner;
import bjc.utils.data.IHolder;
import bjc.utils.data.Identity;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.FunctionalMap;
import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.IMap;
import bjc.utils.funcutils.FuncUtils;
/**
* An adjacency map representing a graph
*
* @author ben
*
* @param <T>
* The type of the nodes in the graph
*/
public class AdjacencyMap<T> {
/**
* Create an adjacency map from a stream of text
*
* @param stream
* The stream of text to read in
*
* @return
* An adjacency map defined by the text
*/
public static AdjacencyMap<Integer> fromStream(final InputStream stream) {
if (stream == null) throw new NullPointerException("Input source must not be null");
/* Create the adjacency map. */
AdjacencyMap<Integer> adjacency;
try (Scanner input = new Scanner(stream)) {
input.useDelimiter("\n");
int vertexCount;
final String possible = input.next();
try {
/* First, read in number of vertices. */
vertexCount = Integer.parseInt(possible);
} catch (final NumberFormatException nfex) {
String msg = String.format(
"The first line must contain the number of vertices. %s is not a valid number",
possible);
final InputMismatchException imex = new InputMismatchException(msg);
imex.initCause(nfex);
throw imex;
}
if (vertexCount <= 0)
throw new InputMismatchException("The number of vertices must be greater than 0");
final IList<Integer> vertices = new FunctionalList<>();
FuncUtils.doTimes(vertexCount, (vertexNo) -> vertices.add(vertexNo));
adjacency = new AdjacencyMap<>(vertices);
final IHolder<Integer> row = new Identity<>(0);
input.forEachRemaining((strang) -> {
readRow(adjacency, vertexCount, row, strang);
});
}
return adjacency;
}
/* Read a row of edges. */
private static void readRow(final AdjacencyMap<Integer> adjacency, final int vertexCount,
final IHolder<Integer> row, final String strang) {
final String[] parts = strang.split(" ");
if (parts.length != vertexCount) {
String msg = String.format("Must specify a weight for all %d vertices", vertexCount);
throw new InputMismatchException(msg);
}
int column = 0;
for (final String part : parts) {
int weight;
try {
weight = Integer.parseInt(part);
} catch (final NumberFormatException nfex) {
String msg = String.format("%d is not a valid weight.", part);
final InputMismatchException imex = new InputMismatchException();
imex.initCause(nfex);
throw imex;
}
adjacency.setWeight(row.getValue(), column, weight);
column++;
}
row.transform((rowNumber) -> rowNumber + 1);
}
/** The backing storage of the map */
private final IMap<T, IMap<T, Integer>> adjacency = new FunctionalMap<>();
/**
* Create a new map from a set of vertices
*
* @param vertices
* The set of vertices to create a map from
*/
public AdjacencyMap(final IList<T> vertices) {
if (vertices == null) throw new NullPointerException("Vertices must not be null");
vertices.forEach(vertex -> {
final IMap<T, Integer> row = new FunctionalMap<>();
vertices.forEach(target -> {
row.put(target, 0);
});
adjacency.put(vertex, row);
});
}
/**
* Check if the graph is directed
*
* @return
* Whether or not the graph is directed
*/
public boolean isDirected() {
final IHolder<Boolean> result = new Identity<>(true);
adjacency.forEach((sourceKey, sourceValue) -> {
sourceValue.forEach((targetKey, targetValue) -> {
final int inverseValue = adjacency.get(targetKey).get(sourceKey);
if (targetValue != inverseValue) {
result.replace(false);
}
});
});
return result.getValue();
}
/**
* Set the weight of an edge.
*
* @param source
* The source node of the edge.
* @param target
* The target node of the edge.
* @param weight
* The weight of the edge.
*/
public void setWeight(final T source, final T target, final int weight) {
if (source == null) {
throw new NullPointerException("Source vertex must not be null");
} else if (target == null) {
throw new NullPointerException("Target vertex must not be null");
}
if (!adjacency.containsKey(source)) {
String msg = String.format("Source vertex %s isn't present in map", source);
throw new IllegalArgumentException(msg);
} else if (!adjacency.containsKey(target)) {
String msg = String.format("Target vertex %s isn't present in map", target);
throw new IllegalArgumentException(msg);
}
adjacency.get(source).put(target, weight);
}
/**
* Convert this to a different graph representation.
*
* @return
* The new representation of this graph.
*/
public Graph<T> toGraph() {
final Graph<T> ret = new Graph<>();
adjacency.forEach((sourceKey, sourceValue) -> {
sourceValue.forEach((targetKey, targetValue) -> {
ret.addEdge(sourceKey, targetKey, targetValue, true);
});
});
return ret;
}
/**
* Convert an adjacency map back into a stream.
*
* @param sink
* The stream to convert to.
*/
public void toStream(final OutputStream sink) {
if (sink == null) throw new NullPointerException("Output source must not be null");
final PrintStream outputPrinter = new PrintStream(sink);
adjacency.forEach((sourceKey, sourceValue) -> {
sourceValue.forEach((targetKey, targetValue) -> {
outputPrinter.printf("%d", targetValue);
});
outputPrinter.println();
});
outputPrinter.close();
}
}
|