In [1]:
%matplotlib inline 

import networkx as nx
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

A simple example with a barbell graph


In [2]:
G=nx.barbell_graph(10,10)

print('nx.core_number(G) : {}'.format(nx.core_number(G)))
print()

k=max(nx.core_number(G).values())
print('max(nx.core_number(G).values()): {}'.format(max(nx.core_number(G).values())))

nx.draw(G)


nx.core_number(G) : {0: 9, 1: 9, 2: 9, 3: 9, 4: 9, 5: 9, 6: 9, 7: 9, 8: 9, 9: 9, 10: 2, 11: 2, 12: 2, 13: 2, 14: 2, 15: 2, 16: 2, 17: 2, 18: 2, 19: 2, 20: 9, 21: 9, 22: 9, 23: 9, 24: 9, 25: 9, 26: 9, 27: 9, 28: 9, 29: 9}

max(nx.core_number(G).values()): 9

In [3]:
k_core = nx.k_core(G)

print('nx.core_number(G) : {}'.format(nx.core_number(G)))
print()

k=max(nx.core_number(G).values())
print('max(nx.core_number(G).values()): {}'.format(max(nx.core_number(G).values())))

nx.draw(k_core)


nx.core_number(G) : {0: 9, 1: 9, 2: 9, 3: 9, 4: 9, 5: 9, 6: 9, 7: 9, 8: 9, 9: 9, 10: 2, 11: 2, 12: 2, 13: 2, 14: 2, 15: 2, 16: 2, 17: 2, 18: 2, 19: 2, 20: 9, 21: 9, 22: 9, 23: 9, 24: 9, 25: 9, 26: 9, 27: 9, 28: 9, 29: 9}

max(nx.core_number(G).values()): 9

In [4]:
for node in k_core:
    print('Node {} | degree {}'.format(node, G.degree(node)))


Node 0 | degree 9
Node 1 | degree 9
Node 2 | degree 9
Node 3 | degree 9
Node 4 | degree 9
Node 5 | degree 9
Node 6 | degree 9
Node 7 | degree 9
Node 8 | degree 9
Node 9 | degree 10
Node 20 | degree 10
Node 21 | degree 9
Node 22 | degree 9
Node 23 | degree 9
Node 24 | degree 9
Node 25 | degree 9
Node 26 | degree 9
Node 27 | degree 9
Node 28 | degree 9
Node 29 | degree 9

In [5]:
for node in k_core:
    print('Node {} | degree {}'.format(node, k_core.degree(node)))


Node 0 | degree 9
Node 1 | degree 9
Node 2 | degree 9
Node 3 | degree 9
Node 4 | degree 9
Node 5 | degree 9
Node 6 | degree 9
Node 7 | degree 9
Node 8 | degree 9
Node 9 | degree 9
Node 20 | degree 9
Node 21 | degree 9
Node 22 | degree 9
Node 23 | degree 9
Node 24 | degree 9
Node 25 | degree 9
Node 26 | degree 9
Node 27 | degree 9
Node 28 | degree 9
Node 29 | degree 9

Higgs-twitter graphs


In [11]:
PATH = "../../data/raw/"

FILE_NETWORK_SOCIAL_EXAMPLE = "higgs-retweet_network.edgelist"

In [14]:
data = pd.read_csv(PATH + FILE_NETWORK_SOCIAL_EXAMPLE, sep=' ', names = ['source', 'target','weight'])
data.head()


Out[14]:
source target weight
0 298960 105232 1
1 95688 3393 1
2 353237 62217 1
3 4974 3571 1
4 241892 8 1

In [15]:
G1 = nx.DiGraph()
for idx,row in data.iterrows():
    G1.add_edge(row['target'], row['source'], weight= row['weight'])

In [18]:
k=max(nx.core_number(G1).values())
print('max(nx.core_number(G).values()): {}'.format(max(nx.core_number(G1).values())))


max(nx.core_number(G).values()): 12

In [45]:
k_core = nx.k_core(G1)
nx.draw(k_core)



In [43]:
k_core = nx.k_core(G1)

pos = nx.spring_layout(k_core,k=0.2)
nx.draw(k_core,with_labels = True, pos=pos,node_size=500, font_size=8)



In [48]:
nx.isolates(k_core)


Out[48]:
[]

In [42]:
k_core = nx.k_core(G1)

pos = nx.spring_layout(k_core,k=0.2)
nx.draw(k_core,with_labels = True, edgelist=[], pos=pos,node_size=500, font_size=8)



In [21]:
for node in k_core:
    print('Node {} | degree {}'.format(node, G1.degree(node)))


Node 53508 | degree 43
Node 76165 | degree 127
Node 56968 | degree 150
Node 28951 | degree 62
Node 60686 | degree 103
Node 64911 | degree 241
Node 5137 | degree 167
Node 59027 | degree 87
Node 19604 | degree 248
Node 75798 | degree 35
Node 103447 | degree 137
Node 59012 | degree 85
Node 206362 | degree 35
Node 49179 | degree 123
Node 98204 | degree 51
Node 137242 | degree 31
Node 3998 | degree 840
Node 137247 | degree 107
Node 16801 | degree 210
Node 3547 | degree 181
Node 677 | degree 5621
Node 6940 | degree 933
Node 35376 | degree 126
Node 26153 | degree 136
Node 26398 | degree 108
Node 67382 | degree 56
Node 110903 | degree 106
Node 42170 | degree 22
Node 42172 | degree 111
Node 13506 | degree 123
Node 1988 | degree 4337
Node 44086 | degree 77
Node 42182 | degree 247
Node 98762 | degree 25
Node 137271 | degree 67
Node 302797 | degree 21
Node 15439 | degree 34
Node 12751 | degree 217
Node 11991 | degree 424
Node 88 | degree 14063
Node 27483 | degree 76
Node 116323 | degree 36
Node 35729 | degree 76
Node 137321 | degree 101
Node 39885 | degree 154
Node 158955 | degree 30
Node 78701 | degree 23
Node 50927 | degree 28
Node 13808 | degree 385
Node 103921 | degree 99
Node 92274 | degree 66
Node 13813 | degree 374
Node 33833 | degree 73
Node 137246 | degree 105
Node 182906 | degree 72
Node 1276 | degree 979
Node 5226 | degree 1663

In [20]:
for node in k_core:
    print('Node {} | degree {}'.format(node, k_core.degree(node)))


Node 53508 | degree 21
Node 76165 | degree 13
Node 56968 | degree 20
Node 28951 | degree 13
Node 60686 | degree 14
Node 64911 | degree 31
Node 5137 | degree 10
Node 59027 | degree 17
Node 19604 | degree 15
Node 75798 | degree 14
Node 103447 | degree 24
Node 59012 | degree 14
Node 206362 | degree 15
Node 49179 | degree 11
Node 98204 | degree 15
Node 137242 | degree 12
Node 3998 | degree 32
Node 137247 | degree 15
Node 16801 | degree 14
Node 3547 | degree 23
Node 677 | degree 12
Node 6940 | degree 8
Node 35376 | degree 14
Node 26153 | degree 14
Node 26398 | degree 12
Node 67382 | degree 8
Node 110903 | degree 16
Node 42170 | degree 12
Node 42172 | degree 22
Node 13506 | degree 15
Node 1988 | degree 13
Node 44086 | degree 6
Node 42182 | degree 15
Node 98762 | degree 12
Node 137271 | degree 12
Node 302797 | degree 13
Node 15439 | degree 12
Node 12751 | degree 10
Node 11991 | degree 38
Node 88 | degree 35
Node 27483 | degree 11
Node 116323 | degree 12
Node 35729 | degree 19
Node 137321 | degree 13
Node 39885 | degree 35
Node 158955 | degree 12
Node 78701 | degree 12
Node 50927 | degree 11
Node 13808 | degree 16
Node 103921 | degree 17
Node 92274 | degree 17
Node 13813 | degree 19
Node 33833 | degree 5
Node 137246 | degree 17
Node 182906 | degree 14
Node 1276 | degree 13
Node 5226 | degree 15