Search in sources :

Example 1 with Graph

use of org.deeplearning4j.graph.graph.Graph in project deeplearning4j by deeplearning4j.

the class TestDeepWalk method generateRandomGraph.

private static Graph<String, String> generateRandomGraph(int nVertices, int nEdgesPerVertex) {
    Random r = new Random(12345);
    Graph<String, String> graph = new Graph<>(nVertices, new StringVertexFactory());
    for (int i = 0; i < nVertices; i++) {
        for (int j = 0; j < nEdgesPerVertex; j++) {
            int to = r.nextInt(nVertices);
            Edge<String> edge = new Edge<>(i, to, i + "--" + to, false);
            graph.addEdge(edge);
        }
    }
    return graph;
}
Also used : Graph(org.deeplearning4j.graph.graph.Graph) IGraph(org.deeplearning4j.graph.api.IGraph) Random(java.util.Random) StringVertexFactory(org.deeplearning4j.graph.vertexfactory.StringVertexFactory) Edge(org.deeplearning4j.graph.api.Edge)

Example 2 with Graph

use of org.deeplearning4j.graph.graph.Graph in project deeplearning4j by deeplearning4j.

the class GraphLoader method loadGraph.

/** Load graph, assuming vertices are in one file and edges are in another file.
     *
     * @param vertexFilePath Path to file containing vertices, one per line
     * @param edgeFilePath Path to the file containing edges, one per line
     * @param vertexLoader VertexLoader, for loading vertices from the file
     * @param edgeLineProcessor EdgeLineProcessor, converts text lines into edges
     * @param allowMultipleEdges whether the graph should allow (or filter out) multiple edges
     * @return IGraph loaded from files
     */
public static <V, E> Graph<V, E> loadGraph(String vertexFilePath, String edgeFilePath, VertexLoader<V> vertexLoader, EdgeLineProcessor<E> edgeLineProcessor, boolean allowMultipleEdges) throws IOException {
    //Assume vertices are in one file
    //And edges are in another file
    List<Vertex<V>> vertices = vertexLoader.loadVertices(vertexFilePath);
    Graph<V, E> graph = new Graph<>(vertices, allowMultipleEdges);
    try (BufferedReader br = new BufferedReader(new FileReader(new File(edgeFilePath)))) {
        String line;
        while ((line = br.readLine()) != null) {
            Edge<E> edge = edgeLineProcessor.processLine(line);
            if (edge != null) {
                graph.addEdge(edge);
            }
        }
    }
    return graph;
}
Also used : Vertex(org.deeplearning4j.graph.api.Vertex) Graph(org.deeplearning4j.graph.graph.Graph) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 3 with Graph

use of org.deeplearning4j.graph.graph.Graph in project deeplearning4j by deeplearning4j.

the class GraphLoader method loadUndirectedGraphEdgeListFile.

/** 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<br>
     * This method assumes that all lines in the file are of the form {@code i<delim>j} where i and j are integers
     * in range 0 to numVertices inclusive, and "<delim>" is the user-provided delimiter
     * @param path Path to the edge list file
     * @param numVertices number of vertices in the graph
     * @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.
     * @return graph
     * @throws IOException if file cannot be read
     */
public static Graph<String, String> loadUndirectedGraphEdgeListFile(String path, int numVertices, String delim, boolean allowMultipleEdges) throws IOException {
    Graph<String, String> graph = new Graph<>(numVertices, allowMultipleEdges, new StringVertexFactory());
    EdgeLineProcessor<String> lineProcessor = new DelimitedEdgeLineProcessor(delim, false);
    try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
        String line;
        while ((line = br.readLine()) != null) {
            Edge<String> edge = lineProcessor.processLine(line);
            if (edge != null) {
                graph.addEdge(edge);
            }
        }
    }
    return graph;
}
Also used : Graph(org.deeplearning4j.graph.graph.Graph) DelimitedEdgeLineProcessor(org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) StringVertexFactory(org.deeplearning4j.graph.vertexfactory.StringVertexFactory) File(java.io.File)

Example 4 with Graph

use of org.deeplearning4j.graph.graph.Graph 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

Graph (org.deeplearning4j.graph.graph.Graph)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileReader (java.io.FileReader)3 StringVertexFactory (org.deeplearning4j.graph.vertexfactory.StringVertexFactory)3 Random (java.util.Random)1 Edge (org.deeplearning4j.graph.api.Edge)1 IGraph (org.deeplearning4j.graph.api.IGraph)1 Vertex (org.deeplearning4j.graph.api.Vertex)1 DelimitedEdgeLineProcessor (org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor)1 WeightedEdgeLineProcessor (org.deeplearning4j.graph.data.impl.WeightedEdgeLineProcessor)1