-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreedyBFS.py
103 lines (92 loc) · 2.26 KB
/
GreedyBFS.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
class Graph:
def __init__(self):
self.adjDict = dict()
self.start_node = "S"
self.goal_node = "S"
self.heuristics = dict()
def add_edge(self, u, v):
if u not in self.adjDict:
self.adjDict[u] = list()
self.adjDict[u].append(v)
def set_heuristic(self, node, value):
self.heuristics[node] = value
def print(self):
for key in self.adjDict:
print(key)
print(self.adjDict[key])
def set_start_node(self, node):
self.start_node = node
def set_goal_node(self, node):
self.goal_node = node
def best_fs(self):
q = list()
visited = list()
q.append([self.heuristics[self.start_node], self.start_node])
while q:
# print(q)
pair = q.pop(0)
node = pair[1]
if node in visited:
continue
visited.append(node)
if node == self.goal_node:
break
children = []
if node in self.adjDict:
children = self.adjDict[node]
for child in children:
q.append([self.heuristics[child], child])
q.sort()
print("path to goal node is: ")
print(visited)
def main():
print("enter input")
graph = Graph()
while True:
tokens = input().split()
parent = tokens[0]
if parent == "-1":
break
children = tokens[1:]
for child in children:
# print(child)
# print(child.split(","))
graph.add_edge(parent, child)
# graph.print()
print("enter nodes and heuristic values")
while True:
tokens = input().split()
node = tokens[0]
if node == "-1":
break
value = int(tokens[1])
graph.set_heuristic(node, value)
start_node = input("enter start node: ")
goal_node = input("enter goal node: ")
graph.set_start_node(start_node)
graph.set_goal_node(goal_node)
graph.best_fs()
# graph.ucs()
# graph.print()
main()
'''
S A B
A C D
B E F
F G
-1
enter nodes and heuristic values
S 7
A 6
B 5
C 3
D 4
E 2
F 1
G 0
-1
enter start node: S
enter goal node: G
path to goal node is:
['S', 'B', 'F', 'G']
'''