File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments