-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBestFS.py
More file actions
127 lines (106 loc) · 3.2 KB
/
BestFS.py
File metadata and controls
127 lines (106 loc) · 3.2 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# A Node class for GBFS Pathfinding
class Node:
def __init__(self, v, weight):
self.v=v
self.weight=weight
# pathNode class will help to store
# the path from src to dest.
class pathNode:
def __init__(self, node, parent):
self.node=node
self.parent=parent
# Function to add edge in the graph.
def addEdge(u, v, weight):
# Add edge u -> v with weight weight.
adj[u].append(Node(v, weight))
# Declaring the adjacency list
adj = []
# Greedy best first search algorithm function
def GBFS(h, V, src, dest):
"""
This function returns a list of
integers that denote the shortest
path found using the GBFS algorithm.
If no path exists from src to dest, we will return an empty list.
"""
# Initializing openList and closeList.
openList = []
closeList = []
# Inserting src in openList.
openList.append(pathNode(src, None))
# Iterating while the openList
# is not empty.
while (openList):
currentNode = openList[0]
currentIndex = 0
# Finding the node with the least 'h' value
for i in range(len(openList)):
if(h[openList[i].node] < h[currentNode.node]):
currentNode = openList[i]
currentIndex = i
# Removing the currentNode from
# the openList and adding it in
# the closeList.
openList.pop(currentIndex)
closeList.append(currentNode)
# If we have reached the destination node.
if(currentNode.node == dest):
# Initializing the 'path' list.
path = []
cur = currentNode
# Adding all the nodes in the
# path list through which we have
# reached to dest.
while(cur != None):
path.append(cur.node)
cur = cur.parent
# Reversing the path, because
# currently it denotes path
# from dest to src.
path.reverse()
return path
# Iterating over adjacents of 'currentNode'
# and adding them to openList if
# they are neither in openList or closeList.
for node in adj[currentNode.node]:
for x in openList:
if(x.node == node.v):
continue
for x in closeList:
if(x.node == node.v):
continue
openList.append(pathNode(node.v, currentNode))
return []
# Driver Code
""" Making the following graph
src = 0
/ | \
/ | \
1 2 3
/\ | /\
/ \ | / \
4 5 6 7 8
/
/
dest = 9
"""
# The total number of vertices.
V = 10
## Initializing the adjacency list
for i in range(V):
adj.append([])
addEdge(0, 1, 2)
addEdge(0, 2, 1)
addEdge(0, 3, 10)
addEdge(1, 4, 3)
addEdge(1, 5, 2)
addEdge(2, 6, 9)
addEdge(3, 7, 5)
addEdge(3, 8, 2)
addEdge(7, 9, 5)
# Defining the heuristic values for each node.
h = [20, 22, 21, 10, 25, 24, 30, 5, 12, 0]
path = GBFS(h, V, 0, 9)
for i in range(len(path) - 1):
print(path[i], end = " -> ")
print(path[(len(path)-1)])