Merge and update to new API
Esse commit está contido em:
+284
-170
@@ -234,7 +234,7 @@ public class ClusteringCoefficient implements Statistics, LongTask {
|
||||
isDirected = graphController.getGraphModel().isDirected();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public double getAverageClusteringCoefficient() {
|
||||
return avgClusteringCoeff;
|
||||
}
|
||||
@@ -256,10 +256,16 @@ public class ClusteringCoefficient implements Statistics, LongTask {
|
||||
public void execute(Graph hgraph, AttributeModel attributeModel) {
|
||||
isCanceled = false;
|
||||
|
||||
HashMap<String, Double> resultValues = new HashMap<String, Double>();
|
||||
|
||||
if (isDirected) {
|
||||
bruteForce(hgraph, attributeModel);
|
||||
avgClusteringCoeff = bruteForce(hgraph, attributeModel);
|
||||
} else {
|
||||
triangles(hgraph);
|
||||
initStartValues(hgraph);
|
||||
resultValues = computeTriangles(hgraph, network, triangles, nodeClustering, isDirected);
|
||||
totalTriangles = resultValues.get("triangles").intValue();
|
||||
avgClusteringCoeff = resultValues.get("clusteringCoefficient");
|
||||
|
||||
}
|
||||
|
||||
//Set results in columns
|
||||
@@ -287,36 +293,79 @@ public class ClusteringCoefficient implements Statistics, LongTask {
|
||||
}
|
||||
}
|
||||
|
||||
private int closest_in_array(int v) {
|
||||
int right = network[v].length() - 1;
|
||||
public void triangles(Graph hgraph) {
|
||||
initStartValues(hgraph);
|
||||
HashMap<String, Double> resultValues = computeTriangles(hgraph, network, triangles,
|
||||
nodeClustering, isDirected);
|
||||
totalTriangles = resultValues.get("triangles").intValue();
|
||||
avgClusteringCoeff = resultValues.get("clusteringCoefficient");
|
||||
}
|
||||
|
||||
public HashMap<String, Double> computeClusteringCoefficient(Graph hgraph, ArrayWrapper[] currentNetwork,
|
||||
int[] currentTriangles, double[] currentNodeClustering, boolean directed) {
|
||||
HashMap<String, Double> resultValues = new HashMap<String, Double>();
|
||||
|
||||
if (isDirected) {
|
||||
double avClusteringCoefficient = bruteForce(hgraph, null);
|
||||
resultValues.put("clusteringCoefficient", avClusteringCoefficient);
|
||||
return resultValues;
|
||||
} else {
|
||||
initStartValues(hgraph);
|
||||
resultValues = computeTriangles(hgraph, currentNetwork, currentTriangles, currentNodeClustering, directed);
|
||||
return resultValues;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void initStartValues(Graph hgraph) {
|
||||
N = hgraph.getNodeCount();
|
||||
K = (int) Math.sqrt(N);
|
||||
nodeClustering = new double[N];
|
||||
network = new ArrayWrapper[N];
|
||||
triangles = new int[N];
|
||||
}
|
||||
|
||||
public int createIndiciesMapAndInitNetwork(Graph hgraph, HashMap<Node, Integer> indicies, ArrayWrapper[] networks, int currentProgress) {
|
||||
int index = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
indicies.put(s, index);
|
||||
networks[index] = new ArrayWrapper();
|
||||
index++;
|
||||
Progress.progress(progress, ++currentProgress);
|
||||
}
|
||||
return currentProgress;
|
||||
}
|
||||
|
||||
private int closest_in_array(ArrayWrapper[] currentNetwork, int v) {
|
||||
int right = currentNetwork[v].length() - 1;
|
||||
|
||||
/* optimization for extreme cases */
|
||||
if (right < 0) {
|
||||
return (-1);
|
||||
}
|
||||
if (network[v].get(0) >= v) {
|
||||
if (currentNetwork[v].get(0) >= v) {
|
||||
return (-1);
|
||||
}
|
||||
if (network[v].get(right) < v) {
|
||||
if (currentNetwork[v].get(right) < v) {
|
||||
return (right);
|
||||
}
|
||||
if (network[v].get(right) == v) {
|
||||
if (currentNetwork[v].get(right) == v) {
|
||||
return (right - 1);
|
||||
}
|
||||
|
||||
int left = 0, mid;
|
||||
while (right > left) {
|
||||
mid = (left + right) / 2;
|
||||
if (v < network[v].get(mid)) {
|
||||
if (v < currentNetwork[v].get(mid)) {
|
||||
right = mid - 1;
|
||||
} else if (v > network[v].get(mid)) {
|
||||
} else if (v > currentNetwork[v].get(mid)) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
return (mid - 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (v > network[v].get(right)) {
|
||||
if (v > currentNetwork[v].get(right)) {
|
||||
return (right);
|
||||
} else {
|
||||
|
||||
@@ -328,38 +377,38 @@ public class ClusteringCoefficient implements Statistics, LongTask {
|
||||
*
|
||||
* @param v - The specific node to count the triangles on.
|
||||
*/
|
||||
private void newVertex(int v) {
|
||||
int[] A = new int[N];
|
||||
private void newVertex(ArrayWrapper[] currentNetwork, int[] currentTrianlgles, int v, int n) {
|
||||
int[] A = new int[n];
|
||||
|
||||
for (int i = network[v].length() - 1; (i >= 0) && (network[v].get(i) > v); i--) {
|
||||
int neighbor = network[v].get(i);
|
||||
A[neighbor] = network[v].getCount(i);
|
||||
for (int i = currentNetwork[v].length() - 1; (i >= 0) && (currentNetwork[v].get(i) > v); i--) {
|
||||
int neighbor = currentNetwork[v].get(i);
|
||||
A[neighbor] = currentNetwork[v].getCount(i);
|
||||
}
|
||||
for (int i = network[v].length() - 1; i >= 0; i--) {
|
||||
int neighbor = network[v].get(i);
|
||||
for (int j = closest_in_array(neighbor); j >= 0; j--) {
|
||||
int next = network[neighbor].get(j);
|
||||
for (int i = currentNetwork[v].length() - 1; i >= 0; i--) {
|
||||
int neighbor = currentNetwork[v].get(i);
|
||||
for (int j = closest_in_array(currentNetwork, neighbor); j >= 0; j--) {
|
||||
int next = currentNetwork[neighbor].get(j);
|
||||
if (A[next] > 0) {
|
||||
triangles[next] += network[v].getCount(i);
|
||||
triangles[v] += network[v].getCount(i);
|
||||
triangles[neighbor] += A[next];
|
||||
currentTrianlgles[next] += currentNetwork[v].getCount(i);
|
||||
currentTrianlgles[v] += currentNetwork[v].getCount(i);
|
||||
currentTrianlgles[neighbor] += A[next];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void tr_link_nohigh(int u, int v, int count) {
|
||||
private void tr_link_nohigh(ArrayWrapper[] currentNetwork, int[] currentTriangles, int u, int v, int count, int k) {
|
||||
int iu = 0, iv = 0, w;
|
||||
while ((iu < network[u].length()) && (iv < network[v].length())) {
|
||||
if (network[u].get(iu) < network[v].get(iv)) {
|
||||
while ((iu < currentNetwork[u].length()) && (iv < currentNetwork[v].length())) {
|
||||
if (currentNetwork[u].get(iu) < currentNetwork[v].get(iv)) {
|
||||
iu++;
|
||||
} else if (network[u].get(iu) > network[v].get(iv)) {
|
||||
} else if (currentNetwork[u].get(iu) > currentNetwork[v].get(iv)) {
|
||||
iv++;
|
||||
} else { /* neighbor in common */
|
||||
|
||||
w = network[u].get(iu);
|
||||
if (w >= K) {
|
||||
triangles[w] += count;
|
||||
w = currentNetwork[u].get(iu);
|
||||
if (w >= k) {
|
||||
currentTriangles[w] += count;
|
||||
}
|
||||
iu++;
|
||||
iv++;
|
||||
@@ -367,147 +416,170 @@ public class ClusteringCoefficient implements Statistics, LongTask {
|
||||
}
|
||||
}
|
||||
|
||||
public void triangles(Graph hgraph) {
|
||||
private HashMap<Node, EdgeWrapper> createNeighbourTable(Graph hgraph, Node node, HashMap<Node, Integer> indicies,
|
||||
ArrayWrapper[] networks, boolean directed) {
|
||||
|
||||
HashMap<Node, EdgeWrapper> neighborTable = new HashMap<Node, EdgeWrapper>();
|
||||
|
||||
if (!directed) {
|
||||
for (Edge edge : hgraph.getEdges(node)) {
|
||||
Node neighbor = hgraph.getOpposite(node, edge);
|
||||
neighborTable.put(neighbor, new EdgeWrapper(1, networks[indicies.get(neighbor)]));
|
||||
}
|
||||
} else {
|
||||
for (Node neighbor : ((DirectedGraph) hgraph).getPredecessors(node)) {
|
||||
neighborTable.put(neighbor, new EdgeWrapper(1, networks[indicies.get(neighbor)]));
|
||||
}
|
||||
|
||||
for (Edge out : ((DirectedGraph) hgraph).getOutEdges(node)) {
|
||||
Node neighbor = out.getTarget();
|
||||
EdgeWrapper ew = neighborTable.get(neighbor);
|
||||
if (ew == null) {
|
||||
neighborTable.put(neighbor, new EdgeWrapper(1, network[indicies.get(neighbor)]));
|
||||
} else {
|
||||
ew.count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return neighborTable;
|
||||
}
|
||||
|
||||
private EdgeWrapper[] getEdges(HashMap<Node, EdgeWrapper> neighborTable) {
|
||||
|
||||
int i = 0;
|
||||
EdgeWrapper[] edges = new EdgeWrapper[neighborTable.size()];
|
||||
for (EdgeWrapper e : neighborTable.values()) {
|
||||
edges[i] = e;
|
||||
i++;
|
||||
}
|
||||
return edges;
|
||||
}
|
||||
|
||||
private int processNetwork(ArrayWrapper[] currentNetwork, int currentProgress) {
|
||||
Arrays.sort(currentNetwork);
|
||||
for (int j = 0; j < N; j++) {
|
||||
currentNetwork[j].setID(j);
|
||||
Progress.progress(progress, ++currentProgress);
|
||||
}
|
||||
|
||||
for (int j = 0; j < N; j++) {
|
||||
Arrays.sort(currentNetwork[j].getArray(), new Renumbering());
|
||||
Progress.progress(progress, ++currentProgress);
|
||||
}
|
||||
return currentProgress;
|
||||
}
|
||||
|
||||
private int computeRemainingTrianles(Graph hgraph, ArrayWrapper[] currentNetwork, int[] currentTriangles, int currentProgress) {
|
||||
int n = hgraph.getNodeCount();
|
||||
int k = (int) Math.sqrt(n);
|
||||
for (int v = n - 1; (v >= 0) && (v >= k); v--) {
|
||||
for (int i = closest_in_array(currentNetwork, v); i >= 0; i--) {
|
||||
int u = currentNetwork[v].get(i);
|
||||
if (u >= k) {
|
||||
tr_link_nohigh(currentNetwork, currentTriangles, u, v, currentNetwork[v].getCount(i), k);
|
||||
}
|
||||
}
|
||||
Progress.progress(progress, ++currentProgress);
|
||||
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return currentProgress;
|
||||
}
|
||||
}
|
||||
return currentProgress;
|
||||
}
|
||||
|
||||
private HashMap<String, Double> computeResultValues(Graph hgraph, ArrayWrapper[] currentNetwork,
|
||||
int[] currentTriangles, double[] currentNodeClusterig, boolean directed, int currentProgress) {
|
||||
int n = hgraph.getNodeCount();
|
||||
HashMap<String, Double> totalValues = new HashMap<String, Double>();
|
||||
int numNodesDegreeGreaterThanOne = 0;
|
||||
int trianglesNumber = 0;
|
||||
double currentClusteringCoefficient = 0;
|
||||
for (int v = 0; v < n; v++) {
|
||||
if (currentNetwork[v].length() > 1) {
|
||||
numNodesDegreeGreaterThanOne++;
|
||||
double cc = currentTriangles[v];
|
||||
trianglesNumber += currentTriangles[v];
|
||||
cc /= (currentNetwork[v].length() * (currentNetwork[v].length() - 1));
|
||||
if (!directed) {
|
||||
cc *= 2.0f;
|
||||
}
|
||||
currentNodeClusterig[v] = cc;
|
||||
currentClusteringCoefficient += cc;
|
||||
}
|
||||
Progress.progress(progress, ++currentProgress);
|
||||
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return totalValues;
|
||||
}
|
||||
}
|
||||
trianglesNumber /= 3;
|
||||
currentClusteringCoefficient /= numNodesDegreeGreaterThanOne;
|
||||
|
||||
totalValues.put("triangles", (double) trianglesNumber);
|
||||
totalValues.put("clusteringCoefficient", currentClusteringCoefficient);
|
||||
return totalValues;
|
||||
}
|
||||
|
||||
private HashMap<String, Double> computeTriangles(Graph hgraph, ArrayWrapper[] currentNetwork, int[] currentTriangles,
|
||||
double[] nodeClustering, boolean directed) {
|
||||
|
||||
HashMap<String, Double> resultValues = new HashMap<String, Double>();
|
||||
int ProgressCount = 0;
|
||||
Progress.start(progress, 7 * hgraph.getNodeCount());
|
||||
|
||||
hgraph.readLock();
|
||||
|
||||
N = hgraph.getNodeCount();
|
||||
nodeClustering = new double[N];
|
||||
int n = hgraph.getNodeCount();
|
||||
|
||||
/**
|
||||
* Create network for processing
|
||||
*/
|
||||
network = new ArrayWrapper[N];
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
* */
|
||||
HashMap<Node, Integer> indicies = new HashMap<Node, Integer>();
|
||||
|
||||
ProgressCount = createIndiciesMapAndInitNetwork(hgraph, indicies, currentNetwork, ProgressCount);
|
||||
|
||||
int index = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
indicies.put(s, index);
|
||||
network[index] = new ArrayWrapper();
|
||||
index++;
|
||||
Progress.progress(progress, ++ProgressCount);
|
||||
}
|
||||
|
||||
index = 0;
|
||||
for (Node node : hgraph.getNodes()) {
|
||||
HashMap<Node, EdgeWrapper> neighborTable = new HashMap<Node, EdgeWrapper>();
|
||||
HashMap<Node, EdgeWrapper> neighborTable = createNeighbourTable(hgraph, node, indicies, currentNetwork, directed);
|
||||
|
||||
if (!isDirected) {
|
||||
for (Edge edge : hgraph.getEdges(node)) {
|
||||
Node neighbor = hgraph.getOpposite(node, edge);
|
||||
neighborTable.put(neighbor, new EdgeWrapper(1, network[indicies.get(neighbor)]));
|
||||
}
|
||||
} else {
|
||||
for (Edge in : ((DirectedGraph) hgraph).getInEdges(node)) {
|
||||
Node neighbor = in.getSource();
|
||||
neighborTable.put(neighbor, new EdgeWrapper(1, network[indicies.get(neighbor)]));
|
||||
}
|
||||
|
||||
for (Edge out : ((DirectedGraph) hgraph).getOutEdges(node)) {
|
||||
Node neighbor = out.getTarget();
|
||||
EdgeWrapper ew = neighborTable.get(neighbor);
|
||||
if (ew == null) {
|
||||
neighborTable.put(neighbor, new EdgeWrapper(1, network[indicies.get(neighbor)]));
|
||||
} else {
|
||||
ew.count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EdgeWrapper[] edges = new EdgeWrapper[neighborTable.size()];
|
||||
int i = 0;
|
||||
for (EdgeWrapper e : neighborTable.values()) {
|
||||
edges[i] = e;
|
||||
i++;
|
||||
}
|
||||
network[index].node = node;
|
||||
network[index].setArray(edges);
|
||||
EdgeWrapper[] edges = getEdges(neighborTable);
|
||||
currentNetwork[index].node = node;
|
||||
currentNetwork[index].setArray(edges);
|
||||
index++;
|
||||
Progress.progress(progress, ++ProgressCount);
|
||||
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
return resultValues;
|
||||
}
|
||||
}
|
||||
|
||||
Arrays.sort(network);
|
||||
for (int j = 0; j < N; j++) {
|
||||
network[j].setID(j);
|
||||
Progress.progress(progress, ++ProgressCount);
|
||||
}
|
||||
ProgressCount = processNetwork(currentNetwork, ProgressCount);
|
||||
|
||||
for (int j = 0; j < N; j++) {
|
||||
Arrays.sort(network[j].getArray(), new Renumbering());
|
||||
Progress.progress(progress, ++ProgressCount);
|
||||
}
|
||||
int k = (int) Math.sqrt(n);
|
||||
|
||||
triangles = new int[N];
|
||||
K = (int) Math.sqrt(N);
|
||||
|
||||
for (int v = 0; v < K && v < N; v++) {
|
||||
newVertex(v);
|
||||
for (int v = 0; v < k && v < n; v++) {
|
||||
newVertex(currentNetwork, currentTriangles, v, n);
|
||||
Progress.progress(progress, ++ProgressCount);
|
||||
}
|
||||
|
||||
/* remaining links */
|
||||
for (int v = N - 1; (v >= 0) && (v >= K); v--) {
|
||||
for (int i = closest_in_array(v); i >= 0; i--) {
|
||||
int u = network[v].get(i);
|
||||
if (u >= K) {
|
||||
tr_link_nohigh(u, v, network[v].getCount(i));
|
||||
}
|
||||
}
|
||||
Progress.progress(progress, ++ProgressCount);
|
||||
ProgressCount = computeRemainingTrianles(hgraph, currentNetwork, currentTriangles, ProgressCount);
|
||||
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//Results and average
|
||||
avgClusteringCoeff = 0;
|
||||
totalTriangles = 0;
|
||||
int numNodesDegreeGreaterThanOne = 0;
|
||||
for (int v = 0; v < N; v++) {
|
||||
if (network[v].length() > 1) {
|
||||
numNodesDegreeGreaterThanOne++;
|
||||
double cc = triangles[v];
|
||||
totalTriangles += triangles[v];
|
||||
cc /= (network[v].length() * (network[v].length() - 1));
|
||||
if (!isDirected) {
|
||||
cc *= 2.0f;
|
||||
}
|
||||
nodeClustering[v] = cc;
|
||||
avgClusteringCoeff += cc;
|
||||
}
|
||||
Progress.progress(progress, ++ProgressCount);
|
||||
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
}
|
||||
}
|
||||
totalTriangles /= 3;
|
||||
avgClusteringCoeff /= numNodesDegreeGreaterThanOne;
|
||||
resultValues = computeResultValues(hgraph, currentNetwork, currentTriangles, nodeClustering, directed, ProgressCount);
|
||||
|
||||
hgraph.readUnlock();
|
||||
return resultValues;
|
||||
}
|
||||
|
||||
private void bruteForce(Graph hgraph, AttributeModel attributeModel) {
|
||||
private double bruteForce(Graph hgraph, AttributeModel attributeModel) {
|
||||
//The atrributes computed by the statistics
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column clusteringCol = nodeTable.getColumn("clustering");
|
||||
if (clusteringCol == null) {
|
||||
clusteringCol = nodeTable.addColumn("clustering", "Clustering Coefficient", Double.class, new Double(0));
|
||||
}
|
||||
Column clusteringColumn = initializeAttributeColunms(attributeModel);
|
||||
|
||||
float totalCC = 0;
|
||||
|
||||
@@ -516,42 +588,13 @@ public class ClusteringCoefficient implements Statistics, LongTask {
|
||||
Progress.start(progress, hgraph.getNodeCount());
|
||||
int node_count = 0;
|
||||
for (Node node : hgraph.getNodes()) {
|
||||
float nodeCC = 0;
|
||||
int neighborhood = 0;
|
||||
NodeIterable neighbors1 = hgraph.getNeighbors(node);
|
||||
for (Node neighbor1 : neighbors1) {
|
||||
neighborhood++;
|
||||
NodeIterable neighbors2 = hgraph.getNeighbors(node);
|
||||
for (Node neighbor2 : neighbors2) {
|
||||
float nodeClusteringCoefficient = computeNodeClusteringCoefficient(hgraph, node, isDirected);
|
||||
|
||||
if (neighbor1 == neighbor2) {
|
||||
continue;
|
||||
}
|
||||
if (isDirected) {
|
||||
if (hgraph.isAdjacent(neighbor1, neighbor2)) {
|
||||
nodeCC++;
|
||||
}
|
||||
if (hgraph.isAdjacent(neighbor2, neighbor1)) {
|
||||
nodeCC++;
|
||||
}
|
||||
} else {
|
||||
if (hgraph.isAdjacent(neighbor1, neighbor2)) {
|
||||
nodeCC++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
nodeCC /= 2.0;
|
||||
if (nodeClusteringCoefficient > -1) {
|
||||
|
||||
if (neighborhood > 1) {
|
||||
float cc = nodeCC / (.5f * neighborhood * (neighborhood - 1));
|
||||
if (isDirected) {
|
||||
cc = nodeCC / (neighborhood * (neighborhood - 1));
|
||||
}
|
||||
saveCalculatedValue(node, clusteringColumn, nodeClusteringCoefficient);
|
||||
|
||||
node.setAttribute(clusteringCol, cc);
|
||||
|
||||
totalCC += cc;
|
||||
totalCC += nodeClusteringCoefficient;
|
||||
}
|
||||
|
||||
if (isCanceled) {
|
||||
@@ -562,9 +605,80 @@ public class ClusteringCoefficient implements Statistics, LongTask {
|
||||
Progress.progress(progress, node_count);
|
||||
|
||||
}
|
||||
avgClusteringCoeff = totalCC / hgraph.getNodeCount();
|
||||
double clusteringCoeff = totalCC / hgraph.getNodeCount();
|
||||
|
||||
hgraph.readUnlockAll();
|
||||
|
||||
return clusteringCoeff;
|
||||
}
|
||||
|
||||
private float increaseCCifNesessary(Graph hgraph, Node neighbor1, Node neighbor2, boolean directed, float nodeCC) {
|
||||
if (neighbor1 == neighbor2) {
|
||||
return nodeCC;
|
||||
}
|
||||
if (directed) {
|
||||
if (hgraph.isAdjacent(neighbor1, neighbor2)) {
|
||||
nodeCC++;
|
||||
}
|
||||
if (hgraph.isAdjacent(neighbor2, neighbor1)) {
|
||||
nodeCC++;
|
||||
}
|
||||
} else {
|
||||
if (hgraph.isAdjacent(neighbor1, neighbor2)) {
|
||||
nodeCC++;
|
||||
}
|
||||
}
|
||||
return nodeCC;
|
||||
}
|
||||
|
||||
private float computeNodeClusteringCoefficient(Graph hgraph, Node node, boolean directed) {
|
||||
float nodeCC = 0;
|
||||
int neighborhood = 0;
|
||||
NodeIterable neighbors1 = hgraph.getNeighbors(node);
|
||||
for (Node neighbor1 : neighbors1) {
|
||||
neighborhood++;
|
||||
NodeIterable neighbors2 = hgraph.getNeighbors(node);
|
||||
|
||||
for (Node neighbor2 : neighbors2) {
|
||||
nodeCC = increaseCCifNesessary(hgraph, neighbor1, neighbor2, directed, nodeCC);
|
||||
}
|
||||
}
|
||||
nodeCC /= 2.0;
|
||||
|
||||
if (neighborhood > 1) {
|
||||
float cc = nodeCC / (.5f * neighborhood * (neighborhood - 1));
|
||||
if (directed) {
|
||||
cc = nodeCC / (neighborhood * (neighborhood - 1));
|
||||
}
|
||||
|
||||
return cc;
|
||||
} else {
|
||||
return -1.f;
|
||||
}
|
||||
}
|
||||
|
||||
private Column initializeAttributeColunms(AttributeModel attributeModel) {
|
||||
|
||||
if (attributeModel == null) {
|
||||
return null;
|
||||
}
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column clusteringCol = nodeTable.getColumn("clustering");
|
||||
|
||||
if (clusteringCol == null) {
|
||||
clusteringCol = nodeTable.addColumn("clustering", "Clustering Coefficient", Double.class, new Double(0));
|
||||
}
|
||||
|
||||
return clusteringCol;
|
||||
}
|
||||
|
||||
private void saveCalculatedValue(Node node, Column clusteringColumn,
|
||||
float nodeClusteringCoefficient) {
|
||||
|
||||
if (clusteringColumn == null) {
|
||||
return;
|
||||
}
|
||||
node.setAttribute(clusteringColumn, nodeClusteringCoefficient);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+110
-46
@@ -43,10 +43,8 @@ package org.gephi.statistics.plugin;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.gephi.attribute.api.AttributeModel;
|
||||
import org.gephi.attribute.api.Column;
|
||||
@@ -54,6 +52,7 @@ import org.gephi.attribute.api.Table;
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.EdgeIterable;
|
||||
import org.gephi.graph.api.Graph;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
@@ -92,48 +91,53 @@ public class ConnectedComponents implements Statistics, LongTask {
|
||||
isDirected = graphController.getGraphModel().isDirected();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(GraphModel graphModel, AttributeModel attributeModel) {
|
||||
isDirected = graphModel.isDirected();
|
||||
isCanceled = false;
|
||||
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraphVisible();
|
||||
|
||||
undirectedGraph.readLock();
|
||||
|
||||
weaklyConnected(undirectedGraph, attributeModel);
|
||||
if (isDirected) {
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraphVisible();
|
||||
top_tarjans(directedGraph, attributeModel);
|
||||
stronglyConnected(directedGraph, attributeModel);
|
||||
}
|
||||
|
||||
undirectedGraph.readUnlock();
|
||||
}
|
||||
|
||||
public void weaklyConnected(UndirectedGraph graph, AttributeModel attributeModel) {
|
||||
isCanceled = false;
|
||||
componentCount = 0;
|
||||
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column componentCol = nodeTable.getColumn(WEAKLY);
|
||||
if (componentCol == null) {
|
||||
componentCol = nodeTable.addColumn(WEAKLY, "Component ID", Integer.class, new Integer(0));
|
||||
}
|
||||
Column componentCol = initializeWeeklyConnectedColumn(attributeModel);
|
||||
|
||||
List<Integer> sizeList = new ArrayList<Integer>();
|
||||
HashMap<Node, Integer> indicies = createIndiciesMap(graph);
|
||||
|
||||
graph.readLock();
|
||||
LinkedList<LinkedList<Node>> components = computeWeeklyConnectedComponents(graph, indicies);
|
||||
|
||||
HashMap<Node, Integer> indicies = new HashMap<Node, Integer>();
|
||||
int index = 0;
|
||||
for (Node s : graph.getNodes()) {
|
||||
indicies.put(s, index);
|
||||
index++;
|
||||
}
|
||||
saveComputedComponents(components, componentCol);
|
||||
|
||||
fillComponentSizeList(components);
|
||||
|
||||
componentCount = components.size();
|
||||
}
|
||||
|
||||
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, graph.getNodeCount());
|
||||
Progress.start(progress, N);
|
||||
|
||||
int seenCount = 0;
|
||||
|
||||
LinkedList<LinkedList<Node>> components = new LinkedList<LinkedList<Node>>();
|
||||
while (seenCount < N) {
|
||||
//The search Q
|
||||
LinkedList<Node> Q = new LinkedList<Node>();
|
||||
@@ -154,7 +158,7 @@ public class ConnectedComponents implements Statistics, LongTask {
|
||||
while (!Q.isEmpty()) {
|
||||
if (isCanceled) {
|
||||
graph.readUnlock();
|
||||
return;
|
||||
return new LinkedList<LinkedList<Node>>();
|
||||
}
|
||||
//Get the next Node and add it to the component list
|
||||
Node u = Q.removeFirst();
|
||||
@@ -180,37 +184,78 @@ public class ConnectedComponents implements Statistics, LongTask {
|
||||
color[indicies.get(u)] = 2;
|
||||
seenCount++;
|
||||
}
|
||||
for (Node s : component) {
|
||||
s.setAttribute(componentCol, componentCount);
|
||||
}
|
||||
sizeList.add(component.size());
|
||||
componentCount++;
|
||||
components.add(component);
|
||||
}
|
||||
graph.readUnlock();
|
||||
return components;
|
||||
}
|
||||
|
||||
componentsSize = new int[sizeList.size()];
|
||||
for (int i = 0; i < sizeList.size(); i++) {
|
||||
componentsSize[i] = sizeList.get(i);
|
||||
private Column initializeWeeklyConnectedColumn(AttributeModel attributeModel) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column componentCol = nodeTable.getColumn(WEAKLY);
|
||||
if (componentCol == null) {
|
||||
componentCol = nodeTable.addColumn(WEAKLY, "Component ID", Integer.class, new Integer(0));
|
||||
}
|
||||
return componentCol;
|
||||
}
|
||||
|
||||
public HashMap<Node, Integer> createIndiciesMap(Graph hgraph) {
|
||||
HashMap<Node, Integer> indicies = new HashMap<Node, Integer>();
|
||||
int index = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
indicies.put(s, index);
|
||||
index++;
|
||||
}
|
||||
return indicies;
|
||||
}
|
||||
|
||||
private void saveComputedComponents(LinkedList<LinkedList<Node>> components, Column componentCol) {
|
||||
int i = 0;
|
||||
for (LinkedList<Node> component : components) {
|
||||
for (Node s : component) {
|
||||
s.setAttribute(componentCol, i);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public void top_tarjans(DirectedGraph graph, AttributeModel attributeModel) {
|
||||
count = 1;
|
||||
stronglyCount = 0;
|
||||
void fillComponentSizeList(LinkedList<LinkedList<Node>> components) {
|
||||
componentsSize = new int[components.size()];
|
||||
for (int i = 0; i < components.size(); i++) {
|
||||
componentsSize[i] = components.get(i).size();
|
||||
}
|
||||
}
|
||||
|
||||
private Column initializeStronglyConnectedColumn(AttributeModel attributeModel) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column componentCol = nodeTable.getColumn(STRONG);
|
||||
if (componentCol == null) {
|
||||
componentCol = nodeTable.addColumn(STRONG, "Strongly-Connected ID", Integer.class, new Integer(0));
|
||||
}
|
||||
return componentCol;
|
||||
}
|
||||
|
||||
graph.readLock();
|
||||
public void stronglyConnected(DirectedGraph hgraph, AttributeModel attributeModel) {
|
||||
count = 1;
|
||||
stronglyCount = 0;
|
||||
|
||||
Column componentCol = initializeStronglyConnectedColumn(attributeModel);
|
||||
|
||||
HashMap<Node, Integer> indicies = createIndiciesMap(hgraph);
|
||||
|
||||
LinkedList<LinkedList<Node>> components = top_tarjans(hgraph, indicies);
|
||||
|
||||
saveComputedComponents(components, componentCol);
|
||||
|
||||
stronglyCount = components.size();
|
||||
}
|
||||
|
||||
public LinkedList<LinkedList<Node>> top_tarjans(DirectedGraph graph, HashMap<Node, Integer> indicies) {
|
||||
|
||||
LinkedList<LinkedList<Node>> allComponents = new LinkedList<LinkedList<Node>>();
|
||||
|
||||
count = 1;
|
||||
stronglyCount = 0;
|
||||
|
||||
HashMap<Node, Integer> indicies = new HashMap<Node, Integer>();
|
||||
int v = 0;
|
||||
for (Node s : graph.getNodes()) {
|
||||
indicies.put(s, v);
|
||||
v++;
|
||||
}
|
||||
int N = graph.getNodeCount();
|
||||
int[] index = new int[N];
|
||||
int[] low_index = new int[N];
|
||||
@@ -231,14 +276,18 @@ public class ConnectedComponents implements Statistics, LongTask {
|
||||
}
|
||||
}
|
||||
if (first == null) {
|
||||
graph.readUnlockAll();
|
||||
return;
|
||||
return allComponents;
|
||||
}
|
||||
|
||||
LinkedList<LinkedList<Node>> components = new LinkedList<LinkedList<Node>>();
|
||||
components = tarjans(components, S, graph, first, index, low_index, indicies);
|
||||
for (LinkedList<Node> component : components) {
|
||||
allComponents.add(component);
|
||||
}
|
||||
tarjans(componentCol, S, graph, first, index, low_index, indicies);
|
||||
}
|
||||
}
|
||||
|
||||
private void tarjans(Column col, LinkedList<Node> S, DirectedGraph graph, Node f, int[] index, int[] low_index, HashMap<Node, Integer> indicies) {
|
||||
private LinkedList<LinkedList<Node>> tarjans(LinkedList<LinkedList<Node>> components, LinkedList<Node> S, DirectedGraph graph, Node f, int[] index, int[] low_index, HashMap<Node, Integer> indicies) {
|
||||
int id = indicies.get(f);
|
||||
index[id] = count;
|
||||
low_index[id] = count;
|
||||
@@ -249,20 +298,22 @@ public class ConnectedComponents implements Statistics, LongTask {
|
||||
Node u = graph.getOpposite(f, e);
|
||||
int x = indicies.get(u);
|
||||
if (index[x] == 0) {
|
||||
tarjans(col, S, graph, u, index, low_index, indicies);
|
||||
tarjans(components, S, graph, u, index, low_index, indicies);
|
||||
low_index[id] = Math.min(low_index[x], low_index[id]);
|
||||
} else if (S.contains(u)) {
|
||||
low_index[id] = Math.min(low_index[id], index[x]);
|
||||
}
|
||||
}
|
||||
LinkedList<Node> currentComponent = new LinkedList<Node>();
|
||||
if (low_index[id] == index[id]) {
|
||||
Node v = null;
|
||||
while (v != f) {
|
||||
v = S.removeFirst();
|
||||
v.setAttribute(col, stronglyCount);
|
||||
currentComponent.add(v);
|
||||
}
|
||||
stronglyCount++;
|
||||
components.add(currentComponent);
|
||||
}
|
||||
return components;
|
||||
}
|
||||
|
||||
public int getConnectedComponentsCount() {
|
||||
@@ -294,6 +345,19 @@ public class ConnectedComponents implements Statistics, LongTask {
|
||||
return maxIndex;
|
||||
}
|
||||
|
||||
public int getComponentNumber(LinkedList<LinkedList<Node>> components, Node node) {
|
||||
int i = 0;
|
||||
for (LinkedList<Node> component : components) {
|
||||
for (Node currentNode : component) {
|
||||
if (currentNode.equals(node)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReport() {
|
||||
Map<Integer, Integer> sizeDist = new HashMap<Integer, Integer>();
|
||||
|
||||
@@ -1,43 +1,43 @@
|
||||
/*
|
||||
Copyright 2008-2011 Gephi
|
||||
Authors : Patick J. McSweeney <pjmcswee@syr.edu>, Sebastien Heymann <seb@gephi.org>
|
||||
Website : http://www.gephi.org
|
||||
Copyright 2008-2011 Gephi
|
||||
Authors : Patick J. McSweeney <pjmcswee@syr.edu>, Sebastien Heymann <seb@gephi.org>
|
||||
Website : http://www.gephi.org
|
||||
|
||||
This file is part of Gephi.
|
||||
This file is part of Gephi.
|
||||
|
||||
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
||||
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
||||
|
||||
Copyright 2011 Gephi Consortium. All rights reserved.
|
||||
Copyright 2011 Gephi Consortium. All rights reserved.
|
||||
|
||||
The contents of this file are subject to the terms of either the GNU
|
||||
General Public License Version 3 only ("GPL") or the Common
|
||||
Development and Distribution License("CDDL") (collectively, the
|
||||
"License"). You may not use this file except in compliance with the
|
||||
License. You can obtain a copy of the License at
|
||||
http://gephi.org/about/legal/license-notice/
|
||||
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
|
||||
specific language governing permissions and limitations under the
|
||||
License. When distributing the software, include this License Header
|
||||
Notice in each file and include the License files at
|
||||
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
|
||||
License Header, with the fields enclosed by brackets [] replaced by
|
||||
your own identifying information:
|
||||
"Portions Copyrighted [year] [name of copyright owner]"
|
||||
The contents of this file are subject to the terms of either the GNU
|
||||
General Public License Version 3 only ("GPL") or the Common
|
||||
Development and Distribution License("CDDL") (collectively, the
|
||||
"License"). You may not use this file except in compliance with the
|
||||
License. You can obtain a copy of the License at
|
||||
http://gephi.org/about/legal/license-notice/
|
||||
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
|
||||
specific language governing permissions and limitations under the
|
||||
License. When distributing the software, include this License Header
|
||||
Notice in each file and include the License files at
|
||||
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
|
||||
License Header, with the fields enclosed by brackets [] replaced by
|
||||
your own identifying information:
|
||||
"Portions Copyrighted [year] [name of copyright owner]"
|
||||
|
||||
If you wish your version of this file to be governed by only the CDDL
|
||||
or only the GPL Version 3, indicate your decision by adding
|
||||
"[Contributor] elects to include this software in this distribution
|
||||
under the [CDDL or GPL Version 3] license." If you do not indicate a
|
||||
single choice of license, a recipient has the option to distribute
|
||||
your version of this file under either the CDDL, the GPL Version 3 or
|
||||
to extend the choice of license to its licensees as provided above.
|
||||
However, if you add GPL Version 3 code and therefore, elected the GPL
|
||||
Version 3 license, then the option applies only if the new code is
|
||||
made subject to such option by the copyright holder.
|
||||
If you wish your version of this file to be governed by only the CDDL
|
||||
or only the GPL Version 3, indicate your decision by adding
|
||||
"[Contributor] elects to include this software in this distribution
|
||||
under the [CDDL or GPL Version 3] license." If you do not indicate a
|
||||
single choice of license, a recipient has the option to distribute
|
||||
your version of this file under either the CDDL, the GPL Version 3 or
|
||||
to extend the choice of license to its licensees as provided above.
|
||||
However, if you add GPL Version 3 code and therefore, elected the GPL
|
||||
Version 3 license, then the option applies only if the new code is
|
||||
made subject to such option by the copyright holder.
|
||||
|
||||
Contributor(s):
|
||||
Contributor(s):
|
||||
|
||||
Portions Copyrighted 2011 Gephi Consortium.
|
||||
Portions Copyrighted 2011 Gephi Consortium.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
@@ -46,8 +46,6 @@ import java.text.NumberFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.gephi.attribute.api.AttributeModel;
|
||||
import org.gephi.attribute.api.Column;
|
||||
import org.gephi.attribute.api.Origin;
|
||||
import org.gephi.attribute.api.Table;
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Graph;
|
||||
@@ -71,11 +69,17 @@ public class Degree implements Statistics, LongTask {
|
||||
public static final String DEGREE = "degree";
|
||||
public static final String AVERAGE_DEGREE = "avgdegree";
|
||||
private boolean isDirected; // only set inside this class
|
||||
/** Remembers if the Cancel function has been called. */
|
||||
/**
|
||||
* Remembers if the Cancel function has been called.
|
||||
*/
|
||||
private boolean isCanceled;
|
||||
/** Keep track of the work done. */
|
||||
/**
|
||||
* Keep track of the work done.
|
||||
*/
|
||||
private ProgressTicket progress;
|
||||
/** */
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private double avgDegree;
|
||||
private Map<Integer, Integer> inDegreeDist;
|
||||
private Map<Integer, Integer> outDegreeDist;
|
||||
@@ -102,54 +106,65 @@ public class Degree implements Statistics, LongTask {
|
||||
public void execute(Graph graph, AttributeModel attributeModel) {
|
||||
isDirected = graph.isDirected();
|
||||
isCanceled = false;
|
||||
inDegreeDist = new HashMap<Integer, Integer>();
|
||||
outDegreeDist = new HashMap<Integer, Integer>();
|
||||
degreeDist = new HashMap<Integer, Integer>();
|
||||
|
||||
//Attributes cols
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column inCol = nodeTable.getColumn(INDEGREE);
|
||||
Column outCol = nodeTable.getColumn(OUTDEGREE);
|
||||
Column degCol = nodeTable.getColumn(DEGREE);
|
||||
|
||||
if (isDirected) {
|
||||
if (inCol == null) {
|
||||
inCol = nodeTable.addColumn(INDEGREE, NbBundle.getMessage(Degree.class, "Degree.nodecolumn.InDegree"), Integer.class, 0);
|
||||
}
|
||||
if (outCol == null) {
|
||||
outCol = nodeTable.addColumn(OUTDEGREE, NbBundle.getMessage(Degree.class, "Degree.nodecolumn.OutDegree"), Integer.class, 0);
|
||||
}
|
||||
}
|
||||
if (degCol == null) {
|
||||
degCol = nodeTable.addColumn(DEGREE, NbBundle.getMessage(Degree.class, "Degree.nodecolumn.Degree"), Integer.class, 0);
|
||||
}
|
||||
initializeDegreeDists();
|
||||
initializeAttributeColunms(attributeModel);
|
||||
|
||||
graph.readLock();
|
||||
|
||||
|
||||
avgDegree = calculateAverageDegree(graph, isDirected, true);
|
||||
|
||||
graph.setAttribute(AVERAGE_DEGREE, avgDegree);
|
||||
|
||||
graph.readUnlockAll();
|
||||
}
|
||||
|
||||
protected int calculateInDegree(DirectedGraph directedGraph, Node n) {
|
||||
return directedGraph.getInDegree(n);
|
||||
}
|
||||
|
||||
protected int calculateOutDegree(DirectedGraph directedGraph, Node n) {
|
||||
return directedGraph.getOutDegree(n);
|
||||
}
|
||||
|
||||
protected int calculateDegree(Graph graph, Node n) {
|
||||
return graph.getDegree(n);
|
||||
}
|
||||
|
||||
protected double calculateAverageDegree(Graph graph, boolean isDirected, boolean updateAttributes) {
|
||||
double averageDegree = 0;
|
||||
|
||||
DirectedGraph directedGraph = null;
|
||||
|
||||
if (isDirected) {
|
||||
directedGraph = (DirectedGraph) graph;
|
||||
}
|
||||
|
||||
Progress.start(progress, graph.getNodeCount());
|
||||
|
||||
for (Node n : graph.getNodes()) {
|
||||
int inDegree = 0;
|
||||
int outDegree = 0;
|
||||
int degree = 0;
|
||||
if (isDirected) {
|
||||
int inDegree = ((DirectedGraph)graph).getInDegree(n);
|
||||
int outDegree = ((DirectedGraph)graph).getOutDegree(n);
|
||||
n.setAttribute(inCol, inDegree);
|
||||
n.setAttribute(outCol, outDegree);
|
||||
if (!inDegreeDist.containsKey(inDegree)) {
|
||||
inDegreeDist.put(inDegree, 0);
|
||||
}
|
||||
inDegreeDist.put(inDegree, inDegreeDist.get(inDegree) + 1);
|
||||
if (!outDegreeDist.containsKey(outDegree)) {
|
||||
outDegreeDist.put(outDegree, 0);
|
||||
}
|
||||
outDegreeDist.put(outDegree, outDegreeDist.get(outDegree) + 1);
|
||||
inDegree = calculateInDegree(directedGraph, n);
|
||||
outDegree = calculateOutDegree(directedGraph, n);
|
||||
}
|
||||
int degree = graph.getDegree(n);
|
||||
n.setAttribute(degCol, degree);
|
||||
avgDegree += degree;
|
||||
if (!degreeDist.containsKey(degree)) {
|
||||
degreeDist.put(degree, 0);
|
||||
degree = calculateDegree(graph, n);
|
||||
|
||||
if (updateAttributes) {
|
||||
n.setAttribute(DEGREE, degree);
|
||||
if (isDirected) {
|
||||
n.setAttribute(INDEGREE, inDegree);
|
||||
n.setAttribute(OUTDEGREE, outDegree);
|
||||
updateDegreeDists(inDegree, outDegree, degree);
|
||||
} else {
|
||||
updateDegreeDists(degree);
|
||||
|
||||
}
|
||||
}
|
||||
degreeDist.put(degree, degreeDist.get(degree) + 1);
|
||||
|
||||
averageDegree += degree;
|
||||
|
||||
if (isCanceled) {
|
||||
break;
|
||||
@@ -157,12 +172,52 @@ public class Degree implements Statistics, LongTask {
|
||||
Progress.progress(progress);
|
||||
}
|
||||
|
||||
avgDegree /= graph.getNodeCount();
|
||||
graph.setAttribute(AVERAGE_DEGREE, avgDegree);
|
||||
averageDegree /= graph.getNodeCount();
|
||||
|
||||
graph.readUnlockAll();
|
||||
|
||||
Progress.finish(progress);
|
||||
return averageDegree;
|
||||
}
|
||||
|
||||
private void initializeAttributeColunms(AttributeModel attributeModel) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
if (isDirected) {
|
||||
if (!nodeTable.hasColumn(INDEGREE)) {
|
||||
nodeTable.addColumn(INDEGREE, NbBundle.getMessage(Degree.class, "Degree.nodecolumn.InDegree"), Integer.class, 0);
|
||||
}
|
||||
if (!nodeTable.hasColumn(OUTDEGREE)) {
|
||||
nodeTable.addColumn(OUTDEGREE, NbBundle.getMessage(Degree.class, "Degree.nodecolumn.OutDegree"), Integer.class, 0);
|
||||
}
|
||||
}
|
||||
if (!nodeTable.hasColumn(DEGREE)) {
|
||||
nodeTable.addColumn(DEGREE, NbBundle.getMessage(Degree.class, "Degree.nodecolumn.Degree"), Integer.class, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeDegreeDists() {
|
||||
inDegreeDist = new HashMap<Integer, Integer>();
|
||||
outDegreeDist = new HashMap<Integer, Integer>();
|
||||
degreeDist = new HashMap<Integer, Integer>();
|
||||
}
|
||||
|
||||
private void updateDegreeDists(int inDegree, int outDegree, int degree) {
|
||||
if (!inDegreeDist.containsKey(inDegree)) {
|
||||
inDegreeDist.put(inDegree, 0);
|
||||
}
|
||||
inDegreeDist.put(inDegree, inDegreeDist.get(inDegree) + 1);
|
||||
if (!outDegreeDist.containsKey(outDegree)) {
|
||||
outDegreeDist.put(outDegree, 0);
|
||||
}
|
||||
outDegreeDist.put(outDegree, outDegreeDist.get(outDegree) + 1);
|
||||
if (!degreeDist.containsKey(degree)) {
|
||||
degreeDist.put(degree, 0);
|
||||
}
|
||||
degreeDist.put(degree, degreeDist.get(degree) + 1);
|
||||
}
|
||||
|
||||
private void updateDegreeDists(int degree) {
|
||||
if (!degreeDist.containsKey(degree)) {
|
||||
degreeDist.put(degree, 0);
|
||||
}
|
||||
degreeDist.put(degree, degreeDist.get(degree) + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,7 +256,7 @@ public class Degree implements Statistics, LongTask {
|
||||
+ "<hr>"
|
||||
+ "<br> <h2> Results: </h2>"
|
||||
+ "Average Degree: " + f.format(avgDegree)
|
||||
+ "<br /><br />"+degreeImageFile
|
||||
+ "<br /><br />" + degreeImageFile
|
||||
+ "</BODY></HTML>";
|
||||
}
|
||||
return report;
|
||||
@@ -263,18 +318,18 @@ public class Degree implements Statistics, LongTask {
|
||||
ChartUtils.decorateChart(chart3);
|
||||
ChartUtils.scaleChart(chart3, dSeries, false);
|
||||
String outdegreeImageFile = ChartUtils.renderChart(chart3, "outdegree-distribution.png");
|
||||
|
||||
|
||||
NumberFormat f = new DecimalFormat("#0.000");
|
||||
|
||||
String report = "<HTML> <BODY> <h1>Degree Report </h1> "
|
||||
+ "<hr>"
|
||||
+ "<br> <h2> Results: </h2>"
|
||||
+ "Average Degree: " + f.format(avgDegree)
|
||||
+ "<br /><br />"+degreeImageFile
|
||||
+ "<br /><br />"+indegreeImageFile
|
||||
+ "<br /><br />"+outdegreeImageFile
|
||||
+ "<br /><br />" + degreeImageFile
|
||||
+ "<br /><br />" + indegreeImageFile
|
||||
+ "<br /><br />" + outdegreeImageFile
|
||||
+ "</BODY></HTML>";
|
||||
|
||||
|
||||
return report;
|
||||
}
|
||||
|
||||
|
||||
+100
-49
@@ -76,7 +76,8 @@ public class EigenvectorCentrality implements Statistics, LongTask {
|
||||
private double sumChange;
|
||||
private ProgressTicket progress;
|
||||
/**
|
||||
* */
|
||||
*
|
||||
*/
|
||||
private boolean isCanceled;
|
||||
private boolean isDirected;
|
||||
|
||||
@@ -86,7 +87,7 @@ public class EigenvectorCentrality implements Statistics, LongTask {
|
||||
isDirected = graphController.getGraphModel().isDirected();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setNumRuns(int numRuns) {
|
||||
this.numRuns = numRuns;
|
||||
}
|
||||
@@ -136,78 +137,128 @@ public class EigenvectorCentrality implements Statistics, LongTask {
|
||||
|
||||
public void execute(Graph hgraph, AttributeModel attributeModel) {
|
||||
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column eigenCol = nodeTable.getColumn(EIGENVECTOR);
|
||||
if (eigenCol == null) {
|
||||
eigenCol = nodeTable.addColumn(EIGENVECTOR, "Eigenvector Centrality", Double.class, new Double(0));
|
||||
}
|
||||
Column column = initializeAttributeColunms(attributeModel);
|
||||
|
||||
int N = hgraph.getNodeCount();
|
||||
hgraph.readLock();
|
||||
|
||||
double[] tmp = new double[N];
|
||||
centralities = new double[N];
|
||||
|
||||
Progress.start(progress, numRuns);
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap<Integer, Node>();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap<Node, Integer>();
|
||||
fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
sumChange = calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, isDirected, numRuns);
|
||||
|
||||
saveCalculatedValues(hgraph, column, indicies, centralities);
|
||||
|
||||
hgraph.readUnlock();
|
||||
|
||||
Progress.finish(progress);
|
||||
}
|
||||
|
||||
private Column initializeAttributeColunms(AttributeModel attributeModel) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column eigenCol = nodeTable.getColumn(EIGENVECTOR);
|
||||
if (eigenCol == null) {
|
||||
eigenCol = nodeTable.addColumn(EIGENVECTOR, "Eigenvector Centrality", Double.class, new Double(0));
|
||||
}
|
||||
return eigenCol;
|
||||
}
|
||||
|
||||
private void saveCalculatedValues(Graph hgraph, Column attributeColumn, HashMap<Integer, Node> indicies,
|
||||
double[] eigCenrtalities) {
|
||||
|
||||
int N = hgraph.getNodeCount();
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
Node s = indicies.get(i);
|
||||
|
||||
s.setAttribute(attributeColumn, eigCenrtalities[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void fillIndiciesMaps(Graph hgraph, double[] eigCentralities, HashMap<Integer, Node> indicies, HashMap<Node, Integer> invIndicies) {
|
||||
if (indicies == null || invIndicies == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
for (Node u : hgraph.getNodes()) {
|
||||
indicies.put(count, u);
|
||||
invIndicies.put(u, count);
|
||||
centralities[count] = 1;
|
||||
eigCentralities[count] = 1;
|
||||
count++;
|
||||
}
|
||||
for (int s = 0; s < numRuns; s++) {
|
||||
double max = 0;
|
||||
for (int i = 0; i < N; i++) {
|
||||
Node u = indicies.get(i);
|
||||
EdgeIterable iter = null;
|
||||
if (isDirected) {
|
||||
iter = ((DirectedGraph) hgraph).getInEdges(u);
|
||||
} else {
|
||||
iter = hgraph.getEdges(u);
|
||||
}
|
||||
}
|
||||
|
||||
for (Edge e : iter) {
|
||||
Node v = hgraph.getOpposite(u, e);
|
||||
Integer id = invIndicies.get(v);
|
||||
tmp[i] += centralities[id];
|
||||
}
|
||||
max = Math.max(max, tmp[i]);
|
||||
if (isCanceled) {
|
||||
return;
|
||||
}
|
||||
private double computeMaxValueAndTempValues(Graph hgraph, HashMap<Integer, Node> indicies, HashMap<Node, Integer> invIndicies,
|
||||
double[] tempValues, double[] centralityValues, boolean directed) {
|
||||
|
||||
double max = 0.;
|
||||
int N = hgraph.getNodeCount();
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
Node u = indicies.get(i);
|
||||
EdgeIterable iter = null;
|
||||
if (directed) {
|
||||
iter = ((DirectedGraph) hgraph).getInEdges(u);
|
||||
} else {
|
||||
iter = hgraph.getEdges(u);
|
||||
}
|
||||
sumChange = 0;
|
||||
for (int k = 0; k < N; k++) {
|
||||
if (max != 0) {
|
||||
sumChange += Math.abs(centralities[k] - (tmp[k] / max));
|
||||
centralities[k] = tmp[k] / max;
|
||||
//tmp[k] = 0;
|
||||
}
|
||||
if (isCanceled) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Edge e : iter) {
|
||||
Node v = hgraph.getOpposite(u, e);
|
||||
Integer id = invIndicies.get(v);
|
||||
tempValues[i] += centralityValues[id];
|
||||
}
|
||||
max = Math.max(max, tempValues[i]);
|
||||
if (isCanceled) {
|
||||
return max;
|
||||
}
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
private double updateValues(Graph hgraph, double[] tempValues, double[] centralityValues, double max) {
|
||||
double sumChanged = 0.;
|
||||
int N = hgraph.getNodeCount();
|
||||
|
||||
for (int k = 0; k < N; k++) {
|
||||
if (max != 0) {
|
||||
sumChanged += Math.abs(centralityValues[k] - (tempValues[k] / max));
|
||||
centralityValues[k] = tempValues[k] / max;
|
||||
}
|
||||
if (isCanceled) {
|
||||
return;
|
||||
return sumChanged;
|
||||
}
|
||||
}
|
||||
|
||||
return sumChanged;
|
||||
}
|
||||
|
||||
public double calculateEigenvectorCentrality(Graph hgraph, double[] eigCentralities,
|
||||
HashMap<Integer, Node> indicies, HashMap<Node, Integer> invIndicies,
|
||||
boolean directed, int numIterations) {
|
||||
|
||||
int N = hgraph.getNodeCount();
|
||||
double sumChanged = 0.;
|
||||
double[] tmp = new double[N];
|
||||
|
||||
for (int s = 0; s < numIterations; s++) {
|
||||
double max = computeMaxValueAndTempValues(hgraph, indicies, invIndicies, tmp, eigCentralities, directed);
|
||||
sumChanged = updateValues(hgraph, tmp, eigCentralities, max);
|
||||
if (isCanceled) {
|
||||
return sumChanged;
|
||||
}
|
||||
|
||||
Progress.progress(progress);
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
Node s = indicies.get(i);
|
||||
s.setAttribute(eigenCol, centralities[i]);
|
||||
if (isCanceled) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
hgraph.readUnlock();
|
||||
|
||||
Progress.finish(progress);
|
||||
return sumChanged;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+18
-4
@@ -45,8 +45,10 @@ import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import org.gephi.attribute.api.AttributeModel;
|
||||
import org.gephi.graph.api.Graph;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.statistics.spi.Statistics;
|
||||
import org.openide.util.Lookup;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -59,6 +61,13 @@ public class GraphDensity implements Statistics {
|
||||
/** */
|
||||
private boolean isDirected;
|
||||
|
||||
public GraphDensity() {
|
||||
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
|
||||
if (graphController != null && graphController.getGraphModel()!= null) {
|
||||
isDirected = graphController.getGraphModel().isDirected();
|
||||
}
|
||||
}
|
||||
|
||||
public void setDirected(boolean isDirected) {
|
||||
this.isDirected = isDirected;
|
||||
}
|
||||
@@ -73,23 +82,28 @@ public class GraphDensity implements Statistics {
|
||||
|
||||
@Override
|
||||
public void execute(GraphModel graphModel, AttributeModel attributeModel) {
|
||||
isDirected = graphModel.isDirected();
|
||||
|
||||
Graph graph;
|
||||
if (isDirected) {
|
||||
graph = graphModel.getDirectedGraphVisible();
|
||||
} else {
|
||||
graph = graphModel.getUndirectedGraphVisible();
|
||||
}
|
||||
|
||||
density = calculateDensity(graph, isDirected);
|
||||
}
|
||||
|
||||
public double calculateDensity(Graph graph, boolean isGraphDirected) {
|
||||
double result;
|
||||
|
||||
double edgesCount = graph.getEdgeCount();
|
||||
double nodesCount = graph.getNodeCount();
|
||||
double multiplier = 1;
|
||||
|
||||
if (!isDirected) {
|
||||
if (!isGraphDirected) {
|
||||
multiplier = 2;
|
||||
}
|
||||
density = (multiplier * edgesCount) / (nodesCount * nodesCount - nodesCount);
|
||||
result = (multiplier * edgesCount) / (nodesCount * nodesCount - nodesCount);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+134
-64
@@ -64,6 +64,7 @@ import org.jfree.data.xy.XYSeries;
|
||||
import org.jfree.data.xy.XYSeriesCollection;
|
||||
import org.openide.util.Exceptions;
|
||||
import org.openide.util.Lookup;
|
||||
import org.openide.util.NbBundle;
|
||||
|
||||
/**
|
||||
* Ref: Ulrik Brandes, A Faster Algorithm for Betweenness Centrality, in Journal
|
||||
@@ -118,6 +119,10 @@ public class GraphDistance implements Statistics, LongTask {
|
||||
public double getDiameter() {
|
||||
return diameter;
|
||||
}
|
||||
|
||||
public double getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
public GraphDistance() {
|
||||
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
|
||||
@@ -146,56 +151,55 @@ public class GraphDistance implements Statistics, LongTask {
|
||||
|
||||
public void execute(Graph hgraph, AttributeModel attributeModel) {
|
||||
isCanceled = false;
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column eccentricityCol = nodeTable.getColumn(ECCENTRICITY);
|
||||
Column closenessCol = nodeTable.getColumn(CLOSENESS);
|
||||
Column betweenessCol = nodeTable.getColumn(BETWEENNESS);
|
||||
if (eccentricityCol == null) {
|
||||
eccentricityCol = nodeTable.addColumn(ECCENTRICITY, "Eccentricity", Double.class, new Double(0));
|
||||
}
|
||||
if (closenessCol == null) {
|
||||
closenessCol = nodeTable.addColumn(CLOSENESS, "Closeness Centrality", Double.class, new Double(0));
|
||||
}
|
||||
if (betweenessCol == null) {
|
||||
betweenessCol = nodeTable.addColumn(BETWEENNESS, "Betweenness Centrality", Double.class, new Double(0));
|
||||
}
|
||||
|
||||
initializeAttributeColunms(attributeModel);
|
||||
|
||||
hgraph.readLock();
|
||||
|
||||
N = hgraph.getNodeCount();
|
||||
|
||||
initializeStartValues();
|
||||
|
||||
HashMap<Node, Integer> indicies = createIndiciesMap(hgraph);
|
||||
|
||||
betweenness = new double[N];
|
||||
eccentricity = new double[N];
|
||||
closeness = new double[N];
|
||||
diameter = 0;
|
||||
avgDist = 0;
|
||||
shortestPaths = 0;
|
||||
radius = Integer.MAX_VALUE;
|
||||
HashMap<Node, Integer> indicies = new HashMap<Node, Integer>();
|
||||
int index = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
indicies.put(s, index);
|
||||
index++;
|
||||
}
|
||||
|
||||
Map<String, double[]> metrics = calculateDistanceMetrics(hgraph, indicies, isDirected, isNormalized);
|
||||
|
||||
eccentricity = metrics.get(ECCENTRICITY);
|
||||
closeness = metrics.get(CLOSENESS);
|
||||
betweenness = metrics.get(BETWEENNESS);
|
||||
|
||||
saveCalculatedValues(hgraph, indicies, eccentricity, betweenness, closeness);
|
||||
|
||||
hgraph.readUnlock();
|
||||
}
|
||||
|
||||
public Map<String, double[]> calculateDistanceMetrics(Graph hgraph, HashMap<Node, Integer> indicies, boolean directed, boolean normalized) {
|
||||
int n = hgraph.getNodeCount();
|
||||
|
||||
HashMap<String, double[]> metrics = new HashMap<String, double[]>();
|
||||
|
||||
double[] nodeEccentricity = new double[n];
|
||||
double[] nodeBetweenness = new double[n];
|
||||
double[] nodeCloseness = new double[n];
|
||||
|
||||
metrics.put(ECCENTRICITY, nodeEccentricity);
|
||||
metrics.put(CLOSENESS, nodeCloseness);
|
||||
metrics.put(BETWEENNESS, nodeBetweenness);
|
||||
|
||||
Progress.start(progress, hgraph.getNodeCount());
|
||||
int count = 0;
|
||||
|
||||
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
Stack<Node> S = new Stack<Node>();
|
||||
|
||||
LinkedList<Node>[] P = new LinkedList[N];
|
||||
double[] theta = new double[N];
|
||||
int[] d = new int[N];
|
||||
for (int j = 0; j < N; j++) {
|
||||
P[j] = new LinkedList<Node>();
|
||||
theta[j] = 0;
|
||||
d[j] = -1;
|
||||
}
|
||||
|
||||
LinkedList<Node>[] P = new LinkedList[n];
|
||||
double[] theta = new double[n];
|
||||
int[] d = new int[n];
|
||||
|
||||
int s_index = indicies.get(s);
|
||||
|
||||
theta[s_index] = 1;
|
||||
d[s_index] = 0;
|
||||
|
||||
setInitParametetrsForNode(s, P, theta, d, s_index, n);
|
||||
|
||||
LinkedList<Node> Q = new LinkedList<Node>();
|
||||
Q.addLast(s);
|
||||
@@ -204,12 +208,7 @@ public class GraphDistance implements Statistics, LongTask {
|
||||
S.push(v);
|
||||
int v_index = indicies.get(v);
|
||||
|
||||
EdgeIterable edgeIter = null;
|
||||
if (isDirected) {
|
||||
edgeIter = ((DirectedGraph) hgraph).getOutEdges(v);
|
||||
} else {
|
||||
edgeIter = hgraph.getEdges(v);
|
||||
}
|
||||
EdgeIterable edgeIter = getEdgeIter(hgraph, v, directed);
|
||||
|
||||
for (Edge edge : edgeIter) {
|
||||
Node reachable = hgraph.getOpposite(v, edge);
|
||||
@@ -226,25 +225,25 @@ public class GraphDistance implements Statistics, LongTask {
|
||||
}
|
||||
}
|
||||
double reachable = 0;
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (d[i] > 0) {
|
||||
avgDist += d[i];
|
||||
eccentricity[s_index] = (int) Math.max(eccentricity[s_index], d[i]);
|
||||
closeness[s_index] += d[i];
|
||||
nodeEccentricity[s_index] = (int) Math.max(nodeEccentricity[s_index], d[i]);
|
||||
nodeCloseness[s_index] += d[i];
|
||||
diameter = Math.max(diameter, d[i]);
|
||||
reachable++;
|
||||
}
|
||||
}
|
||||
|
||||
radius = (int) Math.min(eccentricity[s_index], radius);
|
||||
radius = (int) Math.min(nodeEccentricity[s_index], radius);
|
||||
|
||||
if (reachable != 0) {
|
||||
closeness[s_index] /= reachable;
|
||||
nodeCloseness[s_index] /= reachable;
|
||||
}
|
||||
|
||||
shortestPaths += reachable;
|
||||
|
||||
double[] delta = new double[N];
|
||||
double[] delta = new double[n];
|
||||
while (!S.empty()) {
|
||||
Node w = S.pop();
|
||||
int w_index = indicies.get(w);
|
||||
@@ -255,34 +254,105 @@ public class GraphDistance implements Statistics, LongTask {
|
||||
delta[u_index] += (theta[u_index] / theta[w_index]) * (1 + delta[w_index]);
|
||||
}
|
||||
if (w != s) {
|
||||
betweenness[w_index] += delta[w_index];
|
||||
nodeBetweenness[w_index] += delta[w_index];
|
||||
}
|
||||
}
|
||||
count++;
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
return metrics;
|
||||
}
|
||||
Progress.progress(progress, count);
|
||||
}
|
||||
|
||||
avgDist /= shortestPaths;//mN * (mN - 1.0f);
|
||||
|
||||
calculateCorrection(hgraph, indicies, nodeBetweenness, nodeCloseness, directed, normalized);
|
||||
|
||||
return metrics;
|
||||
}
|
||||
|
||||
private void setInitParametetrsForNode(Node s, LinkedList<Node>[] P, double[] theta, int[] d, int index, int n) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
P[j] = new LinkedList<Node>();
|
||||
theta[j] = 0;
|
||||
d[j] = -1;
|
||||
}
|
||||
theta[index] = 1;
|
||||
d[index] = 0;
|
||||
}
|
||||
|
||||
private EdgeIterable getEdgeIter(Graph hgraph, Node v, boolean directed) {
|
||||
EdgeIterable edgeIter = null;
|
||||
if (directed) {
|
||||
edgeIter = ((DirectedGraph) hgraph).getOutEdges(v);
|
||||
} else {
|
||||
edgeIter = hgraph.getEdges(v);
|
||||
}
|
||||
return edgeIter;
|
||||
}
|
||||
|
||||
private void initializeAttributeColunms(AttributeModel attributeModel) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
if (!nodeTable.hasColumn(ECCENTRICITY)) {
|
||||
nodeTable.addColumn(ECCENTRICITY, "Eccentricity", Double.class, new Double(0));
|
||||
}
|
||||
if (!nodeTable.hasColumn(CLOSENESS)) {
|
||||
nodeTable.addColumn(CLOSENESS, "Closeness Centrality", Double.class, new Double(0));
|
||||
}
|
||||
if (!nodeTable.hasColumn(BETWEENNESS)) {
|
||||
nodeTable.addColumn(BETWEENNESS, "Betweenness Centrality", Double.class, new Double(0));
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<Node, Integer> createIndiciesMap(Graph hgraph) {
|
||||
HashMap<Node, Integer> indicies = new HashMap<Node, Integer>();
|
||||
int index = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
indicies.put(s, index);
|
||||
index++;
|
||||
}
|
||||
return indicies;
|
||||
}
|
||||
|
||||
public void initializeStartValues() {
|
||||
betweenness = new double[N];
|
||||
eccentricity = new double[N];
|
||||
closeness = new double[N];
|
||||
diameter = 0;
|
||||
avgDist = 0;
|
||||
shortestPaths = 0;
|
||||
radius = Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
private void calculateCorrection(Graph hgraph, HashMap<Node, Integer> indicies,
|
||||
double[] nodeBetweenness, double[] nodeCloseness, boolean directed, boolean normalized) {
|
||||
|
||||
int n = hgraph.getNodeCount();
|
||||
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
|
||||
int s_index = indicies.get(s);
|
||||
|
||||
if (!directed) {
|
||||
nodeBetweenness[s_index] /= 2;
|
||||
}
|
||||
if (normalized) {
|
||||
nodeCloseness[s_index] = (nodeCloseness[s_index] == 0) ? 0 : 1.0 / nodeCloseness[s_index];
|
||||
nodeBetweenness[s_index] /= directed ? (n - 1) * (n - 2) : (n - 1) * (n - 2) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void saveCalculatedValues(Graph hgraph, HashMap<Node, Integer> indicies,
|
||||
double[] nodeEccentricity, double[] nodeBetweenness, double[] nodeCloseness) {
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
int s_index = indicies.get(s);
|
||||
|
||||
if (!isDirected) {
|
||||
betweenness[s_index] /= 2;
|
||||
}
|
||||
if (isNormalized) {
|
||||
closeness[s_index] = (closeness[s_index] == 0) ? 0 : 1.0 / closeness[s_index];
|
||||
betweenness[s_index] /= isDirected ? (N - 1) * (N - 2) : (N - 1) * (N - 2) / 2;
|
||||
}
|
||||
s.setAttribute(eccentricityCol, eccentricity[s_index]);
|
||||
s.setAttribute(closenessCol, closeness[s_index]);
|
||||
s.setAttribute(betweenessCol, betweenness[s_index]);
|
||||
s.setAttribute(ECCENTRICITY, nodeEccentricity[s_index]);
|
||||
s.setAttribute(CLOSENESS, nodeCloseness[s_index]);
|
||||
s.setAttribute(BETWEENNESS, nodeBetweenness[s_index]);
|
||||
}
|
||||
hgraph.readUnlock();
|
||||
}
|
||||
|
||||
public void setNormalized(boolean isNormalized) {
|
||||
|
||||
@@ -54,6 +54,7 @@ import org.gephi.graph.api.Graph;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.graph.api.NodeIterable;
|
||||
import org.gephi.statistics.spi.Statistics;
|
||||
import org.gephi.utils.longtask.spi.LongTask;
|
||||
import org.gephi.utils.progress.Progress;
|
||||
@@ -81,9 +82,6 @@ public class Hits implements Statistics, LongTask {
|
||||
private double[] hubs;
|
||||
private boolean useUndirected;
|
||||
private double epsilon = 0.0001;
|
||||
private LinkedList<Node> hub_list;
|
||||
private LinkedList<Node> auth_list;
|
||||
private HashMap<Node, Integer> indicies;
|
||||
|
||||
public Hits() {
|
||||
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
|
||||
@@ -116,140 +114,163 @@ public class Hits implements Statistics, LongTask {
|
||||
}
|
||||
|
||||
public void execute(Graph hgraph, AttributeModel attributeModel) {
|
||||
|
||||
initializeAttributeColunms(attributeModel);
|
||||
|
||||
hgraph.readLock();
|
||||
|
||||
int N = hgraph.getNodeCount();
|
||||
authority = new double[N];
|
||||
hubs = new double[N];
|
||||
|
||||
Map<Node, Integer> indicies = createIndiciesMap(hgraph);
|
||||
|
||||
calculateHits(hgraph, hubs, authority, indicies, !useUndirected, epsilon);
|
||||
|
||||
saveCalculatedValues(hgraph, authority, hubs);
|
||||
|
||||
hgraph.readUnlockAll();
|
||||
}
|
||||
|
||||
public void calculateHits(Graph hgraph, double[] hubValues, double[] authorityValues, Map<Node, Integer> indicies, boolean isDirected, double eps) {
|
||||
|
||||
int N = hgraph.getNodeCount();
|
||||
|
||||
double[] temp_authority = new double[N];
|
||||
double[] temp_hubs = new double[N];
|
||||
|
||||
hub_list = new LinkedList<Node>();
|
||||
auth_list = new LinkedList<Node>();
|
||||
|
||||
initializeStartValues(hubValues, authorityValues);
|
||||
|
||||
Progress.start(progress);
|
||||
|
||||
indicies = new HashMap<Node, Integer>();
|
||||
int index = 0;
|
||||
for (Node node : hgraph.getNodes()) {
|
||||
indicies.put(node, new Integer(index));
|
||||
index++;
|
||||
|
||||
if (!useUndirected) {
|
||||
if (((DirectedGraph) hgraph).getOutDegree(node) > 0) {
|
||||
hub_list.add(node);
|
||||
}
|
||||
if (((DirectedGraph) hgraph).getInDegree(node) > 0) {
|
||||
auth_list.add(node);
|
||||
}
|
||||
} else {
|
||||
if (hgraph.getDegree(node) > 0) {
|
||||
hub_list.add(node);
|
||||
auth_list.add(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Node node : hub_list) {
|
||||
int n_index = indicies.get(node);
|
||||
hubs[n_index] = 1.0f;
|
||||
}
|
||||
for (Node node : auth_list) {
|
||||
int n_index = indicies.get(node);
|
||||
authority[n_index] = 1.0f;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
|
||||
boolean done = true;
|
||||
double auth_sum = 0;
|
||||
for (Node node : auth_list) {
|
||||
|
||||
int n_index = indicies.get(node);
|
||||
temp_authority[n_index] = authority[n_index];
|
||||
EdgeIterable edge_iter;
|
||||
if (!useUndirected) {
|
||||
edge_iter = ((DirectedGraph) hgraph).getInEdges(node);
|
||||
} else {
|
||||
edge_iter = hgraph.getEdges(node);
|
||||
}
|
||||
for (Edge edge : edge_iter) {
|
||||
Node target = hgraph.getOpposite(node, edge);
|
||||
int target_index = indicies.get(target);
|
||||
temp_authority[n_index] += hubs[target_index];
|
||||
}
|
||||
updateAutorithy(hgraph, temp_authority, hubValues, isDirected, indicies);
|
||||
updateHub(hgraph, temp_hubs, temp_authority, isDirected, indicies);
|
||||
|
||||
auth_sum += temp_authority[n_index];
|
||||
if (isCanceled) {
|
||||
break;
|
||||
}
|
||||
done = checkDiff(authorityValues, temp_authority, eps) && checkDiff(hubValues, temp_hubs, eps);
|
||||
|
||||
}
|
||||
System.arraycopy(temp_authority, 0, authorityValues, 0, N);
|
||||
System.arraycopy(temp_hubs, 0, hubValues, 0, N);
|
||||
|
||||
double hub_sum = 0;
|
||||
for (Node node : hub_list) {
|
||||
|
||||
int n_index = indicies.get(node);
|
||||
temp_hubs[n_index] = hubs[n_index];
|
||||
EdgeIterable edge_iter;
|
||||
if (!useUndirected) {
|
||||
edge_iter = ((DirectedGraph) hgraph).getInEdges(node);
|
||||
} else {
|
||||
edge_iter = hgraph.getEdges(node);
|
||||
}
|
||||
for (Edge edge : edge_iter) {
|
||||
Node target = hgraph.getOpposite(node, edge);
|
||||
int target_index = indicies.get(target);
|
||||
temp_hubs[n_index] += authority[target_index];
|
||||
}
|
||||
hub_sum += temp_hubs[n_index];
|
||||
if (isCanceled) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (Node node : auth_list) {
|
||||
int n_index = indicies.get(node);
|
||||
temp_authority[n_index] /= auth_sum;
|
||||
if (((temp_authority[n_index] - authority[n_index]) / authority[n_index]) >= epsilon) {
|
||||
done = false;
|
||||
}
|
||||
}
|
||||
for (Node node : hub_list) {
|
||||
int n_index = indicies.get(node);
|
||||
temp_hubs[n_index] /= hub_sum;
|
||||
if (((temp_hubs[n_index] - hubs[n_index]) / hubs[n_index]) >= epsilon) {
|
||||
done = false;
|
||||
}
|
||||
}
|
||||
|
||||
authority = temp_authority;
|
||||
hubs = temp_hubs;
|
||||
temp_authority = new double[N];
|
||||
temp_hubs = new double[N];
|
||||
// temp_authority = new double[N];
|
||||
// temp_hubs = new double[N]
|
||||
|
||||
if ((done) || (isCanceled)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeAttributeColunms(AttributeModel attributeModel) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column authorityCol = nodeTable.getColumn(AUTHORITY);
|
||||
Column hubsCol = nodeTable.getColumn(HUB);
|
||||
if (authorityCol == null) {
|
||||
authorityCol = nodeTable.addColumn(AUTHORITY, "Authority", Float.class, new Float(0));
|
||||
}
|
||||
if (hubsCol == null) {
|
||||
hubsCol = nodeTable.addColumn(HUB, "Hub", Float.class, new Float(0));
|
||||
}
|
||||
|
||||
if (!nodeTable.hasColumn(AUTHORITY)) {
|
||||
nodeTable.addColumn(AUTHORITY, "Authority", Float.class, new Float(0));
|
||||
}
|
||||
if (!nodeTable.hasColumn(HUB)) {
|
||||
nodeTable.addColumn(HUB, "Hub", Float.class, new Float(0));
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeStartValues(double[] hubValues, double[] authorityValues) {
|
||||
for (int i = 0; i < authorityValues.length; i++) {
|
||||
authorityValues[i] = 1.0;
|
||||
hubValues[i] = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
void updateAutorithy(Graph hgraph, double[] newValues, double[] hubValues, boolean isDirected, Map<Node, Integer> indicies) {
|
||||
double norm = 0;
|
||||
int j = 0;
|
||||
for (Node node : hgraph.getNodes()) {
|
||||
double auth = 0;
|
||||
EdgeIterable edge_iter;
|
||||
if (isDirected) {
|
||||
edge_iter = ((DirectedGraph) hgraph).getInEdges(node);
|
||||
} else {
|
||||
edge_iter = hgraph.getEdges(node);
|
||||
}
|
||||
for (Edge edge : edge_iter) {
|
||||
Node target = hgraph.getOpposite(node, edge);
|
||||
auth += hubValues[indicies.get(target)];
|
||||
}
|
||||
if (auth > 0) {
|
||||
newValues[j] = auth;
|
||||
}
|
||||
norm += newValues[j++];
|
||||
if (isCanceled) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// norm = Math.sqrt(norm);
|
||||
if (norm > 0) {
|
||||
for (int i = 0; i < newValues.length; i++) {
|
||||
newValues[i] = newValues[i] / norm;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateHub(Graph hgraph, double[] newValues, double[] authValues, boolean isDirected, Map<Node, Integer> indicies) {
|
||||
double norm = 0;
|
||||
int j = 0;
|
||||
for (Node node : hgraph.getNodes()) {
|
||||
double hub = 0;
|
||||
EdgeIterable edge_iter;
|
||||
if (isDirected) {
|
||||
edge_iter = ((DirectedGraph) hgraph).getOutEdges(node);
|
||||
} else {
|
||||
edge_iter = hgraph.getEdges(node);
|
||||
}
|
||||
for (Edge edge : edge_iter) {
|
||||
Node target = hgraph.getOpposite(node, edge);
|
||||
hub += authValues[indicies.get(target)];
|
||||
}
|
||||
if(hub > 0) {
|
||||
newValues[j] = hub;
|
||||
}
|
||||
norm += newValues[j++];
|
||||
if (isCanceled) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (norm > 0) {
|
||||
for (int i = 0; i < newValues.length; i++) {
|
||||
newValues[i] = newValues[i] / norm;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkDiff(double[] oldValues, double[] newValues, double epsilon) {
|
||||
|
||||
for (int i = 0; i < oldValues.length; i++) {
|
||||
if (oldValues[i] > 0 && ((newValues[i] - oldValues[i]) / oldValues[i]) >= epsilon) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void saveCalculatedValues(Graph hgraph, double[] nodeAuthority, double[] nodeHubs) {
|
||||
int i = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
int s_index = indicies.get(s);
|
||||
s.setAttribute(authorityCol, (float) authority[s_index]);
|
||||
s.setAttribute(hubsCol, (float) hubs[s_index]);
|
||||
}
|
||||
int s_index = i++;
|
||||
|
||||
hgraph.readUnlockAll();
|
||||
s.setAttribute(AUTHORITY, (float) nodeAuthority[s_index]);
|
||||
s.setAttribute(HUB, (float) nodeHubs[s_index]);
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<Node, Integer> createIndiciesMap(Graph hgraph) {
|
||||
HashMap<Node, Integer> newIndicies = new HashMap<Node, Integer>();
|
||||
int index = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
newIndicies.put(s, index);
|
||||
index++;
|
||||
}
|
||||
return newIndicies;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,9 +281,8 @@ public class Hits implements Statistics, LongTask {
|
||||
public String getReport() {
|
||||
//distribution of hub values
|
||||
Map<Double, Integer> distHubs = new HashMap<Double, Integer>();
|
||||
for (Node node : hub_list) {
|
||||
int n_index = indicies.get(node);
|
||||
Double d = hubs[n_index];
|
||||
for (int i = 0; i < hubs.length; i++) {
|
||||
Double d = hubs[i];
|
||||
if (distHubs.containsKey(d)) {
|
||||
Integer v = distHubs.get(d);
|
||||
distHubs.put(d, v + 1);
|
||||
@@ -273,9 +293,8 @@ public class Hits implements Statistics, LongTask {
|
||||
|
||||
//distribution of authority values
|
||||
Map<Double, Integer> distAuthorities = new HashMap<Double, Integer>();
|
||||
for (Node node : auth_list) {
|
||||
int n_index = indicies.get(node);
|
||||
Double d = authority[n_index];
|
||||
for (int i = 0; i < authority.length; i++) {
|
||||
Double d = authority[i];
|
||||
if (distAuthorities.containsKey(d)) {
|
||||
Integer v = distAuthorities.get(d);
|
||||
distAuthorities.put(d, v + 1);
|
||||
|
||||
+99
-52
@@ -456,15 +456,36 @@ public class Modularity implements Statistics, LongTask {
|
||||
|
||||
public void execute(Graph hgraph, AttributeModel attributeModel) {
|
||||
isCanceled = false;
|
||||
|
||||
hgraph.readLock();
|
||||
|
||||
structure = new Modularity.CommunityStructure(hgraph);
|
||||
int[] comStructure = new int[hgraph.getNodeCount()];
|
||||
|
||||
HashMap<String, Double> computedModularityMetrics = computeModularity(hgraph, structure, comStructure, resolution, isRandomized, useWeight);
|
||||
|
||||
modularity = computedModularityMetrics.get("modularity");
|
||||
modularityResolution = computedModularityMetrics.get("modularityResolution");
|
||||
|
||||
saveValues(comStructure, hgraph, attributeModel, structure);
|
||||
|
||||
hgraph.readUnlock();
|
||||
}
|
||||
|
||||
protected HashMap<String, Double> computeModularity(Graph hgraph, CommunityStructure theStructure, int[] comStructure,
|
||||
double currentResolution, boolean randomized, boolean weighted) {
|
||||
isCanceled = false;
|
||||
Progress.start(progress);
|
||||
Random rand = new Random();
|
||||
hgraph.readLock();
|
||||
structure = new Modularity.CommunityStructure(hgraph);
|
||||
double totalWeight = structure.graphWeightSum;
|
||||
double[] nodeDegrees = structure.weights.clone();
|
||||
|
||||
double totalWeight = theStructure.graphWeightSum;
|
||||
double[] nodeDegrees = theStructure.weights.clone();
|
||||
|
||||
HashMap<String, Double> results = new HashMap<String, Double>();
|
||||
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
return results;
|
||||
}
|
||||
boolean someChange = true;
|
||||
while (someChange) {
|
||||
@@ -473,91 +494,105 @@ public class Modularity implements Statistics, LongTask {
|
||||
while (localChange) {
|
||||
localChange = false;
|
||||
int start = 0;
|
||||
if (isRandomized) {
|
||||
start = Math.abs(rand.nextInt()) % structure.N;
|
||||
if (randomized) {
|
||||
start = Math.abs(rand.nextInt()) % theStructure.N;
|
||||
}
|
||||
int step = 0;
|
||||
for (int i = start; step < structure.N; i = (i + 1) % structure.N) {
|
||||
for (int i = start; step < theStructure.N; i = (i + 1) % theStructure.N) {
|
||||
step++;
|
||||
double best = 0.;
|
||||
Community bestCommunity = null;
|
||||
Community nodecom = structure.nodeCommunities[i];
|
||||
Set<Community> iter = structure.nodeConnectionsWeight[i].keySet();
|
||||
for (Community com : iter) {
|
||||
double qValue = q(i, com);
|
||||
if (qValue > best) {
|
||||
best = qValue;
|
||||
bestCommunity = com;
|
||||
}
|
||||
}
|
||||
if ((structure.nodeCommunities[i] != bestCommunity) && (bestCommunity != null)) {
|
||||
structure.moveNodeTo(i, bestCommunity);
|
||||
Community bestCommunity = updateBestCommunity(theStructure, i, currentResolution);
|
||||
if ((theStructure.nodeCommunities[i] != bestCommunity) && (bestCommunity != null)) {
|
||||
theStructure.moveNodeTo(i, bestCommunity);
|
||||
localChange = true;
|
||||
}
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
return results;
|
||||
}
|
||||
}
|
||||
someChange = localChange || someChange;
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
if (someChange) {
|
||||
structure.zoomOut();
|
||||
theStructure.zoomOut();
|
||||
}
|
||||
}
|
||||
|
||||
int[] comStructure = new int[hgraph.getNodeCount()];
|
||||
fillComStructure(hgraph, theStructure, comStructure);
|
||||
double[] degreeCount = fillDegreeCount(hgraph, theStructure, comStructure, nodeDegrees, weighted);
|
||||
|
||||
double computedModularity = finalQ(comStructure, degreeCount, hgraph, theStructure, totalWeight, 1., weighted);
|
||||
double computedModularityResolution = finalQ(comStructure, degreeCount, hgraph, theStructure, totalWeight, currentResolution, weighted);
|
||||
|
||||
results.put("modularity", computedModularity);
|
||||
results.put("modularityResolution", computedModularityResolution);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
Community updateBestCommunity(CommunityStructure theStructure, int i, double currentResolution) {
|
||||
double best = 0.;
|
||||
Community bestCommunity = null;
|
||||
Set<Community> iter = theStructure.nodeConnectionsWeight[i].keySet();
|
||||
for (Community com : iter) {
|
||||
double qValue = q(i, com, theStructure, currentResolution);
|
||||
if (qValue > best) {
|
||||
best = qValue;
|
||||
bestCommunity = com;
|
||||
}
|
||||
}
|
||||
return bestCommunity;
|
||||
}
|
||||
|
||||
int[] fillComStructure(Graph hgraph, CommunityStructure theStructure, int[] comStructure) {
|
||||
// int[] comStructure = new int[hgraph.getNodeCount()];
|
||||
int count = 0;
|
||||
double[] degreeCount = new double[structure.communities.size()];
|
||||
for (Community com : structure.communities) {
|
||||
|
||||
for (Community com : theStructure.communities) {
|
||||
for (Integer node : com.nodes) {
|
||||
Community hidden = structure.invMap.get(node);
|
||||
Community hidden = theStructure.invMap.get(node);
|
||||
for (Integer nodeInt : hidden.nodes) {
|
||||
comStructure[nodeInt] = count;
|
||||
}
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return comStructure;
|
||||
}
|
||||
|
||||
double[] fillDegreeCount(Graph hgraph, CommunityStructure theStructure, int[] comStructure, double[] nodeDegrees, boolean weighted) {
|
||||
double[] degreeCount = new double[theStructure.communities.size()];
|
||||
|
||||
for (Node node : hgraph.getNodes()) {
|
||||
int index = structure.map.get(node);
|
||||
if (useWeight) {
|
||||
int index = theStructure.map.get(node);
|
||||
if (weighted) {
|
||||
degreeCount[comStructure[index]] += nodeDegrees[index];
|
||||
} else {
|
||||
degreeCount[comStructure[index]] += hgraph.getDegree(node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
modularity = finalQ(comStructure, degreeCount, hgraph, attributeModel, totalWeight, 1.);
|
||||
modularityResolution = finalQ(comStructure, degreeCount, hgraph, attributeModel, totalWeight, resolution);
|
||||
|
||||
hgraph.readUnlock();
|
||||
return degreeCount;
|
||||
}
|
||||
|
||||
private double finalQ(int[] struct, double[] degrees, Graph hgraph, AttributeModel attributeModel, double totalWeight, double usedResolution) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column modCol = nodeTable.getColumn(MODULARITY_CLASS);
|
||||
if (modCol == null) {
|
||||
modCol = nodeTable.addColumn(MODULARITY_CLASS, "Modularity Class", Integer.class, new Integer(0));
|
||||
}
|
||||
private double finalQ(int[] struct, double[] degrees, Graph hgraph,
|
||||
CommunityStructure theStructure, double totalWeight, double usedResolution, boolean weighted) {
|
||||
|
||||
double res = 0;
|
||||
double[] internal = new double[degrees.length];
|
||||
for (Node n : hgraph.getNodes()) {
|
||||
int n_index = structure.map.get(n);
|
||||
n.setAttribute(modCol, struct[n_index]);
|
||||
int n_index = theStructure.map.get(n);
|
||||
for (Node neighbor : hgraph.getNeighbors(n)) {
|
||||
if (n == neighbor) {
|
||||
continue;
|
||||
}
|
||||
int neigh_index = structure.map.get(neighbor);
|
||||
int neigh_index = theStructure.map.get(neighbor);
|
||||
if (struct[neigh_index] == struct[n_index]) {
|
||||
if (useWeight) {
|
||||
if (weighted) {
|
||||
internal[struct[neigh_index]] += hgraph.getEdge(n, neighbor).getWeight();
|
||||
} else {
|
||||
internal[struct[neigh_index]]++;
|
||||
@@ -572,6 +607,18 @@ public class Modularity implements Statistics, LongTask {
|
||||
return res;
|
||||
}
|
||||
|
||||
private void saveValues(int[] struct, Graph hgraph, AttributeModel attributeModel, CommunityStructure theStructure) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column modCol = nodeTable.getColumn(MODULARITY_CLASS);
|
||||
if (modCol == null) {
|
||||
modCol = nodeTable.addColumn(MODULARITY_CLASS, "Modularity Class", Integer.class, new Integer(0));
|
||||
}
|
||||
for (Node n : hgraph.getNodes()) {
|
||||
int n_index = theStructure.map.get(n);;
|
||||
n.setAttribute(modCol, struct[n_index]);
|
||||
}
|
||||
}
|
||||
|
||||
public double getModularity() {
|
||||
return modularity;
|
||||
}
|
||||
@@ -629,19 +676,19 @@ public class Modularity implements Statistics, LongTask {
|
||||
return report;
|
||||
}
|
||||
|
||||
private double q(int node, Community community) {
|
||||
Float edgesToFloat = structure.nodeConnectionsWeight[node].get(community);
|
||||
private double q(int node, Community community, CommunityStructure theStructure, double currentResolution) {
|
||||
Float edgesToFloat = theStructure.nodeConnectionsWeight[node].get(community);
|
||||
double edgesTo = 0;
|
||||
if (edgesToFloat != null) {
|
||||
edgesTo = edgesToFloat.doubleValue();
|
||||
}
|
||||
double weightSum = community.weightSum;
|
||||
double nodeWeight = structure.weights[node];
|
||||
double qValue = resolution * edgesTo - (nodeWeight * weightSum) / (2.0 * structure.graphWeightSum);
|
||||
if ((structure.nodeCommunities[node] == community) && (structure.nodeCommunities[node].size() > 1)) {
|
||||
qValue = resolution * edgesTo - (nodeWeight * (weightSum - nodeWeight)) / (2.0 * structure.graphWeightSum);
|
||||
double nodeWeight = theStructure.weights[node];
|
||||
double qValue = currentResolution * edgesTo - (nodeWeight * weightSum) / (2.0 * theStructure.graphWeightSum);
|
||||
if ((theStructure.nodeCommunities[node] == community) && (theStructure.nodeCommunities[node].size() > 1)) {
|
||||
qValue = currentResolution * edgesTo - (nodeWeight * (weightSum - nodeWeight)) / (2.0 * theStructure.graphWeightSum);
|
||||
}
|
||||
if ((structure.nodeCommunities[node] == community) && (structure.nodeCommunities[node].size() == 1)) {
|
||||
if ((theStructure.nodeCommunities[node] == community) && (theStructure.nodeCommunities[node].size() == 1)) {
|
||||
qValue = 0.;
|
||||
}
|
||||
return qValue;
|
||||
|
||||
+125
-78
@@ -76,23 +76,29 @@ public class PageRank implements Statistics, LongTask {
|
||||
|
||||
public static final String PAGERANK = "pageranks";
|
||||
/**
|
||||
* */
|
||||
*
|
||||
*/
|
||||
private ProgressTicket progress;
|
||||
/**
|
||||
* */
|
||||
*
|
||||
*/
|
||||
private boolean isCanceled;
|
||||
/**
|
||||
* */
|
||||
*
|
||||
*/
|
||||
private double epsilon = 0.001;
|
||||
/**
|
||||
* */
|
||||
*
|
||||
*/
|
||||
private double probability = 0.85;
|
||||
private boolean useEdgeWeight = false;
|
||||
/**
|
||||
* */
|
||||
*
|
||||
*/
|
||||
private double[] pageranks;
|
||||
/**
|
||||
* */
|
||||
*
|
||||
*/
|
||||
private boolean isDirected;
|
||||
|
||||
public PageRank() {
|
||||
@@ -128,27 +134,48 @@ public class PageRank implements Statistics, LongTask {
|
||||
public void execute(Graph hgraph, AttributeModel attributeModel) {
|
||||
isCanceled = false;
|
||||
|
||||
Column column = initializeAttributeColunms(attributeModel);
|
||||
|
||||
hgraph.readLock();
|
||||
|
||||
int N = hgraph.getNodeCount();
|
||||
pageranks = new double[N];
|
||||
double[] temp = new double[N];
|
||||
HashMap<Node, Integer> indicies = new HashMap<Node, Integer>();
|
||||
int index = 0;
|
||||
HashMap<Node, Integer> indicies = createIndiciesMap(hgraph);
|
||||
|
||||
Progress.start(progress);
|
||||
double[] weights = null;
|
||||
if (useEdgeWeight) {
|
||||
weights = new double[N];
|
||||
pageranks = calculatePagerank(hgraph, indicies, isDirected, useEdgeWeight, epsilon, probability);
|
||||
|
||||
saveCalculatedValues(hgraph, column, indicies, pageranks);
|
||||
|
||||
hgraph.readUnlockAll();
|
||||
}
|
||||
|
||||
private Column initializeAttributeColunms(AttributeModel attributeModel) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column pagerankCol = nodeTable.getColumn(PAGERANK);
|
||||
|
||||
if (pagerankCol == null) {
|
||||
pagerankCol = nodeTable.addColumn(PAGERANK, "PageRank", Double.class, new Double(0));
|
||||
}
|
||||
|
||||
return pagerankCol;
|
||||
}
|
||||
|
||||
private void saveCalculatedValues(Graph hgraph, Column attributeColumn, HashMap<Node, Integer> indicies,
|
||||
double[] nodePagrank) {
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
indicies.put(s, index);
|
||||
pageranks[index] = 1.0f / N;
|
||||
if (useEdgeWeight) {
|
||||
int s_index = indicies.get(s);
|
||||
|
||||
s.setAttribute(attributeColumn, nodePagrank[s_index]);
|
||||
}
|
||||
}
|
||||
|
||||
private void setInitialValues(Graph hgraph, double[] pagerankValues, double[] weights, boolean directed, boolean useWeights) {
|
||||
int N = hgraph.getNodeCount();
|
||||
int index = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
pagerankValues[index] = 1.0f / N;
|
||||
if (useWeights) {
|
||||
double sum = 0;
|
||||
EdgeIterable eIter;
|
||||
if (isDirected) {
|
||||
if (directed) {
|
||||
eIter = ((DirectedGraph) hgraph).getOutEdges(s);
|
||||
} else {
|
||||
eIter = ((UndirectedGraph) hgraph).getEdges(s);
|
||||
@@ -160,89 +187,109 @@ public class PageRank implements Statistics, LongTask {
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private double calculateR(Graph hgraph, double[] pagerankValues, HashMap<Node, Integer> indicies, boolean directed, double prob) {
|
||||
int N = hgraph.getNodeCount();
|
||||
double r = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
int s_index = indicies.get(s);
|
||||
boolean out;
|
||||
if (directed) {
|
||||
out = ((DirectedGraph) hgraph).getOutDegree(s) > 0;
|
||||
} else {
|
||||
out = hgraph.getDegree(s) > 0;
|
||||
}
|
||||
|
||||
if (out) {
|
||||
r += (1.0 - prob) * (pagerankValues[s_index] / N);
|
||||
} else {
|
||||
r += (pagerankValues[s_index] / N);
|
||||
}
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return r;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private double updateValueForNode(Graph hgraph, Node s, double[] pagerankValues, double[] weights,
|
||||
HashMap<Node, Integer> indicies, boolean directed, boolean useWeights, double r, double prob) {
|
||||
double res = r;
|
||||
EdgeIterable eIter;
|
||||
if (directed) {
|
||||
eIter = ((DirectedGraph) hgraph).getInEdges(s);
|
||||
} else {
|
||||
eIter = hgraph.getEdges(s);
|
||||
}
|
||||
|
||||
for (Edge edge : eIter) {
|
||||
Node neighbor = hgraph.getOpposite(s, edge);
|
||||
int neigh_index = indicies.get(neighbor);
|
||||
int normalize;
|
||||
if (directed) {
|
||||
normalize = ((DirectedGraph) hgraph).getOutDegree(neighbor);
|
||||
} else {
|
||||
normalize = hgraph.getDegree(neighbor);
|
||||
}
|
||||
if (useWeights) {
|
||||
double weight = edge.getWeight() / weights[neigh_index];
|
||||
res += prob * pagerankValues[neigh_index] * weight;
|
||||
} else {
|
||||
res += prob * (pagerankValues[neigh_index] / normalize);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
double[] calculatePagerank(Graph hgraph, HashMap<Node, Integer> indicies,
|
||||
boolean directed, boolean useWeights, double eps, double prob) {
|
||||
int N = hgraph.getNodeCount();
|
||||
double[] pagerankValues = new double[N];
|
||||
double[] temp = new double[N];
|
||||
|
||||
Progress.start(progress);
|
||||
double[] weights = new double[N];
|
||||
|
||||
setInitialValues(hgraph, pagerankValues, weights, directed, useWeights);
|
||||
|
||||
while (true) {
|
||||
double r = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
int s_index = indicies.get(s);
|
||||
boolean out;
|
||||
if (isDirected) {
|
||||
out = ((DirectedGraph) hgraph).getOutDegree(s) > 0;
|
||||
} else {
|
||||
out = hgraph.getDegree(s) > 0;
|
||||
}
|
||||
|
||||
if (out) {
|
||||
r += (1.0 - probability) * (pageranks[s_index] / N);
|
||||
} else {
|
||||
r += (pageranks[s_index] / N);
|
||||
}
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
}
|
||||
}
|
||||
double r = calculateR(hgraph, pagerankValues, indicies, directed, prob);
|
||||
|
||||
boolean done = true;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
int s_index = indicies.get(s);
|
||||
temp[s_index] = r;
|
||||
temp[s_index] = updateValueForNode(hgraph, s, pagerankValues, weights, indicies, directed, useWeights, r, prob);
|
||||
|
||||
EdgeIterable eIter;
|
||||
if (isDirected) {
|
||||
eIter = ((DirectedGraph) hgraph).getInEdges(s);
|
||||
} else {
|
||||
eIter = hgraph.getEdges(s);
|
||||
}
|
||||
|
||||
for (Edge edge : eIter) {
|
||||
Node neighbor = hgraph.getOpposite(s, edge);
|
||||
int neigh_index = indicies.get(neighbor);
|
||||
int normalize;
|
||||
if (isDirected) {
|
||||
normalize = ((DirectedGraph) hgraph).getOutDegree(neighbor);
|
||||
} else {
|
||||
normalize = hgraph.getDegree(neighbor);
|
||||
}
|
||||
if (useEdgeWeight) {
|
||||
double weight = edge.getWeight() / weights[neigh_index];
|
||||
temp[s_index] += probability * pageranks[neigh_index] * weight;
|
||||
} else {
|
||||
temp[s_index] += probability * (pageranks[neigh_index] / normalize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ((temp[s_index] - pageranks[s_index]) / pageranks[s_index] >= epsilon) {
|
||||
if ((temp[s_index] - pagerankValues[s_index]) / pagerankValues[s_index] >= eps) {
|
||||
done = false;
|
||||
}
|
||||
|
||||
if (isCanceled) {
|
||||
hgraph.readUnlockAll();
|
||||
return;
|
||||
return pagerankValues;
|
||||
}
|
||||
|
||||
}
|
||||
pageranks = temp;
|
||||
pagerankValues = temp;
|
||||
temp = new double[N];
|
||||
if ((done) || (isCanceled)) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return pagerankValues;
|
||||
}
|
||||
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column pangeRanksCol = nodeTable.getColumn(PAGERANK);
|
||||
if (pangeRanksCol == null) {
|
||||
pangeRanksCol = nodeTable.addColumn(PAGERANK, "PageRank", Double.class, new Double(0));
|
||||
}
|
||||
|
||||
public HashMap<Node, Integer> createIndiciesMap(Graph hgraph) {
|
||||
HashMap<Node, Integer> newIndicies = new HashMap<Node, Integer>();
|
||||
int index = 0;
|
||||
for (Node s : hgraph.getNodes()) {
|
||||
int s_index = indicies.get(s);
|
||||
s.setAttribute(pangeRanksCol, pageranks[s_index]);
|
||||
newIndicies.put(s, index);
|
||||
index++;
|
||||
}
|
||||
|
||||
hgraph.readUnlockAll();
|
||||
return newIndicies;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+76
-45
@@ -47,8 +47,8 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import org.gephi.attribute.api.AttributeModel;
|
||||
import org.gephi.attribute.api.Column;
|
||||
import org.gephi.attribute.api.Table;
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.Graph;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
@@ -62,6 +62,7 @@ import org.jfree.chart.JFreeChart;
|
||||
import org.jfree.chart.plot.PlotOrientation;
|
||||
import org.jfree.data.xy.XYSeries;
|
||||
import org.jfree.data.xy.XYSeriesCollection;
|
||||
import org.openide.util.NbBundle;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -93,38 +94,34 @@ public class WeightedDegree implements Statistics, LongTask {
|
||||
public void execute(Graph graph, AttributeModel attributeModel) {
|
||||
isDirected = graph.isDirected();
|
||||
isCanceled = false;
|
||||
degreeDist = new HashMap<Double, Integer>();
|
||||
inDegreeDist = new HashMap<Double, Integer>();
|
||||
outDegreeDist = new HashMap<Double, Integer>();
|
||||
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
Column degCol = nodeTable.getColumn(WDEGREE);
|
||||
Column inCol = nodeTable.getColumn(WINDEGREE);
|
||||
Column outCol = nodeTable.getColumn(WOUTDEGREE);
|
||||
if (degCol == null) {
|
||||
degCol = nodeTable.addColumn(WDEGREE, "Weighted Degree", Double.class, 0.0);
|
||||
}
|
||||
if (isDirected) {
|
||||
if (inCol == null) {
|
||||
inCol = nodeTable.addColumn(WINDEGREE, "Weighted In-Degree", Double.class, 0.0);
|
||||
}
|
||||
if (outCol == null) {
|
||||
outCol = nodeTable.addColumn(WOUTDEGREE, "Weighted Out-Degree", Double.class, 0.0);
|
||||
}
|
||||
}
|
||||
initializeDegreeDists();
|
||||
initializeAttributeColunms(attributeModel);
|
||||
|
||||
graph.readLock();
|
||||
|
||||
avgWDegree = calculateAverageWeightedDegree(graph, isDirected, true);
|
||||
|
||||
graph.readUnlockAll();
|
||||
}
|
||||
|
||||
public double calculateAverageWeightedDegree(Graph graph, boolean isDirected, boolean updateAttributes) {
|
||||
double averageWeightedDegree = 0;
|
||||
|
||||
DirectedGraph directedGraph = null;
|
||||
|
||||
if (isDirected) {
|
||||
directedGraph = (DirectedGraph) graph;
|
||||
}
|
||||
|
||||
Progress.start(progress, graph.getNodeCount());
|
||||
int i = 0;
|
||||
|
||||
for (Node n : graph.getNodes()) {
|
||||
double totalWeight = 0;
|
||||
if (isDirected) {
|
||||
double totalInWeight = 0;
|
||||
double totalOutWeight = 0;
|
||||
for (Iterator it = graph.getEdges(n).iterator(); it.hasNext();) {
|
||||
Edge e = (Edge) it.next();
|
||||
for (Edge e : directedGraph.getEdges(n)) {
|
||||
if (e.getSource().equals(n)) {
|
||||
totalOutWeight += e.getWeight();
|
||||
}
|
||||
@@ -133,41 +130,75 @@ public class WeightedDegree implements Statistics, LongTask {
|
||||
}
|
||||
}
|
||||
totalWeight = totalInWeight + totalOutWeight;
|
||||
n.setAttribute(inCol, totalInWeight);
|
||||
n.setAttribute(outCol, totalOutWeight);
|
||||
if (!inDegreeDist.containsKey(totalInWeight)) {
|
||||
inDegreeDist.put(totalInWeight, 0);
|
||||
}
|
||||
inDegreeDist.put(totalInWeight, inDegreeDist.get(totalInWeight) + 1);
|
||||
if (!outDegreeDist.containsKey(totalOutWeight)) {
|
||||
outDegreeDist.put(totalOutWeight, 0);
|
||||
}
|
||||
outDegreeDist.put(totalOutWeight, outDegreeDist.get(totalOutWeight) + 1);
|
||||
|
||||
n.setAttribute(WINDEGREE, totalInWeight);
|
||||
n.setAttribute(WOUTDEGREE, totalOutWeight);
|
||||
n.setAttribute(WDEGREE, totalWeight);
|
||||
|
||||
updateDegreeDists(totalInWeight, totalOutWeight, totalWeight);
|
||||
} else {
|
||||
for (Iterator it = graph.getEdges(n).iterator(); it.hasNext();) {
|
||||
Edge e = (Edge) it.next();
|
||||
for (Edge e : graph.getEdges(n)) {
|
||||
totalWeight += e.getWeight();
|
||||
}
|
||||
n.setAttribute(WDEGREE, totalWeight);
|
||||
updateDegreeDists(totalWeight);
|
||||
}
|
||||
|
||||
n.setAttribute(degCol, totalWeight);
|
||||
avgWDegree += totalWeight;
|
||||
|
||||
if (!degreeDist.containsKey(totalWeight)) {
|
||||
degreeDist.put(totalWeight, 0);
|
||||
}
|
||||
degreeDist.put(totalWeight, degreeDist.get(totalWeight) + 1);
|
||||
averageWeightedDegree += totalWeight;
|
||||
|
||||
if (isCanceled) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
Progress.progress(progress, i);
|
||||
Progress.progress(progress);
|
||||
}
|
||||
|
||||
avgWDegree /= (isDirected) ? 2 * graph.getNodeCount() : graph.getNodeCount();
|
||||
averageWeightedDegree /= (isDirected) ? 2 * graph.getNodeCount() : graph.getNodeCount();
|
||||
|
||||
graph.readUnlockAll();
|
||||
return averageWeightedDegree;
|
||||
|
||||
}
|
||||
|
||||
private void initializeDegreeDists() {
|
||||
degreeDist = new HashMap<Double, Integer>();
|
||||
inDegreeDist = new HashMap<Double, Integer>();
|
||||
outDegreeDist = new HashMap<Double, Integer>();
|
||||
}
|
||||
|
||||
private void initializeAttributeColunms(AttributeModel attributeModel) {
|
||||
Table nodeTable = attributeModel.getNodeTable();
|
||||
if (isDirected) {
|
||||
if (!nodeTable.hasColumn(WINDEGREE)) {
|
||||
nodeTable.addColumn(WINDEGREE, NbBundle.getMessage(WeightedDegree.class, "WeightedDegree.nodecolumn.InDegree"), Double.class, 0.0);
|
||||
}
|
||||
if (!nodeTable.hasColumn(WOUTDEGREE)) {
|
||||
nodeTable.addColumn(WOUTDEGREE, NbBundle.getMessage(WeightedDegree.class, "WeightedDegree.nodecolumn.OutDegree"), Double.class, 0.0);
|
||||
}
|
||||
}
|
||||
if (!nodeTable.hasColumn(WDEGREE)) {
|
||||
nodeTable.addColumn(WDEGREE, NbBundle.getMessage(WeightedDegree.class, "WeightedDegree.nodecolumn.Degree"), Double.class, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDegreeDists(double winDegree, double woutDegree, double wdegree) {
|
||||
if (!inDegreeDist.containsKey(winDegree)) {
|
||||
inDegreeDist.put(winDegree, 0);
|
||||
}
|
||||
inDegreeDist.put(winDegree, inDegreeDist.get(winDegree) + 1);
|
||||
if (!outDegreeDist.containsKey(woutDegree)) {
|
||||
outDegreeDist.put(woutDegree, 0);
|
||||
}
|
||||
outDegreeDist.put(woutDegree, outDegreeDist.get(woutDegree) + 1);
|
||||
if (!degreeDist.containsKey(wdegree)) {
|
||||
degreeDist.put(wdegree, 0);
|
||||
}
|
||||
degreeDist.put(wdegree, degreeDist.get(wdegree) + 1);
|
||||
}
|
||||
|
||||
private void updateDegreeDists(double wdegree) {
|
||||
if (!degreeDist.containsKey(wdegree)) {
|
||||
degreeDist.put(wdegree, 0);
|
||||
}
|
||||
degreeDist.put(wdegree, degreeDist.get(wdegree) + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+5
-1
@@ -7,4 +7,8 @@ OpenIDE-Module-Short-Description=Standard statistics and metrics algorithms
|
||||
Degree.nodecolumn.InDegree = In-Degree
|
||||
Degree.nodecolumn.OutDegree = Out-Degree
|
||||
Degree.nodecolumn.Degree = Degree
|
||||
Degree.graphcolumn.AverageDegree = Average Degree
|
||||
Degree.graphcolumn.AverageDegree = Average Degree
|
||||
|
||||
WeightedDegree.nodecolumn.InDegree = Weighted In-Degree
|
||||
WeightedDegree.nodecolumn.OutDegree = Weighted Out-Degree
|
||||
WeightedDegree.nodecolumn.Degree = Weighted Degree
|
||||
+492
@@ -0,0 +1,492 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.UndirectedGraph;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.Graph;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.impl.ProjectControllerImpl;
|
||||
import org.openide.util.Lookup;
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author akharitonova
|
||||
*/
|
||||
public class ClusteringCoefficientNGTest {
|
||||
|
||||
private ProjectController pc;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public void setUp() {
|
||||
pc = Lookup.getDefault().lookup(ProjectControllerImpl.class);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void initialize() {
|
||||
pc.newProject();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void clean() {
|
||||
pc.closeCurrentProject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneNodeClusteringCoefficient() {
|
||||
GraphModel graphModel=GraphGenerator.generateCompleteUndirectedGraph(1);
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
ArrayWrapper[] network = new ArrayWrapper[1];
|
||||
int[] triangles = new int[1];
|
||||
double[] nodeClustering = new double[1];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
|
||||
assertEquals(avClusteringCoefficient, Double.NaN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConectedNodesClusteringCoefficient() {
|
||||
GraphModel graphModel=GraphGenerator.generateCompleteUndirectedGraph(2);
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
ArrayWrapper[] network = new ArrayWrapper[2];
|
||||
int[] triangles = new int[2];
|
||||
double[] nodeClustering = new double[2];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
|
||||
assertEquals(avClusteringCoefficient, Double.NaN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullGraphClusteringCoefficient() {
|
||||
GraphModel graphModel=GraphGenerator.generateNullUndirectedGraph(5);
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
ArrayWrapper[] network = new ArrayWrapper[5];
|
||||
int[] triangles = new int[5];
|
||||
double[] nodeClustering = new double[5];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
|
||||
assertEquals(avClusteringCoefficient, Double.NaN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteGraphClusteringCoefficient() {
|
||||
GraphModel graphModel=GraphGenerator.generateCompleteUndirectedGraph(5);
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
ArrayWrapper[] network = new ArrayWrapper[5];
|
||||
int[] triangles = new int[5];
|
||||
double[] nodeClustering = new double[5];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
assertEquals(avClusteringCoefficient, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStarGraphClusteringCoefficient() {
|
||||
GraphModel graphModel=GraphGenerator.generateStarUndirectedGraph(5);
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
ArrayWrapper[] network = new ArrayWrapper[6];
|
||||
int[] triangles = new int[6];
|
||||
double[] nodeClustering = new double[6];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
assertEquals(avClusteringCoefficient, 0.0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSpecial1UndirectedGraphClusteringCoefficient() {
|
||||
GraphModel graphModel=Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph=graphModel.getUndirectedGraph();
|
||||
Node node1=graphModel.factory().newNode("0");
|
||||
Node node2=graphModel.factory().newNode("1");
|
||||
Node node3=graphModel.factory().newNode("2");
|
||||
Node node4=graphModel.factory().newNode("3");
|
||||
Node node5=graphModel.factory().newNode("4");
|
||||
Node node6=graphModel.factory().newNode("5");
|
||||
Node node7=graphModel.factory().newNode("6");
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
undirectedGraph.addNode(node6);
|
||||
undirectedGraph.addNode(node7);
|
||||
Edge edge12=graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge13=graphModel.factory().newEdge(node1, node3, false);
|
||||
Edge edge14=graphModel.factory().newEdge(node1, node4, false);
|
||||
Edge edge15=graphModel.factory().newEdge(node1, node5, false);
|
||||
Edge edge16=graphModel.factory().newEdge(node1, node6, false);
|
||||
Edge edge17=graphModel.factory().newEdge(node1, node7, false);
|
||||
Edge edge23=graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge34=graphModel.factory().newEdge(node3, node4, false);
|
||||
Edge edge45=graphModel.factory().newEdge(node4, node5, false);
|
||||
Edge edge56=graphModel.factory().newEdge(node5, node6, false);
|
||||
Edge edge67=graphModel.factory().newEdge(node6, node7, false);
|
||||
Edge edge72=graphModel.factory().newEdge(node7, node2, false);
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge13);
|
||||
undirectedGraph.addEdge(edge14);
|
||||
undirectedGraph.addEdge(edge15);
|
||||
undirectedGraph.addEdge(edge16);
|
||||
undirectedGraph.addEdge(edge17);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge34);
|
||||
undirectedGraph.addEdge(edge45);
|
||||
undirectedGraph.addEdge(edge56);
|
||||
undirectedGraph.addEdge(edge67);
|
||||
undirectedGraph.addEdge(edge72);
|
||||
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
|
||||
ArrayWrapper[] network = new ArrayWrapper[7];
|
||||
int[] triangles = new int[7];
|
||||
double[] nodeClustering = new double[7];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
|
||||
double cl1 = nodeClustering[0];
|
||||
double cl3 = nodeClustering[2];
|
||||
|
||||
double res3=0.667;
|
||||
double diff = 0.01;
|
||||
|
||||
assertEquals(cl1, 0.4);
|
||||
assertTrue(Math.abs(cl3-res3)<diff);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial2UndirectedGraphClusteringCoefficient() {
|
||||
GraphModel graphModel=Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph=graphModel.getUndirectedGraph();
|
||||
Node node1=graphModel.factory().newNode("0");
|
||||
Node node2=graphModel.factory().newNode("1");
|
||||
Node node3=graphModel.factory().newNode("2");
|
||||
Node node4=graphModel.factory().newNode("3");
|
||||
Node node5=graphModel.factory().newNode("4");
|
||||
Node node6=graphModel.factory().newNode("5");
|
||||
Node node7=graphModel.factory().newNode("6");
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
undirectedGraph.addNode(node6);
|
||||
undirectedGraph.addNode(node7);
|
||||
Edge edge12=graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge23=graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge31=graphModel.factory().newEdge(node3, node1, false);
|
||||
Edge edge14=graphModel.factory().newEdge(node1, node4, false);
|
||||
Edge edge45=graphModel.factory().newEdge(node4, node5, false);
|
||||
Edge edge51=graphModel.factory().newEdge(node5, node1, false);
|
||||
Edge edge16=graphModel.factory().newEdge(node1, node6, false);
|
||||
Edge edge67=graphModel.factory().newEdge(node6, node7, false);
|
||||
Edge edge71=graphModel.factory().newEdge(node7, node1, false);
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge31);
|
||||
undirectedGraph.addEdge(edge14);
|
||||
undirectedGraph.addEdge(edge45);
|
||||
undirectedGraph.addEdge(edge51);
|
||||
undirectedGraph.addEdge(edge16);
|
||||
undirectedGraph.addEdge(edge67);
|
||||
undirectedGraph.addEdge(edge71);
|
||||
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
|
||||
ArrayWrapper[] network = new ArrayWrapper[7];
|
||||
int[] triangles = new int[7];
|
||||
double[] nodeClustering = new double[7];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
|
||||
double cl2 = nodeClustering[1];
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
double resAv=0.8857;
|
||||
double diff = 0.01;
|
||||
|
||||
assertEquals(cl2, 1.0);
|
||||
assertTrue(Math.abs(avClusteringCoefficient-resAv)<diff);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial3UndirectedGraphClusteringCoefficient() {
|
||||
GraphModel graphModel=Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph=graphModel.getUndirectedGraph();
|
||||
Node node1=graphModel.factory().newNode("0");
|
||||
Node node2=graphModel.factory().newNode("1");
|
||||
Node node3=graphModel.factory().newNode("2");
|
||||
Node node4=graphModel.factory().newNode("3");
|
||||
Node node5=graphModel.factory().newNode("4");
|
||||
Node node6=graphModel.factory().newNode("5");
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
undirectedGraph.addNode(node6);
|
||||
Edge edge12=graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge23=graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge31=graphModel.factory().newEdge(node3, node1, false);
|
||||
Edge edge14=graphModel.factory().newEdge(node1, node4, false);
|
||||
Edge edge25=graphModel.factory().newEdge(node2, node5, false);
|
||||
Edge edge36=graphModel.factory().newEdge(node3, node6, false);
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge31);
|
||||
undirectedGraph.addEdge(edge14);
|
||||
undirectedGraph.addEdge(edge25);
|
||||
undirectedGraph.addEdge(edge36);;
|
||||
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
|
||||
ArrayWrapper[] network = new ArrayWrapper[6];
|
||||
int[] triangles = new int[6];
|
||||
double[] nodeClustering = new double[6];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
|
||||
double cl1 = nodeClustering[0];
|
||||
|
||||
double res1=0.333;
|
||||
double diff = 0.01;
|
||||
|
||||
assertTrue(Math.abs(cl1-res1)<diff);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTriangleGraphClusteringCoefficient() {
|
||||
GraphModel graphModel=GraphGenerator.generateCompleteUndirectedGraph(3);
|
||||
Graph hgraph = graphModel.getGraph();
|
||||
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
ArrayWrapper[] network = new ArrayWrapper[3];
|
||||
|
||||
int[] triangles = new int[3];
|
||||
double[] nodeClustering = new double[3];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, false);
|
||||
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
assertEquals(avClusteringCoefficient, 1.0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSpecial1DirectedGraphClusteringCoefficient() {
|
||||
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 edge12=graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge23=graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge24=graphModel.factory().newEdge(node2, node4);
|
||||
Edge edge31=graphModel.factory().newEdge(node3, node1);
|
||||
Edge edge34=graphModel.factory().newEdge(node3, node4);
|
||||
Edge edge41=graphModel.factory().newEdge(node4, node1);
|
||||
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge24);
|
||||
directedGraph.addEdge(edge31);
|
||||
directedGraph.addEdge(edge34);
|
||||
directedGraph.addEdge(edge41);
|
||||
|
||||
|
||||
DirectedGraph hgraph = 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(hgraph, network, triangles, nodeClustering, true);
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
|
||||
assertEquals(avClusteringCoefficient, 0.5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTriangleDirectedGraphClusteringCoefficient() {
|
||||
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");
|
||||
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
|
||||
Edge edge12=graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge21=graphModel.factory().newEdge(node2, node1);
|
||||
Edge edge23=graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge32=graphModel.factory().newEdge(node3, node2);
|
||||
Edge edge31=graphModel.factory().newEdge(node3, node1);
|
||||
Edge edge13=graphModel.factory().newEdge(node1, node3);
|
||||
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge21);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge32);
|
||||
directedGraph.addEdge(edge31);
|
||||
directedGraph.addEdge(edge13);
|
||||
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
ArrayWrapper[] network = new ArrayWrapper[3];
|
||||
int[] triangles = new int[3];
|
||||
double[] nodeClustering = new double[3];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, true);
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
|
||||
|
||||
assertEquals(avClusteringCoefficient, 1.);
|
||||
}
|
||||
|
||||
@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 hgraph = 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(hgraph, network, triangles, nodeClustering, true);
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
double res = 0.4167;
|
||||
double diff = 0.01;
|
||||
|
||||
assertTrue(Math.abs(avClusteringCoefficient-res)<diff);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTriangleNonCompleteDirectedGraphClusteringCoefficient() {
|
||||
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");
|
||||
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
|
||||
Edge edge12=graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge21=graphModel.factory().newEdge(node2, node1);
|
||||
Edge edge23=graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge32=graphModel.factory().newEdge(node3, node2);
|
||||
Edge edge13=graphModel.factory().newEdge(node1, node3);
|
||||
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge21);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge32);
|
||||
directedGraph.addEdge(edge13);
|
||||
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
|
||||
ClusteringCoefficient cc = new ClusteringCoefficient();
|
||||
ArrayWrapper[] network = new ArrayWrapper[3];
|
||||
int[] triangles = new int[3];
|
||||
double[] nodeClustering = new double[3];
|
||||
|
||||
HashMap<String, Double> results = cc.computeClusteringCoefficient(hgraph, network, triangles, nodeClustering, true);
|
||||
double avClusteringCoefficient = results.get("clusteringCoefficient");
|
||||
double res = 0.833;
|
||||
double diff = 0.01;
|
||||
|
||||
assertTrue(Math.abs(avClusteringCoefficient-res)<diff);
|
||||
}
|
||||
}
|
||||
+451
@@ -0,0 +1,451 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.graph.api.UndirectedGraph;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.impl.ProjectControllerImpl;
|
||||
import org.openide.util.Lookup;
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Anna
|
||||
*/
|
||||
public class ConnectedComponentsNGTest {
|
||||
|
||||
private ProjectController pc;
|
||||
|
||||
@BeforeClass
|
||||
public void setUp() {
|
||||
pc = Lookup.getDefault().lookup(ProjectControllerImpl.class);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void initialize() {
|
||||
pc.newProject();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void clean() {
|
||||
pc.closeCurrentProject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComputeOneNodeWeeklyConnectedComponents() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(1);
|
||||
UndirectedGraph graph = graphModel.getUndirectedGraph();
|
||||
Node n = graph.getNode("0");
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = new HashMap<Node, Integer>();
|
||||
indicies.put(n, 0);
|
||||
LinkedList<LinkedList<Node>> components = c.computeWeeklyConnectedComponents(graph, indicies);
|
||||
assertEquals(components.size(), 1);
|
||||
}
|
||||
|
||||
@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<Node, Integer>();
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComputeBarbellGraphWeeklyConnectedComponents() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(4);
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node[] nodes = new Node[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) (i + 4)).toString());
|
||||
nodes[i] = currentNode;
|
||||
undirectedGraph.addNode(currentNode);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = i + 1; j < 4; j++) {
|
||||
Edge currentEdge = graphModel.factory().newEdge(nodes[i], nodes[j], false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
}
|
||||
}
|
||||
Edge currentEdge = graphModel.factory().newEdge(undirectedGraph.getNode("0"), undirectedGraph.getNode("5"), false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
UndirectedGraph graph = graphModel.getUndirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> components = c.computeWeeklyConnectedComponents(graph, indicies);
|
||||
assertEquals(components.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial1UndirectedGraphConnectedComponents() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
Node node4 = graphModel.factory().newNode("3");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge14 = graphModel.factory().newEdge(node1, node4, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge25 = graphModel.factory().newEdge(node2, node5, false);
|
||||
Edge edge35 = graphModel.factory().newEdge(node3, node5, false);
|
||||
Edge edge43 = graphModel.factory().newEdge(node4, node3, false);
|
||||
Edge edge51 = graphModel.factory().newEdge(node5, node1, false);
|
||||
Edge edge54 = graphModel.factory().newEdge(node5, node4, false);
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge14);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge25);
|
||||
undirectedGraph.addEdge(edge35);
|
||||
undirectedGraph.addEdge(edge43);
|
||||
undirectedGraph.addEdge(edge51);
|
||||
undirectedGraph.addEdge(edge54);
|
||||
|
||||
UndirectedGraph graph = graphModel.getUndirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> components = c.computeWeeklyConnectedComponents(graph, indicies);
|
||||
assertEquals(components.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial2UndirectedGraphConnectedComponents() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
Node node4 = graphModel.factory().newNode("3");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
Node node7 = graphModel.factory().newNode("6");
|
||||
Node node8 = graphModel.factory().newNode("7");
|
||||
Node node9 = graphModel.factory().newNode("8");
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
undirectedGraph.addNode(node6);
|
||||
undirectedGraph.addNode(node7);
|
||||
undirectedGraph.addNode(node8);
|
||||
undirectedGraph.addNode(node9);
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
|
||||
Edge edge56 = graphModel.factory().newEdge(node5, node6, false);
|
||||
Edge edge64 = graphModel.factory().newEdge(node6, node4, false);
|
||||
Edge edge75 = graphModel.factory().newEdge(node7, node5, false);
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge45);
|
||||
undirectedGraph.addEdge(edge56);
|
||||
undirectedGraph.addEdge(edge64);
|
||||
undirectedGraph.addEdge(edge75);
|
||||
|
||||
UndirectedGraph graph = graphModel.getUndirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> components = c.computeWeeklyConnectedComponents(graph, indicies);
|
||||
|
||||
int componentNumber3 = c.getComponentNumber(components, node3);
|
||||
int componentNumber4 = c.getComponentNumber(components, node4);
|
||||
int componentNumber7 = c.getComponentNumber(components, node7);
|
||||
int componentNumber8 = c.getComponentNumber(components, node8);
|
||||
|
||||
assertEquals(components.size(), 4);
|
||||
assertEquals(componentNumber4, componentNumber7);
|
||||
assertNotEquals(componentNumber3, componentNumber8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedPathGraphConnectedComponents() {
|
||||
GraphModel graphModel = GraphGenerator.generatePathDirectedGraph(4);
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> components = c.top_tarjans(graph, indicies);
|
||||
assertEquals(components.size(), 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedCyclicGraphConnectedComponents() {
|
||||
GraphModel graphModel = GraphGenerator.generateCyclicDirectedGraph(5);
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> components = c.top_tarjans(graph, indicies);
|
||||
assertEquals(components.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial1DirectedGraphConnectedComponents() {
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge14 = graphModel.factory().newEdge(node1, node4);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge25 = graphModel.factory().newEdge(node2, node5);
|
||||
Edge edge35 = graphModel.factory().newEdge(node3, node5);
|
||||
Edge edge43 = graphModel.factory().newEdge(node4, node3);
|
||||
Edge edge51 = graphModel.factory().newEdge(node5, node1);
|
||||
Edge edge54 = graphModel.factory().newEdge(node5, node4);
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge14);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge25);
|
||||
directedGraph.addEdge(edge35);
|
||||
directedGraph.addEdge(edge43);
|
||||
directedGraph.addEdge(edge51);
|
||||
directedGraph.addEdge(edge54);
|
||||
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> components = c.top_tarjans(graph, indicies);
|
||||
assertEquals(components.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial2DirectedGraphConnectedComponents() {
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge14 = graphModel.factory().newEdge(node1, node4);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge25 = graphModel.factory().newEdge(node2, node5);
|
||||
Edge edge35 = graphModel.factory().newEdge(node3, node5);
|
||||
Edge edge43 = graphModel.factory().newEdge(node4, node3);
|
||||
Edge edge54 = graphModel.factory().newEdge(node5, node4);
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge14);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge25);
|
||||
directedGraph.addEdge(edge35);
|
||||
directedGraph.addEdge(edge43);
|
||||
directedGraph.addEdge(edge54);
|
||||
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> weeklyConnectedComponents = c.computeWeeklyConnectedComponents(graph, indicies);
|
||||
LinkedList<LinkedList<Node>> stronglyConnectedComponents = c.top_tarjans(graph, indicies);
|
||||
int componentNumber1 = c.getComponentNumber(stronglyConnectedComponents, node1);
|
||||
int componentNumber3 = c.getComponentNumber(stronglyConnectedComponents, node3);
|
||||
int componentNumber4 = c.getComponentNumber(stronglyConnectedComponents, node4);
|
||||
int componentNumber5 = c.getComponentNumber(stronglyConnectedComponents, node5);
|
||||
|
||||
assertEquals(stronglyConnectedComponents.size(), 3);
|
||||
assertEquals(weeklyConnectedComponents.size(), 1);
|
||||
assertEquals(componentNumber3, componentNumber5);
|
||||
assertNotEquals(componentNumber1, componentNumber4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial3DirectedGraphConnectedComponents() {
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
Node node7 = graphModel.factory().newNode("6");
|
||||
Node node8 = graphModel.factory().newNode("7");
|
||||
Node node9 = graphModel.factory().newNode("8");
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
directedGraph.addNode(node6);
|
||||
directedGraph.addNode(node7);
|
||||
directedGraph.addNode(node8);
|
||||
directedGraph.addNode(node9);
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5);
|
||||
Edge edge56 = graphModel.factory().newEdge(node5, node6);
|
||||
Edge edge64 = graphModel.factory().newEdge(node6, node4);
|
||||
Edge edge75 = graphModel.factory().newEdge(node7, node5);
|
||||
Edge edge89 = graphModel.factory().newEdge(node8, node9);
|
||||
Edge edge98 = graphModel.factory().newEdge(node9, node8);
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge45);
|
||||
directedGraph.addEdge(edge56);
|
||||
directedGraph.addEdge(edge64);
|
||||
directedGraph.addEdge(edge75);
|
||||
directedGraph.addEdge(edge89);
|
||||
directedGraph.addEdge(edge98);
|
||||
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> stronglyConnectedComponents = c.top_tarjans(graph, indicies);
|
||||
|
||||
assertEquals(stronglyConnectedComponents.size(), 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial4DirectedGraphConnectedComponents() {
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
Node node7 = graphModel.factory().newNode("6");
|
||||
Node node8 = graphModel.factory().newNode("7");
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
directedGraph.addNode(node6);
|
||||
directedGraph.addNode(node7);
|
||||
directedGraph.addNode(node8);
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge34 = graphModel.factory().newEdge(node3, node4);
|
||||
Edge edge41 = graphModel.factory().newEdge(node4, node1);
|
||||
Edge edge56 = graphModel.factory().newEdge(node5, node6);
|
||||
Edge edge67 = graphModel.factory().newEdge(node6, node7);
|
||||
Edge edge78 = graphModel.factory().newEdge(node7, node8);
|
||||
Edge edge85 = graphModel.factory().newEdge(node8, node5);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5);
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge34);
|
||||
directedGraph.addEdge(edge41);
|
||||
directedGraph.addEdge(edge56);
|
||||
directedGraph.addEdge(edge67);
|
||||
directedGraph.addEdge(edge78);
|
||||
directedGraph.addEdge(edge85);
|
||||
directedGraph.addEdge(edge45);
|
||||
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> stronglyConnectedComponents = c.top_tarjans(graph, indicies);
|
||||
|
||||
int componentNumber1 = c.getComponentNumber(stronglyConnectedComponents, node1);
|
||||
int componentNumber5 = c.getComponentNumber(stronglyConnectedComponents, node5);
|
||||
|
||||
assertEquals(stronglyConnectedComponents.size(), 2);
|
||||
assertNotEquals(componentNumber1, componentNumber5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial2UndirectedGraphGiantComponent() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
Node node4 = graphModel.factory().newNode("3");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
Node node7 = graphModel.factory().newNode("6");
|
||||
Node node8 = graphModel.factory().newNode("7");
|
||||
Node node9 = graphModel.factory().newNode("8");
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
undirectedGraph.addNode(node6);
|
||||
undirectedGraph.addNode(node7);
|
||||
undirectedGraph.addNode(node8);
|
||||
undirectedGraph.addNode(node9);
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
|
||||
Edge edge56 = graphModel.factory().newEdge(node5, node6, false);
|
||||
Edge edge64 = graphModel.factory().newEdge(node6, node4, false);
|
||||
Edge edge75 = graphModel.factory().newEdge(node7, node5, false);
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge45);
|
||||
undirectedGraph.addEdge(edge56);
|
||||
undirectedGraph.addEdge(edge64);
|
||||
undirectedGraph.addEdge(edge75);
|
||||
|
||||
UndirectedGraph graph = graphModel.getUndirectedGraph();
|
||||
|
||||
ConnectedComponents c = new ConnectedComponents();
|
||||
HashMap<Node, Integer> indicies = c.createIndiciesMap(graph);
|
||||
LinkedList<LinkedList<Node>> components = c.computeWeeklyConnectedComponents(graph, indicies);
|
||||
c.fillComponentSizeList(components);
|
||||
|
||||
int giantComponent = c.getGiantComponent();
|
||||
int componentNumber5 = c.getComponentNumber(components, node5);
|
||||
|
||||
assertEquals(giantComponent, componentNumber5);
|
||||
}
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.Graph;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.impl.ProjectControllerImpl;
|
||||
import org.openide.util.Lookup;
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Anna
|
||||
*/
|
||||
public class DegreeNGTest {
|
||||
|
||||
private ProjectController pc;
|
||||
|
||||
@BeforeClass
|
||||
public void setUp() {
|
||||
pc = Lookup.getDefault().lookup(ProjectControllerImpl.class);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void initialize() {
|
||||
pc.newProject();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void clean() {
|
||||
pc.closeCurrentProject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneNodeDegree() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(1);
|
||||
Graph graph = graphModel.getGraph();
|
||||
Node n = graph.getNode("0");
|
||||
|
||||
Degree d = new Degree();
|
||||
int degree = d.calculateDegree(graph, n);
|
||||
assertEquals(degree, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullGraphDegree() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(5);
|
||||
Graph graph = graphModel.getGraph();
|
||||
Node n = graph.getNode("1");
|
||||
Degree d = new Degree();
|
||||
int degree = d.calculateDegree(graph, n);
|
||||
double avDegree = d.calculateAverageDegree(graph, false, false);
|
||||
assertEquals(degree, 0);
|
||||
assertEquals(avDegree, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteGraphDegree() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(5);
|
||||
Graph graph = graphModel.getGraph();
|
||||
Node n = graph.getNode("2");
|
||||
Degree d = new Degree();
|
||||
int degree = d.calculateDegree(graph, n);
|
||||
assertEquals(degree, 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStarGraphDegree() {
|
||||
GraphModel graphModel = GraphGenerator.generateStarUndirectedGraph(5);
|
||||
Graph graph = graphModel.getGraph();
|
||||
Node n1 = graph.getNode("0");
|
||||
Node n2 = graph.getNode("1");
|
||||
Degree d = new Degree();
|
||||
int degree1 = d.calculateDegree(graph, n1);
|
||||
int degree2 = d.calculateDegree(graph, n2);
|
||||
double avDegree = d.calculateAverageDegree(graph, false, false);
|
||||
double expectedAvDegree = 1.6667;
|
||||
double diff = Math.abs(avDegree - expectedAvDegree);
|
||||
assertEquals(degree1, 5);
|
||||
assertEquals(degree2, 1);
|
||||
assertTrue(diff < 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCyclicGraphDegree() {
|
||||
GraphModel graphModel = GraphGenerator.generateCyclicUndirectedGraph(5);
|
||||
Graph graph = graphModel.getGraph();
|
||||
Node n = graph.getNode("3");
|
||||
Degree d = new Degree();
|
||||
int degree = d.calculateDegree(graph, n);
|
||||
double avDegree = d.calculateAverageDegree(graph, false, false);
|
||||
assertEquals(degree, 2);
|
||||
assertEquals(avDegree, 2.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedPathGraphDegree() {
|
||||
GraphModel graphModel = GraphGenerator.generatePathDirectedGraph(2);
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
Node n1 = graph.getNode("0");
|
||||
Node n2 = graph.getNode("1");
|
||||
Degree d = new Degree();
|
||||
int inDegree1 = d.calculateInDegree(graph, n1);
|
||||
int inDegree2 = d.calculateInDegree(graph, n2);
|
||||
int outDegree1 = d.calculateOutDegree(graph, n1);
|
||||
double avDegree = d.calculateAverageDegree(graph, true, false);
|
||||
assertEquals(inDegree1, 0);
|
||||
assertEquals(inDegree2, 1);
|
||||
assertEquals(outDegree1, 1);
|
||||
assertEquals(avDegree, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedCyclicGraphDegree() {
|
||||
GraphModel graphModel = GraphGenerator.generateCyclicDirectedGraph(5);
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
Node n1 = graph.getNode("0");
|
||||
Node n3 = graph.getNode("2");
|
||||
Node n5 = graph.getNode("4");
|
||||
Degree d = new Degree();
|
||||
int inDegree3 = d.calculateInDegree(graph, n3);
|
||||
int degree1 = d.calculateDegree(graph, n1);
|
||||
int outDegree5 = d.calculateOutDegree(graph, n5);
|
||||
double avDegree = d.calculateAverageDegree(graph, true, false);
|
||||
assertEquals(inDegree3, 1);
|
||||
assertEquals(degree1, 2);
|
||||
assertEquals(outDegree5, 1);
|
||||
assertEquals(avDegree, 2.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedStarOutGraphDegree() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
directedGraph.addNode(firstNode);
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
directedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(firstNode, currentNode);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
}
|
||||
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
Node n1 = graph.getNode("0");
|
||||
Node n3 = graph.getNode("2");
|
||||
Degree d = new Degree();
|
||||
int inDegree1 = d.calculateInDegree(graph, n1);
|
||||
int outDegree1 = d.calculateOutDegree(graph, n1);
|
||||
int degree3 = d.calculateDegree(graph, n3);
|
||||
|
||||
assertEquals(inDegree1, 0);
|
||||
assertEquals(outDegree1, 5);
|
||||
assertEquals(degree3, 1);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Anna
|
||||
*/
|
||||
public class EdgeWrapperNGTest {
|
||||
|
||||
public EdgeWrapperNGTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSomeMethod() {
|
||||
}
|
||||
}
|
||||
+437
@@ -0,0 +1,437 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.graph.api.UndirectedGraph;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.impl.ProjectControllerImpl;
|
||||
import org.openide.util.Lookup;
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Anna
|
||||
*/
|
||||
public class EigenvectorCentralityNGTest {
|
||||
|
||||
private ProjectController pc;
|
||||
|
||||
@BeforeClass
|
||||
public void setUp() {
|
||||
pc = Lookup.getDefault().lookup(ProjectControllerImpl.class);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void initialize() {
|
||||
pc.newProject();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void clean() {
|
||||
pc.closeCurrentProject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneNodeEigenvectorCentrality() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(1);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[1];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, false, 100);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
int index = invIndicies.get(n1);
|
||||
double ec1 = centralities[index];
|
||||
|
||||
assertEquals(ec1, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConnectedNodesEigenvectorCentrality() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(2);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[2];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, false, 100);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
int index = invIndicies.get(n1);
|
||||
double ec1 = centralities[index];
|
||||
|
||||
assertEquals(ec1, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullGraphEigenvectorCentrality() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(5);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[5];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, false, 100);
|
||||
|
||||
Node n2 = hgraph.getNode("1");
|
||||
int index = invIndicies.get(n2);
|
||||
double ec2 = centralities[index];
|
||||
|
||||
assertEquals(ec2, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteUndirectedGraphEigenvectorCentrality() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(5);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[5];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, false, 100);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n3 = hgraph.getNode("2");
|
||||
int index1 = invIndicies.get(n1);
|
||||
int index3 = invIndicies.get(n3);
|
||||
double ec1 = centralities[index1];
|
||||
double ec3 = centralities[index3];
|
||||
|
||||
assertEquals(ec1, 1.0);
|
||||
assertEquals(ec3, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial1UndirectedGraphEigenvectorCentrlity() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
Node node4 = graphModel.factory().newNode("3");
|
||||
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge34 = graphModel.factory().newEdge(node3, node4, false);
|
||||
Edge edge13 = graphModel.factory().newEdge(node1, node3, false);
|
||||
Edge edge24 = graphModel.factory().newEdge(node2, node4, false);
|
||||
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge34);
|
||||
undirectedGraph.addEdge(edge13);
|
||||
undirectedGraph.addEdge(edge24);
|
||||
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[4];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, false, 100);
|
||||
|
||||
int index1 = invIndicies.get(node1);
|
||||
int index2 = invIndicies.get(node2);
|
||||
int index3 = invIndicies.get(node3);
|
||||
double ec1 = centralities[index1];
|
||||
double ec2 = centralities[index2];
|
||||
double ec3 = centralities[index3];
|
||||
|
||||
assertEquals(ec2, ec3);
|
||||
assertNotEquals(ec1, ec2);
|
||||
assertEquals(ec3, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial2UndirectedGraphEigenvectorCentrlity() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
Node node4 = graphModel.factory().newNode("3");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
|
||||
Edge edge13 = graphModel.factory().newEdge(node1, node3, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge34 = graphModel.factory().newEdge(node3, node4, false);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
|
||||
|
||||
undirectedGraph.addEdge(edge13);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge34);
|
||||
undirectedGraph.addEdge(edge45);
|
||||
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[5];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, false, 100);
|
||||
|
||||
int index2 = invIndicies.get(node2);
|
||||
int index3 = invIndicies.get(node3);
|
||||
int index4 = invIndicies.get(node4);
|
||||
double ec2 = centralities[index2];
|
||||
double ec3 = centralities[index3];
|
||||
double ec4 = centralities[index4];
|
||||
|
||||
double res = 0.765;
|
||||
double diff = 0.01;
|
||||
|
||||
assertTrue(ec2 < ec3);
|
||||
assertTrue(Math.abs(ec4 - res) < diff);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial3UndirectedGraphEigenvectorCentrlity() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
|
||||
Edge edge11 = graphModel.factory().newEdge(node1, node1, false);
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge33 = graphModel.factory().newEdge(node3, node3, false);
|
||||
|
||||
undirectedGraph.addEdge(edge11);
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge33);
|
||||
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[3];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, false, 100);
|
||||
|
||||
int index1 = invIndicies.get(node1);
|
||||
int index2 = invIndicies.get(node2);
|
||||
double ec1 = centralities[index1];
|
||||
double ec2 = centralities[index2];
|
||||
|
||||
assertEquals(ec1, ec2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCyclicDirectedGraphEigenvectorCentrality() {
|
||||
GraphModel graphModel = GraphGenerator.generateCyclicDirectedGraph(3);
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[3];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, true, 100);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
int index1 = invIndicies.get(n1);
|
||||
double ec1 = centralities[index1];
|
||||
|
||||
assertEquals(ec1, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecial1DirectedEigenvectorCentrality() {
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge31 = graphModel.factory().newEdge(node3, node1);
|
||||
Edge edge42 = graphModel.factory().newEdge(node4, node2);
|
||||
Edge edge54 = graphModel.factory().newEdge(node5, node4);
|
||||
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge31);
|
||||
directedGraph.addEdge(edge42);
|
||||
directedGraph.addEdge(edge54);
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[5];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, true, 1000);
|
||||
|
||||
int index1 = invIndicies.get(node1);
|
||||
int index2 = invIndicies.get(node2);
|
||||
int index4 = invIndicies.get(node4);
|
||||
int index5 = invIndicies.get(node5);
|
||||
|
||||
double ec1 = centralities[index1];
|
||||
double ec2 = centralities[index2];
|
||||
double ec4 = centralities[index4];
|
||||
double ec5 = centralities[index5];
|
||||
|
||||
double diff = 0.01;
|
||||
double res0 = 0.;
|
||||
double res1 = 1.;
|
||||
|
||||
assertEquals(ec5, 0.0);
|
||||
assertTrue(Math.abs(ec4 - res0) < diff);
|
||||
assertTrue(Math.abs(ec1 - res1) < diff);
|
||||
assertTrue(Math.abs(ec1 - ec2) < diff);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedStarOutEigenvectorCentrality() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
directedGraph.addNode(firstNode);
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
directedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(firstNode, currentNode);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
}
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[6];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, true, 100);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n2 = hgraph.getNode("1");
|
||||
int index1 = invIndicies.get(n1);
|
||||
int index2 = invIndicies.get(n2);
|
||||
double ec1 = centralities[index1];
|
||||
double ec2 = centralities[index2];
|
||||
|
||||
assertEquals(ec1, 0.0);
|
||||
assertEquals(ec2, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathDirectedGraphEigenvectorCentrality() {
|
||||
GraphModel graphModel = GraphGenerator.generatePathDirectedGraph(4);
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
|
||||
EigenvectorCentrality ec = new EigenvectorCentrality();
|
||||
|
||||
double[] centralities = new double[4];
|
||||
|
||||
HashMap<Integer, Node> indicies = new HashMap();
|
||||
HashMap<Node, Integer> invIndicies = new HashMap();
|
||||
|
||||
ec.fillIndiciesMaps(hgraph, centralities, indicies, invIndicies);
|
||||
|
||||
ec.calculateEigenvectorCentrality(hgraph, centralities, indicies, invIndicies, true, 100);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n4 = hgraph.getNode("3");
|
||||
int index1 = invIndicies.get(n1);
|
||||
int index4 = invIndicies.get(n4);
|
||||
double ec1 = centralities[index1];
|
||||
double ec4 = centralities[index4];
|
||||
|
||||
assertEquals(ec1, 0.0);
|
||||
assertEquals(ec4, 1.0);
|
||||
}
|
||||
}
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.Graph;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.graph.api.UndirectedGraph;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.impl.ProjectControllerImpl;
|
||||
import org.openide.util.Lookup;
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Anna
|
||||
*/
|
||||
public class GraphDensityNGTest {
|
||||
|
||||
private ProjectController pc;
|
||||
|
||||
@BeforeClass
|
||||
public void setUp() {
|
||||
pc = Lookup.getDefault().lookup(ProjectControllerImpl.class);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void initialize() {
|
||||
pc.newProject();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void clean() {
|
||||
pc.closeCurrentProject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneNodeDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(1);
|
||||
Graph graph = graphModel.getGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, false);
|
||||
assertEquals(density, Double.NaN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConnectedNodesDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(2);
|
||||
Graph graph = graphModel.getGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, false);
|
||||
assertEquals(density, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullGraphDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(5);
|
||||
Graph graph = graphModel.getGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, false);
|
||||
assertEquals(density, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteGraphDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(5);
|
||||
Graph graph = graphModel.getGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, false);
|
||||
assertEquals(density, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCyclicGraphDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCyclicUndirectedGraph(6);
|
||||
Graph graph = graphModel.getGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, false);
|
||||
assertEquals(density, 0.4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelfLoopNodeDensity() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node currentNode = graphModel.factory().newNode("0");
|
||||
undirectedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(currentNode, currentNode, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
Graph graph = graphModel.getGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, false);
|
||||
assertEquals(density, Double.POSITIVE_INFINITY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteGraphWithSelfLoopsDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(3);
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node n1 = undirectedGraph.getNode("0");
|
||||
Node n2 = undirectedGraph.getNode("1");
|
||||
Node n3 = undirectedGraph.getNode("2");
|
||||
Edge currentEdge = graphModel.factory().newEdge(n1, n1, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
currentEdge = graphModel.factory().newEdge(n2, n2, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
currentEdge = graphModel.factory().newEdge(n3, n3, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
Graph graph = graphModel.getGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, false);
|
||||
assertEquals(density, 2.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoCompleteGraphsDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(4);
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node[] nodes = new Node[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) (i + 4)).toString());
|
||||
nodes[i] = currentNode;
|
||||
undirectedGraph.addNode(currentNode);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = i + 1; j < 4; j++) {
|
||||
Edge currentEdge = graphModel.factory().newEdge(nodes[i], nodes[j], false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
}
|
||||
}
|
||||
Graph graph = graphModel.getGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, false);
|
||||
double expectedAvDegree = 0.4286;
|
||||
double diff = Math.abs(density - expectedAvDegree);
|
||||
assertTrue(diff < 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedPathGraphDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generatePathDirectedGraph(2);
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, true);
|
||||
assertEquals(density, 0.5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedCyclicGraphDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCyclicDirectedGraph(5);
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, true);
|
||||
assertEquals(density, 0.25);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedCompleteGraphDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteDirectedGraph(5);
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, true);
|
||||
assertEquals(density, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedCompleteGraphWithSelfLoopsDensity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteDirectedGraph(3);
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
Node n1 = directedGraph.getNode("0");
|
||||
Node n2 = directedGraph.getNode("1");
|
||||
Node n3 = directedGraph.getNode("2");
|
||||
Edge currentEdge = graphModel.factory().newEdge(n1, n1);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
currentEdge = graphModel.factory().newEdge(n2, n2);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
currentEdge = graphModel.factory().newEdge(n3, n3);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
|
||||
DirectedGraph graph = graphModel.getDirectedGraph();
|
||||
|
||||
GraphDensity d = new GraphDensity();
|
||||
double density = d.calculateDensity(graph, true);
|
||||
assertEquals(density, 1.5);
|
||||
}
|
||||
}
|
||||
+1616
Diferenças do arquivo suprimidas por serem muito extensas
Carregar Diff
+174
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.graph.api.UndirectedGraph;
|
||||
import org.openide.util.Lookup;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author mbastian
|
||||
*/
|
||||
public class GraphGenerator {
|
||||
|
||||
public static GraphModel generateNullUndirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
for (int i = 0; i < n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
undirectedGraph.addNode(currentNode);
|
||||
}
|
||||
return graphModel;
|
||||
}
|
||||
|
||||
public static GraphModel generateCompleteUndirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node[] nodes = new Node[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
nodes[i] = currentNode;
|
||||
undirectedGraph.addNode(currentNode);
|
||||
}
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
Edge currentEdge = graphModel.factory().newEdge(nodes[i], nodes[j], false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
}
|
||||
}
|
||||
return graphModel;
|
||||
}
|
||||
|
||||
public static GraphModel generatePathUndirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
if (n <= 0) {
|
||||
return graphModel;
|
||||
}
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
undirectedGraph.addNode(firstNode);
|
||||
Node prevNode = firstNode;
|
||||
for (int i = 1; i < n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
undirectedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(prevNode, currentNode, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
prevNode = currentNode;
|
||||
}
|
||||
return graphModel;
|
||||
}
|
||||
|
||||
public static GraphModel generateCyclicUndirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
if (n <= 0) {
|
||||
return graphModel;
|
||||
}
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
undirectedGraph.addNode(firstNode);
|
||||
Node prevNode = firstNode;
|
||||
for (int i = 1; i < n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
undirectedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(prevNode, currentNode, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
prevNode = currentNode;
|
||||
}
|
||||
Edge currentEdge = graphModel.factory().newEdge(prevNode, firstNode, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
return graphModel;
|
||||
}
|
||||
|
||||
//generates graph from n+1 nodes
|
||||
public static GraphModel generateStarUndirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
undirectedGraph.addNode(firstNode);
|
||||
for (int i = 1; i <= n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
undirectedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(firstNode, currentNode, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
}
|
||||
return graphModel;
|
||||
}
|
||||
|
||||
public static GraphModel generateNullDirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
for (int i = 0; i < n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
directedGraph.addNode(currentNode);
|
||||
}
|
||||
return graphModel;
|
||||
}
|
||||
|
||||
public static GraphModel generateCompleteDirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
Node[] nodes = new Node[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
nodes[i] = currentNode;
|
||||
directedGraph.addNode(currentNode);
|
||||
}
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
Edge currentEdge = graphModel.factory().newEdge(nodes[i], nodes[j]);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
currentEdge = graphModel.factory().newEdge(nodes[j], nodes[i]);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
}
|
||||
}
|
||||
return graphModel;
|
||||
}
|
||||
|
||||
public static GraphModel generatePathDirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
if (n <= 0) {
|
||||
return graphModel;
|
||||
}
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
directedGraph.addNode(firstNode);
|
||||
Node prevNode = firstNode;
|
||||
for (int i = 1; i < n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
directedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(prevNode, currentNode);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
prevNode = currentNode;
|
||||
}
|
||||
return graphModel;
|
||||
}
|
||||
|
||||
public static GraphModel generateCyclicDirectedGraph(int n) {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
if (n <= 0) {
|
||||
return graphModel;
|
||||
}
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
directedGraph.addNode(firstNode);
|
||||
Node prevNode = firstNode;
|
||||
for (int i = 1; i < n; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
directedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(prevNode, currentNode);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
prevNode = currentNode;
|
||||
}
|
||||
Edge currentEdge = graphModel.factory().newEdge(prevNode, firstNode);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
return graphModel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,460 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.Graph;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.graph.api.UndirectedGraph;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.impl.ProjectControllerImpl;
|
||||
import org.openide.util.Lookup;
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Anna
|
||||
*/
|
||||
public class HitsNGTest {
|
||||
|
||||
private ProjectController pc;
|
||||
|
||||
@BeforeClass
|
||||
public void setUp() {
|
||||
pc = Lookup.getDefault().lookup(ProjectControllerImpl.class);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void initialize() {
|
||||
pc.newProject();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void clean() {
|
||||
pc.closeCurrentProject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneNodeHits() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(1);
|
||||
Graph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[1];
|
||||
double[] hubs = new double[1];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, false, 0.01);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
int index = indicies.get(n1);
|
||||
double hub1 = hubs[index];
|
||||
double auth1 = authority[index];
|
||||
|
||||
assertEquals(hub1, 1.0);
|
||||
assertEquals(auth1, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConnectedNodesHits() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(2);
|
||||
Graph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[2];
|
||||
double[] hubs = new double[2];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, false, 0.01);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n2 = hgraph.getNode("1");
|
||||
int index1 = indicies.get(n1);
|
||||
int index2 = indicies.get(n2);
|
||||
double hub1 = hubs[index1];
|
||||
double auth2 = authority[index2];
|
||||
|
||||
assertEquals(hub1, 0.5);
|
||||
assertEquals(auth2, 0.5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullGraphHits() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(5);
|
||||
Graph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[5];
|
||||
double[] hubs = new double[5];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority,indicies, false, 0.01);
|
||||
|
||||
Node n2 = hgraph.getNode("1");
|
||||
Node n3 = hgraph.getNode("2");
|
||||
int index2 = indicies.get(n2);
|
||||
int index3 = indicies.get(n3);
|
||||
double hub2 = hubs[index2];
|
||||
double auth3 = authority[index3];
|
||||
|
||||
assertEquals(hub2, 0.2);
|
||||
assertEquals(auth3, 0.2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteGraphHits() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(5);
|
||||
Graph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[5];
|
||||
double[] hubs = new double[5];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, false, 0.01);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n5 = hgraph.getNode("4");
|
||||
int index1 = indicies.get(n1);
|
||||
int index5 = indicies.get(n5);
|
||||
double hub1 = hubs[index1];
|
||||
double auth5 = authority[index5];
|
||||
|
||||
assertEquals(hub1, 0.2);
|
||||
assertEquals(auth5, 0.2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStarGraphHits() {
|
||||
GraphModel graphModel = GraphGenerator.generateStarUndirectedGraph(5);
|
||||
Graph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[6];
|
||||
double[] hubs = new double[6];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, false, 0.01);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n3 = hgraph.getNode("2");
|
||||
Node n4 = hgraph.getNode("3");
|
||||
int index1 = indicies.get(n1);
|
||||
int index3 = indicies.get(n3);
|
||||
int index4 = indicies.get(n4);
|
||||
|
||||
double hub1 = hubs[index1];
|
||||
double hub3 = hubs[index3];
|
||||
double auth1 = authority[index1];
|
||||
double auth4 = authority[index4];
|
||||
|
||||
boolean b1 = hub1 > hub3;
|
||||
boolean b2 = auth1 > auth4;
|
||||
|
||||
assertTrue(b1);
|
||||
assertTrue(b2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGraphWithSelfLoopsHits() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge11 = graphModel.factory().newEdge(node1, node1, false);
|
||||
Edge edge33 = graphModel.factory().newEdge(node3, node3, false);
|
||||
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge11);
|
||||
undirectedGraph.addEdge(edge33);
|
||||
|
||||
Graph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Hits hit = new Hits();
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
double[] authority = new double[3];
|
||||
double[] hubs = new double[3];
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, false, 0.01);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n2 = hgraph.getNode("1");
|
||||
int index1 = indicies.get(n1);
|
||||
int index2 = indicies.get(n2);
|
||||
|
||||
double hub1 = hubs[index1];
|
||||
double hub2 = hubs[index2];
|
||||
|
||||
boolean b1 = hub2 > hub1;
|
||||
|
||||
assertTrue(b1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedSpecial1GraphHits() {
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
|
||||
Edge edge14 = graphModel.factory().newEdge(node1, node4);
|
||||
Edge edge15 = graphModel.factory().newEdge(node1, node5);
|
||||
Edge edge24 = graphModel.factory().newEdge(node2, node4);
|
||||
Edge edge25 = graphModel.factory().newEdge(node2, node5);
|
||||
Edge edge34 = graphModel.factory().newEdge(node3, node4);
|
||||
Edge edge35 = graphModel.factory().newEdge(node3, node5);
|
||||
|
||||
directedGraph.addEdge(edge14);
|
||||
directedGraph.addEdge(edge15);
|
||||
directedGraph.addEdge(edge24);
|
||||
directedGraph.addEdge(edge25);
|
||||
directedGraph.addEdge(edge34);
|
||||
directedGraph.addEdge(edge35);
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[5];
|
||||
double[] hubs = new double[5];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, true, 0.01);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n2 = hgraph.getNode("1");
|
||||
Node n4 = hgraph.getNode("3");
|
||||
Node n5 = hgraph.getNode("4");
|
||||
|
||||
int index1 = indicies.get(n1);
|
||||
int index2 = indicies.get(n2);
|
||||
int index4 = indicies.get(n4);
|
||||
int index5 = indicies.get(n5);
|
||||
|
||||
double hub1 = hubs[index1];
|
||||
double hub4 = hubs[index4];
|
||||
double auth2 = authority[index2];
|
||||
double auth5 = authority[index5];
|
||||
|
||||
double res = 0.333;
|
||||
double diff = 0.01;
|
||||
|
||||
assertTrue(Math.abs(hub1 - res) < diff);
|
||||
assertEquals(hub4, 0.);
|
||||
assertEquals(auth2, 0.);
|
||||
assertEquals(auth5, 0.5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedStarOutGraphHits() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
directedGraph.addNode(firstNode);
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
directedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(firstNode, currentNode);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
}
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[6];
|
||||
double[] hubs = new double[6];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, true, 0.01);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n3 = hgraph.getNode("2");
|
||||
|
||||
int index1 = indicies.get(n1);
|
||||
int index3 = indicies.get(n3);
|
||||
|
||||
double hub1 = hubs[index1];
|
||||
double hub3 = hubs[index3];
|
||||
double auth1 = authority[index1];
|
||||
double auth3 = authority[index3];
|
||||
|
||||
double res = 0.146;
|
||||
double diff = 0.01;
|
||||
|
||||
assertEquals(hub1, 1.);
|
||||
assertEquals(auth1, 0.);
|
||||
assertEquals(hub3, 0.);
|
||||
assertEquals(auth3, 0.2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedSpecial2GraphHits() {
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
directedGraph.addNode(node6);
|
||||
|
||||
Edge edge21 = graphModel.factory().newEdge(node2, node1);
|
||||
Edge edge31 = graphModel.factory().newEdge(node3, node1);
|
||||
Edge edge41 = graphModel.factory().newEdge(node4, node1);
|
||||
Edge edge51 = graphModel.factory().newEdge(node5, node1);
|
||||
Edge edge36 = graphModel.factory().newEdge(node3, node6);
|
||||
Edge edge46 = graphModel.factory().newEdge(node4, node6);
|
||||
Edge edge56 = graphModel.factory().newEdge(node5, node6);
|
||||
|
||||
directedGraph.addEdge(edge21);
|
||||
directedGraph.addEdge(edge31);
|
||||
directedGraph.addEdge(edge41);
|
||||
directedGraph.addEdge(edge51);
|
||||
directedGraph.addEdge(edge36);
|
||||
directedGraph.addEdge(edge46);
|
||||
directedGraph.addEdge(edge56);
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[6];
|
||||
double[] hubs = new double[6];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, true, 0.01);
|
||||
|
||||
int index1 = indicies.get(node1);
|
||||
int index2 = indicies.get(node2);
|
||||
int index3 = indicies.get(node3);
|
||||
int index5 = indicies.get(node5);
|
||||
int index6 = indicies.get(node6);
|
||||
|
||||
double hub2 = hubs[index2];
|
||||
double hub3 = hubs[index3];
|
||||
double hub5 = hubs[index5];
|
||||
double hub6 = hubs[index6];
|
||||
double auth1 = authority[index1];
|
||||
double auth3 = authority[index3];
|
||||
double auth6 = authority[index6];
|
||||
|
||||
assertEquals(hub3, hub5);
|
||||
assertTrue(hub3 > hub2);
|
||||
assertTrue(auth1 > auth6);
|
||||
assertEquals(hub6, 0.);
|
||||
assertEquals(auth3, 0.);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedSpecial3GraphHits() {
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
directedGraph.addNode(node6);
|
||||
|
||||
Edge edge15 = graphModel.factory().newEdge(node1, node5);
|
||||
Edge edge25 = graphModel.factory().newEdge(node2, node5);
|
||||
Edge edge35 = graphModel.factory().newEdge(node3, node5);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5);
|
||||
Edge edge56 = graphModel.factory().newEdge(node5, node6);
|
||||
|
||||
directedGraph.addEdge(edge15);
|
||||
directedGraph.addEdge(edge25);
|
||||
directedGraph.addEdge(edge35);
|
||||
directedGraph.addEdge(edge45);
|
||||
directedGraph.addEdge(edge56);
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
Hits hit = new Hits();
|
||||
|
||||
double[] authority = new double[6];
|
||||
double[] hubs = new double[6];
|
||||
|
||||
HashMap<Node, Integer> indicies = hit.createIndiciesMap(hgraph);
|
||||
|
||||
hit.calculateHits(hgraph, hubs, authority, indicies, true, 0.01);
|
||||
|
||||
int index1 = indicies.get(node1);
|
||||
int index3 = indicies.get(node3);
|
||||
int index5 = indicies.get(node5);
|
||||
int index6 = indicies.get(node6);
|
||||
|
||||
double hub1 = hubs[index1];
|
||||
double hub3 = hubs[index3];
|
||||
double hub5 = hubs[index5];
|
||||
double auth5 = authority[index5];
|
||||
double auth6 = authority[index6];
|
||||
|
||||
assertEquals(hub1, hub3);
|
||||
assertTrue(hub1 > hub5);
|
||||
assertTrue(auth5 > auth6);
|
||||
}
|
||||
|
||||
}
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.graph.api.UndirectedGraph;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.impl.ProjectControllerImpl;
|
||||
import org.openide.util.Lookup;
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Anna
|
||||
*/
|
||||
public class ModularityNGTest {
|
||||
|
||||
private ProjectController pc;
|
||||
|
||||
@BeforeClass
|
||||
public void setUp() {
|
||||
pc = Lookup.getDefault().lookup(ProjectControllerImpl.class);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void initialize() {
|
||||
pc.newProject();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void clean() {
|
||||
pc.closeCurrentProject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConnectedNodesModularity() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(2);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Modularity mod = new Modularity();
|
||||
|
||||
Modularity.CommunityStructure theStructure = mod.new CommunityStructure(hgraph);
|
||||
int[] comStructure = new int[hgraph.getNodeCount()];
|
||||
|
||||
HashMap<String, Double> modularityValues = mod.computeModularity(hgraph, theStructure, comStructure,
|
||||
1., true, false);
|
||||
|
||||
double modValue = modularityValues.get("modularity");
|
||||
int class1 = comStructure[0];
|
||||
int class2 = comStructure[1];
|
||||
|
||||
assertEquals(modValue, 0.0);
|
||||
assertEquals(class1, class2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGraphWithouLinksModularity() {
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(5);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Modularity mod = new Modularity();
|
||||
|
||||
Modularity.CommunityStructure theStructure = mod.new CommunityStructure(hgraph);
|
||||
int[] comStructure = new int[hgraph.getNodeCount()];
|
||||
|
||||
HashMap<String, Double> modularityValues = mod.computeModularity(hgraph, theStructure, comStructure,
|
||||
1., true, false);
|
||||
|
||||
double modValue = modularityValues.get("modularity");
|
||||
|
||||
assertEquals(modValue, Double.NaN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComputeBarbellGraphModularityNormalResolution() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(4);
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node[] nodes = new Node[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) (i + 4)).toString());
|
||||
nodes[i] = currentNode;
|
||||
undirectedGraph.addNode(currentNode);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = i + 1; j < 4; j++) {
|
||||
Edge currentEdge = graphModel.factory().newEdge(nodes[i], nodes[j], false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
}
|
||||
}
|
||||
Edge currentEdge = graphModel.factory().newEdge(undirectedGraph.getNode("0"), undirectedGraph.getNode("5"), false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
UndirectedGraph graph = graphModel.getUndirectedGraph();
|
||||
|
||||
Modularity mod = new Modularity();
|
||||
|
||||
Modularity.CommunityStructure theStructure = mod.new CommunityStructure(graph);
|
||||
int[] comStructure = new int[graph.getNodeCount()];
|
||||
|
||||
HashMap<String, Double> modularityValues = mod.computeModularity(graph, theStructure, comStructure,
|
||||
1., true, false);
|
||||
|
||||
double modValue = modularityValues.get("modularity");
|
||||
|
||||
int class4 = comStructure[0];
|
||||
int class5 = comStructure[5];
|
||||
|
||||
boolean correctResult = (class4 != class5 || modValue == 0.);
|
||||
|
||||
assertTrue(correctResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComputeBarbellGraphHighResolution() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(4);
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node[] nodes = new Node[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) (i + 4)).toString());
|
||||
nodes[i] = currentNode;
|
||||
undirectedGraph.addNode(currentNode);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = i + 1; j < 4; j++) {
|
||||
Edge currentEdge = graphModel.factory().newEdge(nodes[i], nodes[j], false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
}
|
||||
}
|
||||
Edge currentEdge = graphModel.factory().newEdge(undirectedGraph.getNode("0"), undirectedGraph.getNode("5"), false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
UndirectedGraph graph = graphModel.getUndirectedGraph();
|
||||
|
||||
Modularity mod = new Modularity();
|
||||
|
||||
Modularity.CommunityStructure theStructure = mod.new CommunityStructure(graph);
|
||||
int[] comStructure = new int[graph.getNodeCount()];
|
||||
|
||||
HashMap<String, Double> modularityValues = mod.computeModularity(graph, theStructure, comStructure,
|
||||
100., true, false);
|
||||
|
||||
double modValue = modularityValues.get("modularity");
|
||||
|
||||
int class4 = comStructure[0];
|
||||
int class5 = comStructure[5];
|
||||
|
||||
assertEquals(modValue, 0.0);
|
||||
assertEquals(class4, class5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComputeBarbellGraphModularityHasHighWeight() {
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(4);
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node[] nodes = new Node[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) (i + 4)).toString());
|
||||
nodes[i] = currentNode;
|
||||
undirectedGraph.addNode(currentNode);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = i + 1; j < 4; j++) {
|
||||
Edge currentEdge = graphModel.factory().newEdge(nodes[i], nodes[j], false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
}
|
||||
}
|
||||
Edge currentEdge = graphModel.factory().newEdge(undirectedGraph.getNode("0"), undirectedGraph.getNode("5"), 0, 100.f, false);
|
||||
undirectedGraph.addEdge(currentEdge);
|
||||
UndirectedGraph graph = graphModel.getUndirectedGraph();
|
||||
|
||||
Modularity mod = new Modularity();
|
||||
|
||||
Modularity.CommunityStructure theStructure = mod.new CommunityStructure(graph);
|
||||
int[] comStructure = new int[graph.getNodeCount()];
|
||||
|
||||
HashMap<String, Double> modularityValues = mod.computeModularity(graph, theStructure, comStructure,
|
||||
1., true, true);
|
||||
|
||||
int class4 = comStructure[0];
|
||||
int class5 = comStructure[5];
|
||||
|
||||
assertEquals(class4, class5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCyclicWithWeightsGraphModularity() {
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
Node node4 = graphModel.factory().newNode("3");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
Node node7 = graphModel.factory().newNode("6");
|
||||
Node node8 = graphModel.factory().newNode("7");
|
||||
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
undirectedGraph.addNode(node6);
|
||||
undirectedGraph.addNode(node7);
|
||||
undirectedGraph.addNode(node8);
|
||||
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2, 0, 10.f, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, false);
|
||||
Edge edge34 = graphModel.factory().newEdge(node3, node4, 0, 10.f, false);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
|
||||
Edge edge56 = graphModel.factory().newEdge(node5, node6, 0, 10.f, false);
|
||||
Edge edge67 = graphModel.factory().newEdge(node6, node7, false);
|
||||
Edge edge78 = graphModel.factory().newEdge(node7, node8, 0, 10.f, false);
|
||||
Edge edge81 = graphModel.factory().newEdge(node8, node1, false);
|
||||
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge34);
|
||||
undirectedGraph.addEdge(edge45);
|
||||
undirectedGraph.addEdge(edge56);
|
||||
undirectedGraph.addEdge(edge67);
|
||||
undirectedGraph.addEdge(edge78);
|
||||
undirectedGraph.addEdge(edge81);
|
||||
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
Modularity mod = new Modularity();
|
||||
|
||||
Modularity.CommunityStructure theStructure = mod.new CommunityStructure(hgraph);
|
||||
int[] comStructure = new int[hgraph.getNodeCount()];
|
||||
|
||||
HashMap<String, Double> modularityValues = mod.computeModularity(hgraph, theStructure, comStructure,
|
||||
1., true, true);
|
||||
|
||||
int class1 = comStructure[0];
|
||||
int class2 = comStructure[1];
|
||||
int class4 = comStructure[3];
|
||||
int class5 = comStructure[4];
|
||||
int class7 = comStructure[6];
|
||||
int class8 = comStructure[7];
|
||||
|
||||
assertEquals(class1, class2);
|
||||
assertEquals(class7, class8);
|
||||
assertNotEquals(class4, class5);
|
||||
}
|
||||
}
|
||||
+450
@@ -0,0 +1,450 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.gephi.statistics.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import org.gephi.graph.api.DirectedGraph;
|
||||
import org.gephi.graph.api.UndirectedGraph;
|
||||
import org.gephi.graph.api.Edge;
|
||||
import org.gephi.graph.api.GraphController;
|
||||
import org.gephi.graph.api.GraphModel;
|
||||
import org.gephi.graph.api.Node;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.impl.ProjectControllerImpl;
|
||||
import org.openide.util.Lookup;
|
||||
import static org.testng.Assert.*;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Anna
|
||||
*/
|
||||
public class PageRankNGTest {
|
||||
|
||||
private ProjectController pc;
|
||||
|
||||
@BeforeClass
|
||||
public void setUp() {
|
||||
pc = Lookup.getDefault().lookup(ProjectControllerImpl.class);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void initialize() {
|
||||
pc.newProject();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void clean() {
|
||||
pc.closeCurrentProject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneNodePageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(1);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, false, false, 0.001, 0.85);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
int index = indicies.get(n1);
|
||||
double pr1 = pageRank[index];
|
||||
|
||||
assertEquals(pr1, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoConnectedNodesPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = GraphGenerator.generatePathUndirectedGraph(2);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, false, false, 0.001, 0.85);
|
||||
|
||||
Node n2 = hgraph.getNode("1");
|
||||
int index = indicies.get(n2);
|
||||
double pr2 = pageRank[index];
|
||||
|
||||
assertEquals(pr2, 0.5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullGraphPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(5);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, false, false, 0.001, 0.85);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n4 = hgraph.getNode("3");
|
||||
int index1 = indicies.get(n1);
|
||||
int index4 = indicies.get(n4);
|
||||
double pr1 = pageRank[index1];
|
||||
double pr4 = pageRank[index4];
|
||||
double res = 0.2d;
|
||||
|
||||
double diff1 = Math.abs(pr1 - res);
|
||||
double diff4 = Math.abs(pr4 - res);
|
||||
assertTrue(diff1 < 0.01);
|
||||
assertTrue(diff4 < 0.01);
|
||||
assertEquals(pr1, pr4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteGraphPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(5);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, false, false, 0.001, 0.85);
|
||||
|
||||
Node n2 = hgraph.getNode("2");
|
||||
int index2 = indicies.get(n2);
|
||||
double pr2 = pageRank[index2];
|
||||
double res = 0.2d;
|
||||
|
||||
double diff2 = Math.abs(pr2 - res);
|
||||
assertTrue(diff2 < 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCyclicGraphPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = GraphGenerator.generateCyclicUndirectedGraph(6);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, false, false, 0.001, 0.6);
|
||||
|
||||
Node n4 = hgraph.getNode("3");
|
||||
int index4 = indicies.get(n4);
|
||||
double pr4 = pageRank[index4];
|
||||
double res = 0.1667;
|
||||
|
||||
double diff4 = Math.abs(pr4 - res);
|
||||
assertTrue(diff4 < 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStarGraphPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = GraphGenerator.generateStarUndirectedGraph(5);
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, false, false, 0.001, 0.6);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n2 = hgraph.getNode("1");
|
||||
Node n3 = hgraph.getNode("2");
|
||||
Node n4 = hgraph.getNode("3");
|
||||
Node n5 = hgraph.getNode("4");
|
||||
Node n6 = hgraph.getNode("5");
|
||||
|
||||
int index1 = indicies.get(n1);
|
||||
int index2 = indicies.get(n2);
|
||||
int index3 = indicies.get(n3);
|
||||
int index4 = indicies.get(n4);
|
||||
int index5 = indicies.get(n5);
|
||||
int index6 = indicies.get(n6);
|
||||
|
||||
double pr1 = pageRank[index1];
|
||||
double pr2 = pageRank[index2];
|
||||
double pr3 = pageRank[index3];
|
||||
double pr4 = pageRank[index4];
|
||||
double pr5 = pageRank[index5];
|
||||
double pr6 = pageRank[index6];
|
||||
|
||||
boolean oneMoreThree = pr1 > pr3;
|
||||
double res = 1.;
|
||||
double diff = 0.01;
|
||||
double sum = pr1 + pr2 + pr3 + pr4 + pr5 + pr6;
|
||||
|
||||
assertTrue(oneMoreThree);
|
||||
assertEquals(pr2, pr4);
|
||||
assertTrue(Math.abs(sum - res) < diff);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathDirectedGraphPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = GraphGenerator.generatePathDirectedGraph(4);
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, true, false, 0.001, 0.85);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n2 = hgraph.getNode("1");
|
||||
Node n3 = hgraph.getNode("2");
|
||||
Node n4 = hgraph.getNode("3");
|
||||
|
||||
int index1 = indicies.get(n1);
|
||||
int index2 = indicies.get(n2);
|
||||
int index3 = indicies.get(n3);
|
||||
int index4 = indicies.get(n4);
|
||||
|
||||
double pr1 = pageRank[index1];
|
||||
double pr2 = pageRank[index2];
|
||||
double pr3 = pageRank[index3];
|
||||
double pr4 = pageRank[index4];
|
||||
|
||||
double res = 1.;
|
||||
double diff = 0.01;
|
||||
double sum = pr1 + pr2 + pr3 + pr4;
|
||||
|
||||
assertTrue(pr1 < pr2);
|
||||
assertTrue(pr2 < pr4);
|
||||
assertTrue(Math.abs(sum - res) < diff);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCyclicDirectedGraphPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = GraphGenerator.generateCyclicDirectedGraph(5);
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, true, false, 0.001, 0.85);
|
||||
|
||||
Node n3 = hgraph.getNode("2");
|
||||
|
||||
int index3 = indicies.get(n3);
|
||||
|
||||
double pr3 = pageRank[index3];
|
||||
double res = 0.2d;
|
||||
|
||||
double diff3 = Math.abs(pr3 - res);
|
||||
assertTrue(diff3 < 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedSpecial1GraphPageRank() {
|
||||
pc.newProject();
|
||||
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");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
Node node7 = graphModel.factory().newNode("6");
|
||||
Node node8 = graphModel.factory().newNode("7");
|
||||
Node node9 = graphModel.factory().newNode("8");
|
||||
|
||||
directedGraph.addNode(node1);
|
||||
directedGraph.addNode(node2);
|
||||
directedGraph.addNode(node3);
|
||||
directedGraph.addNode(node4);
|
||||
directedGraph.addNode(node5);
|
||||
directedGraph.addNode(node6);
|
||||
directedGraph.addNode(node7);
|
||||
directedGraph.addNode(node8);
|
||||
directedGraph.addNode(node9);
|
||||
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3);
|
||||
Edge edge31 = graphModel.factory().newEdge(node3, node1);
|
||||
Edge edge14 = graphModel.factory().newEdge(node1, node4);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5);
|
||||
Edge edge51 = graphModel.factory().newEdge(node5, node1);
|
||||
Edge edge16 = graphModel.factory().newEdge(node1, node6);
|
||||
Edge edge67 = graphModel.factory().newEdge(node6, node7);
|
||||
Edge edge71 = graphModel.factory().newEdge(node7, node1);
|
||||
Edge edge18 = graphModel.factory().newEdge(node1, node8);
|
||||
Edge edge89 = graphModel.factory().newEdge(node8, node9);
|
||||
Edge edge91 = graphModel.factory().newEdge(node9, node1);
|
||||
|
||||
directedGraph.addEdge(edge12);
|
||||
directedGraph.addEdge(edge23);
|
||||
directedGraph.addEdge(edge31);
|
||||
directedGraph.addEdge(edge14);
|
||||
directedGraph.addEdge(edge45);
|
||||
directedGraph.addEdge(edge51);
|
||||
directedGraph.addEdge(edge16);
|
||||
directedGraph.addEdge(edge67);
|
||||
directedGraph.addEdge(edge71);
|
||||
directedGraph.addEdge(edge18);
|
||||
directedGraph.addEdge(edge89);
|
||||
directedGraph.addEdge(edge91);
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, true, false, 0.001, 0.85);
|
||||
|
||||
int index1 = indicies.get(node1);
|
||||
int index2 = indicies.get(node2);
|
||||
int index3 = indicies.get(node3);
|
||||
|
||||
double pr1 = pageRank[index1];
|
||||
double pr2 = pageRank[index2];
|
||||
double pr3 = pageRank[index3];
|
||||
|
||||
assertTrue(pr1 > pr2);
|
||||
assertTrue(pr2 < pr3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectedStarOutGraphPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
|
||||
DirectedGraph directedGraph = graphModel.getDirectedGraph();
|
||||
Node firstNode = graphModel.factory().newNode("0");
|
||||
directedGraph.addNode(firstNode);
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
|
||||
directedGraph.addNode(currentNode);
|
||||
Edge currentEdge = graphModel.factory().newEdge(firstNode, currentNode);
|
||||
directedGraph.addEdge(currentEdge);
|
||||
}
|
||||
|
||||
DirectedGraph hgraph = graphModel.getDirectedGraph();
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, true, false, 0.001, 0.85);
|
||||
|
||||
Node n1 = hgraph.getNode("0");
|
||||
Node n2 = hgraph.getNode("1");
|
||||
Node n3 = hgraph.getNode("2");
|
||||
Node n5 = hgraph.getNode("4");
|
||||
|
||||
int index1 = indicies.get(n1);
|
||||
int index2 = indicies.get(n2);
|
||||
int index3 = indicies.get(n3);
|
||||
int index5 = indicies.get(n5);
|
||||
|
||||
double pr1 = pageRank[index1];
|
||||
double pr2 = pageRank[index2];
|
||||
double pr3 = pageRank[index3];
|
||||
double pr5 = pageRank[index5];
|
||||
|
||||
double res = 0.146;
|
||||
double diff = 0.01;
|
||||
|
||||
assertTrue(pr1 < pr3);
|
||||
assertEquals(pr2, pr5);
|
||||
assertTrue(Math.abs(pr1 - res) < diff);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUndirectedWeightedGraphPageRank() {
|
||||
pc.newProject();
|
||||
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
|
||||
|
||||
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
|
||||
Node node1 = graphModel.factory().newNode("0");
|
||||
Node node2 = graphModel.factory().newNode("1");
|
||||
Node node3 = graphModel.factory().newNode("2");
|
||||
Node node4 = graphModel.factory().newNode("3");
|
||||
Node node5 = graphModel.factory().newNode("4");
|
||||
Node node6 = graphModel.factory().newNode("5");
|
||||
|
||||
undirectedGraph.addNode(node1);
|
||||
undirectedGraph.addNode(node2);
|
||||
undirectedGraph.addNode(node3);
|
||||
undirectedGraph.addNode(node4);
|
||||
undirectedGraph.addNode(node5);
|
||||
undirectedGraph.addNode(node6);
|
||||
|
||||
Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
|
||||
Edge edge23 = graphModel.factory().newEdge(node2, node3, 0, 10, false);
|
||||
Edge edge34 = graphModel.factory().newEdge(node3, node4, false);
|
||||
Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
|
||||
Edge edge56 = graphModel.factory().newEdge(node5, node6, false);
|
||||
Edge edge61 = graphModel.factory().newEdge(node6, node1, false);
|
||||
|
||||
undirectedGraph.addEdge(edge12);
|
||||
undirectedGraph.addEdge(edge23);
|
||||
undirectedGraph.addEdge(edge34);
|
||||
undirectedGraph.addEdge(edge45);
|
||||
undirectedGraph.addEdge(edge56);
|
||||
undirectedGraph.addEdge(edge61);
|
||||
|
||||
UndirectedGraph hgraph = graphModel.getUndirectedGraph();
|
||||
PageRank pr = new PageRank();
|
||||
|
||||
double[] pageRank;
|
||||
|
||||
HashMap<Node, Integer> indicies = pr.createIndiciesMap(hgraph);
|
||||
|
||||
pageRank = pr.calculatePagerank(hgraph, indicies, false, true, 0.001, 0.85);
|
||||
|
||||
int index1 = indicies.get(node1);
|
||||
int index2 = indicies.get(node2);
|
||||
int index3 = indicies.get(node3);
|
||||
int index6 = indicies.get(node6);
|
||||
|
||||
double diff = 0.01;
|
||||
|
||||
double pr1 = pageRank[index1];
|
||||
double pr2 = pageRank[index2];
|
||||
double pr3 = pageRank[index3];
|
||||
double pr6 = pageRank[index6];
|
||||
|
||||
assertTrue(Math.abs(pr2 - pr3) < diff);
|
||||
assertTrue(pr1 < pr2);
|
||||
assertTrue(pr1 < pr6);
|
||||
}
|
||||
}
|
||||
Referência em uma Nova Issue
Bloquear um usuário