Here is the solution. It does not give you the directly requested adjacency matrix, but it gives you what you need to create it yourself.
Executing this code gives the following result:
{1: {1}, 2: {2, 3}, 3: {2, 3}}
This is a dictionary in which each mnbr has an associated mnbrs adjacency mnbrs . This is actually an adjacency list, which is a compressed adjacency matrix. You can expand it and build the matrix that you requested using dictionary keys and values in the form of row and column indexes.
Hope this helps. Arthur.
EDIT: I introduced the adjacency list approach so that you can implement your own adjacency matrix construction. But you should consider really using this data structure if your data is sparse. See http://en.wikipedia.org/wiki/Adjacency_list
EDIT 2: add code to convert adjacencyList to small smart adjacencyMatrix
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 #create an empty matrix for i in range(len(adjacencyList.keys())): self.matrix.append( [0]*(len(adjacencyList.keys())) ) for key in adjacencyList.keys(): for value in adjacencyList[key]: self[key-1][value-1] = 1 def __str__(self): # return self.__repr__() is another possibility that just print the list of list # see python doc about difference between __str__ and __repr__ #label first line string = self.label + "\t" for i in range(len(self.matrix)): string += str(i+1) + "\t" string += "\n" #for each matrix line : 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))
This code gives the following result:
mbr 1 2 3 1 1 0 0 2 0 1 1 3 0 1 1 m.areAdjacent(1,2) : False m.areAdjacent(2,3) : True