class Graph:
def __init__(self, gdict=None):
if gdict is None:
gdict = {}
self.gdict = gdict
def bfs(self, start, end):
queue = []
queue.append([start])
print('queue',queue)
while queue:
path = queue.pop(0)
node = path[-1]
print('path',path)
if node == end:
return path
for adjacent in self.gdict.get(node, []):
print('path in for loop',path)
new_path = list(path)
print('newpath',new_path)
new_path.append(adjacent)
queue.append(new_path)
print('queue in for loop',queue)
customDict = { "a" : ["b", "c"],
"b" : ["d", "g"],
"c" : ["d", "e"],
"d" : ["f"],
"e" : ["f"],
"g" : ["f"]
}
g = Graph(customDict)
print(g.bfs("a", "g"))
this is a code for Single Source shortest path in graph using BFS my doubt is in for loop when new_path = list(path) is used i get a different answer new_path = path i get a differnt answer what does this list() do when i use without the list function the path is modified why....I have many print statements for debugging and for my understanding so please ignore