In [115]:
import numpy as np
import pandas as pd

In [8]:
A = np.mat('0 1 1 0 0; 1 0 1 1 1; 1 1 0 0 0; 0 1 0 0 0; 0 1 0 0 0')

In [9]:
A


Out[9]:
matrix([[0, 1, 1, 0, 0],
        [1, 0, 1, 1, 1],
        [1, 1, 0, 0, 0],
        [0, 1, 0, 0, 0],
        [0, 1, 0, 0, 0]])

In [25]:
shape = A.shape
shape


Out[25]:
(5, 5)

In [26]:
Z = np.matmul(A,A)
Z


Out[26]:
matrix([[2, 1, 1, 1, 1],
        [1, 4, 1, 0, 0],
        [1, 1, 2, 1, 1],
        [1, 0, 1, 1, 1],
        [1, 0, 1, 1, 1]])

In [226]:
def Seidel(A):
    n = A.shape[0]
    if all(A[i, j] for i in range(n) for j in range(n) if i != j):
        # diagonal of the matrix is 0, rest is 1 ...
        print('... found ...')
        return A
    
    Z = np.matmul(A, A)
    B = np.zeros((n, n))
    for i in range(n):
        for j in range(n):
            if i == j:
                break
            if A[i, j] == 1 or Z[i, j] > 0:
                B[i, j] = 1
    
    B = B + np.transpose(B)
    T = Seidel(B)
    X = T*A
    
    degree = [ sum( A[i, j] for j in range(n) ) for i in range(n) ]
    D = np.zeros((n, n))
    for i in range(n):
        for j in range(n):
            if i == j:
                break
            
            t = T[i, j]
            v = (2 * t)
            
            if (X[i, j] < t * degree[j]):
                v -= 1
                
            D[i, j] = v

    return D + np.transpose(D)

In [228]:
Seidel(A)


... found ...
Out[228]:
array([[ 0.,  1.,  1.,  2.,  2.],
       [ 1.,  0.,  1.,  1.,  1.],
       [ 1.,  1.,  0.,  2.,  2.],
       [ 2.,  1.,  2.,  0.,  2.],
       [ 2.,  1.,  2.,  2.,  0.]])

In [213]:
(5 - 1) / np.array([sum(D[j, i] for j in range(5)) for i in range(5)])


Out[213]:
array([ 0.66666667,  1.        ,  0.66666667,  0.57142857,  0.57142857])

In [167]:
## real world.

In [229]:
index = 0
authors = { }
for author in pd.read_csv(r'C:\Users\dmpas\data\reddit\_nodes\2011-01_nodes.csv', index_col='author').index:
    authors[author] = index
    index += 1

n = len(authors.keys())

In [230]:
matrix = np.zeros((n,n))
with open(r'C:\Users\dmpas\data\reddit\_edges\2011-01_edgelist.csv') as file:
    for line in file.readlines():
        _ = line.split(',')

        ai = authors[_[0]]
        bi = authors[_[1]]
        weight = int(_[2])

        matrix[ai,bi] += weight
        matrix[bi,ai] += weight

In [231]:
authors


Out[231]:
{'AyKarsi': 0,
 'Ddraig': 1,
 'Jescro': 2,
 'NoahTheDuke': 3,
 'Slackbeing': 4,
 'TheAncientGoat': 5,
 '_red': 6,
 'chrisrico': 7,
 'doublec': 8,
 'gigitrix': 9,
 'gvsteve': 10,
 'inertia186': 11,
 'itsnotlupus': 12,
 'joelthelion': 13,
 'loungedaddy': 14,
 'obi-nine': 15,
 'sgornick': 16,
 'swinewine': 17,
 'thefirststarfighter': 18}

In [232]:
matrix


Out[232]:
array([[ 0.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         1.,  0.,  0.,  1.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,
         0.,  1.,  1.,  0.,  0.,  1.],
       [ 0.,  1.,  0.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,
         0.,  1.,  1.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         1.,  0.,  0.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  0.,  0.,  0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,
         0.,  1.,  1.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         1.,  0.,  0.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  0.,  1.,  0.,  0.,  1.,  0.,  1.,  0.,  1.,  1.,
         0.,  1.,  1.,  0.,  0.,  1.],
       [ 0.,  1.,  1.,  0.,  1.,  0.,  1.,  0.,  0.,  1.,  0.,  1.,  1.,
         0.,  1.,  1.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,
         1.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  0.,  0.,  0.,  1.,  1.,
         0.,  1.,  1.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,
         1.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  0.,  1.,
         1.,  1.,  1.,  0.,  0.,  1.],
       [ 0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  1.,  0.,
         0.,  1.,  1.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,  1.,  0.,  1.,  1.,  0.,
         0.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  0.,  1.,  0.,  0.,  1.],
       [ 0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,
         1.,  1.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         1.,  0.,  0.,  0.,  1.,  0.],
       [ 1.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         1.,  0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,
         0.,  1.,  1.,  0.,  0.,  0.]])

In [233]:
D = Seidel(matrix)
D


... found ...
Out[233]:
array([[ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,
         1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         0.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  0.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  0.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  0.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  0.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  0.]])

In [234]:
closeness_centrality = (n - 1) / np.array([sum(D[j, i] for j in range(n)) for i in range(n)])
closeness_centrality


Out[234]:
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
        1.,  1.,  1.,  1.,  1.,  1.])

In [ ]: