forked from ryaninhust/influenx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.py
41 lines (31 loc) · 1.44 KB
/
seed.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
import random
from igraph import Graph
def generate_seed_graph(origin_graph, k):
g = Graph.Read_Ncol(origin_graph, directed=False)
g = g.simplify()
degree_dict = {}
vcounts = g.vcount()
init_seed = random.randint(0, vcounts)
seed_graph = Graph()
seed_graph.add_vertex(init_seed)
degree_dict[init_seed] = g.neighborhood_size(init_seed) - 1
while seed_graph.vcount() != k:
choiced_vertex = random.choice(seed_graph.vs)
choiced_neighor = random.choice(g.neighbors(choiced_vertex['name']))
if str(choiced_neighor) in seed_graph.vs['name']:
continue
seed_graph.add_vertex(choiced_neighor)
degree_dict[choiced_neighor] = g.neighborhood_size(choiced_neighor) - 1
choiced_neighor_neighor = g.neighbors(choiced_neighor)
#choiced_neighor_neighor = [str(i) for i in choiced_neighor_neighor]
existed_nodes = set(choiced_neighor_neighor) & set(seed_graph.vs['name'])
for node in existed_nodes:
choiced_neighor_id = filter(lambda x: x['name'] == choiced_neighor, seed_graph.vs)[0].index
node_id = filter(lambda x: x['name'] == node, seed_graph.vs)[0].index
seed_graph.add_edge(choiced_neighor_id, node_id)
return seed_graph, degree_dict
if __name__ == "__main__":
seed_graph, degrees = generate_seed_graph('data/egofb.txt', 2)
print seed_graph.vs['name']
print seed_graph.get_edgelist()
print degrees