### Update clustering coefficient computation.

parent 1240daac
 ... @@ -251,7 +251,7 @@ ... @@ -251,7 +251,7 @@ junit junit junit junit 4.4 4.3.1 false false ... ...
 ... @@ -372,49 +372,47 @@ public class Toolkit extends ... @@ -372,49 +372,47 @@ public class Toolkit extends } } /** /** * Clustering coefficient for one node of the graph. * Clustering coefficient for one node of the graph. For a node i with * degree k, if Ni is the neighborhood of i (a set of nodes), clustering * coefficient of i is defined as the count of edge e_uv with u,v in Ni * divided by the maximum possible count, ie. k * (k-1) / 2. * * This method only works with undirected graphs. * * * @param node * @param node * The node to compute the clustering coefficient for. * The node to compute the clustering coefficient for. * @return The clustering coefficient for this node. * @return The clustering coefficient for this node. * @complexity O(d^2) where d is the degree of the given node. * @complexity O(d^2) where d is the degree of the given node. * @reference D. J. Watts and Steven Strogatz (June 1998). * "Collective dynamics of 'small-world' networks" . Nature 393 * (6684): 440–442 */ */ public static double clusteringCoefficient(Node node) { public static double clusteringCoefficient(Node node) { double coef = 0.0; double coef = 0.0; int n = node.getDegree(); int n = node.getDegree(); if (n > 1) { if (n > 1) { // Collect the neighbour nodes. Node[] nodes = new Node[n]; Node[] nodes = new Node[n]; HashSet set = new HashSet(); int i = 0; for (Edge edge : node.getEdgeSet()) nodes[i++] = edge.getOpposite(node); // Count the number of edges between these nodes. // // Collect the neighbor nodes. // for (int i = 0; i < n; i++) nodes[i] = node.getEdge(i).getOpposite(node); for (i = 0; i < n; ++i) // For all neighbour nodes. // { // Check all edge possibilities for (int j = 0; j < n; ++j) // For all other nodes of this // // clique. for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) if (j != i) { if (j != i) { Edge e = nodes[j].getEdgeToward(nodes[i].getId()); Edge e = nodes[j].getEdgeToward(nodes[i].getId()); if (e != null) { if (e != null && e.getSourceNode() == nodes[j]) // if( ! set.contains( e ) ) coef++; set.add(e); } } } } } double ne = set.size(); double max = (n * (n - 1)) / 2.0; coef = ne / max; coef /= (n * (n - 1)) / 2.0; } } return coef; return coef; ... @@ -1346,9 +1344,9 @@ public class Toolkit extends ... @@ -1346,9 +1344,9 @@ public class Toolkit extends * The adjacency matrix of a graph is a n times n matrix * The adjacency matrix of a graph is a n times n matrix * {@code a}, where n is the number of nodes of the graph. The * {@code a}, where n is the number of nodes of the graph. The * element {@code a[i][j]} of this matrix is equal to the number of edges * element {@code a[i][j]} of this matrix is equal to the number of edges * from the node {@code graph.getNode(i)} to the node * from the node {@code graph.getNode(i)} to the node {@code * {@code graph.getNode(j)}. An undirected edge between i-th and j-th node * graph.getNode(j)}. An undirected edge between i-th and j-th node is * is counted twice: in {@code a[i][j]} and in {@code a[j][i]}. * counted twice: in {@code a[i][j]} and in {@code a[j][i]}. * * * @param graph * @param graph * A graph. * A graph. ... @@ -1380,9 +1378,9 @@ public class Toolkit extends ... @@ -1380,9 +1378,9 @@ public class Toolkit extends * The adjacency matrix of a graph is a n times n matrix * The adjacency matrix of a graph is a n times n matrix * {@code a}, where n is the number of nodes of the graph. The * {@code a}, where n is the number of nodes of the graph. The * element {@code a[i][j]} of this matrix is equal to the number of edges * element {@code a[i][j]} of this matrix is equal to the number of edges * from the node {@code graph.getNode(i)} to the node * from the node {@code graph.getNode(i)} to the node {@code * {@code graph.getNode(j)}. An undirected edge between i-th and j-th node * graph.getNode(j)}. An undirected edge between i-th and j-th node is * is counted twice: in {@code a[i][j]} and in {@code a[j][i]}. * counted twice: in {@code a[i][j]} and in {@code a[j][i]}. * * * @param graph * @param graph * A graph * A graph ... @@ -1406,12 +1404,12 @@ public class Toolkit extends ... @@ -1406,12 +1404,12 @@ public class Toolkit extends * number of edges of the graph. The coefficients {@code a[i][j]} of this * number of edges of the graph. The coefficients {@code a[i][j]} of this * matrix have the following values: * matrix have the following values: *
*
*
• -1 if {@code graph.getEdge(j)} is directed and *
• -1 if {@code graph.getEdge(j)} is directed and {@code * {@code graph.getNode(i)} is its source.
• * graph.getNode(i)} is its source. *
• 1 if {@code graph.getEdge(j)} is undirected and *
• 1 if {@code graph.getEdge(j)} is undirected and {@code * {@code graph.getNode(i)} is its source.
• * graph.getNode(i)} is its source. *
• 1 if {@code graph.getNode(i)} is the target of *
• 1 if {@code graph.getNode(i)} is the target of {@code * {@code graph.getEdge(j)}.
• * graph.getEdge(j)}. *
• 0 otherwise. *
• 0 otherwise. *
*
* In the special case when the j-th edge is a loop connecting the i-th node * In the special case when the j-th edge is a loop connecting the i-th node ... @@ -1449,12 +1447,12 @@ public class Toolkit extends ... @@ -1449,12 +1447,12 @@ public class Toolkit extends * number of edges of the graph. The coefficients {@code a[i][j]} of this * number of edges of the graph. The coefficients {@code a[i][j]} of this * matrix have the following values: * matrix have the following values: *
*
*
• -1 if {@code graph.getEdge(j)} is directed and *
• -1 if {@code graph.getEdge(j)} is directed and {@code * {@code graph.getNode(i)} is its source.
• * graph.getNode(i)} is its source. *
• 1 if {@code graph.getEdge(j)} is undirected and *
• 1 if {@code graph.getEdge(j)} is undirected and {@code * {@code graph.getNode(i)} is its source.
• * graph.getNode(i)} is its source. *
• 1 if {@code graph.getNode(i)} is the target of *
• 1 if {@code graph.getNode(i)} is the target of {@code * {@code graph.getEdge(j)}.
• * graph.getEdge(j)}. *
• 0 otherwise.
• *
• 0 otherwise.
• *
*
* In the special case when the j-th edge is a loop connecting the i-th node * In the special case when the j-th edge is a loop connecting the i-th node ... ...
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!