samedi 2 mars 2013

First test graph

I use NetworkX with python 2.7


import networkx as nx 
gr = nx.Graph() 

gr.add_nodes_from(['A','B','C','D','E','F','G','H']) 

gr.add_edge('A','B',distance=20) 
gr.add_edge('A','D',distance=50) 
gr.add_edge('A','G',distance=100) 
gr.add_edge('B','C',distance=20) 
gr.add_edge('C','F',distance=50) 
gr.add_edge('B','D',distance=30) 
gr.add_edge('D','G',distance=30) 
gr.add_edge('D','E',distance=50) 
gr.add_edge('E','F',distance=50)
gr.add_edge('G','H',distance=50) 
gr.add_edge('H','F',distance=150) 

 














And, i find the most efficient path (A - F)

nx.shortest_path(gr, source='A', target='F', weight="distance") 

 

















Finnaly, i find all path (A -F)

nx.all_shortest_paths(gr, source='A', target='F')

 















The file is here

Aucun commentaire:

Enregistrer un commentaire