1
2
3
4
5
6
7 package edu.uci.ics.jung.algorithms.matrix;
8
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.List;
12 import java.util.Map;
13
14 import org.apache.commons.collections15.BidiMap;
15 import org.apache.commons.collections15.Factory;
16
17 import cern.colt.matrix.DoubleMatrix1D;
18 import cern.colt.matrix.DoubleMatrix2D;
19 import cern.colt.matrix.impl.DenseDoubleMatrix1D;
20 import cern.colt.matrix.impl.SparseDoubleMatrix2D;
21 import cern.colt.matrix.linalg.Algebra;
22 import edu.uci.ics.jung.algorithms.util.ConstantMap;
23 import edu.uci.ics.jung.algorithms.util.Indexer;
24 import edu.uci.ics.jung.graph.Graph;
25 import edu.uci.ics.jung.graph.UndirectedGraph;
26
27
28
29
30
31
32
33
34
35
36
37
38 public class GraphMatrixOperations
39 {
40
41
42
43
44
45
46
47
48
49
50
51 @SuppressWarnings("unchecked")
52 public static <V,E> Graph<V,E> square(Graph<V,E> g,
53 Factory<E> edgeFactory, MatrixElementOperations<E> meo)
54 {
55
56 Graph<V, E> squaredGraph = null;
57 try {
58 squaredGraph = g.getClass().newInstance();
59 } catch (InstantiationException e3) {
60 e3.printStackTrace();
61 } catch (IllegalAccessException e3) {
62 e3.printStackTrace();
63 }
64
65 Collection<V> vertices = g.getVertices();
66 for (V v : vertices)
67 {
68 squaredGraph.addVertex(v);
69 }
70 for (V v : vertices)
71 {
72 for (V src : g.getPredecessors(v))
73 {
74
75 E e1 = g.findEdge(src,v);
76 for (V dest : g.getSuccessors(v))
77 {
78
79 E e2 = g.findEdge(v,dest);
80
81 Number pathData = meo.computePathData(e1, e2);
82 E e = squaredGraph.findEdge(src,dest);
83
84 if (e == null) {
85 e = edgeFactory.create();
86 squaredGraph.addEdge(e, src, dest);
87 }
88 meo.mergePaths(e, pathData);
89 }
90 }
91 }
92 return squaredGraph;
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public static <V,E> Graph<V,E> matrixToGraph(DoubleMatrix2D matrix,
115 Factory<? extends Graph<V,E>> graphFactory,
116 Factory<V> vertexFactory, Factory<E> edgeFactory,
117 Map<E,Number> nev)
118 {
119 if (matrix.rows() != matrix.columns())
120 {
121 throw new IllegalArgumentException("Matrix must be square.");
122 }
123 int size = matrix.rows();
124
125 Graph<V,E> graph = graphFactory.create();
126
127 for(int i = 0; i < size; i++)
128 {
129 V vertex = vertexFactory.create();
130 graph.addVertex(vertex);
131 }
132
133 List<V> vertices = new ArrayList<V>(graph.getVertices());
134 for (int i = 0; i < size; i++)
135 {
136 for (int j = 0; j < size; j++)
137 {
138 double value = matrix.getQuick(i, j);
139 if (value != 0)
140 {
141 E e = edgeFactory.create();
142 if (graph.addEdge(e, vertices.get(i), vertices.get(j)))
143 {
144 if (e != null && nev != null)
145 nev.put(e, value);
146 }
147 }
148 }
149 }
150
151
152 return graph;
153 }
154
155
156
157
158
159
160
161 public static <V,E> Graph<V,E> matrixToGraph(DoubleMatrix2D matrix,
162 Factory<? extends Graph<V,E>> graphFactory,
163 Factory<V> vertexFactory, Factory<E> edgeFactory)
164 {
165 return GraphMatrixOperations.<V,E>matrixToGraph(matrix,
166 graphFactory, vertexFactory, edgeFactory, null);
167 }
168
169
170
171
172
173
174
175 public static <V,E> SparseDoubleMatrix2D graphToSparseMatrix(Graph<V,E> g)
176 {
177 return graphToSparseMatrix(g, null);
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public static <V,E> SparseDoubleMatrix2D graphToSparseMatrix(Graph<V,E> g, Map<E,Number> nev)
194 {
195 if (nev == null)
196 nev = new ConstantMap<E,Number>(1);
197 int numVertices = g.getVertexCount();
198 SparseDoubleMatrix2D matrix = new SparseDoubleMatrix2D(numVertices,
199 numVertices);
200
201 BidiMap<V,Integer> indexer = Indexer.<V>create(g.getVertices());
202 int i=0;
203
204 for(V v : g.getVertices())
205 {
206 for (E e : g.getOutEdges(v))
207 {
208 V w = g.getOpposite(v,e);
209 int j = indexer.get(w);
210 matrix.set(i, j, matrix.getQuick(i,j) + nev.get(e).doubleValue());
211 }
212 i++;
213 }
214 return matrix;
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228 public static <V,E> SparseDoubleMatrix2D createVertexDegreeDiagonalMatrix(Graph<V,E> graph)
229 {
230 int numVertices = graph.getVertexCount();
231 SparseDoubleMatrix2D matrix = new SparseDoubleMatrix2D(numVertices,
232 numVertices);
233 int i = 0;
234 for (V v : graph.getVertices())
235 {
236 matrix.set(i, i, graph.degree(v));
237 i++;
238 }
239 return matrix;
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 public static <V,E> DoubleMatrix2D computeVoltagePotentialMatrix(
259 UndirectedGraph<V,E> graph)
260 {
261 int numVertices = graph.getVertexCount();
262
263 DoubleMatrix2D A = GraphMatrixOperations.graphToSparseMatrix(graph,
264 null);
265
266 DoubleMatrix2D D = GraphMatrixOperations
267 .createVertexDegreeDiagonalMatrix(graph);
268 DoubleMatrix2D temp = new SparseDoubleMatrix2D(numVertices - 1,
269 numVertices - 1);
270
271 for (int i = 0; i < numVertices - 1; i++)
272 {
273 for (int j = 0; j < numVertices - 1; j++)
274 {
275 temp.set(i, j, D.get(i, j) - A.get(i, j));
276 }
277 }
278 Algebra algebra = new Algebra();
279 DoubleMatrix2D tempInverse = algebra.inverse(temp);
280 DoubleMatrix2D T = new SparseDoubleMatrix2D(numVertices, numVertices);
281
282 for (int i = 0; i < numVertices - 1; i++)
283 {
284 for (int j = 0; j < numVertices - 1; j++)
285 {
286 T.set(i, j, tempInverse.get(i, j));
287 }
288 }
289 return T;
290 }
291
292
293
294
295
296
297
298
299 public static <V> DoubleMatrix1D mapTo1DMatrix(Map<V,Number> map)
300 {
301 int numVertices = map.size();
302 DoubleMatrix1D vector = new DenseDoubleMatrix1D(numVertices);
303 int i = 0;
304 for (V v : map.keySet())
305 {
306 vector.set(i, map.get(v).doubleValue());
307 i++;
308 }
309 return vector;
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 public static <V,E> DoubleMatrix2D computeMeanFirstPassageMatrix(Graph<V,E> G,
337 Map<E,Number> edgeWeights, DoubleMatrix1D stationaryDistribution)
338 {
339 DoubleMatrix2D temp = GraphMatrixOperations.graphToSparseMatrix(G,
340 edgeWeights);
341 for (int i = 0; i < temp.rows(); i++)
342 {
343 for (int j = 0; j < temp.columns(); j++)
344 {
345 double value = -1 * temp.get(i, j)
346 + stationaryDistribution.get(j);
347 if (i == j)
348 value += 1;
349 if (value != 0)
350 temp.set(i, j, value);
351 }
352 }
353 Algebra algebra = new Algebra();
354 DoubleMatrix2D fundamentalMatrix = algebra.inverse(temp);
355 temp = new SparseDoubleMatrix2D(temp.rows(), temp.columns());
356 for (int i = 0; i < temp.rows(); i++)
357 {
358 for (int j = 0; j < temp.columns(); j++)
359 {
360 double value = -1.0 * fundamentalMatrix.get(i, j);
361 value += fundamentalMatrix.get(j, j);
362 if (i == j)
363 value += 1;
364 if (value != 0)
365 temp.set(i, j, value);
366 }
367 }
368 DoubleMatrix2D stationaryMatrixDiagonal = new SparseDoubleMatrix2D(temp
369 .rows(), temp.columns());
370 int numVertices = stationaryDistribution.size();
371 for (int i = 0; i < numVertices; i++)
372 stationaryMatrixDiagonal.set(i, i, 1.0 / stationaryDistribution
373 .get(i));
374 DoubleMatrix2D meanFirstPassageMatrix = algebra.mult(temp,
375 stationaryMatrixDiagonal);
376 return meanFirstPassageMatrix;
377 }
378 }