====== TQ cores (degrees) ======
[[https://github.com/bavla/TQ/tree/master/Cores|Data for examples]],
[[https://github.com/bavla/Nets/tree/master/source|libraries Nets and TQ]]
The TQ cores algorithm was presented at the Applied Statistics Conference 2016 in Ribno, Slovenia.
gdir = "c:/users/batagelj/work/python/graph/Nets"
wdir = "C:/Users/batagelj/work/Python/graph/Nets/coresTQ"
import sys, os, json
sys.path = [gdir]+sys.path; os.chdir(wdir)
from Nets import Network as N
from TQ import *
from datetime import datetime
# prLevel = 2 # basic reports
prLevel = 1 # detailed reports
fJSON = 'ExampleA.json'
# fJSON = 'TerrorNews50.json'
# fJSON = "Terror.json"
# fJSON = 'stem.json'
def TQcores(G,prLevel=2):
D = { u: G.TQnetDeg(u) for u in G._nodes }
if prLevel<2: print("Deg =",D,"\n")
Core = { u: [d for d in D[u] if d[2]==0] for u in G.nodes() }
# core number = 0
D = { u: [d for d in D[u] if d[2]>0] for u in G.nodes() }
D = { u: d for u,d in D.items() if d!=[] }
Dmin = { u: min([e[2] for e in d]) for u,d in D.items() }
step = 0
while len(D)>0:
step += 1
dmin,u = min( (v,k) for k,v in Dmin.items() )
if (step % 500 == 1) or (prLevel < 2):
print("{0:3d}. dmin={1:3d} node={2:4d}".format(step,dmin,u))
core = [ d for d in D[u] if d[2] == dmin ]
Core[u] = TQ.sum(Core[u],core)
change = TQ.setConst(core,-1)
D[u] = TQ.cutGE(TQ.sum(D[u],change),dmin)
if len(D[u])==0: del D[u]; del Dmin[u]
else: Dmin[u] = min([e[2] for e in D[u]])
for link in G.star(u):
v = G.twin(u,link)
if not(v in D): continue
chLink = TQ.extract(G.getLink(link,'tq'),change)
if chLink==[]: continue
diff = TQ.cutGE(TQ.sum(D[v],chLink),0)
if len(diff)==0: del D[v]; del Dmin[v]
else:
D[v] = TQ.standard([(sd,fd,max(vd,dmin)) for sd,fd,vd in diff])
Dmin[v] = min([e[2] for e in D[v]])
print("{0:3d}. dmin={1:3d} node={2:4d}".format(step,dmin,u))
return(Core)
G = N.loadNetsJSON(fJSON); G.delLoops()
print("Temporal cores in: ",fJSON)
t1 = datetime.now()
print("started: ",t1.ctime(),"\n")
Core = TQcores(G)
if prLevel<2: print("\n-----\nCore =",Core)
t2 = datetime.now()
print("\nfinished: ",t2.ctime(),"\ntime used: ", t2-t1)
>>>
==== RESTART: C:\Users\batagelj\work\Python\graph\Nets\coresTQ\CoresTQ.py ====
Temporal cores in: ExampleA.json
started: Thu Jul 9 01:08:35 2020
1. dmin= 1 node= 1
31. dmin= 3 node= 11
-----
Core = {
1: [(1, 10, 1)],
2: [(1, 2, 2), (2, 5, 3), (5, 10, 2)],
3: [(1, 2, 2), (2, 5, 3), (5, 10, 2)],
4: [(1, 2, 2), (2, 5, 3), (5, 10, 2)],
5: [(1, 2, 2), (2, 5, 3), (5, 10, 2)],
6: [(1, 3, 2), (3, 5, 3), (5, 10, 2)],
7: [(1, 3, 2), (3, 5, 3), (5, 10, 2)],
8: [(1, 3, 2), (3, 5, 3), (5, 10, 2)],
9: [(1, 10, 2)],
10: [(1, 10, 2)],
11: [(1, 3, 2), (3, 5, 3), (5, 10, 2)],
12: [(1, 10, 1)],
13: [(1, 10, 1)],
14: [(1, 4, 1), (4, 6, 2), (6, 10, 1)],
15: [(1, 10, 1)],
16: [(1, 10, 2)],
17: [(1, 10, 2)],
18: [(1, 10, 2)],
19: [(1, 10, 1)] }
finished: Thu Jul 9 01:08:36 2020
time used: 0:00:01.257072
>>>
>>>
==== RESTART: C:\Users\batagelj\work\Python\graph\Nets\coresTQ\CoresTQ.py ====
network: exampleB
simple violence
simple= True directed= True org= 1 mode= 1 multirel= False temporal= True
nodes= 29 links= 176 arcs= 176 edges= 0
Tmin= 1 Tmax= 49
Temporal cores in: violenceTQ.json
started: Thu Jul 9 01:34:36 2020
1. dmin= 1 node= 1
161. dmin= 5 node= 16
finished: Thu Jul 9 01:34:37 2020
time used: 0:00:00.358021
>>> C4 = TQ.TQdictCut(Core,4)
>>> for v in C4:
print("{0:3d} : {1:11s} ".format(v,G.getNode(v,'lab')),C4[v])
1 : undefined [(17, 18, 4), (25, 26, 4), (29, 30, 5), (31, 32, 4), (38, 40, 4), (41, 42, 4),
(48, 49, 4)]
2 : ? [(31, 32, 4), (33, 34, 4)]
3 : people [(25, 26, 4), (29, 30, 4), (31, 32, 4), (33, 34, 4), (35, 36, 4), (39, 40, 4),
(41, 42, 4), (48, 49, 4)]
4 : police [(17, 18, 4), (29, 30, 5), (31, 32, 4), (33, 34, 4), (35, 36, 4), (39, 40, 4)]
7 : fascists [(25, 26, 4), (29, 30, 5), (31, 32, 4), (33, 34, 4), (35, 36, 4), (38, 40, 4),
(41, 42, 4), (48, 49, 4)]
8 : communists [(29, 30, 5), (31, 32, 4)]
10 : socialists [(17, 18, 4), (29, 30, 4), (35, 36, 4), (39, 40, 4)]
16 : workers [(17, 18, 4), (29, 30, 5), (31, 32, 4), (35, 36, 4), (38, 40, 4), (41, 42, 4)]
17 : the right [(17, 18, 4)]
20 : students [(17, 18, 4)]
21 : catholics [(33, 34, 4)]
25 : republicans [(31, 32, 4)]
26 : thugs [(29, 30, 4)]
>>>