-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS-frontier-class-node.py
59 lines (47 loc) · 1.26 KB
/
DFS-frontier-class-node.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
class Node:
def __init__(self, name):
self.name = name
self.children = []
def addChild(self, list):
for c in list:
self.children.append(c)
def DFS(initialState, goal):
frontier = [initialState]
explored = []
while frontier:
s = 'frontier: '
for i in frontier:
s += i.name + ' '
print(s)
state = frontier.pop()
explored.append(state)
if goal == state.name:
return explored
for child in state.children:
if child not in (explored and frontier):
frontier.append(child)
return False
if __name__ == '__main__':
nodeA = Node('A')
nodeB = Node('B')
nodeC = Node('C')
nodeD = Node('D')
nodeE = Node('E')
nodeF = Node('F')
nodeG = Node('G')
nodeH = Node('H')
nodeI = Node('I')
nodeJ = Node('J')
nodeK = Node('K')
nodeL = Node('L')
nodeM = Node('M')
nodeN = Node('N')
nodeO = Node('O')
nodeA.addChild([nodeB, nodeC])
nodeB.addChild([nodeD, nodeE])
nodeC.addChild([nodeF, nodeG])
nodeD.addChild([nodeH, nodeI])
nodeE.addChild([nodeJ, nodeK])
nodeF.addChild([nodeL, nodeM])
nodeG.addChild([nodeN, nodeO])
result = DFS(nodeA, 'H')