Search in sources :

Example 1 with EdgeIterable

use of org.gephi.graph.api.EdgeIterable in project gephi by gephi.

the class PageRank method setInitialValues.

private void setInitialValues(Graph graph, double[] pagerankValues, double[] weights, boolean directed, boolean useWeights) {
    int N = graph.getNodeCount();
    int index = 0;
    for (Node s : graph.getNodes()) {
        pagerankValues[index] = 1.0f / N;
        if (useWeights) {
            double sum = 0;
            EdgeIterable eIter;
            if (directed) {
                eIter = ((DirectedGraph) graph).getOutEdges(s);
            } else {
                eIter = ((UndirectedGraph) graph).getEdges(s);
            }
            for (Edge edge : eIter) {
                sum += edge.getWeight();
            }
            weights[index] = sum;
        }
        index++;
    }
}
Also used : EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge)

Example 2 with EdgeIterable

use of org.gephi.graph.api.EdgeIterable in project gephi by gephi.

the class ConnectedComponents method computeWeeklyConnectedComponents.

public LinkedList<LinkedList<Node>> computeWeeklyConnectedComponents(Graph graph, HashMap<Node, Integer> indicies) {
    int N = graph.getNodeCount();
    //Keep track of which nodes have been seen
    int[] color = new int[N];
    Progress.start(progress, N);
    int seenCount = 0;
    LinkedList<LinkedList<Node>> components = new LinkedList<>();
    while (seenCount < N) {
        //The search Q
        LinkedList<Node> Q = new LinkedList<>();
        //The component-list
        LinkedList<Node> component = new LinkedList<>();
        //Seed the seach Q
        NodeIterable iter = graph.getNodes();
        for (Node first : iter) {
            if (color[indicies.get(first)] == 0) {
                Q.add(first);
                iter.doBreak();
                break;
            }
        }
        //While there are more nodes to search
        while (!Q.isEmpty()) {
            if (isCanceled) {
                return new LinkedList<>();
            }
            //Get the next Node and add it to the component list
            Node u = Q.removeFirst();
            component.add(u);
            //Iterate over all of u's neighbors
            EdgeIterable edgeIter = graph.getEdges(u);
            //For each neighbor
            for (Edge edge : edgeIter) {
                Node reachable = graph.getOpposite(u, edge);
                int id = indicies.get(reachable);
                //If this neighbor is unvisited
                if (color[id] == 0) {
                    color[id] = 1;
                    //Add it to the search Q
                    Q.addLast(reachable);
                    //Mark it as used 
                    Progress.progress(progress, seenCount);
                }
            }
            color[indicies.get(u)] = 2;
            seenCount++;
        }
        components.add(component);
    }
    return components;
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge) LinkedList(java.util.LinkedList)

Example 3 with EdgeIterable

use of org.gephi.graph.api.EdgeIterable in project gephi by gephi.

the class EigenvectorCentrality method computeMaxValueAndTempValues.

private double computeMaxValueAndTempValues(Graph graph, HashMap<Integer, Node> indicies, HashMap<Node, Integer> invIndicies, double[] tempValues, double[] centralityValues, boolean directed) {
    double max = 0.;
    int N = graph.getNodeCount();
    for (int i = 0; i < N; i++) {
        Node u = indicies.get(i);
        EdgeIterable iter;
        if (directed) {
            iter = ((DirectedGraph) graph).getInEdges(u);
        } else {
            iter = graph.getEdges(u);
        }
        for (Edge e : iter) {
            Node v = graph.getOpposite(u, e);
            Integer id = invIndicies.get(v);
            tempValues[i] += centralityValues[id];
        }
        max = Math.max(max, tempValues[i]);
        if (isCanceled) {
            return max;
        }
    }
    return max;
}
Also used : EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge)

Example 4 with EdgeIterable

use of org.gephi.graph.api.EdgeIterable in project gephi by gephi.

the class Hits method updateAutorithy.

void updateAutorithy(Graph graph, double[] newValues, double[] hubValues, boolean isDirected, Map<Node, Integer> indices) {
    double norm = 0;
    for (Node q : indices.keySet()) {
        double auth = 0;
        EdgeIterable edge_iter;
        if (isDirected) {
            edge_iter = ((DirectedGraph) graph).getInEdges(q);
        } else {
            edge_iter = graph.getEdges(q);
        }
        for (Edge edge : edge_iter) {
            if (!edge.isSelfLoop()) {
                Node p = graph.getOpposite(q, edge);
                auth += hubValues[indices.get(p)];
            }
        }
        newValues[indices.get(q)] = auth;
        norm += auth * auth;
        if (isCanceled) {
            return;
        }
    }
    norm = Math.sqrt(norm);
    if (norm > 0) {
        for (int i = 0; i < newValues.length; i++) {
            newValues[i] = newValues[i] / norm;
        }
    }
}
Also used : EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge)

Example 5 with EdgeIterable

use of org.gephi.graph.api.EdgeIterable in project gephi by gephi.

the class ExporterPajek method exportData.

private void exportData(Graph graph) throws Exception {
    int max = graph.getNodeCount(), i = 1;
    HashMap<String, Integer> idx = new HashMap<>(3 * max / 2 + 1);
    Progress.start(progressTicket, max);
    writer.append("*Vertices " + max + "\n");
    NodeIterable nodeIterable = graph.getNodes();
    for (Node node : nodeIterable) {
        writer.append(Integer.toString(i));
        writer.append(" \"" + node.getLabel() + "\"");
        if (exportPosition) {
            writer.append(" " + node.x() + " " + node.y() + " " + node.z());
        }
        writer.append("\n");
        // assigns Ids from the interval [1..max]
        idx.put(node.getId().toString(), i++);
        if (cancel) {
            nodeIterable.doBreak();
            return;
        }
    }
    if (graph.isUndirected()) {
        writer.append("*Edges\n");
    } else {
        writer.append("*Arcs\n");
    }
    EdgeIterable edgeIterable = graph.getEdges();
    for (Edge edge : edgeIterable) {
        if (cancel) {
            edgeIterable.doBreak();
            return;
        }
        writer.append(Integer.toString(idx.get(edge.getSource().getId().toString())) + " ");
        writer.append(Integer.toString(idx.get(edge.getTarget().getId().toString())));
        if (exportEdgeWeight) {
            writer.append(" " + edge.getWeight());
        }
        writer.append("\n");
        Progress.progress(progressTicket);
    }
    Progress.finish(progressTicket);
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) HashMap(java.util.HashMap) EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge)

Aggregations

Edge (org.gephi.graph.api.Edge)18 EdgeIterable (org.gephi.graph.api.EdgeIterable)18 Node (org.gephi.graph.api.Node)16 NodeIterable (org.gephi.graph.api.NodeIterable)8 LinkedList (java.util.LinkedList)3 Object2ObjectOpenHashMap (it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)2 Column (org.gephi.graph.api.Column)2 Object2DoubleOpenHashMap (it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap)1 ObjectOpenHashSet (it.unimi.dsi.fastutil.objects.ObjectOpenHashSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Element (org.w3c.dom.Element)1