~ubuntu-branches/ubuntu/utopic/python-networkx/utopic

« back to all changes in this revision

Viewing changes to networkx/algorithms/centrality/closeness.py

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-08-11 12:41:30 UTC
  • mfrom: (1.4.1) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120811124130-whr6uso7fncyg8bi
Tags: 1.7-1
* New upstream release
* debian/patches/changeset_*
  - removed, included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#    Pieter Swart <swart@lanl.gov>
9
9
#    All rights reserved.
10
10
#    BSD license.
 
11
import functools
 
12
import networkx as nx
11
13
__author__ = "\n".join(['Aric Hagberg (hagberg@lanl.gov)',
12
14
                        'Pieter Swart (swart@lanl.gov)',
13
15
                        'Sasha Gutfraind (ag362@cornell.edu)'])
14
 
 
15
16
__all__ = ['closeness_centrality']
16
17
 
17
 
import networkx as nx
18
18
 
19
 
def closeness_centrality(G,v=None,weighted_edges=False,normalized=True):
 
19
def closeness_centrality(G, v=None, distance=None, normalized=True):
20
20
    """Compute closeness centrality for nodes.
21
21
 
22
22
    Closeness centrality at a node is 1/average distance to all other nodes.
27
27
      A networkx graph 
28
28
    v : node, optional
29
29
      Return only the value for node v
30
 
    weighted_edges : bool, optional
31
 
      If True use edge weights in computing degree and size.
 
30
    distance : string key, optional (default=None)
 
31
      Use specified edge key as edge distance. 
 
32
      If True, use 'weight' as the edge key.
32
33
    normalized : bool, optional      
33
34
      If True (default) normalize by the graph size.
34
35
 
50
51
    algorithm computes the closeness centrality for each connected
51
52
    part separately.
52
53
    """
53
 
    if weighted_edges:
54
 
        path_length=nx.single_source_dijkstra_path_length
 
54
    if distance is not None:
 
55
        if distance is True: distance='weight'
 
56
        path_length=functools.partial(nx.single_source_dijkstra_path_length,
 
57
                                      weight=distance)
55
58
    else:
56
59
        path_length=nx.single_source_shortest_path_length
57
60