Class MatrixFile<V,​E>

  • All Implemented Interfaces:
    GraphFile<V,​E>

    public class MatrixFile<V,​E>
    extends java.lang.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
     
    • Constructor Summary

      Constructors 
      Constructor Description
      MatrixFile​(java.util.Map<E,​java.lang.Number> weightKey, org.apache.commons.collections4.Factory<? extends edu.uci.ics.jung.graph.Graph<V,​E>> graphFactory, org.apache.commons.collections4.Factory<V> vertexFactory, org.apache.commons.collections4.Factory<E> edgeFactory)
      Constructs MatrixFile instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      edu.uci.ics.jung.graph.Graph<V,​E> load​(java.io.BufferedReader reader)
      Loads a graph from an input reader
      edu.uci.ics.jung.graph.Graph<V,​E> load​(java.lang.String filename)
      Loads a graph from a file.
      void save​(edu.uci.ics.jung.graph.Graph<V,​E> graph, java.lang.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​(java.util.Map<E,​java.lang.Number> weightKey,
                          org.apache.commons.collections4.Factory<? extends edu.uci.ics.jung.graph.Graph<V,​E>> graphFactory,
                          org.apache.commons.collections4.Factory<V> vertexFactory,
                          org.apache.commons.collections4.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 edu.uci.ics.jung.graph.Graph<V,​E> load​(java.io.BufferedReader reader)
                                                     throws java.io.IOException
        Loads a graph from an input reader
        Parameters:
        reader - the input reader
        Returns:
        the graph
        Throws:
        java.io.IOException
      • load

        public edu.uci.ics.jung.graph.Graph<V,​E> load​(java.lang.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)