Skip to content

Instantly share code, notes, and snippets.

@Fkawala
Last active July 12, 2016 09:58
Show Gist options
  • Save Fkawala/afd0a666619c1d2716a5 to your computer and use it in GitHub Desktop.
Save Fkawala/afd0a666619c1d2716a5 to your computer and use it in GitHub Desktop.
MWE graph_tool shortest path / distance with multiple targets
import graph_tool as gt
from graph_tool.topology import shortest_path, shortest_distance
g = gt.Graph()
v = list(g.add_vertex(10))
for i in range(9):
g.add_edge(i,i+1)
# MVE of shortest_path with several targets
vlists, elists = shortest_path(g,v[0], v[-3:])
for l in shortest_path(g,v[0], v[-3:]):
for org, a in zip(v[-3:], l):
print org, ':\t', a, '\n'
# MVE of shortest_distance with several targets
print shortest_distance(g,v[0], v[-3:])
# shortest_{distance,path} are backward compatible
print shortest_distance(g, v[0], v[3])
print shortest_path(g, v[0], v[3])
@Fkawala
Copy link
Author

Fkawala commented May 3, 2015

the output is as follows:

7 : [<Edge object with source '0' and target '1' at 0x7f24057e0cc8>, <Edge object with source '1' and target '2' at 0x7f24057e0c30>, <Edge object with source '2' and target '3' at 0x7f24057e0640>, <Edge object with source '3' and target '4' at 0x7f24057e05a8>, <Edge object with source '4' and target '5' at 0x7f24057e0510>, <Edge object with source '5' and target '6' at 0x7f24057e0478>, <Edge object with source '6' and target '7' at 0x7f24057e03e0>] 

8 : [<Edge object with source '0' and target '1' at 0x7f24057e0cc8>, <Edge object with source '1' and target '2' at 0x7f24057e0c30>, <Edge object with source '2' and target '3' at 0x7f24057e0640>, <Edge object with source '3' and target '4' at 0x7f24057e05a8>, <Edge object with source '4' and target '5' at 0x7f24057e0510>, <Edge object with source '5' and target '6' at 0x7f24057e0478>, <Edge object with source '6' and target '7' at 0x7f24057e03e0>, <Edge object with source '7' and target '8' at 0x7f24057e0348>] 

9 : [<Edge object with source '0' and target '1' at 0x7f24057e0cc8>, <Edge object with source '1' and target '2' at 0x7f24057e0c30>, <Edge object with source '2' and target '3' at 0x7f24057e0640>, <Edge object with source '3' and target '4' at 0x7f24057e05a8>, <Edge object with source '4' and target '5' at 0x7f24057e0510>, <Edge object with source '5' and target '6' at 0x7f24057e0478>, <Edge object with source '6' and target '7' at 0x7f24057e03e0>, <Edge object with source '7' and target '8' at 0x7f24057e0348>, <Edge object with source '8' and target '9' at 0x7f24057e02b0>] 

7 : [<Vertex object with index '0' at 0x7f24057dfb50>, <Vertex object with index '1' at 0x7f24057dfad0>, <Vertex object with index '2' at 0x7f24057df550>, <Vertex object with index '3' at 0x7f24057df4d0>, <Vertex object with index '4' at 0x7f24057df450>, <Vertex object with index '5' at 0x7f24057df3d0>, <Vertex object with index '6' at 0x7f24057df350>, <Vertex object with index '7' at 0x7f24057df2d0>] 

8 : [<Vertex object with index '0' at 0x7f24057dfb50>, <Vertex object with index '1' at 0x7f24057dfad0>, <Vertex object with index '2' at 0x7f24057df550>, <Vertex object with index '3' at 0x7f24057df4d0>, <Vertex object with index '4' at 0x7f24057df450>, <Vertex object with index '5' at 0x7f24057df3d0>, <Vertex object with index '6' at 0x7f24057df350>, <Vertex object with index '7' at 0x7f24057df2d0>, <Vertex object with index '8' at 0x7f24070dddd0>] 

9 : [<Vertex object with index '0' at 0x7f24057dfb50>, <Vertex object with index '1' at 0x7f24057dfad0>, <Vertex object with index '2' at 0x7f24057df550>, <Vertex object with index '3' at 0x7f24057df4d0>, <Vertex object with index '4' at 0x7f24057df450>, <Vertex object with index '5' at 0x7f24057df3d0>, <Vertex object with index '6' at 0x7f24057df350>, <Vertex object with index '7' at 0x7f24057df2d0>, <Vertex object with index '8' at 0x7f24070dddd0>, <Vertex object with index '9' at 0x7f24070ddcd0>] 

[7 8 9]
3
([<Edge object with source '0' and target '1' at 0x7f24057e0640>, <Edge object with source '1' and target '2' at 0x7f24057e0c30>, <Edge object with source '2' and target '3' at 0x7f24057e0cc8>], [<Vertex object with index '0' at 0x7f24057df950>, <Vertex object with index '1' at 0x7f24057df9d0>, <Vertex object with index '2' at 0x7f24070dde50>, <Vertex object with index '3' at 0x7f24070dd9d0>])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment