Skip to content

Commit

Permalink
Merge pull request #2538 from fawad1386/master
Browse files Browse the repository at this point in the history
Depth First Search(Weighted Graph)
  • Loading branch information
fineanmol authored Oct 5, 2022
2 parents 5ce9886 + 7aab41d commit b5d9943
Showing 1 changed file with 31 additions and 0 deletions.
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')

0 comments on commit b5d9943

Please sign in to comment.