def btwns(self):
     """
     Find the egocentric betweenness centrality for this node.
     """
     ret = 0.0
     
     # Find the neighbors of this node's neighbors.
     nbrs = {}
     for nbr in self.nbrs():
         nbrs[nbr] = nbr.nbrs()
     nbrs[self] = self.nbrs()
     
     # We need the node keys sorted properly to build a contact matrix.
     snodes = sorted(nbrs.keys())
     
     # Generate a contact matrix.
     cmatrix = []
     for node1 in snodes:
         cmatrix.append([int(node1 != node2 and node1 in nbrs[node2]) for node2 in snodes])
     
     for node1, node2 in combinations(snodes, 2):
         row = snodes.index(node1)
         col = snodes.index(node2)
         if not cmatrix[row][col]:
             s = sum(map(mul, cmatrix[row][:], cmatrix[:][col]))
             if s > 0:
                 ret += 1 / float(s)
     
     return ret    
Exemple #2
0
 def btwns(self, paths):
     """
     Find the betweenness centrality for this node.
     
     Key arguments:
     paths -- a dictionary of all the shortest paths.
     """
     ret = 0.0
     
     for node1, node2 in combinations(paths.keys(), 2):
         # There theoretically can be no shortests paths if we deleted
         #  a bridge, but the density of the new clusters didn't meet the threshold.
         if node1 != self and node2 != self and paths[node1][node2]:
             # Look at all shortest paths from node1 to node2.
             ret += sum([1 for path in paths[node1][node2] if self in path]) / float(len(paths[node1][node2]))
     
     return ret  
Exemple #3
0
 def cluster_coeff(self):
     """
     Find the clustering coefficient.
     """
     if len(self.nodes) < 2:
         return 0.0
     
     num = 0.0
     for node in self.nodes:
         # Special case, node with only one neighbor.
         if node.deg() < 2:
             continue
         
         edges = set()
         for nbr1, nbr2 in combinations(node.nbrs(), 2):
             edges = edges.union(set(nbr1.edges).intersection(nbr2.edges))
         num += (2 * len(edges)) / float(node.deg() * (node.deg() - 1))
     
     return num / len(self.nodes)
Exemple #4
0
 def btwns(self, paths):
     """
     Find the betweenness centrality for this edge.
     
     Key arguments:
     paths -- a dictionary of all the shortest paths.
     """
     ret = 0.0
     
     for node1, node2 in combinations(paths.keys(), 2):
         # There theoretically can be no shortests paths if we deleted
         #  a bridge, but the density of the new clusters didn't meet the threshold.
         if paths[node1][node2] != None:
             # A direct path from one to another still counts as a shortest path.
             if len(paths[node1][node2]) == 0:
                 ret += 1.0
             else:
                 ret += sum([1 for path in paths[node1][node2] if self.node1 in path and self.node2 in path]) / float(len(paths[node1][node2]))
     
     return ret