0% found this document useful (0 votes)
6 views1 page

GBFS

Uploaded by

mysticpatel20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

GBFS

Uploaded by

mysticpatel20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

from collections import defaultdict

class Graph:
def __init__(self):
self.graph = defaultdict(list)
self.hcost = []
def addEdge(self, u, v, c):
self.graph[u].append((v, c))
self.graph[v].append((u, c))
def setcost(self, h):
self.hcost = h
def GBF(self, s, goal):
visited = [False] * (len(self.graph))
frontier_q = []
cost_q = []
frontier_q.append(s)
cost_q.append(0)
visited[s] = True
while frontier_q:
print("frontier_q",frontier_q)
print("cost_q",cost_q)
minpos = cost_q.index(min(cost_q))
print("minpos",minpos)
ct = cost_q.pop(minpos)
print("ct",ct)
s = frontier_q.pop(minpos)
print(s,"cost:", ct)
if s == goal:
print("Goal found")
return
for i, j in self.graph[s]:
if not visited[i]:
frontier_q.append(i)
cost_q.append(j + ct)
visited[i] = True
print("Goal notfound")
g = Graph()
g.addEdge(0, 1, 1)
g.addEdge(0, 3, 4)
g.addEdge(1, 2, 3)
g.addEdge(2, 3, 5)
g.addEdge(3, 4, 6)
g.addEdge(4, 5, 7)
g.addEdge(3, 5, 4)
g.GBF(0, 5)

You might also like