KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > ext > MatrixExporter


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* ------------------
26  * MatrixExporter.java
27  * ------------------
28  * (C) Copyright 2005-2006, by Charles Fry and Contributors.
29  *
30  * Original Author: Charles Fry
31  *
32  * $Id: MatrixExporter.java 504 2006-07-03 02:37:26Z perfecthash $
33  *
34  * Changes
35  * -------
36  * 13-Dec-2005 : Initial Version (CF);
37  *
38  */

39 package org.jgrapht.ext;
40
41 import java.io.*;
42
43 import java.util.*;
44
45 import org.jgrapht.*;
46 import org.jgrapht.util.*;
47
48
49 /**
50  * Exports a graph to a plain text matrix format, which can be processed by
51  * matrix manipulation software, such as <a HREF="http://rs.cipr.uib.no/mtj/">
52  * MTJ</a> or <a HREF="http://www.mathworks.com/products/matlab/">MATLAB</a>.
53  *
54  * @author Charles Fry
55  */

56 public class MatrixExporter<V, E>
57 {
58
59     //~ Instance fields -------------------------------------------------------
60

61     private String JavaDoc delimiter = " ";
62     private String JavaDoc prefix = "";
63     private String JavaDoc suffix = "";
64
65     //~ Constructors ----------------------------------------------------------
66

67     /**
68      * Creates a new MatrixExporter object.
69      */

70     public MatrixExporter()
71     {
72     }
73
74     //~ Methods ---------------------------------------------------------------
75

76     private void println(
77         PrintWriter out,
78         String JavaDoc fromName,
79         String JavaDoc toName,
80         String JavaDoc value)
81     {
82         out.println(
83             prefix + fromName + suffix + delimiter
84             + prefix + toName + suffix + delimiter
85             + prefix + value + suffix);
86     }
87
88     /**
89      * Exports the specified graph into a plain text file format containing a
90      * sparse representation of the graph's adjacency matrix. The value stored
91      * in each position of the matrix indicates the number of edges between two
92      * vertices. With an undirected graph, the adjacency matrix is symetric.
93      *
94      * @param output the writer to which the graph to be exported.
95      * @param g the graph to be exported.
96      */

97     public void exportAdjacencyMatrix(Writer output, UndirectedGraph<V, E> g)
98     {
99         PrintWriter out = new PrintWriter(output);
100
101         VertexNameProvider<V> nameProvider = new IntegerNameProvider<V>();
102         for (V from : g.vertexSet()) {
103             // assign ids in vertex set iteration order
104
nameProvider.getVertexName(from);
105         }
106
107         for (V from : g.vertexSet()) {
108             exportAdjacencyMatrixVertex(
109                 out,
110                 nameProvider,
111                 from,
112                 Graphs.neighborListOf(g, from));
113         }
114
115         out.flush();
116     }
117
118     /**
119      * Exports the specified graph into a plain text file format containing a
120      * sparse representation of the graph's adjacency matrix. The value stored
121      * in each position of the matrix indicates the number of directed edges
122      * going from one vertex to another.
123      *
124      * @param output the writer to which the graph to be exported.
125      * @param g the graph to be exported.
126      */

127     public void exportAdjacencyMatrix(Writer output, DirectedGraph<V, E> g)
128     {
129         PrintWriter out = new PrintWriter(output);
130
131         VertexNameProvider<V> nameProvider = new IntegerNameProvider<V>();
132         for (V from : g.vertexSet()) {
133             // assign ids in vertex set iteration order
134
nameProvider.getVertexName(from);
135         }
136
137         for (V from : g.vertexSet()) {
138             exportAdjacencyMatrixVertex(
139                 out,
140                 nameProvider,
141                 from,
142                 Graphs.successorListOf(g, from));
143         }
144
145         out.flush();
146     }
147
148     private void exportAdjacencyMatrixVertex(
149         PrintWriter out,
150         VertexNameProvider<V> nameProvider,
151         V from,
152         List<V> neighbors)
153     {
154         String JavaDoc fromName = nameProvider.getVertexName(from);
155         Map<String JavaDoc, ModifiableInteger> counts =
156             new LinkedHashMap<String JavaDoc, ModifiableInteger>();
157         for (V to : neighbors) {
158             String JavaDoc toName = nameProvider.getVertexName(to);
159             ModifiableInteger count = counts.get(toName);
160             if (count == null) {
161                 count = new ModifiableInteger(0);
162                 counts.put(toName, count);
163             }
164
165             count.increment();
166             if (from.equals(to)) {
167                 // count loops twice, once for each end
168
count.increment();
169             }
170         }
171         for (Map.Entry<String JavaDoc, ModifiableInteger> entry : counts.entrySet()) {
172             String JavaDoc toName = entry.getKey();
173             ModifiableInteger count = entry.getValue();
174             println(out, fromName, toName, count.toString());
175         }
176     }
177
178     /**
179      * Exports the specified graph into a plain text file format containing a
180      * sparse representation of the graph's Laplacian matrix. Laplacian matrices
181      * are only defined for simple graphs, so edge direction, multiple edges,
182      * loops, and weights are all ignored when creating the Laplacian matrix. If
183      * you're unsure about Laplacian matrices, see: <a
184      * HREF="http://mathworld.wolfram.com/LaplacianMatrix.html">
185      * http://mathworld.wolfram.com/LaplacianMatrix.html</a>.
186      *
187      * @param output the writer to which the graph is to be exported.
188      * @param g the graph to be exported.
189      */

190     public void exportLaplacianMatrix(Writer output, UndirectedGraph<V, E> g)
191     {
192         PrintWriter out = new PrintWriter(output);
193
194         VertexNameProvider<V> nameProvider = new IntegerNameProvider<V>();
195         for (V from : g.vertexSet()) {
196             // assign ids in vertex set iteration order
197
nameProvider.getVertexName(from);
198         }
199
200         for (V from : g.vertexSet()) {
201             String JavaDoc fromName = nameProvider.getVertexName(from);
202
203             // TODO modify Graphs to return neighbor sets
204
List<V> neighbors = Graphs.neighborListOf(g, from);
205             println(
206                 out,
207                 fromName,
208                 fromName,
209                 Integer.toString(neighbors.size()));
210             for (V to : neighbors) {
211                 String JavaDoc toName = nameProvider.getVertexName(to);
212                 println(out, fromName, toName, "-1");
213             }
214         }
215
216         out.flush();
217     }
218
219     /**
220      * Exports the specified graph into a plain text file format containing a
221      * sparse representation of the graph's normalized Laplacian matrix.
222      * Laplacian matrices are only defined for simple graphs, so edge direction,
223      * multiple edges, loops, and weights are all ignored when creating the
224      * Laplacian matrix. If you're unsure about normalized Laplacian matrices,
225      * see: <a HREF="http://mathworld.wolfram.com/LaplacianMatrix.html">
226      * http://mathworld.wolfram.com/LaplacianMatrix.html</a>.
227      *
228      * @param output the writer to which the graph is to be exported.
229      * @param g the graph to be exported.
230      */

231     public void exportNormalizedLaplacianMatrix(
232         Writer output,
233         UndirectedGraph<V, E> g)
234     {
235         PrintWriter out = new PrintWriter(output);
236
237         VertexNameProvider<V> nameProvider = new IntegerNameProvider<V>();
238         for (V from : g.vertexSet()) {
239             // assign ids in vertex set iteration order
240
nameProvider.getVertexName(from);
241         }
242
243         for (V from : g.vertexSet()) {
244             String JavaDoc fromName = nameProvider.getVertexName(from);
245             Set<V> neighbors =
246                 new LinkedHashSet<V>(Graphs.neighborListOf(g, from));
247             if (neighbors.isEmpty()) {
248                 println(out, fromName, fromName, "0");
249             } else {
250                 println(out, fromName, fromName, "1");
251
252                 for (V to : neighbors) {
253                     String JavaDoc toName = nameProvider.getVertexName(to);
254                     double value =
255                         -1 / Math.sqrt(g.degreeOf(from) * g.degreeOf(to));
256                     println(out, fromName, toName, Double.toString(value));
257                 }
258             }
259         }
260
261         out.flush();
262     }
263 }
264
Popular Tags