Eigenvector centrality for nodes in the graph (cfr. Google's PageRank). Eigenvector centrality is a measure of the importance of a node in a directed network. It rewards nodes with a high potential of (indirectly) connecting to high-scoring nodes. Nodes with no incoming con
(graph, normalized=True, reversed=True, rating={}, iterations=100, tolerance=0.0001)
| 935 | return b |
| 936 | |
| 937 | def eigenvector_centrality(graph, normalized=True, reversed=True, rating={}, iterations=100, tolerance=0.0001): |
| 938 | """ Eigenvector centrality for nodes in the graph (cfr. Google's PageRank). |
| 939 | Eigenvector centrality is a measure of the importance of a node in a directed network. |
| 940 | It rewards nodes with a high potential of (indirectly) connecting to high-scoring nodes. |
| 941 | Nodes with no incoming connections have a score of zero. |
| 942 | If you want to measure outgoing connections, reversed should be False. |
| 943 | """ |
| 944 | # Based on: NetworkX, Aric Hagberg (hagberg@lanl.gov) |
| 945 | # http://python-networkx.sourcearchive.com/documentation/1.0.1/centrality_8py-source.html |
| 946 | # Note: much faster than betweenness centrality (which grows exponentially). |
| 947 | def normalize(vector): |
| 948 | w = 1.0 / (sum(vector.values()) or 1) |
| 949 | for node in vector: |
| 950 | vector[node] *= w |
| 951 | return vector |
| 952 | G = adjacency(graph, directed=True, reversed=reversed) |
| 953 | v = normalize(dict([(n, random()) for n in graph])) # Node ID => weight vector. |
| 954 | # Eigenvector calculation using the power iteration method: y = Ax. |
| 955 | # It has no guarantee of convergence. |
| 956 | for i in range(iterations): |
| 957 | v0 = v |
| 958 | v = dict.fromkeys(v0.iterkeys(), 0) |
| 959 | for n1 in v: |
| 960 | for n2 in G[n1]: |
| 961 | v[n1] += 0.01 + v0[n2] * G[n1][n2] * rating.get(n1, 1) |
| 962 | normalize(v) |
| 963 | e = sum([abs(v[n]-v0[n]) for n in v]) # Check for convergence. |
| 964 | if e < len(G) * tolerance: |
| 965 | # Normalize between 0.0 and 1.0. |
| 966 | m = normalized and max(v.values()) or 1 |
| 967 | v = dict((id, w/m) for id, w in v.iteritems()) |
| 968 | return v |
| 969 | warn("node weight is 0 because eigenvector_centrality() did not converge.", Warning) |
| 970 | return dict((n, 0) for n in G) |
| 971 | |
| 972 | # a | b => all elements from a and all the elements from b. |
| 973 | # a & b => elements that appear in a as well as in b. |
no test coverage detected
searching dependent graphs…