-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.py
85 lines (70 loc) · 3.75 KB
/
testutil.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
#-------------------------------------------------------------------------------
# Copyright (c) 2013 Jose Antonio Martin H. ([email protected]).
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the GNU Public License v3.0
# which accompanies this distribution, and is available at
# http://www.gnu.org/licenses/gpl.html
#
# Contributors:
# Jose Antonio Martin H. ([email protected]) - initial API and implementation
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
import planegraphs as pg
import graphio as gio
#-----------------------------------------------------------------------------------------------
def linear_space(start, end, count):
""" Returns a vector containing count evently spaced intervals
(count + 1 evenly spaced points) """
count -= 1
delta = (end - start) / float(count)
return [start, ] + map(lambda x:delta * x + start, range(1, count)) + [end, ]
def get_grp_index(path):
strfilename = path + "labels.txt"
if not os.path.isfile(strfilename):
with open(path + "stats.txt", "w"):
return 1
with open(strfilename, "r") as f:
lines = f.readlines()
return max(len(lines) + 1, 1)
def save_graph(grpindex, G, colorable, txtline, flabels, fstats, path, file_prefix = 'planar_'):
N = G.order()
M = G.size()
strFile = file_prefix + grpindex
str_prologue = "c a pseudo random planar graph \n"
str_prologue += "c generated by Jose Antonio Martin H. \n"
str_prologue += "c 3-colorable: " + str(colorable) + "\n"
str_prologue += "c graph planar_density: " + str(G.planar_density()) + "% w.r.t 3|V|-6 \n"
gio.save_to_edge_list(strFile, G, path, str_prologue)
flabels.write(grpindex + " ; " + str(colorable) + "\n")
fstats.write(txtline + "\n")
def save_witness(grpindex, colorable, witness, path = "", file_prefix = 'planar_'):
f = open(path + file_prefix + grpindex + "_witness.txt", "w")
str_tex = ""
str_tex += "******************************************** \n"
str_tex += "* 3-coloring witness file * \n"
str_tex += "* Generated by Jose Antonio Martin H. * \n"
str_tex += "******************************************** \n"
str_tex += "\n"
str_tex += "file: " + file_prefix + grpindex + ".col.txt \n"
str_tex += '3-colorable: ' + str(colorable) + '\n'
if colorable: str_tex += "A 3-coloring defined as three independent sets 1, 2, 3. \n"
else: str_tex += 'A 3-uncoloring defined as a sequence of vertex identifications.\n'
utl = """
****************************************************************************************************
* A 3-uncoloring defined as a sequence of vertex identifications. *
* G/(x,y) is a vertex contraction. *
* G+(x,y) is an edge addition. *
* K112: G/(x,y) due to a diamond subgraph. *
* T31: G/(x,y) due to a failed contraction in a tadpole subgraph that unavoidably lead to a K4. *
* E: G/(x,y) due to a failed edge adition that unavoidably lead to a K4.\n *
****************************************************************************************************
"""
f.write(str_tex)
f.write(witness)
f.close()
def save_counter_example(strFile, G, path):
str_prologue = "c a random planar graph \n"
str_prologue += "c this is a counter example to the Reduce3COL algorithm \n"
str_prologue += "c generated by Jose Antonio Martin H. \n"
gio.save_to_edge_list(strFile, G, path, str_prologue)