Skip to content

Commit 9671ed2

Browse files
author
syamkumar
authored
created python.py
performed breadth first search using python
1 parent 7480583 commit 9671ed2

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

BestFirstSearch/python.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class SearchNode:
2+
def __init__(self, action, state, parent):
3+
self.state = state
4+
self.action = action
5+
self.parent = parent
6+
def path(self):
7+
if self.parent == None:
8+
return [(self.action, self.state)]
9+
else:
10+
return self.parent.path() + [(self.action, self.state)]
11+
def inPath(self, s):
12+
if s == self.state:
13+
return True
14+
elif self.parent == None:
15+
return False
16+
else:
17+
return self.parent.inPath(s)
18+
19+
def breadthFirstSearch(initialState, goalTest, actions, successor):
20+
agenda = Queue()
21+
if goalTest(initialState):
22+
return [(None, initialState)]
23+
agenda.push(SearchNode(None, initialState, None))
24+
while not agenda.isEmpty():
25+
parent = agenda.pop()
26+
newChildStates = []
27+
for a in actions(parent.state):
28+
newS = successor(parent.state, a)
29+
newN = SearchNode(a, newS, parent)
30+
if goalTest(newS):
31+
return newN.path()
32+
elif newS in newChildStates:
33+
pass
34+
elif parent.inPath(newS):
35+
pass
36+
else:
37+
newChildStates.append(newS)
38+
agenda.push(newN)
39+
return None

0 commit comments

Comments
 (0)