Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Python/Depth First Search(Weighted Graph).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Using a Python dictionary to act as an adjacency list
graph = {
'A' : [[8,'B'],[5,'E']],
'B' : [[2,'D'],[1,'C']],
'E' : [[1,'G']],
'G' : [],
'C' : [[3,'F']],
'F' : [[4,'G']],
'D' : [[3,'E']]

}
print("The Path Is = ",end = " ")
found=0
visited = set()
def dfs(visited, graph, node,goal):
global found
if found==1:
return
elif node not in visited:
print(node,end=" ")
if node ==goal:
print ("\n***Goal Found***")
found=1
return
visited.add(node)
templist=graph[node]
templist.sort()
for neighbour in templist:
if len(neighbour)>0:
dfs(visited, graph,neighbour[1],'G')
dfs(visited, graph, 'A','G')