# Differences

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

 cs190c:lab12_09 [2009/04/10 11:33]tang cs190c:lab12_09 [2009/04/29 23:01] (current) Both sides previous revision Previous revision 2009/04/29 23:01 tang 2009/04/10 11:33 tang 2009/04/10 09:01 seh 2009/04/10 09:00 seh created Next revision Previous revision 2009/04/29 23:01 tang 2009/04/10 11:33 tang 2009/04/10 09:01 seh 2009/04/10 09:00 seh created 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 ===== + ​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 +

cs190c/lab12_09.txt ยท Last modified: 2009/04/29 23:01 (external edit)