This repository was archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.py
More file actions
99 lines (74 loc) · 2.12 KB
/
gen.py
File metadata and controls
99 lines (74 loc) · 2.12 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
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
# ------------------------------------------------------------------------
# graph generator for max-flow in python3
#
# command line arguments:
# n = number of vertices
# p = arc probability (0 <= p <= 1)
# r = maximum range of capacity
# s = random seed
# f = output file name
#
# example: python3 gen.py 10 0.4 100 1234 data.in
#
# It generates either:
#
# 1) a feasible graph, that is, there exists a path between vertice 1 and
# vertice n, or
#
# 2) an infeasible graph, that is, there exists no path between vertice 1 and
# vertice n. In this case, the output file contains only "-1"
#
# Note that 2) may arise if the probability is too small
#
# ------------------------------------------------------------------------
import random
import sys
# ---------------------------
# graph traversal
# ---------------------------
def dfs(L, v, n, i):
if i == n:
return True
if v[i] == False:
v[i] = True
for j in L[i]:
if dfs(L, v, n, j) == True:
return True
return False
# ----------------------------
# check that a path exists
# ----------------------------
def check(L, n):
v = [False] * (n+1)
return dfs(L, v, n, 1)
# ---------------------------
# main function
# --------------------------
def main():
argv = sys.argv[1:]
n = int(argv[0]) # number of vertices
p = float(argv[1]) # arc probability
r = int(argv[2]) # maximum range of capacity
s = int(argv[3]) # random seed
f = argv[4]
random.seed(s)
M = []
L = [[] for i in range(n+1)]
m = 0
for i in range(1,n):
for j in range(i+1,n+1):
if random.random() < p:
m = m + 1
M.append([i,j])
L[i].append(j)
fout = open(f,"w")
if check(L,n) == True:
fout.write(str(n) + " " + str(m) + "\n")
for i in M:
fout.write(str(i[0]) + " " + str(i[1]) + " ")
fout.write(str(random.randint(1,r)) + "\n")
else:
fout.write("-1")
fout.close()
if __name__ == "__main__":
main()