-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_edge.py
44 lines (38 loc) · 1.36 KB
/
add_edge.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
import random
import collections
filename = "ca-2.txt"
with open(filename) as file:
lines = file.read().splitlines()
refill = False
addRandom = True
#refill missing number
if refill == True:
newlines = []
preNode = int(lines[0][0])
for line in lines[1:]:
temp = line.split()
while int(temp[0]) > preNode+1 and random.randint(0,50)>39:
newlines.append(str(preNode + 1) + ' ' + str(random.randint(0, 246)))
preNode = min(preNode+1, int(temp[0]))
newlines.append(line)
if addRandom == True:
#add strong cluster
graph = collections.defaultdict(list)
for line in lines[1:]:
temp = line.split()
from_v, to_v = int(temp[0]),int(temp[1])
graph[from_v].append(to_v)
graph[to_v].append(from_v)
newlines= []
for line in lines[1:]:
temp = line.split()
if random.randint(0,100)>95:
rand_0 = random.randint(0,len(graph[int(temp[0])])-1)
rand_1 = random.randint(0,len(graph[int(temp[1])])-1)
newlines.append(str(int(temp[0]) + 1) + ' ' + str(graph[int(temp[1])][rand_1]))
newlines.append(str(int(temp[1]) + 1) + ' ' + str(graph[int(temp[0])][rand_0]))
newlines.append(line)
print(newlines)
print(len(newlines))
with open(filename, 'w') as output_file:
output_file.writelines(line + "\n" for line in newlines)