Created
September 7, 2016 17:34
-
-
Save glamp/55de1957a8ba9d372c02038851b7e2f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
from yhat import YhatModel, Yhat | |
graphdict = { | |
"Houston": ["Austin", "Dallas", "Oklahoma City"], | |
"Austin": ["New Orleans", "Dallas"], | |
"New Orleans": ["Austin"], | |
"Dallas": ["New York"], | |
"Oklahoma City": ["Austin", "Dallas"], | |
"New York": ["London", "Berlin", "Chicago", "San Francisco"], | |
"San Francisco": ["Tokyo"] | |
} | |
graph = nx.Graph(graphdict) | |
def find_shortest_path(source, target): | |
return nx.shortest_path(graph, source=source, target=target) | |
class Pathfinder(YhatModel): | |
REQUIREMENTS = [ | |
"networkx" | |
] | |
def execute(self, data): | |
source = data['source'] | |
target = data['target'] | |
if source not in graph.nodes(): | |
return { "shortestPath": None, "error": "{} is not in our network".format(source)} | |
if target not in graph.nodes(): | |
return { "shortestPath": None, "error": "{} is not in our network".format(target)} | |
path = find_shortest_path(source, target) | |
return { "shortestPath": path } | |
pf = Pathfinder() | |
print pf.execute({ "source": "Houston", "target": "Oklahoma City" }) | |
print pf.execute({ "source": "Houston", "target": "Chicago" }) | |
print pf.execute({ "source": "Houston", "target": "Tokyo" }) | |
yh = Yhat(USERNAME, APIKEY, URL) | |
yh.deploy("Pathfinder", Pathfinder, globals()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment