-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimacs.py
96 lines (76 loc) · 2.14 KB
/
dimacs.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
# Algorytmy Grafowe
# Piotr Faliszewski 2019
# Load graph in the DIMACS ascii format + weights
def loadCNFFormula( name ):
"""Load a CNF formula in the DIMACS ascii format from
the file "name" and return it as a list of clauses
Returns (V,F)
V -- highest variable number
F -- list of clauses"""
V = 0
L = []
f = open( name, "r" )
lines = f.readlines()
for l in lines:
s = l.split()
if(len(s) < 1): continue
if( s[0] == "c" ):
print(s)
continue
elif( s[0] == "p" ):
V = int(s[2])
else:
clause = [int(v) for v in s[:-1]]
L.append(clause)
f.close()
return (V,L)
def loadWeightedGraph( name ):
"""Load a graph in the DIMACS ascii format (with weights) from
the file "name" and return it as a list of sets
Returns (V,L)
V -- number of vertices (1, ..., V)
L -- list of edges in the format (x,y,w): edge between x and y with weight w (x<y)"""
V = 0
L = []
f = open( name, "r" )
lines = f.readlines()
for l in lines:
s = l.split()
if(len(s) < 1): continue
if( s[0] == "c" ):
continue
elif( s[0] == "p" ):
V = int(s[2])
elif( s[0] == "e" ):
(a,b,c) = (int(s[1]), int(s[2]), int(s[3]))
(x,y,c) = (min(a,b), max(a,b), c)
L.append((x,y,c))
f.close()
return (V,L)
def loadDirectedWeightedGraph( name ):
"""Load a directed graph in the DIMACS ascii format (with weights) from
the file "name" and return it as a list of sets
Returns (V,L)
V -- number of vertices (1, ..., V)
L -- list of edges in the format (x,y,w): edge between x and y with weight w"""
V = 0
L = []
f = open( name, "r" )
lines = f.readlines()
for l in lines:
s = l.split()
if(len(s) < 1): continue
if( s[0] == "c" ):
continue
elif( s[0] == "p" ):
V = int(s[2])
elif( s[0] == "e" ):
(a,b,c) = (int(s[1]), int(s[2]), int(s[3]))
L.append((a,b,c))
f.close()
return (V,L)
def readSolution(name):
"""Read the expected solution from the first line of the graph file"""
with open(name, 'r') as f:
line = f.readline()
return line.split()[-1]