-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.py
More file actions
31 lines (29 loc) · 847 Bytes
/
DFS.py
File metadata and controls
31 lines (29 loc) · 847 Bytes
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
from collections import defaultdict
class Graph:
def __init__(self,vertices):
self.V=vertices
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def DFSUtil(self,visited,u):
print(u,end=' ')
for i in self.graph[u]:
if visited[i]==False:
visited[i]=True
self.DFSUtil(visited,i)
def DFS(self,u):
visited=[False]*self.V
visited[u]=True
self.DFSUtil(visited,u)
q=int(input('enter no. of edges\n'))
nov=int(input('enter no. of vertices\n'))
g = Graph(nov)
for i in range(q):
a,b=input("enter edge(u,v)\n").split()
a=int(a)
b=int(b)
g.addEdge(a, b)
g.addEdge(b,a)
w=int(input("enter source vertex\n"))
print ("Following is Bfs")
g.DFS(w)