adjacencyList = {(1): {1}, (2): {2, 3}, (3): {2, 3}}


class AdjacencyMatrix:

    def __init__(self, adjacencyList, label=''):
        """ 
        Instanciation method of the class.
        Create an adjacency matrix from an adjacencyList.
        It is supposed that graph vertices are labeled with numbers from 1 to n.
        """
self.matrix = []
self.label = label
for i in range(len(list(adjacencyList.keys()))):
    self.matrix.append([0] * len(list(adjacencyList.keys())))
for key in list(adjacencyList.keys()):
    for value in adjacencyList[key]:
        self[key - 1][value - 1] = 1

def __str__(self):
    string = self.label + '\t'
for i in range(len(self.matrix)):
    string += str(i + 1) + '\t'
string += '\n'
for row in range(len(self.matrix)):
    string += str(row + 1) + '\t'
for column in range(len(self.matrix)):
    string += str(self[row][column]) + '\t'
string += '\n'
return string

def __repr__(self):
    return str(self.matrix)

def __getitem__(self, index):
    """ Allow to access matrix element using matrix[index][index] syntax """
return self.matrix.__getitem__(index)

def __setitem__(self, index, item):
    """ Allow to set matrix element using matrix[index][index] = value syntax """
return self.matrix.__setitem__(index, item)

def areAdjacent(self, i, j):
    return self[i - 1][j - 1] == 1
m = AdjacencyMatrix(adjacencyList, label='mbr')
print(m)
print(('m.areAdjacent(1,2) :', m.areAdjacent(1, 2)))
print(('m.areAdjacent(2,3) :', m.areAdjacent(2, 3)))
Create adjacency matrix in python from csv dataset