# Adjacency Matrix Representation of a Graph

``````class Graph():

def __init__(self, matrix_size):
self.matrix = []
self.matrix_size = matrix_size

# add to our matrix using the size passed in

for i in range(matrix_size):
self.matrix.append([0 for i in  range(matrix_size)])

def print_matrix(self):
"""Prints our matrix out"""

for row in self.matrix:
print(row)

def size(self):
"""Returns Size of the Matrix"""
return self.matrix_size

if vertex_1 == vertex_2:
print("Same vertex passed in!")
return

self.matrix[vertex_1][vertex_2] = 1
self.matrix[vertex_2][vertex_1] = 1

def remove_edge(self, vertex_1, vertex_2):

if self.matrix[vertex_1][vertex_2] == 0:
print("No edge exisited between the 2")
return

self.matrix[vertex_1][vertex_2] = 0
self.matrix[vertex_2][vertex_1] = 0

``````

Create a Graph Instance:

``````g = Graph(5)

print("Matrix Before:")
g.print_matrix()

print("Size of the Matrix: ", g.size())

g.print_matrix()

# removing edges from Matrix
g.remove_edge(0,3)
g.remove_edge(0,2)

print("Matrix after removing certain Edges:")
g.print_matrix()

``````

Output the Graph:

``````Matrix Before:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
Size of the Matrix:  5
[0, 0, 1, 0, 0]
[0, 0, 1, 0, 0]
[1, 1, 0, 1, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 0, 0]
No edge exisited between the 2
Matrix after removing certain Edges:
[0, 0, 0, 0, 0]
[0, 0, 1, 0, 0]
[0, 1, 0, 1, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 0, 0]

``````