use of org.gephi.graph.api.Node 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;
}
use of org.gephi.graph.api.Node in project gephi by gephi.
the class ConnectedComponents method createIndiciesMap.
public HashMap<Node, Integer> createIndiciesMap(Graph graph) {
HashMap<Node, Integer> indicies = new HashMap<>();
int index = 0;
for (Node s : graph.getNodes()) {
indicies.put(s, index);
index++;
}
return indicies;
}
use of org.gephi.graph.api.Node 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;
}
use of org.gephi.graph.api.Node in project gephi by gephi.
the class ClusteringCoefficientNGTest method testSpecial2DirectedGraphClusteringCoefficient.
@Test
public void testSpecial2DirectedGraphClusteringCoefficient() {
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
DirectedGraph directedGraph = graphModel.getDirectedGraph();
Node node1 = graphModel.factory().newNode("0");
Node node2 = graphModel.factory().newNode("1");
Node node3 = graphModel.factory().newNode("2");
Node node4 = graphModel.factory().newNode("3");
directedGraph.addNode(node1);
directedGraph.addNode(node2);
directedGraph.addNode(node3);
directedGraph.addNode(node4);
Edge edge21 = graphModel.factory().newEdge(node2, node1);
Edge edge24 = graphModel.factory().newEdge(node2, node4);
Edge edge31 = graphModel.factory().newEdge(node3, node1);
Edge edge32 = graphModel.factory().newEdge(node3, node2);
Edge edge43 = graphModel.factory().newEdge(node4, node3);
directedGraph.addEdge(edge21);
directedGraph.addEdge(edge24);
directedGraph.addEdge(edge31);
directedGraph.addEdge(edge32);
directedGraph.addEdge(edge43);
DirectedGraph graph = graphModel.getDirectedGraph();
ClusteringCoefficient cc = new ClusteringCoefficient();
ArrayWrapper[] network = new ArrayWrapper[4];
int[] triangles = new int[4];
double[] nodeClustering = new double[4];
HashMap<String, Double> results = cc.computeClusteringCoefficient(graph, network, triangles, nodeClustering, true);
double avClusteringCoefficient = results.get("clusteringCoefficient");
double res = 0.4167;
double diff = 0.01;
assertTrue(Math.abs(avClusteringCoefficient - res) < diff);
}
use of org.gephi.graph.api.Node in project gephi by gephi.
the class ConnectedComponentsNGTest method testNullGraphWeeklyConnectedComponents.
@Test
public void testNullGraphWeeklyConnectedComponents() {
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(5);
UndirectedGraph graph = graphModel.getUndirectedGraph();
Node n0 = graph.getNode("0");
Node n1 = graph.getNode("1");
Node n2 = graph.getNode("2");
Node n3 = graph.getNode("3");
Node n4 = graph.getNode("4");
ConnectedComponents c = new ConnectedComponents();
HashMap<Node, Integer> indicies = new HashMap<>();
indicies.put(n0, 0);
indicies.put(n1, 1);
indicies.put(n2, 2);
indicies.put(n3, 3);
indicies.put(n4, 4);
LinkedList<LinkedList<Node>> components = c.computeWeeklyConnectedComponents(graph, indicies);
assertEquals(components.size(), 5);
}
Aggregations