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;
}
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);
}
}
}
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;
}
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;
}
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;
}
Aggregations