edu.uci.ics.jung.io
Class MatrixFile<V,E>

java.lang.Object
  extended by edu.uci.ics.jung.io.MatrixFile<V,E>
All Implemented Interfaces:
GraphFile<V,E>

public class MatrixFile<V,E>
extends Object
implements GraphFile<V,E>

Basic I/O handler for ascii matrix files. An ascii matrix is simply a square matrix where 0 values for cell (i,j) indicates no edge exists between vertex i and vertex j and non-zero values indicates there is an edge. If a non-null weight key is specified then it will be used to treat the non-zero values as a weight stored in the edges' user data keyed off the specified weight key value.

When loading a graph from a file, a symmetric graph will result in the construction of an undirected sparse graph while a non-symmetric graph will result in the construction of a directed sparse graph.

For example the following ascii matrix when loaded using the code:
MatrixFile mf = new MatrixFile(null);
Graph g = mf.load(filename);

will produce an undirected sparse matrix with no weights:

 0 1 0 1
 1 0 0 1
 0 0 0 0
 1 1 0 0
 

whereas the following ascii matrix when loaded using the code:
MatrixFile mf = new MatrixFile("WEIGHT");
Graph g = mf.load(filename);

will produce a directed sparse matrix with double weight values stored in the edges user data under the key "WEIGHT" :

 0 .5 10 0
 0 1 0 0
 0 0 0 -30
 5 0 0 0
 

Author:
Scott, Tom Nelson - converted to jung2

Constructor Summary
MatrixFile(Map<E,Number> weightKey, org.apache.commons.collections15.Factory<? extends Graph<V,E>> graphFactory, org.apache.commons.collections15.Factory<V> vertexFactory, org.apache.commons.collections15.Factory<E> edgeFactory)
          Constructs MatrixFile instance.
 
Method Summary
 Graph<V,E> load(BufferedReader reader)
          Loads a graph from an input reader
 Graph<V,E> load(String filename)
          Loads a graph from a file.
 void save(Graph<V,E> graph, String filename)
          Saves a graph to a file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MatrixFile

public MatrixFile(Map<E,Number> weightKey,
                  org.apache.commons.collections15.Factory<? extends Graph<V,E>> graphFactory,
                  org.apache.commons.collections15.Factory<V> vertexFactory,
                  org.apache.commons.collections15.Factory<E> edgeFactory)
Constructs MatrixFile instance. If weightKey is not null then, it will attempt to use that key to store and retreive weights from the edges' UserData.

Method Detail

load

public Graph<V,E> load(BufferedReader reader)
                throws IOException
Loads a graph from an input reader

Parameters:
reader - the input reader
Returns:
the graph
Throws:
IOException

load

public Graph<V,E> load(String filename)
Loads a graph from a file.

Specified by:
load in interface GraphFile<V,E>
Parameters:
filename - the location and name of the file
Returns:
the graph
See Also:
GraphFile.load(java.lang.String)

save

public void save(Graph<V,E> graph,
                 String filename)
Saves a graph to a file

Specified by:
save in interface GraphFile<V,E>
Parameters:
graph - the location and name of the file
filename - the graph
See Also:
GraphFile.save(edu.uci.ics.jung.graph.Graph, java.lang.String)


Copyright © 2009. All Rights Reserved.