- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB2.py
More file actions
Latest commit
63 lines (54 loc) · 1.79 KB
/
Copy pathLAB2.py
File metadata and controls
63 lines (54 loc) · 1.79 KB
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
fromtypingimportList
fromCrowdScienceLabimportcolors
fromUtilsimportdrawGraph
NODE_N=0
matrix=None# use matrix as static var.
# return list, list[i] = k means node i has k friends.
deffriend_num() ->List[int]:
li= []
foriinrange(0, NODE_N):
k=0
forjinrange(0, NODE_N):
ifi!=jandmatrix[i][j] ==1:
k+=1
li.append(k)
returnli
# This experiment studies the content of friendship paradox.
# For moral considerations, one-side friends are not considered,
# so undirected simple graph is adopted.
if__name__=='__main__':
NODE_N=int(input(f"\nPlease enter the {colors.BOLD}number of node{colors.ENDC} in the graph.\n"))
print("Please enter the adjacent matrix")
matrix= [list(map(int, input().split())) foriinrange(NODE_N)]
drawGraph(matrix)
li=friend_num()
up=0
foriinrange(0, NODE_N):
yes=0
aver=0
forjinrange(0, NODE_N):
ifmatrix[i][j] ==1andi!=j:
aver+=li[j]
yes+=1
aver/=yes
ifli[i] <aver:
up+=1
foriinrange(0, NODE_N):
print(f"Node\t{colors.BOLD}{i}{colors.ENDC}\thas\t{colors.BOLD}{li[i]}{colors.ENDC}\tfriends.")
print(f"ratio that match the friendship paradox: {colors.BOLD}%.2f{colors.BOLD}"% (up/NODE_N))
'''
Result:
Please enter the number of node in the graph.
4
Please enter the adjacent matrix
0 1 1 0
1 0 0 1
1 1 0 1
0 1 1 0
Node 0 has 2 friends.
Node 1 has 2 friends.
Node 2 has 3 friends.
Node 3 has 2 friends.
ratio that match the friendship paradox: 0.50
Process finished with exit code 0
'''