-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_functions
71 lines (57 loc) · 1.53 KB
/
python_functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import networkx as nx
from numpy import random as rd
import numpy as np
def computeZfromGraph(seeds,G,a=0.01,b=0.1):
def f(G,seeds,z):
k=0.
for seed in seeds:
H=nx.DiGraph(G)
y1=G.node[seed]['fitness']
H.remove_node(seed)
for j in H.nodes():
y2=G.node[j]['fitness']
k+=z*y1*y2/(1+z*y1*y2)
k+=-nx.Graph(G).degree()[seed]
return k
if sum(nx.degree(G,seeds).values())==0:
return 0.
f_a=f(G,seeds,a)
f_b=f(G,seeds,b)
count=0
while f_a*f_b>0 and count<=100:
count+=1
if f_a>0:
b=a
a/=10.
if f_a<0:
a=b
b*=10.
f_a=f(G,seeds,a)
f_b=f(G,seeds,b)
c=(a+b)/2
while (b-a)>pow(10,np.log(c)/np.log(10.)-2):
c=(a+b)/2
if f(G,seeds,c)==0.:
return c
else:
if f(G,seeds,c)*f(G,seeds,a)<=0:
b=c
if f(G,seeds,c)*f(G,seeds,b)<=0:
a=c
return c
def fitModelz(G,z):
H=G.nodes()
l=G.nodes()
G0=nx.Graph()
G0.add_nodes_from(G)
for i in l:
H.remove(i)
G0.node[i]['fitness']=G.node[i]['fitness']
y1=G.node[i]['fitness']
for j in H:
y2=G.node[j]['fitness']
p_i_j=z*y1*y2/(1+z*y1*y2)
coin=rd.binomial(1, p_i_j)
if coin==1 and (not G0.has_edge(i,j)):
G0.add_edge(i,j) #a ogni link un peso uguale
return G0