Skip to content

Commit 23a68e1

Browse files
Merge pull request souravjain540#99 from oops-aman/add
added depth first search
2 parents ce16bfa + bb83279 commit 23a68e1

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

dfs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#DFS
2+
n = int(input("Enter the number of nodes : "))
3+
graph = {};
4+
for i in range(n):
5+
temp = list(map(str, input().split()))
6+
if len(temp) > 1:
7+
graph[temp[0]] = temp[1:]
8+
else:
9+
graph[temp[0]] = []
10+
11+
visited = set();
12+
13+
def dfs(visited, graph, node):
14+
if node not in visited:
15+
print(node, end = ' ')
16+
17+
visited.add(node)
18+
for neighbour in graph[node]:
19+
dfs(visited, graph, neighbour);
20+
21+
source = str(input("Enter the source node : "))
22+
print("Following DFS is : ")
23+
dfs(visited, graph, source)

0 commit comments

Comments
 (0)