How to create an adjacency matrix of mutual friendships from facebook in python


I am doing a project on Social Network Analysis of Facebook Network. I had to get all my friends and who of my friends are friends with each other, mutual frindships inside my network. I did that, I got all id’s of my friends and adjacencies and now I have to form an adjacency matric which indicates if 2 of my friends are friends. For example:
A and B are friends, A and C are friends, but B and C are not friends. This would look like this:

  A  B  C

A 0  1  1

B 1  0  0

C 1  0  0

Because I have the list of id’s and adjacencies already in python, I should also do the matrix in python, so if you have any ideas or a basic algorithm how to enter 1’s and 0’s I would appreciate it.

Asked By: Ensar Jusufovic



I think this structure is better implemented as a graph. For example, take a look at NetworkX.

Anyway, if you really need matrices, a matrix can simply be implement as a list of lists, like this:

m = [[0, 1, 1],
     [1, 0, 0],
     [1, 0, 0],]

But if you intend to do any matrix manipulation, you should check out the numpy library.

Answered By: Chewie

I solved the problem, it just required 2 for loops to go thorugh the list and compare whether the users id is in the adjacency list, if that is the case, make that entry 1, otherwise 0.

Answered By: Ensar Jusufovic