File: kevin_bacon.py

package info (click to toggle)
python-networkx 0.32-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,332 kB
  • ctags: 1,020
  • sloc: python: 21,197; makefile: 67; sh: 11
file content (74 lines) | stat: -rw-r--r-- 2,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
"""
An example using networkx.XGraph(multiedges=True).

Calculate the Kevin Bacon numbers of some actors.  The data in
kevin_bacon.dat is a subset from all movies with Kevin Bacon in
the cast, as taken from the example in the Boost Graph Library.
cf. http://www.boost.org/libs/graph/doc/kevin_bacon.html

"""
__author__ = """Pieter Swart (swart@lanl.gov)"""
__date__ = "$Date: 2005-04-13 16:16:18 -0600 (Wed, 13 Apr 2005) $"
__credits__ = """"""
__revision__ = ""

#    Copyright (C) 2004 by 
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#    Distributed under the terms of the GNU Lesser General Public License
#    http://www.gnu.org/copyleft/lesser.html

from networkx import *

def kevin_bacon_graph():
    """Return the graph of actors connected by a movie.

    Uses data from kevin_bacon.dat from Boost Graph Library.
    Each edge between two actors contain the movie name.
    
    """
    try:
        datafile=open("kevin_bacon.dat",'rb')
    except IOError:
        print "kevin_bacon.dat not found."
        raise

    G=XGraph(multiedges=True)

    for line in datafile.read().splitlines():
        actor1, movie, actor2 = line.split(';')
        G.add_edge(actor1, actor2, movie)
    return G

if __name__ == '__main__':
    from networkx import *

    G=kevin_bacon_graph()
    print "Loaded the kevin_bacon_graph from kevin_bacon.dat"
    print "Graph has %d actors with %d edges"\
          %(number_of_nodes(G),number_of_edges(G))
    
    bacon_no={}
    for a in G:
        bacon_no[a]=shortest_path_length(G,a,'Kevin Bacon')

    for a in G:
        print "%s's bacon number is %d" %(a,bacon_no[a])

    try:
        # draw using graphviz interface
        # node size = 50*degree
        # node color corresponds to bacon number
        import pylab as P
        draw_graphviz(G,prog='fdp',
                      with_labels=False,
                      node_size=[50*G.degree(n) for n in G],
                      node_color=P.array([bacon_no[n] for n in G]),
                      cmap=P.cm.gray
                      )
        P.savefig("kevin_bacon.png")
        print "created kevin_bacon.png"
    except:
        pass