49
49
class Graph :
50
50
def __init__ (self , vertices ):
51
51
self ._vertices = vertices
52
- self ._adjacent_matrix = np .array ((vertices , vertices ))
52
+ self ._adjacent_matrix = np .zeros ((vertices , vertices ))
53
53
54
54
def insert_edge (self , u , v , weight_edge = 1 ):
55
55
self ._adjacent_matrix [u ][v ] = weight_edge
@@ -98,4 +98,27 @@ def indegree(self, v):
98
98
99
99
# Helper function to display adjacency matrix
100
100
def display_adjacent_matrix (self ):
101
- print (self ._adjacent_matrix )
101
+ print (self ._adjacent_matrix )
102
+
103
+ # Undirected Graph
104
+ G = Graph (4 )
105
+ G .display_adjacent_matrix ()
106
+ print ('Vertices:' , G .vertex_count ())
107
+ print ('Edges:' , G .edge_count ())
108
+ G .insert_edge (0 , 1 )
109
+ G .insert_edge (0 , 2 )
110
+ G .insert_edge (1 , 0 )
111
+ G .insert_edge (1 , 2 )
112
+ G .insert_edge (2 , 0 )
113
+ G .insert_edge (2 , 1 )
114
+ G .insert_edge (2 , 3 )
115
+ G .insert_edge (3 , 2 )
116
+ G .display_adjacent_matrix ()
117
+ print ('Vertices:' , G .vertex_count ())
118
+ print ('Edges:' , G .edge_count ())
119
+ G .edges_print ()
120
+ print ('Edge between 1-3' , G .exist_edge (1 , 3 ))
121
+ print ('Edge between 1-2' , G .exist_edge (1 , 2 ))
122
+ print ('Degree' , G .indegree (2 ))
123
+ G .remove_edge (1 ,2 )
124
+ print ('Edge between 1-2' , G .exist_edge (1 , 2 ))
0 commit comments