-
Notifications
You must be signed in to change notification settings - Fork 1
/
LA_nonoverlapping.py
65 lines (57 loc) · 1.42 KB
/
LA_nonoverlapping.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
import networkx as nx
import matplotlib .pyplot as plt
G=nx.karate_club_graph()
pos = nx.spring_layout(G)
labels = {}
rank_int = []
c = []
rank = []
w_int = []
j = 0
k = []
popped = []
inter = []
inter_sum1 = []
fc = []
prev_rank = []
prev_c = []
sum_rank = 0
#Community set
c = [0, 3, 33, 12, 29, 5, 15, 10]
print "Community under test : ", c
def find_sp(u):
q = []
for n in G:
labels[n] = n
sp_u = nx.shortest_path(G,u,n)
if len(sp_u) == 2:
q.append(n)
return q
#Computing distance of all nodes connected to the community
for i in c:
rank_int.append(find_sp(i))
rank.append(len(find_sp(i)))
#Computing the summation of distances
for i in rank:
sum_rank = sum_rank + i
#Computing the distance of nodes within the community
for i in c:
c.pop(rank.index(max(rank)))
rank_int.pop(rank.index(max(rank)))
rank.pop(rank.index(max(rank)))
inter_sum = 0
w_int = []
for list in rank_int:
count = 0
for num in list:
for m in c:
if num == m:
count = count + 1
inter_sum = inter_sum + count
w_int.append(count)
inter_sum1.append(inter_sum)
inter.append(w_int)
# Computing the fitness function value
for i in inter_sum1:
fc.append(float(i) / float(sum_rank))
print "\nFitness function value = ", fc