Skip to content
Snippets Groups Projects
Commit e974f076 authored by Magnus.Ersdal's avatar Magnus.Ersdal
Browse files

sped up simplegraph.py with 10-30%

parent 881269d2
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,7 @@ def find_all_paths(graph, start, end, path=[]): ...@@ -37,7 +37,7 @@ def find_all_paths(graph, start, end, path=[]):
paths.append(newpath) paths.append(newpath)
return paths return paths
"""
def find_shortest_path(graph, start, end, path=[], level=0): def find_shortest_path(graph, start, end, path=[], level=0):
#print("start:", start, "end:",end) #print("start:", start, "end:",end)
path = path + [start] path = path + [start]
...@@ -69,17 +69,69 @@ def find_shortest_path(graph, start, end, path=[], level=0): ...@@ -69,17 +69,69 @@ def find_shortest_path(graph, start, end, path=[], level=0):
shortest = b shortest = b
path = [start] + shortest path = [start] + shortest
return path return path
"""
def find_shortest_path(graph, start, end, path=[]):
"""this is called first, fsp_post is called second"""
#print("start:", start, "end:",end)
path = path + [start]
# print("PATH :",path, "start",start,"end",end)
if start == end and end not in graph[start]:
# from the next node, which is the fastest way to the end?
alt0, alt1 = graph[start] # get the two alternatives
first = find_shortest_path(graph, alt0, end, [])
second = find_shortest_path(graph, alt1, end, [])
shortest = fsp_select_shortest(first, second)
path = [start] + shortest
return path
elif start == end:
return path
if not start in graph.keys():
# print("error, start node not in graph")
return None
shortest = None
for node in graph[start]:
if node not in path:
newpath = fsp_post(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
def fsp_select_shortest(first, second):
mini = min(len(first), len(second))
if len(first) == len(second):
# lengths are equal.
if 'IDLE' in first:
shortest = first
if 'IDLE' in second:
shortest = second
else:
shortest = first
else:
if mini == len(first):
shortest = first
else:
shortest = second
return shortest
# elif start == end: def fsp_post(graph, start, end, path=[]):
# path = find_path_back(graph, start, path) """faster version of find_shortest_path, because of checks in first call"""
#print("start:", start, "end:",end)
path = path + [start]
# print("PATH :",path, "start",start,"end",end)
if start == end:
return path
if not start in graph.keys(): if not start in graph.keys():
# print("error, start node not in graph") # print("error, start node not in graph")
return None return None
shortest = None shortest = None
for node in graph[start]: for node in graph[start]:
if node not in path: if node not in path:
newpath = find_shortest_path(graph, node, end, path, level=level+1) newpath = fsp_post(graph, node, end, path)
if newpath: if newpath:
if not shortest or len(newpath) < len(shortest): if not shortest or len(newpath) < len(shortest):
shortest = newpath shortest = newpath
return shortest return shortest
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment