alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (RandomWalkGraphIteratorProvider.java)

This example Java source code file (RandomWalkGraphIteratorProvider.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

arraylist, graphwalkiterator, graphwalkiteratorprovider, igraph, list, noedgehandling, override, randomwalkgraphiteratorprovider, util

The RandomWalkGraphIteratorProvider.java Java example source code

package org.deeplearning4j.graph.iterator.parallel;

import org.deeplearning4j.graph.api.IGraph;
import org.deeplearning4j.graph.api.NoEdgeHandling;
import org.deeplearning4j.graph.iterator.GraphWalkIterator;
import org.deeplearning4j.graph.iterator.RandomWalkIterator;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**Random walk graph iterator provider: given a graph, split up the generation of random walks
 * for parallel learning. Specifically: with N threads and V vertices:
 * - First iterator generates random walks starting at vertices 0 to V/N
 * - Second iterator generates random walks starting at vertices V/N+1 to 2*V/N
 * - and so on
 * @param <V> Vertex type
 */
public class RandomWalkGraphIteratorProvider<V> implements GraphWalkIteratorProvider {

    private IGraph<V,?> graph;
    private int walkLength;
    private Random rng;
    private NoEdgeHandling mode;

    public RandomWalkGraphIteratorProvider( IGraph<V,?> graph, int walkLength ){
        this(graph, walkLength, System.currentTimeMillis(), NoEdgeHandling.EXCEPTION_ON_DISCONNECTED);
    }

    public RandomWalkGraphIteratorProvider( IGraph<V,?> graph, int walkLength, long seed, NoEdgeHandling mode ){
        this.graph = graph;
        this.walkLength = walkLength;
        this.rng = new Random(seed);
        this.mode = mode;
    }


    @Override
    public List<GraphWalkIterator getGraphWalkIterators(int numIterators) {
        int nVertices = graph.numVertices();
        if(numIterators > nVertices) numIterators = nVertices;

        int verticesPerIter = nVertices / numIterators;

        List<GraphWalkIterator list = new ArrayList<>(numIterators);
        int last = 0;
        for( int i=0; i<numIterators; i++ ){
            int from = last;
            int to = Math.min(nVertices,from+verticesPerIter);
            if(i == numIterators - 1) to = nVertices;

            GraphWalkIterator<V> iter = new RandomWalkIterator(graph,walkLength,rng.nextLong(),mode,from,to);
            list.add(iter);
            last = to;
        }

        return list;
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java RandomWalkGraphIteratorProvider.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.