-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGraph.py
111 lines (88 loc) · 2.92 KB
/
Graph.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from collections import defaultdict
from State import TERMINAL_STATE
import time
class Graph:
def __init__(self):
self.bfs_parent = {}
self.dfs_parent = {}
self.expandedBFS = 0
self.expandedDFS = 0
def BFS(self, s):
self.expandedBFS = 0
self.bfs_parent[s] = None
visited = {(s.missionaries, s.cannibals, s.dir): True}
s.level = 0
start_time = time.time()
queue = [s]
while queue:
self.expandedBFS += 1
u = queue.pop(0)
if u.isGoalState():
print("No of Expanded Nodes: " + str(self.expandedBFS))
print("No of Explored Nodes: " + str(visited.__len__()))
queue.clear()
self.bfs_parent[TERMINAL_STATE] = u
return self.bfs_parent
# Stops searching after a certain time/node limit
t = time.time() - start_time
if t > u.CONSTANTS.MAX_TIME or self.expandedBFS > u.CONSTANTS.MAX_NODES:
if t > u.CONSTANTS.MAX_TIME:
print("%.2fs EXCEEDED TIME LIMIT of %.2fs" % (t, u.CONSTANTS.MAX_TIME))
else:
print("EXCEEDED NODE LIMIT of %d" % u.CONSTANTS.MAX_NODES)
print("No of Expanded Nodes: " + str(self.expandedBFS))
print("No of Explored Nodes: " + str(visited.__len__()))
queue.clear()
return {}
for v in reversed(u.successors()):
if (v.missionaries, v.cannibals, v.dir) not in visited.keys():
self.bfs_parent[v] = u
v.level = u.level + 1
queue.append(v)
visited[(v.missionaries, v.cannibals, v.dir)] = True
return {}
def DFS(self, s):
self.expandedDFS = 0
self.dfs_parent[s] = None
visited = {(s.missionaries, s.cannibals, s.dir): True}
start_time = time.time()
stack = [s]
while stack:
u = stack.pop()
self.expandedDFS += 1
if u.isGoalState():
print("No of Expanded Nodes: " + str(self.expandedDFS))
print("No of Explored Nodes: " + str(visited.__len__()))
self.dfs_parent[TERMINAL_STATE] = u
stack.clear()
return self.dfs_parent
t = time.time() - start_time
# Stops searching after a certain time/node limit
if t > u.CONSTANTS.MAX_TIME or self.expandedDFS > u.CONSTANTS.MAX_NODES:
if t > u.CONSTANTS.MAX_TIME:
print("%.2fs EXCEEDED TIME LIMIT of %.2fs" % (t, u.CONSTANTS.MAX_TIME))
else:
print("EXCEEDED NODE LIMIT of %d" % u.CONSTANTS.MAX_NODES)
print("No of Expanded Nodes: " + str(self.expandedDFS))
print("No of Explored Nodes: " + str(visited.__len__()))
stack.clear()
return {}
for v in u.successors():
if (v.missionaries, v.cannibals, v.dir) not in visited.keys():
visited[(v.missionaries, v.cannibals, v.dir)] = True
self.dfs_parent[v] = u
stack.append(v)
return {}
# Prints the path returned by BFS/DFS
def printPath(self, parentList, tail):
if tail is None:
return
if parentList == {} or parentList is None: # tail not in parentList.keys():
return
if tail == TERMINAL_STATE: tail = parentList[tail]
stack = []
while tail is not None:
stack.append(tail)
tail = parentList[tail]
while stack:
print(stack.pop())