Skip to content

Commit 28577da

Browse files
Merge pull request #648 from sriharsha200/my-techdoc-contribution
Graph traversal BFS and DFS in Python
2 parents 06e13e5 + bf4ae8b commit 28577da

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Data Structures/Graphs/BFS.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# BFS Traversal of Graph using Adjacency Matrix Representation
2+
# Function To implement BFS (Breath First Search)
3+
def Breathfirstsearch(matrix,start):
4+
visit=[start] #Keep Track of Visited Vertex
5+
queue=[start] #BFS is implemented using queue
6+
while(len(queue)) :
7+
ele=queue.pop(0) #pop the element from queue ie first element from list
8+
for i in range(len(matrix)):
9+
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
10+
visit.append(i)
11+
queue.append(i)
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=Breathfirstsearch(matrix,start)
32+
print("BFS order of the given graph is ",end=" ")
33+
print(*ans)

Data Structures/Graphs/DFS.PY

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

0 commit comments

Comments
 (0)