forked from aaronshaver/graph-unique-paths
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.py
34 lines (30 loc) · 1019 Bytes
/
puzzle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python3
class Puzzle:
def get_all_exits(self, graph):
exits = []
for key, value in graph.items():
for item in value:
if 'Exit' in item:
exits += item
return exits
def find_all_paths(self, graph, start, end, path=None):
if path is None:
path = []
path = path + [start]
if start == end:
return [path]
if start not in graph:
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = self.find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
def solve(self, graph=None):
unique_paths = []
for exit in self.get_all_exits(graph):
for start, connected_nodes in graph.items():
unique_paths += self.find_all_paths(graph, start, exit)
return unique_paths