-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGRAPH_CREATION_SET.PY
More file actions
45 lines (38 loc) · 1.24 KB
/
GRAPH_CREATION_SET.PY
File metadata and controls
45 lines (38 loc) · 1.24 KB
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
35
36
37
38
39
40
41
42
43
44
45
# graph
# -----------------------------------------------------------------------------------------------------------
# 0-----------------1\
# | / | \
# | / | \
# | / | 2
# | / | /
# | / | /
# 4 ---------------3/
#
# -----------------------------------------------------------------------------------------------------------
class Graph:
def __init__(self,edge):
self.edge = edge
def get_path(self,start,end,path):
path = path + [start]
if start==end:
return [path]
if start not in self.edge:
return []
all_paths = []
for node in self.edge[start]:
if node not in path:
new_path = self.get_path(node,end,path)
for i in new_path:
all_paths.append(i)
return all_paths
if __name__ == '__main__':
route ={
0:set([1,4]),
1:set([0,2,3,4]),
2:set([1,3]),
3:set([1,2,4]),
4:set([0,1,3]),
}
graph = Graph(route)
path=[]
print(graph.get_path(0,2,path))