Created
August 7, 2011 04:57
-
-
Save jamiely/1130061 to your computer and use it in GitHub Desktop.
Information Centrality Algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # http://code.google.com/p/pythonxy/source/browse/src/python/networkx/PURELIB/networkx/algorithms/centrality/current_flow_closeness.py?spec=svn.xy-27.02180602c04dd0f66f2ae2f0fa0d56fbc2adcdf6&repo=xy-27&name=02180602c0&r=02180602c04dd0f66f2ae2f0fa0d56fbc2adcdf6 | |
| """ | |
| Current-flow closeness centrality measures. | |
| """ | |
| # Copyright (C) 2010 by | |
| # Aric Hagberg <hagberg@lanl.gov> | |
| # Dan Schult <dschult@colgate.edu> | |
| # Pieter Swart <swart@lanl.gov> | |
| # All rights reserved. | |
| # BSD license. | |
| __author__ = """Aric Hagberg (hagberg@lanl.gov)""" | |
| __all__ = ['current_flow_closeness_centrality','information_centrality'] | |
| import networkx as nx | |
| def current_flow_closeness_centrality(G,normalized=True): | |
| """Compute current-flow closeness centrality for nodes. | |
| A variant of closeness centrality based on effective | |
| resistance between nodes in a network. This metric | |
| is also known as information centrality. | |
| Parameters | |
| ---------- | |
| G : graph | |
| A networkx graph | |
| normalized : bool, optional | |
| If True the values are normalized by 1/(n-1) where n is the | |
| number of nodes in G. | |
| Returns | |
| ------- | |
| nodes : dictionary | |
| Dictionary of nodes with current flow closeness centrality as the value. | |
| See Also | |
| -------- | |
| closeness_centrality | |
| Notes | |
| ----- | |
| The algorithm is from Brandes [1]_. | |
| See also [2]_ for the original definition of information centrality. | |
| References | |
| ---------- | |
| .. [1] Ulrik Brandes and Daniel Fleischer, | |
| Centrality Measures Based on Current Flow. | |
| Proc. 22nd Symp. Theoretical Aspects of Computer Science (STACS '05). | |
| LNCS 3404, pp. 533-544. Springer-Verlag, 2005. | |
| http://www.inf.uni-konstanz.de/algo/publications/bf-cmbcf-05.pdf | |
| .. [2] Stephenson, K. and Zelen, M. | |
| Rethinking centrality: Methods and examples. | |
| Social Networks. Volume 11, Issue 1, March 1989, pp. 1-37 | |
| http://dx.doi.org/10.1016/0378-8733(89)90016-6 | |
| """ | |
| try: | |
| import numpy as np | |
| except ImportError: | |
| raise ImportError("flow_closeness_centrality() requires NumPy: http://scipy.org/ ") | |
| if G.is_directed(): | |
| raise nx.NetworkXError(\ | |
| "current_flow_closeness_centrality() not defined for digraphs.") | |
| if not nx.is_connected(G): | |
| raise nx.NetworkXError("Graph not connected.") | |
| betweenness=dict.fromkeys(G,0.0) # b[v]=0 for v in G | |
| n=len(G) | |
| mapping=dict(zip(G,list(range(n)))) # map nodes to integers | |
| C=_compute_C(G) | |
| for v in G: | |
| vi=mapping[v] | |
| for w in G: | |
| wi=mapping[w] | |
| betweenness[v]+=C[vi,vi]-2*C[wi,vi] | |
| betweenness[w]+=C[vi,vi] | |
| if normalized: | |
| nb=len(betweenness)-1.0 | |
| else: | |
| nb=1.0 | |
| for v in G: | |
| betweenness[v]=nb/(betweenness[v]) | |
| return betweenness | |
| def _compute_C(G): | |
| """Compute inverse of Laplacian.""" | |
| try: | |
| import numpy as np | |
| except ImportError: | |
| raise ImportError("flow_closeness_centrality() requires NumPy: http://scipy.org/ ") | |
| L=nx.laplacian(G) # use ordering of G.nodes() | |
| # remove first row and column | |
| LR=L[1:,1:] | |
| LRinv=np.linalg.inv(LR) | |
| C=np.zeros(L.shape) | |
| C[1:,1:]=LRinv | |
| return C | |
| information_centrality=current_flow_closeness_centrality | |
| # fixture for nose tests | |
| def setup_module(module): | |
| from nose import SkipTest | |
| try: | |
| import numpy | |
| except: | |
| raise SkipTest("NumPy not available") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/sna/html/infocent.html | |
| infocent {sna} R Documentation | |
| Find Information Centrality Scores of Network Positions | |
| Description | |
| infocent takes a graph stack (dat) and returns the information centralities of positions within one graph (indicated by nodes and g, respectively). This function is compatible with centralization, and will return the theoretical maximum absolute deviation (from maximum) conditional on size (which is used by centralization to normalize the observed centralization score). | |
| Usage | |
| infocent(dat, g=1, nodes=c(1:dim(dat)[2]), gmode="digraph", | |
| diag=FALSE, cmode="weak", tmaxdev=FALSE, rescale=FALSE,tol=1e-20) | |
| Arguments | |
| dat Data array to be analyzed. By assumption, the first dimension of the array indexes the graph, with the next two indexing the actors. Alternately, this can be an n x n matrix (if only one graph is involved). | |
| g Integer indicating the index of the graph for which centralities are to be calculated. By default, g==1. | |
| nodes List indicating which nodes are to be included in the calculation. By default, all nodes are included. | |
| gmode String indicating the type of graph being evaluated. "digraph" indicates that edges should be interpreted as directed; "graph" indicates that edges are undirected. This is currently ignored. | |
| diag Boolean indicating whether or not the diagonal should be treated as valid data. Set this true if and only if the data can contain loops. diag is FALSE by default. | |
| cmode The rule to be used by symmetrize when symmetrizing dichotomous data; must be one of "weak" (for an OR rule), "strong" for an AND rule), "upper" (for a max rule), or "lower" (for a min rule). Set to "weak" by default, this parameter obviously has no effect on symmetric data. | |
| tmaxdev Boolean indicating whether or not the theoretical maximum absolute deviation from the maximum nodal centrality should be returned. By default, tmaxdev==FALSE. | |
| rescale If true, centrality scores are rescaled such that they sum to 1. | |
| tol Tolerance for near-singularities during matrix inversion (see solve) | |
| Details | |
| Actor information centrality is a hybrid measure which relates to both path-length indices (e.g., closeness, graph centrality) and to walk-based eigenmeasures (e.g., eigenvector centrality, Bonacich power). In particular, the information centrality of a given actor can be understood to be the harmonic average of the ``bandwidth'' for all paths originating with said individual (where the bandwidth is taken to be inversely related to path length). Formally, the index is constructed as follows. First, we take G to be an undirected (but possibly valued) graph – symmetrizing if necessary – with (possibly valued) adjacency matrix A. From this, we remove all isolates (whose information centralities are zero in any event) and proceed to create the weighted connection matrix | |
| C = B^-1 | |
| where B is a pseudo-adjacency matrix formed by replacing the diagonal of 1-A with one plus each actor's degree. Given the above, let T be the trace of C with sum S_T, and let S_R be an arbitrary row sum (all rows of C have the same sum). The information centrality scores are then equal to | |
| C_I = ( T + (S_T-2S_R)/|V(G)| )^-1 | |
| (recalling that the scores for any omitted vertices are 0). | |
| In general, actors with higher information centrality are predicted to have greater control over the flow of information within a network; highly information-central individuals tend to have a large number of short paths to many others within the social structure. Because the raw centrality values can be difficult to interpret directly, rescaled values are sometimes preferred (see the rescale option). Though the use of path weights suggest information centrality as a possible replacement for closeness, the problem of inverting the B matrix poses problems of its own; as with all such measures, caution is advised on disconnected or degenerate structures. | |
| Value | |
| A vector containing the centrality scores | |
| Note | |
| The theoretical maximum deviation used here is not obtained with the star network; rather, the maximum occurs for an empty graph with one complete dyad, which is the model used here. | |
| Author(s) | |
| David Barron david.barron@jesus.ox.ac.uk | |
| Carter T. Butts buttsc@uci.edu | |
| References | |
| Stephenson, K., and Zelen, M. (1989). ``Rethinking Centrality: Methods and Applications.'' Social Networks, 11, 1-37. | |
| Wasserman, S., and Faust, K. (1994). ``Social Network Analysis: Methods and Applications.'' Cambridge: Cambridge University Press. | |
| See Also | |
| evcent, bonpow, closeness, graphcent, centralization | |
| Examples | |
| #Generate some test data | |
| dat<-rgraph(10,mode="graph") | |
| #Compute information centrality scores | |
| infocent(dat) | |
| [Package Contents] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //http://code.google.com/p/mason/source/browse/trunk/contrib/socialnets/sim/field/network/stats/actorcentrality/InformationCentrality.java?r=355 | |
| /* | |
| Copyright 2010 by Sean Luke and George Mason University | |
| Licensed under the Academic Free License version 3.0 | |
| See the file "LICENSE" for more information | |
| */ | |
| package sim.field.network.stats.actorcentrality; | |
| import sim.field.network.stats.*; | |
| import sim.field.network.*; | |
| import sim.util.mantissa.linalg.*; | |
| import java.util.*; | |
| /** | |
| * Actor Information Centrality (Wasserman and Faust, page 195 for undirected and page 201 for | |
| * directed graphs). | |
| * | |
| * <p>Note that this works for both dichotomous and valued relations (i.e. hops and weights). | |
| * | |
| * <p>It requires a matrix inversion that should be reused by all <code>getMeasure()</code> calls. | |
| * | |
| * @author Gabriel Catalin Balan | |
| */ | |
| public class InformationCentrality extends NodeIndex | |
| { | |
| final double normalizationDenominator; | |
| final double[] ci; | |
| public InformationCentrality(final Network network, final EdgeMetric weightFn) | |
| { | |
| super(network); | |
| int n = network.allNodes.numObjs; | |
| //imagine I move all isolated nodes at the end, | |
| //so the non isolated nodes are compact, so I can go ahead with the matrix inversion. | |
| //but when I compute the final stats I want the old order. | |
| int[] indirect = new int[n]; | |
| for(int i=0;i<n;i++) | |
| indirect[i]=i; | |
| int isolated = 0; | |
| Iterator nodeIO = network.indexOutInHash.values().iterator(); | |
| for(int k=0;k<n;k++) | |
| { | |
| Network.IndexOutIn ioi = (Network.IndexOutIn)nodeIO.next(); | |
| int index= ioi.index; | |
| if(ioi.out == null) | |
| { | |
| isolated++; | |
| indirect[n-isolated] = indirect[index]; | |
| indirect[index] = n-isolated; | |
| } | |
| } | |
| int nonIsolatedN = n -isolated; | |
| GeneralSquareMatrix a = new GeneralSquareMatrix(nonIsolatedN); | |
| //pseudo-adjacency matrix formed by replacing the diagonal of 1-X | |
| // with one plus each actor's degree | |
| for(int i=0;i<nonIsolatedN;i++) | |
| for(int j=0;j<nonIsolatedN;j++) | |
| a.setElement(i, j, 1); | |
| //TODO too many functioncalls | |
| nodeIO = network.indexOutInHash.values().iterator(); | |
| for(int k=0;k<n;k++) | |
| { | |
| Network.IndexOutIn ioi = (Network.IndexOutIn)nodeIO.next(); | |
| int index= ioi.index; | |
| if(ioi.out == null) continue; | |
| int outDegree = ioi.out.numObjs; | |
| int indirectIndex = indirect[index]; | |
| Object nodeObj = network.allNodes.objs[ioi.index]; | |
| for(int i=0;i<outDegree;i++) | |
| { | |
| Edge e = (Edge)ioi.out.objs[i]; | |
| // this is getNodeIndex without the function call | |
| int j = ((Network.IndexOutIn)network.indexOutInHash.get(e.getOtherNode(nodeObj))).index; | |
| a.setElement(indirectIndex, indirect[j], 1-weightFn.getWeight(e)); | |
| // multigraphs will have edges ignored here | |
| } | |
| //there better not be self loops | |
| a.setElement(indirectIndex, indirectIndex, 1+outDegree); | |
| } | |
| Matrix c; | |
| double R=0, T=0; | |
| ci = new double[n]; | |
| //TODO maybe we could find a in-place matrix multiplication procedure | |
| try | |
| { | |
| c = a.getInverse(0.0); | |
| for(int i=0;i<nonIsolatedN;i++) | |
| { | |
| T+=c.getElement(i, i); | |
| R+=c.getElement(0, i); | |
| } | |
| for(int i=0;i<n;i++) | |
| { | |
| int indirectI = indirect[i]; | |
| ci[i]= (indirectI>=nonIsolatedN)? Double.POSITIVE_INFINITY : c.getElement(indirectI, indirectI); | |
| //I want the ci values of the isolated nodes to be 0, so 1 /positive infinity does the trick | |
| } | |
| }catch(SingularMatrixException ex) | |
| { | |
| throw new RuntimeException("Singular Matrix"); | |
| } | |
| double k = (T-2*R)/nonIsolatedN; | |
| double sum = 0; | |
| for(int i=0;i<n;i++) | |
| { | |
| ci[i]=1d/(ci[i]+k); | |
| sum+=ci[i]; | |
| } | |
| normalizationDenominator = sum; | |
| } | |
| public double getValue(Object node) { | |
| return ci[network.getNodeIndex(node)]; | |
| } | |
| public double getValue(int nodeIndex) { | |
| return ci[nodeIndex]; | |
| } | |
| /** | |
| * Sum_{i} getMeasure(i) | |
| */ | |
| public double getMaxValue(){return normalizationDenominator;} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment