Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cs190c:lab12_09 [2009/04/10 11:33]
tang
cs190c:lab12_09 [2009/04/29 23:01] (current)
Line 45: Line 45:
  
 Here is a Cytoscape session file that may help you visualize the graphs: {{cs190c:​lab12.zip|}} Here is a Cytoscape session file that may help you visualize the graphs: {{cs190c:​lab12.zip|}}
 +
 +===== Solution =====
 +<code python>​import networkx as nx
 + 
 +def import_graph():​
 +    fname = raw_input("​Enter the filename of the graph: ");
 +    G = nx.Graph()
 + 
 +    f = open(fname)
 +    for edge in f:
 +        a,b = edge.split('​\t'​)
 +        G.add_edge(a.strip(),​ b.strip()) ​    
 +    f.close()
 + 
 +    return G
 +
 +if __name__ == '​__main__':​
 +    G = import_graph()
 +    actor1 = raw_input('​Enter the name of the first actor: ')
 +    actor2 = raw_input('​Enter the name of the second actor: ')
 +    path = nx.path.shortest_path(G,​ actor1, actor2)
 +    num = 0 # connection number
 +    if len(path) > 1:
 +        num = len(path) / 2
 +        for i in xrange(0, len(path) - 1, 2):
 +            print path[i], 'was in', path[i+1], '​with',​ path[i+2]
 +    print 'The connection number of', actor1, '​to',​ actor2, '​is',​ num
 +</​code>​
 
cs190c/lab12_09.txt ยท Last modified: 2009/04/29 23:01 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki