@icr0/

dfs-implementation

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#      0
#    /   \
#   1     2
#    \   /
#      3
# 
n = int(input())
graph = [[] for i in range(n)]
for i in range(n):
  u,v = [int(i) for i in input().split(' ')]
  graph[u].append(v)

visited = [False]*n

def dfs(cur):
  global visited
  print(f"DFS CURRENT NODE: {cur}")
  visited[cur] = True
  for nextnode in graph[cur]:
    if visited[nextnode] == False:
      dfs(nextnode)

dfs(0)
if visited[3]==True:
  print("reachable")
else:
  print("its not.")