Skip to content

Commit 370e17a

Browse files
committed
Graph traversal BFS and DFS in Python
1 parent 35892cb commit 370e17a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Data Structures/Graphs/BFS.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def Breathfirstsearch(matrix,start):
2+
visit=[start] #Keep Track of Visited elements
3+
queue=[start] #BFS is implemented using queue
4+
while(len(queue)) :
5+
ele=queue.pop(0) #pop the element from queue ie first element from list
6+
for i in range(len(matrix)):
7+
if matrix[ele][i]==1 and i not in visit: # check the adjacent elements and push into the queue if the vertix was not visited
8+
visit.append(i)
9+
queue.append(i)
10+
return visit
11+
def valid(li,n):
12+
if len(li)==n: #each row should contain n elements
13+
for i in li:
14+
if i!=0 and i!=1: #each row should contain 0's and 1's
15+
return False
16+
return True
17+
# main function
18+
if __name__ == "__main__":
19+
n=int(input("Enter the no of vertices\n")) # Read no of vertices
20+
print("Enter adjacency matrix of graph") # Read adjacency matrix
21+
matrix=[]
22+
for _ in range(n):
23+
li=list(map(int,input().split()))
24+
if(valid(li,n)==False): # check wheather the given list is in valid adjacency matrix or not should contain 0's and 1's
25+
print("Enter valid n*n matrix")
26+
exit # Terminate the program for incorrect details
27+
matrix.append(li)
28+
start=int(input("Enter the starting vertix\n"))
29+
ans=Breathfirstsearch(matrix,start)
30+
print("BFS order of the given graph is ",end=" ")
31+
print(*ans)

Data Structures/Graphs/DFS.PY

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def Depthfirstsearch(matrix,start):
2+
visit=[start] #Keep Track of Visited elements
3+
stack=[start] #DFS is implemented using stack
4+
while(len(stack)) :
5+
ele=stack.pop(-1) #pop the element from stack ie last element from list
6+
for i in range(len(matrix)):
7+
if matrix[ele][i]==1 and i not in visit: # check the adjacent elements and push into the stack if the vertix was not visited
8+
stack.append(ele)
9+
visit.append(i)
10+
stack.append(i)
11+
break
12+
return visit
13+
def valid(li,n):
14+
if len(li)==n: #each row should contain n elements
15+
for i in li:
16+
if i!=0 and i!=1: #each row should contain 0's and 1's
17+
return False
18+
return True
19+
# main function
20+
if __name__ == "__main__":
21+
n=int(input("Enter the no of vertices\n")) # Read no of vertices
22+
print("Enter adjacency matrix of graph") # Read adjacency matrix
23+
matrix=[]
24+
for _ in range(n):
25+
li=list(map(int,input().split()))
26+
if(valid(li,n)==False): # check wheather the given list is in valid adjacency matrix or not should contain 0's and 1's
27+
print("Enter valid n*n matrix")
28+
exit # Terminate the program for incorrect details
29+
matrix.append(li)
30+
start=int(input("Enter the starting vertix\n"))
31+
ans=Depthfirstsearch(matrix,start)
32+
print("DFS of the given graph is ",end=" ")
33+
print(*ans)

0 commit comments

Comments
 (0)