Skip to content

Commit

Permalink
Depth First Search(Weighted Graph)
Browse files Browse the repository at this point in the history
  • Loading branch information
fawad1386 authored Oct 5, 2022
1 parent 3c1ff67 commit 7aab41d
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 7aab41d

Please sign in to comment.