Skip to content

Commit

Permalink
fix bug in acyclic_orientations, see issue 38758
Browse files Browse the repository at this point in the history
return a properly relabeld digraph, not just the arc labeling to
describe the orientation.
  • Loading branch information
dcoudert committed Oct 3, 2024
1 parent b0f09d9 commit 8e7f772
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/sage/graphs/orientations.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ def acyclic_orientations(G):
.. NOTE::
The function assumes that the input graph is undirected and the edges are unlabelled.
The function assumes that the input graph is undirected and the edges
are unlabelled.
EXAMPLES:
To count number acyclic orientations for a graph::
To count the number of acyclic orientations for a graph::
sage: g = Graph([(0, 3), (0, 4), (3, 4), (1, 3), (1, 2), (2, 3), (2, 4)])
sage: it = g.acyclic_orientations()
Expand All @@ -80,11 +81,21 @@ def acyclic_orientations(G):
Test for arbitrary vertex labels::
sage: g_str = Graph([('abc', 'def'), ('ghi', 'def'), ('xyz', 'abc'), ('xyz', 'uvw'), ('uvw', 'abc'), ('uvw', 'ghi')])
sage: g_str = Graph([('abc', 'def'), ('ghi', 'def'), ('xyz', 'abc'),
....: ('xyz', 'uvw'), ('uvw', 'abc'), ('uvw', 'ghi')])
sage: it = g_str.acyclic_orientations()
sage: len(list(it))
42
Check that the method returns properly relabeled acyclic digraphs::
sage: g = Graph([(0, 1), (1, 2), (2, 3), (3, 0), (0, 2)])
sage: orientations = set([frozenset(d.edges(labels=false)) for d in g.acyclic_orientations()])
sage: len(orientations)
18
sage: all(d.is_directed_acyclic() for d in g.acyclic_orientations())
True
TESTS:
To count the number of acyclic orientations for a graph with 0 vertices::
Expand Down Expand Up @@ -291,8 +302,9 @@ def helper(G, globO, m, k):

# Iterate over acyclic orientations and create relabeled graphs
for orientation in orientations:
relabeled_graph = DiGraph([(reverse_vertex_labels[u], reverse_vertex_labels[v], label) for (u, v), label in orientation.items()])
yield relabeled_graph
D = DiGraph([(u, v) if label else (v, u) for (u, v), label in orientation.items()])
D.relabel(perm=reverse_vertex_labels, inplace=True)
yield D


def strong_orientations_iterator(G):
Expand Down

0 comments on commit 8e7f772

Please sign in to comment.