in cassovary-examples/src/main/java/RandomWalkJava.java [34:90]
public static void main(String[] args) {
int numNodes = 3;
if (args.length > 0) numNodes = Integer.parseInt(args[0]);
System.out.printf("Generating a random graph with %d nodes...\n", numNodes);
DirectedGraph graph = TestGraphs.generateRandomGraph(numNodes,
TestGraphs.getProbEdgeRandomDirected(numNodes, Math.min(10, numNodes)),
StoredGraphDir.BothInOut(), new Random());
System.out.printf("Generated a random directed graph with %s nodes and %s edges.\n", graph.nodeCount(),
graph.edgeCount());
// Generate walk parameters
long numSteps = 1000 * 1000;
final scala.Option<Object> wpNone = scala.Option.apply(null);
final scala.Option<Object> wpTwo = scala.Option.apply((Object)2);
RandomWalkParams walkParams = new RandomWalkParams(numSteps, 0.1, wpNone, wpTwo, wpNone, false, GraphDir.OutDir(),
false, false);
GraphUtils graphUtils = new GraphUtils(graph);
// Do the walk and measure how long it took
System.out.printf("Now doing a random walk of %s steps from Node 0...\n", numSteps);
long startTime = System.nanoTime();
Tuple2<scala.collection.Map<Object, Object>, scala.Option<scala.collection.Map<Object,
Object2IntMap<DirectedPath>>>> lm =
graphUtils.calculatePersonalizedReputation(0, walkParams);
long endTime = System.nanoTime();
Map<Integer, Integer> neighbors = scalaIntsMapToJavaMap(lm._1());
System.out.printf("Random walk visited %s nodes in %s ms:\n", neighbors.size(), (endTime - startTime)/1000000);
// Sort neighbors (or nodes) in descending number of visits and take the top 10 neighbors
List<Integer> topNeighbors = Ordering.natural().onResultOf(Functions.forMap(neighbors)).reverse()
.immutableSortedCopy(neighbors.keySet());
if (topNeighbors.size() > 10) topNeighbors = topNeighbors.subList(0, 10);
// Print the top 10 neighbors (and paths)
System.out.printf("%8s%10s\t%s\n", "NodeID", "#Visits", "Top 2 Paths with counts");
for (int id : topNeighbors) {
int numVisits = neighbors.get(id);
System.out.printf("%8s%10s\t", id, numVisits);
if (lm._2().isDefined()) { // If Option is not None
Object2IntMap<DirectedPath> paths = lm._2().get().apply(id);
int remaining = paths.size();
for (Map.Entry<DirectedPath, Integer> ef : paths.entrySet()) {
// Print a directed path and #visits along that path
int[] nodes = ef.getKey().nodes();
for (int i = 0; i < nodes.length; i++) {
if (i != 0) System.out.printf("->%d", nodes[i]);
else System.out.printf("%d", nodes[i]);
}
System.out.printf(" (%d)", ef.getValue());
if (remaining > 1) System.out.printf(" | ");
remaining--;
}
}
System.out.println();
}
}