Skip to content

Commit

Permalink
Polished RandomWalk example, minor README change
Browse files Browse the repository at this point in the history
  • Loading branch information
jcccf committed Jun 28, 2012
1 parent 6f07623 commit cbeaf53
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/ExamplesREADME.md
Expand Up @@ -13,5 +13,5 @@ The ```java``` directory contains the same examples, but written in Java. See ``
in that directory to see how to build HelloGraph.java. As above, you can also run other examples like
```examples/java/build_run_example.sh RandomWalk 10000```.

* Note - as cassovary was designed primarily for Scala users, the Java examples are considerably
more verbose. Improvements on Java interfaces are a work in progress.
* Note - as Cassovary was designed primarily for Scala users, the Java API is less polished. Improvements
on the Java interfaces are a work in progress.
19 changes: 8 additions & 11 deletions examples/java/RandomWalk.java
Expand Up @@ -57,29 +57,26 @@ public static void main(String[] args) {

// 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.currentTimeMillis();
long startTime = System.nanoTime();
Tuple2<Int2IntMap, scala.Option<Int2ObjectMap<Object2IntMap<DirectedPath>>>> lm =
graphUtils.calculatePersonalizedReputation(0, walkParams);
long endTime = System.currentTimeMillis();
long endTime = System.nanoTime();
Int2IntMap neighbors = lm._1;
System.out.printf("Random walk visited %s nodes in %s ms:\n", neighbors.size(), endTime - startTime);
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
// 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());
.immutableSortedCopy(neighbors.keySet()).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");
int count = 0;
for (int id : topNeighbors) {
if (count >= 10) break;
count++;
int numVisits = neighbors.get(id);
System.out.printf("%8s%10s\t", id, numVisits);
if (lm._2.isDefined()) { // If Option is not None
Object2IntMap<DirectedPath> dp = lm._2.get().get(id);
int remaining = dp.size();
for (Map.Entry<DirectedPath, Integer> ef : dp.entrySet()) {
Object2IntMap<DirectedPath> paths = lm._2.get().get(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++) {
Expand Down
Expand Up @@ -211,7 +211,7 @@ object GraphUtils {
* and will likely restart to a start node when visiting the same node twice
* @param dir traverse out-direction or in-direction
* @param stable if true, use a fixed random number generator in the random walk
* @param filterHOmeNodeByNumEdges if true, home node will be checked for maxNumEdgesThresh
* @param filterHomeNodeByNumEdges if true, home node will be checked for maxNumEdgesThresh
* when visited during random walk
*/
case class RandomWalkParams(numSteps: Long,
Expand Down

0 comments on commit cbeaf53

Please sign in to comment.