This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMP_simple.py
137 lines (120 loc) · 3.79 KB
/
IMP_simple.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import argparse
import random
import time
import os
# from memory_profiler import profile
# @profile
def imm():
r = sampling()
print('len', len(r))
s_k = node_select(r)
return s_k
def node_select(r):
s_k = set()
vertex_list_map = {}
vertex_frequency_map = {}
i: int = 0
r_length = len(r)
while i < r_length:
for v in r[i]:
if v not in vertex_list_map:
vertex_list_map[v] = set()
vertex_list_map[v].add(i)
i += 1
for v in vertex_list_map:
vertex_frequency_map[v] = len(vertex_list_map[v])
for i in range(0, k):
v = None
max_frequency = -1
for v in vertex_frequency_map:
frequency = vertex_frequency_map[v]
if frequency > max_frequency:
v = v
max_frequency = frequency
s_k.add(v)
for rr_id in vertex_list_map[v]:
for v1 in r[rr_id]:
if v1 == v:
continue
vertex_frequency_map[v1] -= 1
vertex_list_map[v1].remove(rr_id)
del [vertex_list_map[v]]
del [vertex_frequency_map[v]]
return s_k
def generate_rr(v):
activated = set()
activate_set = set()
activate_set.add(v)
activated.add(v)
if model == 'IC':
while len(activate_set) > 0:
new_activate_set = set()
for act in activate_set:
if act not in in_graph:
continue
for (source, dest, weight) in in_graph[act]:
if source in activated:
continue
prob = random.random()
if weight > prob:
new_activate_set.add(source)
activated.add(source)
activate_set = new_activate_set
else:
while len(activate_set) > 0:
new_activate_set = set()
for act in activate_set:
if act not in in_graph:
continue
in_degree = len(in_graph[act])
rand_idx = random.randint(0, in_degree - 1)
source = in_graph[act][rand_idx][0]
if source not in activated:
activated.add(source)
new_activate_set.add(source)
activate_set = new_activate_set
return activated
def sampling():
r = []
while (time.time() - start) < time_limit:
v = random.randint(1, n)
rr = generate_rr(v)
r.append(rr)
print('len', len(r))
return r
# -i C:\Users\Jiash\Desktop\IMP\DatasetOnTestPlatform\NetHEPT.txt -k 5 -m IC -t 60
if __name__ == '__main__':
start = time.time()
random.seed(start)
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--network', type=str)
parser.add_argument('-k', '--seedCount', type=int)
parser.add_argument('-m', '--model', type=str)
parser.add_argument('-t', '--time', type=int)
args = parser.parse_args()
network = args.network
k = args.seedCount
model = args.model
time_limit = args.time
time_limit = time_limit * 0.5
fin = open(network)
lines = fin.readlines()
fin.close()
line0 = lines[0]
n = int(line0.split(' ')[0])
edgeNumber = int(line0.split(' ')[1])
in_graph = {}
for line in lines[1:]:
tokens = line.split(' ')
s = int(tokens[0])
d = int(tokens[1])
w = float(tokens[2])
if d not in in_graph:
in_graph[d] = []
in_graph[d].append((s, d, w))
seeds = imm()
for vertex in seeds:
print(vertex)
end = time.time()
print(end - start)
os._exit