Commit 9f949bc0 authored by sbalev's avatar sbalev

PageRank implementation

parent fae4e2a3
package org.graphstream.algorithm.test;
import org.graphstream.algorithm.PageRank;
import org.graphstream.algorithm.generator.DorogovtsevMendesGenerator;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
public class DemoPageRank {
public static void main(String[] args) throws InterruptedException {
Graph graph = new SingleGraph("test");
graph.addAttribute("ui.antialias", true);
graph.addAttribute("ui.stylesheet",
"node {fill-color: red; size-mode: dyn-size;} edge {fill-color:grey;}");
graph.display();
DorogovtsevMendesGenerator generator = new DorogovtsevMendesGenerator();
generator.setDirectedEdges(true, true);
generator.addSink(graph);
PageRank pageRank = new PageRank();
pageRank.setVerbose(true);
pageRank.init(graph);
generator.begin();
while (graph.getNodeCount() < 100) {
generator.nextEvents();
for (Node node : graph) {
double rank = pageRank.getRank(node);
node.addAttribute("ui.size",
5 + Math.sqrt(graph.getNodeCount() * rank * 20));
node.addAttribute("ui.label",
String.format("%.2f%%", rank * 100));
}
Thread.sleep(1000);
}
}
}
package org.graphstream.algorithm.test;
import org.graphstream.algorithm.PageRank;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.SingleGraph;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestPageRank {
// Example from the Wikipedia's article
public static Graph toyGraph() {
Graph g = new SingleGraph("test", false, true);
String[] edgeIds = {"BC", "CB", "DA", "DB", "ED", "EB", "EF", "FB", "FE", "GB", "GE", "HB", "HE", "IB", "IE", "JE", "KE"};
for (String id : edgeIds)
g.addEdge(id, id.substring(0, 1), id.substring(1,2), true);
return g;
}
@Test
public void testRank() {
Graph g = toyGraph();
PageRank pr = new PageRank();
pr.init(g);
pr.compute();
assertEquals(3.3, 100 * pr.getRank(g.getNode("A")), 1.0e-1);
assertEquals(38.4, 100 * pr.getRank(g.getNode("B")), 1.0e-1);
assertEquals(34.3, 100 * pr.getRank(g.getNode("C")), 1.0e-1);
assertEquals(3.9, 100 * pr.getRank(g.getNode("D")), 1.0e-1);
assertEquals(8.1, 100 * pr.getRank(g.getNode("E")), 1.0e-1);
assertEquals(3.9, 100 * pr.getRank(g.getNode("F")), 1.0e-1);
assertEquals(1.6, 100 * pr.getRank(g.getNode("G")), 1.0e-1);
assertEquals(1.6, 100 * pr.getRank(g.getNode("H")), 1.0e-1);
assertEquals(1.6, 100 * pr.getRank(g.getNode("I")), 1.0e-1);
assertEquals(1.6, 100 * pr.getRank(g.getNode("J")), 1.0e-1);
assertEquals(1.6, 100 * pr.getRank(g.getNode("K")), 1.0e-1);
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment