Class GraphLoader


  • public class GraphLoader
    extends Object
    Utility methods for loading graphs
    • Method Detail

      • loadUndirectedGraphEdgeListFile

        public static Graph<String,​String> loadUndirectedGraphEdgeListFile​(String path,
                                                                                 int numVertices,
                                                                                 String delim)
                                                                          throws IOException
        Simple method for loading an undirected graph, where the graph is represented by a edge list with one edge per line with a delimiter in between
        This method assumes that all lines in the file are of the form i<delim>j where i and j are integers in range 0 to numVertices inclusive, and "" is the user-provided delimiter Note: this method calls loadUndirectedGraphEdgeListFile(String, int, String, boolean) with allowMultipleEdges = true.
        Parameters:
        path - Path to the edge list file
        numVertices - number of vertices in the graph
        Returns:
        graph
        Throws:
        IOException - if file cannot be read
      • loadUndirectedGraphEdgeListFile

        public static Graph<String,​String> loadUndirectedGraphEdgeListFile​(String path,
                                                                                 int numVertices,
                                                                                 String delim,
                                                                                 boolean allowMultipleEdges)
                                                                          throws IOException
        Simple method for loading an undirected graph, where the graph is represented by a edge list with one edge per line with a delimiter in between
        This method assumes that all lines in the file are of the form i<delim>j where i and j are integers in range 0 to numVertices inclusive, and "" is the user-provided delimiter
        Parameters:
        path - Path to the edge list file
        numVertices - number of vertices in the graph
        allowMultipleEdges - If set to false, the graph will not allow multiple edges between any two vertices to exist. However, checking for duplicates during graph loading can be costly, so use allowMultipleEdges=true when possible.
        Returns:
        graph
        Throws:
        IOException - if file cannot be read
      • loadWeightedEdgeListFile

        public static Graph<String,​Double> loadWeightedEdgeListFile​(String path,
                                                                          int numVertices,
                                                                          String delim,
                                                                          boolean directed,
                                                                          String... ignoreLinesStartingWith)
                                                                   throws IOException
        Method for loading a weighted graph from an edge list file, where each edge (inc. weight) is represented by a single line. Graph may be directed or undirected
        This method assumes that edges are of the format: fromIndex<delim>toIndex<delim>edgeWeight where <delim> is the delimiter. Note: this method calls loadWeightedEdgeListFile(String, int, String, boolean, boolean, String...) with allowMultipleEdges = true.
        Parameters:
        path - Path to the edge list file
        numVertices - The number of vertices in the graph
        delim - The delimiter used in the file (typically: "," or " " etc)
        directed - whether the edges should be treated as directed (true) or undirected (false)
        ignoreLinesStartingWith - Starting characters for comment lines. May be null. For example: "//" or "#"
        Returns:
        The graph
        Throws:
        IOException
      • loadWeightedEdgeListFile

        public static Graph<String,​Double> loadWeightedEdgeListFile​(String path,
                                                                          int numVertices,
                                                                          String delim,
                                                                          boolean directed,
                                                                          boolean allowMultipleEdges,
                                                                          String... ignoreLinesStartingWith)
                                                                   throws IOException
        Method for loading a weighted graph from an edge list file, where each edge (inc. weight) is represented by a single line. Graph may be directed or undirected
        This method assumes that edges are of the format: fromIndex<delim>toIndex<delim>edgeWeight where <delim> is the delimiter.
        Parameters:
        path - Path to the edge list file
        numVertices - The number of vertices in the graph
        delim - The delimiter used in the file (typically: "," or " " etc)
        directed - whether the edges should be treated as directed (true) or undirected (false)
        allowMultipleEdges - If set to false, the graph will not allow multiple edges between any two vertices to exist. However, checking for duplicates during graph loading can be costly, so use allowMultipleEdges=true when possible.
        ignoreLinesStartingWith - Starting characters for comment lines. May be null. For example: "//" or "#"
        Returns:
        The graph
        Throws:
        IOException
      • loadGraph

        public static <V,​E> Graph<V,​E> loadGraph​(String path,
                                                             EdgeLineProcessor<E> lineProcessor,
                                                             VertexFactory<V> vertexFactory,
                                                             int numVertices,
                                                             boolean allowMultipleEdges)
                                                      throws IOException
        Load a graph into memory, using a given EdgeLineProcessor. Assume one edge per line
        Parameters:
        path - Path to the file containing the edges, one per line
        lineProcessor - EdgeLineProcessor used to convert lines of text into a graph (or null for comment lines etc)
        vertexFactory - Used to create vertices
        numVertices - number of vertices in the graph
        allowMultipleEdges - whether the graph should allow multiple edges between a given pair of vertices or not
        Returns:
        IGraph
        Throws:
        IOException
      • loadGraph

        public static <V,​E> Graph<V,​E> loadGraph​(String vertexFilePath,
                                                             String edgeFilePath,
                                                             VertexLoader<V> vertexLoader,
                                                             EdgeLineProcessor<E> edgeLineProcessor,
                                                             boolean allowMultipleEdges)
                                                      throws IOException
        Load graph, assuming vertices are in one file and edges are in another file.
        Parameters:
        vertexFilePath - Path to file containing vertices, one per line
        edgeFilePath - Path to the file containing edges, one per line
        vertexLoader - VertexLoader, for loading vertices from the file
        edgeLineProcessor - EdgeLineProcessor, converts text lines into edges
        allowMultipleEdges - whether the graph should allow (or filter out) multiple edges
        Returns:
        IGraph loaded from files
        Throws:
        IOException