Skip to content

Commit bb83279

Browse files
committed
added depth first search
1 parent f3dabfe commit bb83279

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

dfs.py

+23
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)