Search in sources :

Example 1 with WeightedEdgeLineProcessor

use of org.deeplearning4j.graph.data.impl.WeightedEdgeLineProcessor in project deeplearning4j by deeplearning4j.

the class GraphLoader method loadWeightedEdgeListFile.

/**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<br>
     * This method assumes that edges are of the format: {@code fromIndex<delim>toIndex<delim>edgeWeight} where {@code <delim>}
     * is the delimiter.
     * @param path Path to the edge list file
     * @param numVertices The number of vertices in the graph
     * @param delim The delimiter used in the file (typically: "," or " " etc)
     * @param directed whether the edges should be treated as directed (true) or undirected (false)
     * @param 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.
     * @param ignoreLinesStartingWith Starting characters for comment lines. May be null. For example: "//" or "#"
     * @return The graph
     * @throws IOException
     */
public static Graph<String, Double> loadWeightedEdgeListFile(String path, int numVertices, String delim, boolean directed, boolean allowMultipleEdges, String... ignoreLinesStartingWith) throws IOException {
    Graph<String, Double> graph = new Graph<>(numVertices, allowMultipleEdges, new StringVertexFactory());
    EdgeLineProcessor<Double> lineProcessor = new WeightedEdgeLineProcessor(delim, directed, ignoreLinesStartingWith);
    try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
        String line;
        while ((line = br.readLine()) != null) {
            Edge<Double> edge = lineProcessor.processLine(line);
            if (edge != null) {
                graph.addEdge(edge);
            }
        }
    }
    return graph;
}
Also used : Graph(org.deeplearning4j.graph.graph.Graph) WeightedEdgeLineProcessor(org.deeplearning4j.graph.data.impl.WeightedEdgeLineProcessor) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) StringVertexFactory(org.deeplearning4j.graph.vertexfactory.StringVertexFactory) File(java.io.File)

Aggregations

BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 WeightedEdgeLineProcessor (org.deeplearning4j.graph.data.impl.WeightedEdgeLineProcessor)1 Graph (org.deeplearning4j.graph.graph.Graph)1 StringVertexFactory (org.deeplearning4j.graph.vertexfactory.StringVertexFactory)1