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 6adf769 commit 15e1f60Copy full SHA for 15e1f60
graph-nodes.py
@@ -0,0 +1,25 @@
1
+# return list of all nodes in the graph
2
+from collections import defaultdict
3
+
4
5
+def dfs(graph, start, visited, path):
6
+ path.append(start)
7
+ visited[start] = True
8
+ for neighbour in graph[start]:
9
+ if not visited[neighbour]:
10
+ dfs(graph, neighbour, visited, path)
11
+ return path
12
13
14
+v, e = map(int, input().split())
15
+graph = defaultdict(list)
16
+for i in range(e):
17
+ u, x = input().split()
18
+ graph[u].append(x)
19
+ graph[x].append(u)
20
21
+path = []
22
+start = 'A'
23
+visited = defaultdict(bool)
24
+traverse = dfs(graph, start, visited, path)
25
+print(traverse)
0 commit comments