-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS(Python).py
More file actions
77 lines (56 loc) · 1.06 KB
/
DFS(Python).py
File metadata and controls
77 lines (56 loc) · 1.06 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import sys
from collections import defaultdict
sys.stdin = open('in.txt', 'r')
sys.stdout = open('out.txt', 'w')
# Code from here
"""def main(n,e,graph):
vis=[0]*(n+1)
def dfs(v):
vis[v]=1 #Function within Function Solution
print(v,end=" ")
for i in graph[v]:
if(vis[i]==0):
dfs(i)
dfs(1)
"""
class Solution(object):
def main(self,n,e,graph):
self.graph= graph
self.vis= [0]*(n+1)
self.dfs(1) # Class Solution (Recommended)
def dfs(self,v):
self.vis[v]=1
print(v,end=" ")
for i in self.graph[v]:
if(self.vis[i]==0):
self.dfs(i)
if __name__ == "__main__":
n, e = map(int, input().split()) # n is no of vertices and e is no of edges
graph = defaultdict(list) # adjacency list
for _ in range(e):
v1 ,v2 =map(int,input().split())
graph[v1].append(v2)
graph[v2].append(v1)
# main(n,e,graph)
a=Solution()
a.main(n,e,graph)
"""
Input are provided as :-
7 6 (n and e)
1 2 (e edges)
1 5
2 3
2 4
5 6
5 7
(or)
1
|___2
| |___3
| |___4
|
|___5
|___6
|___7
Binary Tree...
"""