-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHCS.py
35 lines (28 loc) · 959 Bytes
/
HCS.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
graph={'A':[(8,'B'),(20,'C')],
'B':[(25,'A'),(20,'C'),(6,'D')],
'C':[(25,'A'),(8,'B'),(12,'E')],
'D':[(8,'B'),(12,'E'),(0,'F')],
'E':[(20,'C'),(6,'D'),(0,'F')],
'F':[(6,'D'),(12,'E')]
}
def HillClimbing(graph,startNode,goalNode):
queue=[startNode]
visited=[]
success=[startNode]
while queue:
node=queue.pop(queue.index(min(queue)))
if node[1] not in visited:
size = len(success)-1
if node[0] <= success[size-1][0]:
visited.append(node[1])
success.append(node)
else:
print("Goal not found.")
return
if node[1] == goalNode:
return visited
for n in (graph[node[1]]):
queue.append(n)
return visited
path=HillClimbing(graph,(25,'A'),'F')
print("path is:",path)