We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f3dabfe commit bb83279Copy full SHA for bb83279
dfs.py
@@ -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