forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.py
58 lines (56 loc) · 1.84 KB
/
dfs.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
57
58
# Python3 Program to print BFS traversal
# From a given source vertex.
from collections import defaultdict
# This class represents a directed graph
# using adjacency list representation
class Graph:
# Constructor
def __init__(self):
# default dictionary to store graph
self.graph = defaultdict(list)
def addEdge(self,u,v):
'''function to add an edge to graph '''
self.graph[u].append(v)
def BFS(self, s):
'''Function to print a BFS of graph '''
# Mark all the vertices as not visited
visited = [False] * (len(self.graph))
# Create a queue for BFS
queue = []
# Mark the source node as visited and enqueue it .
queue.append(s)
visited[s] = True
while queue:
# Dequeue a vertex from queue and print it
s = queue.pop(0)
print (s, end = " ")
# Get all adjacent vertices of the dequeued vertex s.
# In case an adjacent has not been visited, then mark it visited and enqueue it
for i in self.graph[s]:
if visited[i] == False:
queue.append(i)
visited[i] = True
# Driver code
if __name__ == "__main__":
g = Graph()
# (OR)
'''
edges=int(input("Please enter the number of edges you need (min 2) :\t "))
j=1
while edges >0:
print("Edge"+str(j)+":")
x=int(input("Enter x-axis value : \t"))
y=int(input("Enter y-axis value : \t"))
g.addEdge(x,y)
j+=1
edges-=1
'''
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
print ("Following is Breadth First Traversal"
" (starting from vertex 2)")
g.BFS(2)