Search in sources :

Example 1 with DirectedSparseGraph

use of edu.uci.ics.jung.graph.DirectedSparseGraph in project Cloud9 by lintool.

the class SequentialPersonalizedPageRank method main.

@SuppressWarnings({ "static-access" })
public static void main(String[] args) throws IOException {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT));
    options.addOption(OptionBuilder.withArgName("val").hasArg().withDescription("random jump factor").create(JUMP));
    options.addOption(OptionBuilder.withArgName("node").hasArg().withDescription("source node (i.e., destination of the random jump)").create(SOURCE));
    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }
    if (!cmdline.hasOption(INPUT) || !cmdline.hasOption(SOURCE)) {
        System.out.println("args: " + Arrays.toString(args));
        HelpFormatter formatter = new HelpFormatter();
        formatter.setWidth(120);
        formatter.printHelp(SequentialPersonalizedPageRank.class.getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        System.exit(-1);
    }
    String infile = cmdline.getOptionValue(INPUT);
    final String source = cmdline.getOptionValue(SOURCE);
    float alpha = cmdline.hasOption(JUMP) ? Float.parseFloat(cmdline.getOptionValue(JUMP)) : 0.15f;
    int edgeCnt = 0;
    DirectedSparseGraph<String, Integer> graph = new DirectedSparseGraph<String, Integer>();
    BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream(infile)));
    String line;
    while ((line = data.readLine()) != null) {
        line.trim();
        String[] arr = line.split("\\t");
        for (int i = 1; i < arr.length; i++) {
            graph.addEdge(new Integer(edgeCnt++), arr[0], arr[i]);
        }
    }
    data.close();
    if (!graph.containsVertex(source)) {
        System.err.println("Error: source node not found in the graph!");
        System.exit(-1);
    }
    WeakComponentClusterer<String, Integer> clusterer = new WeakComponentClusterer<String, Integer>();
    Set<Set<String>> components = clusterer.transform(graph);
    int numComponents = components.size();
    System.out.println("Number of components: " + numComponents);
    System.out.println("Number of edges: " + graph.getEdgeCount());
    System.out.println("Number of nodes: " + graph.getVertexCount());
    System.out.println("Random jump factor: " + alpha);
    // Compute personalized PageRank.
    PageRankWithPriors<String, Integer> ranker = new PageRankWithPriors<String, Integer>(graph, new Transformer<String, Double>() {

        @Override
        public Double transform(String vertex) {
            return vertex.equals(source) ? 1.0 : 0;
        }
    }, alpha);
    ranker.evaluate();
    // Use priority queue to sort vertices by PageRank values.
    PriorityQueue<Ranking<String>> q = new PriorityQueue<Ranking<String>>();
    int i = 0;
    for (String pmid : graph.getVertices()) {
        q.add(new Ranking<String>(i++, ranker.getVertexScore(pmid), pmid));
    }
    // Print PageRank values.
    System.out.println("\nPageRank of nodes, in descending order:");
    Ranking<String> r = null;
    while ((r = q.poll()) != null) {
        System.out.println(r.rankScore + "\t" + r.getRanked());
    }
}
Also used : Options(org.apache.commons.cli.Options) Set(java.util.Set) GnuParser(org.apache.commons.cli.GnuParser) PageRankWithPriors(edu.uci.ics.jung.algorithms.scoring.PageRankWithPriors) HelpFormatter(org.apache.commons.cli.HelpFormatter) Ranking(edu.uci.ics.jung.algorithms.importance.Ranking) CommandLineParser(org.apache.commons.cli.CommandLineParser) DirectedSparseGraph(edu.uci.ics.jung.graph.DirectedSparseGraph) InputStreamReader(java.io.InputStreamReader) PriorityQueue(java.util.PriorityQueue) FileInputStream(java.io.FileInputStream) WeakComponentClusterer(edu.uci.ics.jung.algorithms.cluster.WeakComponentClusterer) CommandLine(org.apache.commons.cli.CommandLine) BufferedReader(java.io.BufferedReader) ParseException(org.apache.commons.cli.ParseException)

Example 2 with DirectedSparseGraph

use of edu.uci.ics.jung.graph.DirectedSparseGraph in project Cloud9 by lintool.

the class SequentialPageRank method main.

@SuppressWarnings({ "static-access" })
public static void main(String[] args) throws IOException {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT));
    options.addOption(OptionBuilder.withArgName("val").hasArg().withDescription("random jump factor").create(JUMP));
    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }
    if (!cmdline.hasOption(INPUT)) {
        System.out.println("args: " + Arrays.toString(args));
        HelpFormatter formatter = new HelpFormatter();
        formatter.setWidth(120);
        formatter.printHelp(SequentialPageRank.class.getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        System.exit(-1);
    }
    String infile = cmdline.getOptionValue(INPUT);
    float alpha = cmdline.hasOption(JUMP) ? Float.parseFloat(cmdline.getOptionValue(JUMP)) : 0.15f;
    int edgeCnt = 0;
    DirectedSparseGraph<String, Integer> graph = new DirectedSparseGraph<String, Integer>();
    BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream(infile)));
    String line;
    while ((line = data.readLine()) != null) {
        line.trim();
        String[] arr = line.split("\\t");
        for (int i = 1; i < arr.length; i++) {
            graph.addEdge(new Integer(edgeCnt++), arr[0], arr[i]);
        }
    }
    data.close();
    WeakComponentClusterer<String, Integer> clusterer = new WeakComponentClusterer<String, Integer>();
    Set<Set<String>> components = clusterer.transform(graph);
    int numComponents = components.size();
    System.out.println("Number of components: " + numComponents);
    System.out.println("Number of edges: " + graph.getEdgeCount());
    System.out.println("Number of nodes: " + graph.getVertexCount());
    System.out.println("Random jump factor: " + alpha);
    // Compute PageRank.
    PageRank<String, Integer> ranker = new PageRank<String, Integer>(graph, alpha);
    ranker.evaluate();
    // Use priority queue to sort vertices by PageRank values.
    PriorityQueue<Ranking<String>> q = new PriorityQueue<Ranking<String>>();
    int i = 0;
    for (String pmid : graph.getVertices()) {
        q.add(new Ranking<String>(i++, ranker.getVertexScore(pmid), pmid));
    }
    // Print PageRank values.
    System.out.println("\nPageRank of nodes, in descending order:");
    Ranking<String> r = null;
    while ((r = q.poll()) != null) {
        System.out.println(r.rankScore + "\t" + r.getRanked());
    }
}
Also used : Options(org.apache.commons.cli.Options) Set(java.util.Set) GnuParser(org.apache.commons.cli.GnuParser) HelpFormatter(org.apache.commons.cli.HelpFormatter) Ranking(edu.uci.ics.jung.algorithms.importance.Ranking) CommandLineParser(org.apache.commons.cli.CommandLineParser) DirectedSparseGraph(edu.uci.ics.jung.graph.DirectedSparseGraph) InputStreamReader(java.io.InputStreamReader) PageRank(edu.uci.ics.jung.algorithms.scoring.PageRank) PriorityQueue(java.util.PriorityQueue) FileInputStream(java.io.FileInputStream) WeakComponentClusterer(edu.uci.ics.jung.algorithms.cluster.WeakComponentClusterer) CommandLine(org.apache.commons.cli.CommandLine) BufferedReader(java.io.BufferedReader) ParseException(org.apache.commons.cli.ParseException)

Example 3 with DirectedSparseGraph

use of edu.uci.ics.jung.graph.DirectedSparseGraph in project opennms by OpenNMS.

the class BreadcrumbPathCalculator method getIncomingEdgeMap.

static Map<VertexRef, EdgeRef> getIncomingEdgeMap(TopologyServiceClient topologyServiceClient) {
    // Convert to JUNG graph
    // We build one big graph out of all graph providers in order to determine the shortest path between each vertex
    // when we want to calculate the SHORTEST_PATH_TO_ROOT
    final DirectedSparseGraph<VertexRef, EdgeRef> sparseGraph = new DirectedSparseGraph<>();
    topologyServiceClient.getGraphProviders().forEach(eachGraph -> {
        for (Vertex eachVertex : eachGraph.getVertices(new IgnoreHopCriteria())) {
            sparseGraph.addVertex(eachVertex);
        }
        for (EdgeRef eachEdge : eachGraph.getEdges()) {
            sparseGraph.addEdge(eachEdge, ((Edge) eachEdge).getSource().getVertex(), ((Edge) eachEdge).getTarget().getVertex());
        }
    });
    // Link the layers
    final IdGenerator idGenerator = new IdGenerator();
    sparseGraph.getVertices().forEach(eachVertex -> {
        topologyServiceClient.getOppositeVertices(eachVertex).forEach(oppositeVertex -> {
            sparseGraph.addEdge(new AbstractEdge("$$outer-space$$", "" + idGenerator.nextId(), eachVertex, oppositeVertex), eachVertex, oppositeVertex);
        });
    });
    // Create dummy root
    sparseGraph.addVertex(rootVertex);
    for (Vertex eachVertex : topologyServiceClient.getDefaultGraphProvider().getVertices(new IgnoreHopCriteria())) {
        sparseGraph.addEdge(new AbstractEdge("$$outer-space$$", "" + idGenerator.nextId(), rootVertex, eachVertex), rootVertex, eachVertex);
    }
    // Build shortest path for graph
    final UnweightedShortestPath<VertexRef, EdgeRef> shortestPath = new UnweightedShortestPath<>(sparseGraph);
    Map<VertexRef, EdgeRef> incomingEdgeMap = shortestPath.getIncomingEdgeMap(rootVertex);
    return incomingEdgeMap;
}
Also used : Vertex(org.opennms.features.topology.api.topo.Vertex) AbstractVertex(org.opennms.features.topology.api.topo.AbstractVertex) AbstractEdge(org.opennms.features.topology.api.topo.AbstractEdge) IgnoreHopCriteria(org.opennms.features.topology.api.support.IgnoreHopCriteria) UnweightedShortestPath(edu.uci.ics.jung.algorithms.shortestpath.UnweightedShortestPath) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge) AbstractEdge(org.opennms.features.topology.api.topo.AbstractEdge) DirectedSparseGraph(edu.uci.ics.jung.graph.DirectedSparseGraph)

Aggregations

DirectedSparseGraph (edu.uci.ics.jung.graph.DirectedSparseGraph)3 WeakComponentClusterer (edu.uci.ics.jung.algorithms.cluster.WeakComponentClusterer)2 Ranking (edu.uci.ics.jung.algorithms.importance.Ranking)2 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 PriorityQueue (java.util.PriorityQueue)2 Set (java.util.Set)2 CommandLine (org.apache.commons.cli.CommandLine)2 CommandLineParser (org.apache.commons.cli.CommandLineParser)2 GnuParser (org.apache.commons.cli.GnuParser)2 HelpFormatter (org.apache.commons.cli.HelpFormatter)2 Options (org.apache.commons.cli.Options)2 ParseException (org.apache.commons.cli.ParseException)2 PageRank (edu.uci.ics.jung.algorithms.scoring.PageRank)1 PageRankWithPriors (edu.uci.ics.jung.algorithms.scoring.PageRankWithPriors)1 UnweightedShortestPath (edu.uci.ics.jung.algorithms.shortestpath.UnweightedShortestPath)1 IgnoreHopCriteria (org.opennms.features.topology.api.support.IgnoreHopCriteria)1 AbstractEdge (org.opennms.features.topology.api.topo.AbstractEdge)1 AbstractVertex (org.opennms.features.topology.api.topo.AbstractVertex)1