-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.py
More file actions
69 lines (54 loc) · 1.61 KB
/
Copy pathshared.py
File metadata and controls
69 lines (54 loc) · 1.61 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
64
65
66
67
68
69
import networkx as nx
from operator import itemgetter
'''
load_graph takes a string name and returns the graph corresponding to the
input network's name
'''
def load_graph(nw_name):
path = './data/cit-' + nw_name + '/out.cit-' + nw_name
G = nx.read_edgelist(path, comments='%', create_using=nx.DiGraph())
return G
'''
gets a list of tuples of node ids and count of neighbors sorted hi-lo by
in-degree count
'''
def in_degree_top_k(G, k):
if (k <= 0):
print('please use k>0')
return
descending = sorted(G.in_degree().items(), key=itemgetter(1), reverse=True)
k = min(k, len(descending))
return descending[:k]
'''
gets a list of tuples of node ids and count of neighbors sorted hi-lo by
out-degree
'''
def out_degree_top_k(G, k):
if (k <= 0):
print('please use k>0')
return
descending = sorted(G.out_degree().items(), key=itemgetter(1), reverse=True)
k = min(k, len(descending))
return descending[:k]
'''
gets a list of node ids sorted by neighbor count with nodes
with highest count at the front of the returned lists
Returns a list of integer ids
'''
def in_degree_top_k_ids(G, k):
if (k <= 0):
print('please use k>0')
return
top_tups = in_degree_top_k(G, k)
return [int(tup[0]) for tup in top_tups]
'''
gets a list of node ids sorted by neighbor count with nodes
with highest count at the front of the returned lists
Returns a list of integer ids
'''
def out_degree_top_k_ids(G, k):
if (k <= 0):
print('please use k>0')
return
top_tups = out_degree_top_k(G, k)
return [int(tup[0]) for tup in top_tups]