-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim2.py
2247 lines (1898 loc) · 60.7 KB
/
sim2.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 21 16:57:47 2016
@author: Longjie Li
"""
import networkx as nx
import numpy as np
import scipy.stats.stats as stats
import math
from scipy.special import comb
import weight_clustering3 as wc3
#计算顶点间的相似度
#@param graph 训练网络
#@param method 计算相似度的方法
#@param p1 相似度计算方法中的参数1
#@param p2 相似度计算方法中的参数2
def similarities(graph, method):
method = method.upper()
# 几个常用的方法
if method == 'CN':
return common_neighbors_index(graph)
if method == 'RA':
return resource_allocation_index(graph)
if method == 'LNB_CN':
return LNB(graph, 'CN')
if method == 'LNB_AA':
return LNB(graph, 'AA')
if method == 'LNB_RA':
return LNB(graph, 'RA')
if method == 'CLNB_CN':
return CLNB(graph, 'CN')
if method == 'CLNB2_CN':
return CLNB2(graph, 'CN')
if method == 'CLNB2_AA':
return CLNB2(graph, 'AA')
if method == 'CLNB2_RA':
return CLNB2(graph, 'RA')
if method == 'JACCARD':
return jaccard_coefficient(graph)
if method == 'AA':
return adamic_adar_index(graph)
if method == 'PA':
return preferential_attachment_index(graph)
if method == 'ADP':
return adaptive_degree_penalization(graph, 2.5)
if method == 'CCLP':
return CCLP(graph)
if method == 'NLC':
return NLC(graph)
if method == 'LCCL':
return local_path_index(graph, 1)
if method == 'ERA':
return extend_resource_allocation_index(graph, 0.04)
if method == 'CN_PA':
return CN_PA(graph, 0.001)
if method == 'LP':
return local_path_index(graph, 0.001)
if method == 'CNAD':
return common_neighbors_and_distance(graph, 5)
if method == 'HCR':
return HCR(graph, 0.01)
if method == 'CAR':
return CAR(graph)
if method == 'CRA':
return CRA(graph)
if method == 'CAA':
return CAA(graph)
if method == 'CJC':
return CJC(graph)
if method == 'CPA':
return CPA(graph)
if method == 'MI':
return MI(graph)
if method == 'MI2':
return MI2(graph)
if method == 'MI5':
return MI5(graph)
if method == 'CMI':
return CMI(graph)
if method == 'CMI2':
return CMI2(graph)
if method == 'MA':
return MA(graph)
if method == 'MM':
return MM(graph, 0.001)
if method == 'MA2':
return Madm(graph)
if method == 'MA3':
return Madm2(graph, 0.001)
if method == 'LB':
return (graph)
else:
raise Exception('方法错误', method)
###############################################################################
'''
average degree of a network
'''
def average_degree(G):
s = 0
for v in G:
s += nx.degree(G, v)
# end for
return s / nx.number_of_nodes(G)
# end def
def pair(x, y):
if (x < y):
return (x, y)
else:
return (y, x)
#end if
#end def
'''
insert a ground node into network
'''
def add_ground_node(G):
node_num = nx.number_of_nodes(G)
u = node_num
G.add_node(u)
for v in range(node_num):
G.add_edge(u, v)
# end for
# end def
'''
remove the ground node from network
'''
def remove_ground_node(G):
node_num = nx.number_of_nodes(G)
u = node_num - 1
G.remove_node(u)
# end def
def shortest_path_length(G, source, target, cutoff=None):
"""Compute shortest path length between source and target
Parameters
----------
G : NetworkX graph
source : node label
Starting node for path
target : node label
ending node for path
cutoff : integer, optional
Depth to stop the search.
If length(source, target) <= cutoff, return length(source, target)
Else return cutoff + 1
Returns
-------
length : integer
"""
level = 0 # the current level
nextlevel = {source: 1} # list of nodes to check at next level
paths = {source: [source]} # paths dictionary (paths to key from source)
if cutoff == 0:
return 1
#end if
while nextlevel:
thislevel = nextlevel
nextlevel = {}
for v in thislevel:
for w in G[v]:
if w not in paths:
paths[w] = paths[v] + [w]
nextlevel[w] = 1
if w == target:
return level + 1 # 找的目标节点了
#end if
#end if
#end for
#end for
level = level + 1
if (cutoff is not None and cutoff <= level):
break
#end if
#end while
return cutoff + 1
#end def
def dist(u, v, dist_dict):
if u not in dist_dict.keys():
return 0
# end if
if v not in dist_dict[u].keys():
return 0
# end if
return dist_dict[u][v]
# end def
###############################################################################
"""
Link prediction algorithms.
"""
def trans(m):
a = [[] for i in m[0]]
for i in m:
for j in range(len(i)):
a[j].append(i[j])
return a
# Coefficient-variation weight (CV)
def get_weights(matrix, m, n):
x_mean_list = [sum(matrix[i]) / n for i in range(m)]
cv_list = [0 for i in range(m)]
# # 计算平均值
# for i in range(m):
# x = 0
# for j in range(n):
# x += matrix[i][j]
# # end for
# x_mean_list[i] = x / n
# # end for
# 计算s
n_minus_one = n - 1
for i in range(m):
s = 0
for j in range(n):
s += math.pow(matrix[i][j] - x_mean_list[i], 2)
# end for
s = s / n_minus_one
s = math.sqrt(s)
cv_list[i] = s / x_mean_list[i]
# end for
sum_cv = sum(cv_list)
weight_list = [cv_list[i] / sum_cv for i in range(m)]
return weight_list
def get_weights2(matrix, m, n):
weight_list = [0 for i in range(m)]
alpha = -1 / math.log(n) # -1/ln(n)
sum_weight = 0
for i in range(m): # 方法的个数
beta = 0
for j in range(n):
r = matrix[i][j]
try:
beta += r * math.log(r)
except ValueError:
pass
# end for
H = alpha * beta
gamma = 1 - H
weight_list[i] = gamma
sum_weight += gamma
# end for
for i in range(m):
weight_list[i] /= sum_weight
# end for
return weight_list
def LB(G):
sim_dict = {}
node = nx.nodes(G)
non_edge = nx.non_edges(G)
block = [i for i in range(nx.number_of_nodes(G))]
for i in node:
node_block = []
node_block.append(i)
for j in nx.neighbors(G, i):
node_block.append(j)
block[i] = node_block
belong_block = [i for i in range(nx.number_of_nodes(G))]
for i in node:
belong_node = []
for j in block:
if i in j:
belong_node.append(j)
belong_block[i] = belong_node
for u, v in non_edge:
s = 0
block_u = belong_block[u]
block_v = belong_block[v]
for i in block_u:
for j in block_v:
if (i != j):
s += len(set(i) & set(j)) / (len(i) * len(j))
else:
s += 2 / (nx.degree(G, i[0]) + 1)
sim_dict[(u, v)] = s
return sim_dict
def MA(G):
sim_dic_ra = resource_allocation_index(G)
sim_dic_lp = local_path_index(G, 0.001)
sim_dic_car = CAR(G)
sim_dic = {}
ebunch = nx.non_edges(G)
ra_list = []
lp_list = []
car_list = []
i = 0
for u, v in ebunch:
i = i + 1
ra_list.append(sim_dic_ra[u, v])
lp_list.append(sim_dic_lp[u, v])
car_list.append(sim_dic_car[u, v])
matrix = []
size = len(lp_list)
for j in range(size):
matrix.append([ra_list[j], lp_list[j], car_list[j]])
matrix = trans(matrix)
weightlist = get_weights(matrix, 3, size)
ebunch = nx.non_edges(G)
for u, v in ebunch:
sim_dic[u, v] = weightlist[0] * sim_dic_ra[u, v] + weightlist[1] * sim_dic_lp[u, v] + weightlist[2] * \
sim_dic_car[u, v]
return sim_dic
def MM(G, alpha):
num_basic_method = 3 # 基本方法的个数,RA,CAR,LP
# 1. 计算LP
sim_dict = local_path_index(G, alpha)
size = len(sim_dict)
sim_matrix = np.zeros((num_basic_method, size))
index_to_pair_list = [0 for i in range(size)]
pair_to_index_dict = {}
square_sum_list = [0 for i in range(num_basic_method)]
#sum_list = [0 for i in range(num_basic_method)]
m = 0
i = 0
for k in sim_dict.keys():
s = sim_dict[k]
pair_to_index_dict[k] = i
index_to_pair_list[i] = k
sim_matrix[m][i] = s
square_sum_list[m] += s * s
#sum_list[m] += s
i += 1
# end for
m += 1
# 2. RA
sim_dict = resource_allocation_index(G)
for k in sim_dict.keys():
s = sim_dict[k]
i = pair_to_index_dict[k]
sim_matrix[m][i] = s
square_sum_list[m] += s * s
# end for
m += 1
# 3. CAR
sim_dict = CAR(G)
for k in sim_dict.keys():
s = sim_dict[k]
i = pair_to_index_dict[k]
sim_matrix[m][i] = s
square_sum_list[m] += s * s
# sum_list[m] += s
# end for
sim_dict.clear()
# normalzie sim_matrix
for i in range(num_basic_method):
s = math.sqrt(square_sum_list[i])
for j in range(size):
sim_matrix[i][j] /= s
# end for
# end for
# 加权
weight_list = get_weights(sim_matrix, num_basic_method, size)
for i in range(num_basic_method):
sim_matrix[i] *= weight_list[i]
# end for
# 求和
for j in range(size):
s = 0
for i in range(num_basic_method):
s += sim_matrix[i][j]
# end for
sim_dict[index_to_pair_list[j]] = s
# end for
return sim_dict
# end def
#
def Madm(G):
sim_dic_ra = resource_allocation_index(G)
sim_dic_lp = local_path_index(G, 0.001)
sim_dic_car = CAR(G)
sim_dic = {}
ebunch = nx.non_edges(G)
ra_list = []
lp_list = []
car_list = []
i = 0
for u, v in ebunch:
i = i + 1
ra_list.append(sim_dic_ra[u, v])
lp_list.append(sim_dic_lp[u, v])
car_list.append(sim_dic_car[u, v])
matrix = []
size = len(lp_list)
for j in range(size):
matrix.append([ra_list[j], lp_list[j], car_list[j]])
t_matrix = trans(matrix)
weightlist = get_weights(t_matrix, 3, size)
ebunch = nx.non_edges(G)
for u, v in ebunch:
if sim_dic_ra[u, v] == 0:
sim_dic_ra[u, v] = 0.001
if sim_dic_lp[u, v] == 0:
sim_dic_lp[u, v] = 0.001
if sim_dic_car[u, v] == 0:
sim_dic_car[u, v] = 0.001
sim_dic[u, v] = math.exp(weightlist[0] * math.log2(sim_dic_ra[u, v]) + weightlist[1] * math.log2(sim_dic_lp[u, v]) + weightlist[2] * math.log2(sim_dic_car[u, v]))
return sim_dic
def Madm2(G, alpha):
num_basic_method = 3 # 基本方法的个数,RA,CAR,LP
sim_dic = {}
# 1. 计算LP
sim_dict_lp = local_path_index(G, alpha)
size = len(sim_dict_lp)
sim_matrix = np.zeros((num_basic_method, size))
index_to_pair_list = [0 for i in range(size)]
pair_to_index_dict = {}
square_sum_list = [0 for i in range(num_basic_method)]
# sum_list = [0 for i in range(num_basic_method)]
m = 0
i = 0
for k in sim_dict_lp.keys():
s = sim_dict_lp[k]
pair_to_index_dict[k] = i
index_to_pair_list[i] = k
sim_matrix[m][i] = s
square_sum_list[m] += s * s
# sum_list[m] += s
i += 1
# end for
m += 1
# 2. RA
sim_dict_ra = resource_allocation_index(G)
for k in sim_dict_ra.keys():
s = sim_dict_ra[k]
i = pair_to_index_dict[k]
sim_matrix[m][i] = s
square_sum_list[m] += s * s
# end for
m += 1
# 3. CAR
sim_dict_car = CAR(G)
for k in sim_dict_car.keys():
s = sim_dict_car[k]
i = pair_to_index_dict[k]
sim_matrix[m][i] = s
square_sum_list[m] += s * s
# sum_list[m] += s
# end for
#sim_dict_car.clear()
# normalzie sim_matrix
for i in range(num_basic_method):
s = math.sqrt(square_sum_list[i])
for j in range(size):
sim_matrix[i][j] /= s
# end for
# end for
weight_list = get_weights(sim_matrix, num_basic_method, size)
a = weight_list[0]
b = weight_list[1]
c = weight_list[2]
ebunch = nx.non_edges(G)
for u, v in ebunch:
if sim_dict_lp[u, v] == 0:
sim_dict_lp[u, v] = 0.001
if sim_dict_ra[u, v] == 0:
sim_dict_ra[u, v] = 0.001
if sim_dict_car[u, v] == 0:
sim_dict_car[u, v] = 0.001
sim_dic[u, v] = math.exp(a * math.log2(sim_dict_lp[u, v]) + b * math.log2(sim_dict_ra[u, v]) + c * math.log2(sim_dict_car[u, v]))
return sim_dic
# MI
def MI(G):
#G = nx.read_edgelist(graph_file)
node_num = nx.number_of_nodes(G)
edge_num = nx.number_of_edges(G)
nodes = nx.nodes(G)
beta = -math.log2(0.0001)
# 首先计算$P(L^1_{xy})$,其实不需要计算顶点对之间的概率,只需要不同度之间的概率
nodes_Degree_dict = {}
degree_list = []
for v in nodes:
nodes_Degree_dict[v] = nx.degree(G, v)
degree_list.append(nx.degree(G, v))
degree_list = [nx.degree(G, v) for v in nodes]
distinct_degree_list = list(set(degree_list))
size = len(distinct_degree_list)
self_Connect_dict = {}
for x in range(size):
k_x = distinct_degree_list[x]
for y in range(x, size):
k_y = distinct_degree_list[y]
p0 = 1
(k_n, k_m) = pair(k_x, k_y)
a = edge_num + 1
b = edge_num - k_m + 1
for i in range(1, k_n + 1):
p0 *= (b - i) / (a - i)
# end for
if p0 == 1:
self_Connect_dict[(k_n, k_m)] = beta
self_Connect_dict[(k_m, k_n)] = beta
else:
self_Connect_dict[(k_n, k_m)] = -math.log2(1 - p0)
self_Connect_dict[(k_m, k_n)] = -math.log2(1 - p0)
#print (str(k_n) + "," + str(k_m))
#print (self_Connect_dict[(k_n, k_m)])
# =============================================================================
# for i in range(size):
# print(str(i) + "----" + str(distinct_degree_list[i]))
# =============================================================================
# 计算以z为公共邻居的两个顶点间存在链接的互信息
#mutual_info_list = [0 for z in range(node_num)]
self_Conditional_dict = {}
for z in nodes:
k_z = nodes_Degree_dict[z]
if k_z > 1:
alpha = 2 / (k_z * (k_z - 1))
cc_z = nx.clustering(G, z)
if cc_z == 0:
log_c = beta
else:
log_c = -math.log2(cc_z)
# end if
s = 0
neighbor_list = nx.neighbors(G,z)
size = len(neighbor_list)
for i in range(size):
m = neighbor_list[i]
for j in range(i+1,size):
n = neighbor_list[j]
if i!=j:
s += (self_Connect_dict[(nodes_Degree_dict[m], nodes_Degree_dict[n])] - log_c)
self_Conditional_dict[z] = alpha * s
sim_dict = {} # 存储相似度的字典
ebunch = nx.non_edges(G)
for x, y in ebunch:
s = 0
#(k_x, k_y) = pair(degree_list[x], degree_list[y])
for z in nx.common_neighbors(G, x, y):
s += self_Conditional_dict[z]
sim_dict[(x, y)] = s - self_Connect_dict[(nodes_Degree_dict[x], nodes_Degree_dict[y])]
#sim_dict[(y, x)] = s - self_Connect_dict[(nodes_Degree_dict[x], nodes_Degree_dict[y])]
# end if
# end for
return sim_dict
# end def
def MI2(G):
#G = nx.read_edgelist(graph_file)
node_num = nx.number_of_nodes(G)
edge_num = nx.number_of_edges(G)
sim_dict = {} # �洢���ƶȵ��ֵ�
I_pConnect_dict = {}
pDisConnect = 1
edges = nx.edges(G)
ebunch = nx.non_edges(G)
for u, v in edges:
uDegree = nx.degree(G, u)
vDegree = nx.degree(G, v)
for i in range(1,vDegree + 1):
pDisConnect = pDisConnect * (((edge_num - uDegree) - i + 1) / (edge_num - i + 1))
pConnect = 1 - pDisConnect
if pConnect == 0:
I_pConnect = -math.log2(0.0001)
else:
I_pConnect = -math.log2(pConnect)
I_pConnect_dict[(u, v)] = I_pConnect
I_pConnect_dict[(v, u)] = I_pConnect
pDisConnect = 1
for m, n in ebunch:
mDegree = nx.degree(G, m)
nDegree = nx.degree(G, n)
for i in range(1,nDegree + 1):
pDisConnect = pDisConnect * (((edge_num - mDegree) - i + 1) / (edge_num - i + 1))
pConnect = 1 - pDisConnect
if pConnect == 0:
I_pConnect = -math.log2(0.0001)
else:
I_pConnect = -math.log2(pConnect)
I_pConnect_dict[(m, n)] = I_pConnect
I_pConnect_dict[(n, m)] = I_pConnect
pDisConnect = 1
ebunchs = nx.non_edges(G)
for u, v in ebunchs:
pMutual_Information = 0
I_pConnect = I_pConnect_dict[(u, v)]
for z in nx.common_neighbors(G, u, v):
neighbor_num = len(list(nx.neighbors(G,z)))
neighbor_list = nx.neighbors(G,z)
for m in range(len(neighbor_list)):
for n in range(m+1,len(neighbor_list)):
if m!=n:
I_ppConnect = I_pConnect_dict[(neighbor_list[m], neighbor_list[n])]
if nx.clustering(G,z) == 0:
pMutual_Information = pMutual_Information + (2 / (neighbor_num * (neighbor_num - 1))) * ((I_ppConnect) - (-math.log2(0.0001)))
else:
pMutual_Information = pMutual_Information + ( 2/ (neighbor_num * (neighbor_num - 1))) * ((I_ppConnect)-(-math.log2(nx.clustering(G,z))))
sim_dict[(u, v)] = -(I_pConnect - pMutual_Information)
sim_dict[(v, u)] = -(I_pConnect - pMutual_Information)
#print(i)
return sim_dict
def MI5(G):
beta = -math.log2(0.0001)
sim_dict = {}
edge_num = nx.number_of_edges(G)
node_num = nx.number_of_nodes(G)
alpha = -math.log2(edge_num/(node_num*(node_num-1)/2))
degree_pair = {}
edge = nx.edges(G)
nodes = nx.nodes(G)
nodes_Degree_dict = {}
degree_list = []
for v in nodes:
nodes_Degree_dict[v] = nx.degree(G, v)
degree_list.append(nx.degree(G, v))
#计算图中不同的边的个数
distinct_degree_list = list(set(degree_list))
size = len(distinct_degree_list)
for i in range(size):
for j in range(i, size):
(di, dj) = pair(distinct_degree_list[i], distinct_degree_list[j])
degree_pair[di, dj] = 0
for u, v in edge:
d1 = nx.degree(G, u)
d2 = nx.degree(G, v)
d1, d2 = pair(d1, d2)
degree_pair[d1,d2] = degree_pair[d1,d2] + 1
#计算连接的互信息
self_Connect_dict = {}
for x in range(size):
k_x = distinct_degree_list[x]
for y in range(x, size):
k_y = distinct_degree_list[y]
(k_n, k_m) = pair(k_x, k_y)
if(degree_pair[k_n, k_m] == 0):
self_Connect_dict[k_n, k_m] = alpha
self_Connect_dict[k_m, k_n] = alpha
else:
self_Connect_dict[k_n, k_m] = -math.log2(degree_pair[k_n, k_m]/edge_num)
self_Connect_dict[k_m, k_n] = -math.log2(degree_pair[k_n, k_m] / edge_num)
# 计算以z为公共邻居的两个顶点间存在链接的互信息
self_Conditional_dict = {}
for z in nodes:
k_z = nodes_Degree_dict[z]
if k_z > 1:
alpha = 2 / (k_z * (k_z - 1))
cc_z = nx.clustering(G, z)
if cc_z == 0:
log_c = beta
else:
log_c = -math.log2(cc_z)
# end if
s = 0
neighbor_list = nx.neighbors(G, z)
size = len(neighbor_list)
for i in range(size):
m = neighbor_list[i]
for j in range(i + 1, size):
n = neighbor_list[j]
if i != j:
s += (self_Connect_dict[(nodes_Degree_dict[m], nodes_Degree_dict[n])] - log_c)
self_Conditional_dict[z] = alpha * s
sim_dict = {} # 存储相似度的字典
ebunch = nx.non_edges(G)
for x, y in ebunch:
s = 0
# (k_x, k_y) = pair(degree_list[x], degree_list[y])
for z in nx.common_neighbors(G, x, y):
s += self_Conditional_dict[z]
sim_dict[(x, y)] = s - self_Connect_dict[
(nodes_Degree_dict[x], nodes_Degree_dict[y])]
return sim_dict
#考虑公共邻居之间的相连的边
def CMI(G):
a = 0.1
node_num = nx.number_of_nodes(G)
edge_num = nx.number_of_edges(G)
nodes = nx.nodes(G)
beta = -math.log2(0.0001)
# 首先计算$P(L^1_{xy})$,其实不需要计算顶点对之间的概率,只需要不同度之间的概率
nodes_Degree_dict = {}
degree_list = []
for v in nodes:
nodes_Degree_dict[v] = nx.degree(G, v)
degree_list.append(nx.degree(G, v))
degree_list = [nx.degree(G, v) for v in nodes]
distinct_degree_list = list(set(degree_list))
size = len(distinct_degree_list)
self_Connect_dict = {}
for x in range(size):
k_x = distinct_degree_list[x]
for y in range(x, size):
k_y = distinct_degree_list[y]
p0 = 1
(k_n, k_m) = pair(k_x, k_y)
a = edge_num + 1
b = edge_num - k_m + 1
for i in range(1, k_n + 1):
p0 *= (b - i) / (a - i)
# end for
if p0 == 1:
self_Connect_dict[(k_n, k_m)] = beta
self_Connect_dict[(k_m, k_n)] = beta
else:
self_Connect_dict[(k_n, k_m)] = -math.log2(1 - p0)
self_Connect_dict[(k_m, k_n)] = -math.log2(1 - p0)
# print (str(k_n) + "," + str(k_m))
# print (self_Connect_dict[(k_n, k_m)])
# =============================================================================
# for i in range(size):
# print(str(i) + "----" + str(distinct_degree_list[i]))
# =============================================================================
# 计算以z为公共邻居的两个顶点间存在链接的互信息
# mutual_info_list = [0 for z in range(node_num)]
self_Conditional_dict = {}
for z in nodes:
k_z = nodes_Degree_dict[z]
if k_z > 1:
alpha = 2 / (k_z * (k_z - 1))
cc_z = nx.clustering(G, z)
if cc_z == 0:
log_c = beta
else:
log_c = -math.log2(cc_z)
# end if
s = 0
neighbor_list = nx.neighbors(G, z)
size = len(neighbor_list)
for i in range(size):
m = neighbor_list[i]
for j in range(i + 1, size):
n = neighbor_list[j]
if i != j:
s += (self_Connect_dict[(nodes_Degree_dict[m], nodes_Degree_dict[n])] - log_c)
self_Conditional_dict[z] = alpha * s
# 计算节点对公共邻居之间相连的边的个数
ebunch = nx.non_edges(G)
neighbor_dict = {}
for m, n in ebunch:
com_nei = nx.common_neighbors(G, m, n)
i = 0
for x in com_nei:
for y in com_nei:
if (m != n) & (G.has_edge(x, y)):
i = i + 1
neighbor_dict[m, n] = i
sim_dict = {} # 存储相似度的字典
ebunch = nx.non_edges(G)
for x, y in ebunch:
s = 0
# (k_x, k_y) = pair(degree_list[x], degree_list[y])
for z in nx.common_neighbors(G, x, y):
s += self_Conditional_dict[z]
sim_dict[(x, y)] = s * (1 + neighbor_dict[x, y]*0.8) - self_Connect_dict[(nodes_Degree_dict[x], nodes_Degree_dict[y])]
# print(sim_dict)
return sim_dict
def CMI2(G):
beta = -math.log2(0.0001)
sim_dict = {}
edge_num = nx.number_of_edges(G)
node_num = nx.number_of_nodes(G)
alpha = -math.log2(edge_num/(node_num*(node_num-1)/2))
degree_pair = {}
edge = nx.edges(G)
nodes = nx.nodes(G)
nodes_Degree_dict = {}
degree_list = []
for v in nodes:
nodes_Degree_dict[v] = nx.degree(G, v)
degree_list.append(nx.degree(G, v))
#计算图中不同的边的个数
distinct_degree_list = list(set(degree_list))
size = len(distinct_degree_list)
for i in range(size):
for j in range(i, size):
(di, dj) = pair(distinct_degree_list[i], distinct_degree_list[j])
degree_pair[di, dj] = 0
for u, v in edge:
d1 = nx.degree(G, u)
d2 = nx.degree(G, v)
d1, d2 = pair(d1, d2)
degree_pair[d1,d2] = degree_pair[d1,d2] + 1
#计算连接的互信息
self_Connect_dict = {}
for x in range(size):
k_x = distinct_degree_list[x]
for y in range(x, size):
k_y = distinct_degree_list[y]
(k_n, k_m) = pair(k_x, k_y)
if(degree_pair[k_n, k_m] == 0):
self_Connect_dict[k_n, k_m] = alpha
self_Connect_dict[k_m, k_n] = alpha
else:
self_Connect_dict[k_n, k_m] = -math.log2(degree_pair[k_n, k_m]/edge_num)
self_Connect_dict[k_m, k_n] = -math.log2(degree_pair[k_n, k_m] / edge_num)
# 计算以z为公共邻居的两个顶点间存在链接的互信息
self_Conditional_dict = {}
for z in nodes:
k_z = nodes_Degree_dict[z]
if k_z > 1:
alpha = 2 / (k_z * (k_z - 1))
cc_z = nx.clustering(G, z)
if cc_z == 0:
log_c = beta
else:
log_c = -math.log2(cc_z)
# end if
s = 0
neighbor_list = nx.neighbors(G, z)
size = len(neighbor_list)
for i in range(size):
m = neighbor_list[i]
for j in range(i + 1, size):
n = neighbor_list[j]
if i != j:
s += (self_Connect_dict[(nodes_Degree_dict[m], nodes_Degree_dict[n])] - log_c)
self_Conditional_dict[z] = alpha * s
# 计算节点对公共邻居之间相连的边的个数
ebunch = nx.non_edges(G)
neighbor_dict = {}
for m, n in ebunch:
com_nei = nx.common_neighbors(G, m, n)
i = 0
for x in com_nei:
for y in com_nei:
if (m != n) & (G.has_edge(x, y)):
i = i + 1
neighbor_dict[m, n] = i
sim_dict = {} # 存储相似度的字典
ebunch = nx.non_edges(G)
for x, y in ebunch:
s = 0
# (k_x, k_y) = pair(degree_list[x], degree_list[y])
for z in nx.common_neighbors(G, x, y):
s += self_Conditional_dict[z]
sim_dict[(x, y)] = s * (1 + neighbor_dict[x, y]*0.8) - self_Connect_dict[
(nodes_Degree_dict[x], nodes_Degree_dict[y])]
return sim_dict
# CN
def common_neighbors_index(G):
#print("one time")
node_num = nx.number_of_nodes(G)
edge_num = nx.number_of_edges(G)
# print (node_num)
# print (edge_num)
ebunch = nx.non_edges(G)
sim_dict = {} # 存储相似度的字典
for u, v in ebunch:
s = len(list(nx.common_neighbors(G, u, v)))
# s = 0
# for w in nx.common_neighbors(G, u, v):
# s += 1
#if (s > 0):
sim_dict[(u, v)] = s
#sim_dict[(v, u)] = s
#end if
#end for
return sim_dict
#end def
# PA
def preferential_attachment_index(G):
ebunch = nx.non_edges(G)
sim_dict = {} # 存储相似度的字典
degree_list = [0 for i in range(G.number_of_nodes())]
for v in range(G.number_of_nodes()):
degree_list[v] = nx.degree(G, v)
# end for
for u, v in ebunch:
s = degree_list[u] * degree_list[v]
if (s > 0):
sim_dict[(u, v)] = s
#end if
#end for
return sim_dict
#end def
# Jaccard
def jaccard_coefficient(G):
ebunch = nx.non_edges(G)
sim_dict = {} # 存储相似度的字典