Skip to content

Commit 15e1f60

Browse files
authored
Part 2- Graph theory -> O(v+e)
O(v+e) => O(vertices+edges)
1 parent 6adf769 commit 15e1f60

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: graph-nodes.py

+25
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)