2
2
The Implementation will be same as Directed Graph, but this time the connection will be done both ways.
3
3
If v1 => v2 then v2 => v1
4
4
Or v1 <=> v2
5
- '''
5
+ '''
6
+
7
+ # Simple Graph Implementation
8
+
9
+ class Graph :
10
+ def __init__ (self , number_of_nodes ): # Initialise Graph Data Structure
11
+ self .number_of_nodes = number_of_nodes + 1 # Number of nodes will have to be increased by one
12
+ self .graph = [[0 for x in range (number_of_nodes + 1 )] for y in range (number_of_nodes + 1 )]
13
+
14
+ def withInBounds (self , v1 , v2 ): # Function to check if the value is within matrix bounds
15
+ return v1 >= 0 and v1 <= self .number_of_nodes and v2 >= 0 and v2 <= self .number_of_nodes
16
+
17
+ def insertEdge (self , v1 , v2 ): # Function to inser edge in the Graph
18
+ if self .withInBounds (v1 , v2 ): # Check if values are within bounds
19
+ self .graph [v1 ][v2 ] = 1 # Change the value of the node from 0 to 1
20
+ self .graph [v2 ][v1 ] = 1 # Adding the two way relation between the vertices to make in undirected
21
+
22
+ def printGraph (self ): # Functipon to Print Graph
23
+ for i in range (self .number_of_nodes ):
24
+ for j in range (len (self .graph [i ])):
25
+ if self .graph [i ][j ]:
26
+ print (i , '=>' , j )
0 commit comments