Skip to content

Instantly share code, notes, and snippets.

@bombsimon
Last active July 22, 2019 13:40
Show Gist options
  • Save bombsimon/6f7ac002c52139ea9683d66674d0082a to your computer and use it in GitHub Desktop.
Save bombsimon/6f7ac002c52139ea9683d66674d0082a to your computer and use it in GitHub Desktop.
Example to show shortes route
#!/usr/bin/env python3
""" Find shortest path """
import networkx as nx
def main():
""" Main method """
objs = [
{"src": "SWE", "dst": "LDN"},
{"src": "SWE", "dst": "CPH"},
{"src": "SWE", "dst": "LAX"},
{"src": "LAX", "dst": "NYC"},
{"src": "LDN", "dst": "CPH"},
{"src": "CPH", "dst": "SWE"},
{"src": "CPH", "dst": "SPA"},
{"src": "SPA", "dst": "BEI"},
{"src": "LDN", "dst": "BEI"},
]
graph = nx.Graph()
for obj in objs:
graph.add_edge(obj["src"], obj["dst"])
print(nx.shortest_path(graph, source="SWE", target="BEI"))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment