Skip to content

Commit

Permalink
Trac #33628: Fig bug in graphs.RandomToleranceGraph
Browse files Browse the repository at this point in the history
Part of #32544:
{{{
sage -t --long --warn-long 291.5 --random-
seed=184268751501867371650794470659094787547
src/sage/graphs/generators/random.py
**********************************************************************
File "src/sage/graphs/generators/random.py", line 1527, in
sage.graphs.generators.random.RandomToleranceGraph
Failed example:
    g = graphs.RandomToleranceGraph(8)
Exception raised:
    Traceback (most recent call last):
      File "/Users/dcoudert/sage/local/var/lib/sage/venv-
python3.9/lib/python3.9/site-packages/sage/doctest/forker.py", line 695,
in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/Users/dcoudert/sage/local/var/lib/sage/venv-
python3.9/lib/python3.9/site-packages/sage/doctest/forker.py", line
1093, in compile_and_execute
        exec(compiled, globs)
      File "<doctest
sage.graphs.generators.random.RandomToleranceGraph[0]>", line 1, in
<module>
        g = graphs.RandomToleranceGraph(Integer(8))
      File "/Users/dcoudert/sage/local/var/lib/sage/venv-
python3.9/lib/python3.9/site-packages/sage/graphs/generators/random.py",
line 1553, in RandomToleranceGraph
        return ToleranceGraph(tolrep)
      File "/Users/dcoudert/sage/local/var/lib/sage/venv-
python3.9/lib/python3.9/site-
packages/sage/graphs/generators/intersection.py", line 351, in
ToleranceGraph
        raise ValueError("Invalid tolerance representation at position
"+str(i)+"; third value must be positive!")
    ValueError: Invalid tolerance representation at position 2; third
value must be positive!
}}}
Method `graphs.ToleranceGraph` raises an error when a tolerance is `<=
0`.

URL: https://trac.sagemath.org/33628
Reported by: dcoudert
Ticket author(s): David Coudert
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Apr 2, 2022
2 parents 5ee8a50 + b4c34bb commit b886d89
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
41 changes: 23 additions & 18 deletions src/sage/graphs/generators/intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ def ToleranceGraph(tolrep):
Return the graph generated by the tolerance representation ``tolrep``.
The tolerance representation ``tolrep`` is described by the list
`((l_0,r_0,t_0), (l_1,r_1,t_1), ..., (l_k,r_k,t_k))` where `I_i = (l_i,r_i)`
denotes a closed interval on the real line with `l_i < r_i` and `t_i` a
positive value, called tolerance. This representation generates the
tolerance graph with the vertex set {0,1, ..., k} and the edge set `{(i,j):
|I_i \cap I_j| \ge \min{t_i, t_j}}` where `|I_i \cap I_j|` denotes the
length of the intersection of `I_i` and `I_j`.
`((l_0,r_0,t_0), (l_1,r_1,t_1), \ldots, (l_k,r_k,t_k))` where `I_i =
(l_i,r_i)` denotes a closed interval on the real line with `l_i < r_i` and
`t_i` a strictly positive value, called tolerance. This representation
generates the tolerance graph with the vertex set `\{0,1, \ldots, k\}` and
the edge set `\{(i,j): |I_i \cap I_j| \ge \min\{t_i, t_j\}\}` where `|I_i
\cap I_j|` denotes the length of the intersection of `I_i` and `I_j`.
INPUT:
Expand All @@ -296,9 +296,9 @@ def ToleranceGraph(tolrep):
.. NOTE::
The vertices are named 0, 1, ..., k. The tolerance representation used
to create the graph is saved with the graph and can be recovered using
``get_vertex()`` or ``get_vertices()``.
The vertices are named `0, 1, \ldots, k`. The tolerance representation
used to create the graph is saved with the graph and can be recovered
using ``get_vertex()`` or ``get_vertices()``.
EXAMPLES:
Expand Down Expand Up @@ -342,24 +342,29 @@ def ToleranceGraph(tolrep):
sage: g = graphs.ToleranceGraph(tolrep)
Traceback (most recent call last):
...
ValueError: Invalid tolerance representation at position 0; third value must be positive!
ValueError: Invalid tolerance representation at position 0; third value must be > 0
sage: g = graphs.ToleranceGraph([(1, 2, 0)])
Traceback (most recent call last):
...
ValueError: Invalid tolerance representation at position 0; third value must be > 0
"""
n = len(tolrep)

for i in range(n):
if tolrep[i][2] <= 0:
raise ValueError("Invalid tolerance representation at position "+str(i)+"; third value must be positive!")
raise ValueError("Invalid tolerance representation at position "
"{}; third value must be > 0".format(i))

g = Graph(n)
g = Graph(n, name="Tolerance Graph")

for i in range(n-1):
li,ri,ti = tolrep[i]
for j in range(i+1,n):
lj,rj,tj = tolrep[j]
if min(ri,rj) - max(li,lj) >= min(ti,tj):
for i in range(n):
li, ri, ti = tolrep[i]
for j in range(i + 1, n):
lj, rj, tj = tolrep[j]
if min(ri, rj) - max(li, lj) >= min(ti, tj):
g.add_edge(i,j)

rep = dict( zip(range(n),tolrep) )
rep = dict(zip(range(n), tolrep))
g.set_vertices(rep)

return g
Expand Down
7 changes: 5 additions & 2 deletions src/sage/graphs/generators/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,9 +1548,12 @@ def RandomToleranceGraph(n):
r = randint(0, W)
if l > r:
l, r = r, l
tolrep.append((l, r, randint(0, W)))
# The tolerance value must be > 0
tolrep.append((l, r, randint(1, W)))

return ToleranceGraph(tolrep)
g = ToleranceGraph(tolrep)
g.name("Random tolerance graph")
return g


# uniform random triangulation using Schaeffer-Poulalhon algorithm
Expand Down

0 comments on commit b886d89

Please sign in to comment.