Skip to content

Commit

Permalink
sagemathgh-38269: Fix lex_BFS (and co.) for directed graphs
Browse files Browse the repository at this point in the history
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

For directed graphs, the semantic of `lex_BFS`, `lex_DFS`, `lex_DOWN`
and `lex_UP` methods are not consistent. In some parts of the code,
directed graphs were converted into undirected graphs before the actual
computation and in some other parts (in particular in the check function
`_is_valid_lex_BFS_order`) directed graphs were explicitly considered as
directed.

In this PR, the following changes are implemented:
- always consider (and convert) directed graphs into undirected graphs
for `lex_*` methods
- do the same conversion in `_is_valid_lex_BFS_order` for consistency
- add the following line in the documentation of the `lex_*` methods:
```py
r"""
   Loops and multiple edges are ignored during the computation of <name
of the method> and
   directed graphs are converted to undirected graphs.
"""
```
- change some doctests: with the previous changes, methods can return a
different (but still valid) order
- set the copy of the graph to immutable

Fixes sagemath#38234

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#38269
Reported by: cyrilbouvier
Reviewer(s): cyrilbouvier, David Coudert
  • Loading branch information
Release Manager committed Jul 20, 2024
2 parents 1d07ba4 + 73c6e1c commit 43eef3b
Showing 1 changed file with 51 additions and 26 deletions.
77 changes: 51 additions & 26 deletions src/sage/graphs/traversals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,19 @@ def _is_valid_lex_BFS_order(G, L):
sage: G = DiGraph("I?O@??A?CCA?A??C??")
sage: _is_valid_lex_BFS_order(G, [0, 7, 1, 2, 3, 4, 5, 8, 6, 9])
False
sage: _is_valid_lex_BFS_order(G, G.lex_BFS())
True
sage: H = G.to_undirected()
sage: _is_valid_lex_BFS_order(H, G.lex_BFS())
True
sage: _is_valid_lex_BFS_order(G, H.lex_BFS())
True
"""
# Convert G to a simple undirected graph
if G.has_loops() or G.has_multiple_edges() or G.is_directed():
G = G.to_simple(immutable=True, to_undirected=True)

cdef int n = G.order()

if set(L) != set(G):
Expand All @@ -86,11 +97,9 @@ def _is_valid_lex_BFS_order(G, L):
cdef dict L_inv = {u: i for i, u in enumerate(L)}
cdef int pos_a, pos_b, pos_c

neighbors = G.neighbor_in_iterator if G.is_directed() else G.neighbor_iterator

for pos_a in range(n - 1, -1, -1):
a = L[pos_a]
for c in neighbors(a):
for c in G.neighbor_iterator(a):
pos_c = L_inv[c]
if pos_c > pos_a:
continue
Expand All @@ -99,7 +108,7 @@ def _is_valid_lex_BFS_order(G, L):
if G.has_edge(c, b):
continue
if any(L_inv[d] < pos_c and not G.has_edge(d, a)
for d in neighbors(b)):
for d in G.neighbor_iterator(b)):
# The condition is satisfied for a < b < c
continue
return False
Expand Down Expand Up @@ -259,6 +268,9 @@ def lex_BFS(G, reverse=False, tree=False, initial_vertex=None, algorithm="fast")
complexity for ``SparseGraph``. For ``DenseGraph``, the complexity is
`O(n^2)`. See [HMPV2000]_ and next section for more details.
Loops and multiple edges are ignored during the computation of ``lex_BFS``
and directed graphs are converted to undirected graphs.
ALGORITHM:
The ``"fast"`` algorithm is the `O(n + m)` time algorithm proposed in
Expand Down Expand Up @@ -315,10 +327,11 @@ def lex_BFS(G, reverse=False, tree=False, initial_vertex=None, algorithm="fast")
The method also works for directed graphs::
sage: G = DiGraph([(1, 2), (2, 3), (1, 3)])
sage: G.lex_BFS(initial_vertex=2, algorithm="slow")
[2, 3, 1]
sage: G.lex_BFS(initial_vertex=2, algorithm="fast")
[2, 3, 1]
sage: correct_anwsers = [[2, 1, 3], [2, 3, 1]]
sage: G.lex_BFS(initial_vertex=2, algorithm="slow") in correct_anwsers
True
sage: G.lex_BFS(initial_vertex=2, algorithm="fast") in correct_anwsers
True
For a Chordal Graph, a reversed Lex BFS is a Perfect Elimination Order::
Expand Down Expand Up @@ -408,9 +421,9 @@ def lex_BFS(G, reverse=False, tree=False, initial_vertex=None, algorithm="fast")
if tree:
from sage.graphs.digraph import DiGraph

# Loops and multiple edges are not needed in Lex BFS
if G.has_loops() or G.has_multiple_edges():
G = G.to_simple(immutable=False)
# Convert G to a simple undirected graph
if G.has_loops() or G.has_multiple_edges() or G.is_directed():
G = G.to_simple(immutable=True, to_undirected=True)

cdef size_t n = G.order()
if not n:
Expand Down Expand Up @@ -497,6 +510,9 @@ def lex_UP(G, reverse=False, tree=False, initial_vertex=None):
- ``initial_vertex`` -- (default: ``None``); the first vertex to
consider
Loops and multiple edges are ignored during the computation of ``lex_UP``
and directed graphs are converted to undirected graphs.
ALGORITHM:
This algorithm maintains for each vertex left in the graph a code
Expand Down Expand Up @@ -538,8 +554,9 @@ def lex_UP(G, reverse=False, tree=False, initial_vertex=None):
The method also works for directed graphs::
sage: G = DiGraph([(1, 2), (2, 3), (1, 3)])
sage: G.lex_UP(initial_vertex=2)
[2, 3, 1]
sage: correct_anwsers = [[2, 1, 3], [2, 3, 1]]
sage: G.lex_UP(initial_vertex=2) in correct_anwsers
True
Different orderings for different traversals::
Expand Down Expand Up @@ -587,9 +604,9 @@ def lex_UP(G, reverse=False, tree=False, initial_vertex=None):
if initial_vertex is not None and initial_vertex not in G:
raise ValueError("'{}' is not a graph vertex".format(initial_vertex))

# Loops and multiple edges are not needed in Lex UP
if G.allows_loops() or G.allows_multiple_edges():
G = G.to_simple(immutable=False)
# Convert G to a simple undirected graph
if G.has_loops() or G.has_multiple_edges() or G.is_directed():
G = G.to_simple(immutable=True, to_undirected=True)

cdef int nV = G.order()

Expand Down Expand Up @@ -671,6 +688,9 @@ def lex_DFS(G, reverse=False, tree=False, initial_vertex=None):
- ``initial_vertex`` -- (default: ``None``); the first vertex to
consider
Loops and multiple edges are ignored during the computation of ``lex_DFS``
and directed graphs are converted to undirected graphs.
ALGORITHM:
This algorithm maintains for each vertex left in the graph a code
Expand Down Expand Up @@ -711,8 +731,9 @@ def lex_DFS(G, reverse=False, tree=False, initial_vertex=None):
The method also works for directed graphs::
sage: G = DiGraph([(1, 2), (2, 3), (1, 3)])
sage: G.lex_DFS(initial_vertex=2)
[2, 3, 1]
sage: correct_anwsers = [[2, 1, 3], [2, 3, 1]]
sage: G.lex_DFS(initial_vertex=2) in correct_anwsers
True
Different orderings for different traversals::
Expand Down Expand Up @@ -760,9 +781,9 @@ def lex_DFS(G, reverse=False, tree=False, initial_vertex=None):
if initial_vertex is not None and initial_vertex not in G:
raise ValueError("'{}' is not a graph vertex".format(initial_vertex))

# Loops and multiple edges are not needed in Lex DFS
if G.allows_loops() or G.allows_multiple_edges():
G = G.to_simple(immutable=False)
# Convert G to a simple undirected graph
if G.has_loops() or G.has_multiple_edges() or G.is_directed():
G = G.to_simple(immutable=True, to_undirected=True)

cdef int nV = G.order()

Expand Down Expand Up @@ -845,6 +866,9 @@ def lex_DOWN(G, reverse=False, tree=False, initial_vertex=None):
- ``initial_vertex`` -- (default: ``None``); the first vertex to
consider
Loops and multiple edges are ignored during the computation of ``lex_DOWN``
and directed graphs are converted to undirected graphs.
ALGORITHM:
This algorithm maintains for each vertex left in the graph a code
Expand Down Expand Up @@ -886,8 +910,9 @@ def lex_DOWN(G, reverse=False, tree=False, initial_vertex=None):
The method also works for directed graphs::
sage: G = DiGraph([(1, 2), (2, 3), (1, 3)])
sage: G.lex_DOWN(initial_vertex=2)
[2, 3, 1]
sage: correct_anwsers = [[2, 1, 3], [2, 3, 1]]
sage: G.lex_DOWN(initial_vertex=2) in correct_anwsers
True
Different orderings for different traversals::
Expand Down Expand Up @@ -935,9 +960,9 @@ def lex_DOWN(G, reverse=False, tree=False, initial_vertex=None):
if initial_vertex is not None and initial_vertex not in G:
raise ValueError("'{}' is not a graph vertex".format(initial_vertex))

# Loops and multiple edges are not needed in Lex DOWN
if G.allows_loops() or G.allows_multiple_edges():
G = G.to_simple(immutable=False)
# Convert G to a simple undirected graph
if G.has_loops() or G.has_multiple_edges() or G.is_directed():
G = G.to_simple(immutable=True, to_undirected=True)

cdef int nV = G.order()

Expand Down

0 comments on commit 43eef3b

Please sign in to comment.