Search in sources :

Example 16 with NodeIterable

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

the class ConnectedComponents method computeWeaklyConnectedComponents.

public LinkedList<LinkedList<Node>> computeWeaklyConnectedComponents(Graph graph, HashMap<Node, Integer> indices) {
    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 search Q
        NodeIterable iter = graph.getNodes();
        for (Node next : iter) {
            if (color[indices.get(next)] == 0) {
                Q.add(next);
                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);
            color[indices.get(u)] = 2;
            // 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 = indices.get(reachable);
                // If this neighbor is unvisited
                if (color[id] == 0) {
                    // Mark it as used
                    color[id] = 1;
                    // Add it to the search Q
                    Q.addLast(reachable);
                }
            }
            seenCount++;
            Progress.progress(progress, 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 17 with NodeIterable

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

the class ConnectedComponents method top_tarjans.

public LinkedList<LinkedList<Node>> top_tarjans(DirectedGraph graph, HashMap<Node, Integer> indices) {
    LinkedList<LinkedList<Node>> allComponents = new LinkedList<>();
    count = 1;
    stronglyCount = 0;
    int N = graph.getNodeCount();
    int[] index = new int[N];
    int[] low_index = new int[N];
    while (true) {
        // The search Q
        LinkedList<Node> S = new LinkedList<>();
        // The component-list
        // LinkedList<Node> component = new LinkedList<Node>();
        // Seed the seach Q
        Node first = null;
        NodeIterable iter = graph.getNodes();
        for (Node u : iter) {
            if (index[indices.get(u)] == 0) {
                first = u;
                iter.doBreak();
                break;
            }
        }
        if (first == null) {
            return allComponents;
        }
        LinkedList<LinkedList<Node>> components = new LinkedList<>();
        components = tarjans(components, S, graph, first, index, low_index, indices);
        for (LinkedList<Node> component : components) {
            allComponents.add(component);
        }
    }
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) Node(org.gephi.graph.api.Node) LinkedList(java.util.LinkedList)

Example 18 with NodeIterable

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

the class PageRank method calculateInNeighborsPerNode.

private Map<Node, Set<Node>> calculateInNeighborsPerNode(Graph graph, boolean directed) {
    Map<Node, Set<Node>> inNeighborsPerNode = new Object2ObjectOpenHashMap<>();
    NodeIterable nodesIterable = graph.getNodes();
    for (Node node : nodesIterable) {
        Set<Node> nodeInNeighbors = new ObjectOpenHashSet<>();
        EdgeIterable edgesIterable;
        if (directed) {
            edgesIterable = ((DirectedGraph) graph).getInEdges(node);
        } else {
            edgesIterable = graph.getEdges(node);
        }
        for (Edge edge : edgesIterable) {
            if (!edge.isSelfLoop()) {
                Node neighbor = graph.getOpposite(node, edge);
                nodeInNeighbors.add(neighbor);
            }
            if (isCanceled) {
                edgesIterable.doBreak();
                break;
            }
        }
        inNeighborsPerNode.put(node, nodeInNeighbors);
        if (isCanceled) {
            nodesIterable.doBreak();
            break;
        }
    }
    return inNeighborsPerNode;
}
Also used : ObjectOpenHashSet(it.unimi.dsi.fastutil.objects.ObjectOpenHashSet) HashSet(java.util.HashSet) Set(java.util.Set) ObjectOpenHashSet(it.unimi.dsi.fastutil.objects.ObjectOpenHashSet) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) NodeIterable(org.gephi.graph.api.NodeIterable) EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge)

Example 19 with NodeIterable

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

the class PageRank method calculateInWeightPerNodeAndNeighbor.

private Map<Node, Object2DoubleOpenHashMap<Node>> calculateInWeightPerNodeAndNeighbor(Graph graph, boolean directed, boolean useWeights) {
    Object2ObjectOpenHashMap<Node, Object2DoubleOpenHashMap<Node>> inWeightPerNodeAndNeighbor = new Object2ObjectOpenHashMap<>();
    if (useWeights) {
        NodeIterable nodesIterable = graph.getNodes();
        for (Node node : nodesIterable) {
            Object2DoubleOpenHashMap<Node> inWeightPerNeighbor = new Object2DoubleOpenHashMap<>();
            inWeightPerNeighbor.defaultReturnValue(0);
            EdgeIterable edgesIterable;
            if (directed) {
                edgesIterable = ((DirectedGraph) graph).getInEdges(node);
            } else {
                edgesIterable = graph.getEdges(node);
            }
            for (Edge edge : edgesIterable) {
                if (!edge.isSelfLoop()) {
                    Node neighbor = graph.getOpposite(node, edge);
                    inWeightPerNeighbor.addTo(neighbor, edge.getWeight());
                }
                if (isCanceled) {
                    edgesIterable.doBreak();
                    break;
                }
            }
            if (isCanceled) {
                nodesIterable.doBreak();
                break;
            }
            inWeightPerNodeAndNeighbor.put(node, inWeightPerNeighbor);
        }
    }
    return inWeightPerNodeAndNeighbor;
}
Also used : Object2DoubleOpenHashMap(it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) NodeIterable(org.gephi.graph.api.NodeIterable) EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge)

Example 20 with NodeIterable

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

the class PageRank method calculatePagerank.

double[] calculatePagerank(Graph graph, HashMap<Node, Integer> indicies, boolean directed, boolean useWeights, double eps, double prob) {
    int N = graph.getNodeCount();
    double[] pagerankValues = new double[N];
    double[] temp = new double[N];
    Progress.start(progress);
    final double[] weights = useWeights ? new double[N] : null;
    final Map<Node, Set<Node>> inNeighborsPerNode = calculateInNeighborsPerNode(graph, directed);
    final Map<Node, Object2DoubleOpenHashMap<Node>> inWeightPerNodeAndNeighbor = calculateInWeightPerNodeAndNeighbor(graph, directed, useWeights);
    setInitialValues(graph, indicies, pagerankValues, weights, directed, useWeights);
    while (true) {
        boolean done = true;
        double r = calculateR(graph, pagerankValues, indicies, directed, prob);
        NodeIterable nodesIterable = graph.getNodes();
        for (Node s : nodesIterable) {
            int s_index = indicies.get(s);
            temp[s_index] = updateValueForNode(graph, s, pagerankValues, weights, indicies, directed, useWeights, r, prob, inNeighborsPerNode, inWeightPerNodeAndNeighbor.get(s));
            if ((temp[s_index] - pagerankValues[s_index]) / pagerankValues[s_index] >= eps) {
                done = false;
            }
            if (isCanceled) {
                nodesIterable.doBreak();
                return pagerankValues;
            }
        }
        pagerankValues = temp;
        temp = new double[N];
        if ((done) || (isCanceled)) {
            break;
        }
    }
    return pagerankValues;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ObjectOpenHashSet(it.unimi.dsi.fastutil.objects.ObjectOpenHashSet) Object2DoubleOpenHashMap(it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap) NodeIterable(org.gephi.graph.api.NodeIterable) Node(org.gephi.graph.api.Node)

Aggregations

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