-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.py
More file actions
29 lines (24 loc) · 1.01 KB
/
Copy pathtask2.py
File metadata and controls
29 lines (24 loc) · 1.01 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
import matplotlib.pyplot as plt
import networkx as nx
from collections import Counter
from distance import diameter, radius, center, periphery, average_shortest_path_length, single_source_shortest_path_length
def do_task2():
print("Task 2")
g = nx.read_gexf("data/undir_graph.gexf")
d = dict(nx.degree(g, g.nodes))
print(sorted(d.items(), key=lambda x: x[1], reverse=True))
print("Average vertex degree:", sum(d.values())/len(d))
print("Diameter:", diameter(g))
print("Radius:", radius(g))
print("Center:", center(g))
print("Periphery:", periphery(g))
print("Average path length:", average_shortest_path_length(g))
degrees = sorted([d for n, d in g.degree()], reverse=True)
degrees = Counter(degrees)
plt.hist(degrees.keys(), bins=max(degrees.keys()), weights=[i / len(g.nodes()) for i in degrees.values()],
rwidth=0.9)
plt.xlabel("Vertex degree")
plt.ylabel("Probability")
plt.savefig("outputs/hist.png")
if __name__ == "__main__":
do_task2()