-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfof.py
62 lines (54 loc) · 2.04 KB
/
fof.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
import math
import buildNetworkX as bnx
import networkx
def friendsOfFriendsNX(G):
fof = {}
for node in list(G.nodes()):
fof[node] = set()
for friend in list(G.neighbors(node)):
for friendOfFriend in list(G.neighbors(friend)):
if (not friendOfFriend in G.neighbors(node)) and (friendOfFriend != node):
fof[node].add(friendOfFriend)
return fof
def CommunityMembership(G: networkx.DiGraph):
communities = []
for node in list(G.nodes()):
community = set([node])
community.update(G.neighbors(node))
communities.append(community)
scores = {}
for node in list(G.nodes()):
scores[node] = {}
neighbors = set(G.neighbors(node))
for community in communities:
if len(neighbors.intersection(community)) > 0:
for contact in community:
if contact not in neighbors and contact != node:
if contact not in scores[node]:
scores[node][contact] = 0
scores[node][contact] = scores[node][contact] + 1
suggestions = {}
for node in list(G.nodes()):
sortedSuggestions = sorted(scores[node].items(), key=lambda e: e[1], reverse=True)
suggestions[node] = [x[0] for x in sortedSuggestions[:math.floor(len(sortedSuggestions)/3)]]
return suggestions
def test():
adjacencyLists = {}
adjacencyLists['1'] = {'2', '3'}
adjacencyLists['2'] = {'1', '5'}
adjacencyLists['3'] = {'2', '4'}
g = bnx.buildNetworkXFromAM(adjacencyLists)
result2 = friendsOfFriendsNX(g)
if '2' in result2['1']:
print("test failed! 2 in result!")
return
elif '3' in result2['1']:
print('test failed! 3 in result!')
elif '1' in result2['1']:
print('test failed! 1 in result!')
elif not '4' in result2['1']:
print('test failed! 4 not in result!')
elif not '5' in result2['1']:
print('test failed! 5 not in result!')
else:
print('tests pased!')