-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs1.py
executable file
·56 lines (42 loc) · 1.02 KB
/
bfs1.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
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
from collections import defaultdict
class BFS:
def __init__( self ):
self.graph = defaultdict( list )
def addEdge( self,u, v ):
self.graph[u].append(v)
self.graph[v].append(u)
def bfs(self, s, visited):
queue = []
queue.append(s)
visited[s] = True
while queue:
s = queue.pop(0)
print ("{} {}".format(s," "))
for i in self.graph[s]:
if visited[i] == False:
queue.append(i)
visited[i] = True
def main():
bfs_obj = BFS()
visited = dict()
while(1):
choice = raw_input("\nCreate Edge - 1\nBFS - 2\nExit-3\n")
if choice == '1':
n = int( raw_input("Enter number of edges\n") )
while n!=0:
u = raw_input("First vertex:\t")
v = raw_input("Second vertex:\t")
visited[u] = False
visited[v] = False
bfs_obj.addEdge( u, v )
n=n-1
elif choice == '2':
u = raw_input("Enter source node:\t")
print("\n")
bfs_obj.bfs(u, visited)
elif choice == '3':
break
else:
print("Enter valid input")
if __name__ == '__main__':
main()