-
-
Notifications
You must be signed in to change notification settings - Fork 688
some improvements in sage/graphs/orientations.py #35981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vbraun
merged 5 commits into
sagemath:develop
from
dcoudert:graphs/improve_orientations
Aug 13, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb8c43b
some improvements in sage/graphs/orientations.py
dcoudert 35b0888
Merge branch 'develop' into graphs/improve_orientations
dcoudert 1d50539
PR 35981: fix review comments
dcoudert a030d35
PR 35981: typo
dcoudert 80fc1f3
Merge branch 'develop' into graphs/improve_orientations
dcoudert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,17 +25,31 @@ | |
| Methods | ||
| ------- | ||
| """ | ||
| # **************************************************************************** | ||
| # Copyright (C) 2017 Kolja Knauer <[email protected]> | ||
| # 2017 Petru Valicov <[email protected]> | ||
| # 2017-2023 David Coudert <[email protected]> | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # https://www.gnu.org/licenses/ | ||
| # **************************************************************************** | ||
|
|
||
| from copy import copy | ||
| from sage.graphs.digraph import DiGraph | ||
|
|
||
|
|
||
| def strong_orientations_iterator(G): | ||
| r""" | ||
| Returns an iterator over all strong orientations of a graph `G`. | ||
| Return an iterator over all strong orientations of a graph `G`. | ||
|
|
||
| A strong orientation of a graph is an orientation of its edges such that | ||
| the obtained digraph is strongly connected (i.e. there exist a directed path | ||
| between each pair of vertices). | ||
| A strong orientation of a graph is an orientation of its edges such that the | ||
| obtained digraph is strongly connected (i.e. there exist a directed path | ||
| between each pair of vertices). According to Robbins' theorem (see the | ||
| :wikipedia:`Robbins_theorem`), the graphs that have strong orientations are | ||
| exactly the 2-edge-connected graphs (i.e., the bridgeless graphs). | ||
|
|
||
| ALGORITHM: | ||
|
|
||
|
|
@@ -84,7 +98,7 @@ def strong_orientations_iterator(G): | |
|
|
||
| A tree cannot be strongly oriented:: | ||
|
|
||
| sage: g = graphs.RandomTree(100) | ||
| sage: g = graphs.RandomTree(10) | ||
| sage: len(list(g.strong_orientations_iterator())) | ||
| 0 | ||
|
|
||
|
|
@@ -115,44 +129,37 @@ def strong_orientations_iterator(G): | |
| sage: g = graphs.PetersenGraph() | ||
| sage: nr1 = len(list(g.strong_orientations_iterator())) | ||
| sage: nr2 = g.tutte_polynomial()(0,2) | ||
| sage: nr1 == nr2/2 # The Tutte polynomial counts also the symmetrical orientations | ||
| sage: nr1 == nr2/2 # The Tutte polynomial counts also the symmetrical orientations | ||
| True | ||
|
|
||
| """ | ||
| # if the graph has a bridge or is disconnected, | ||
| # then it cannot be strongly oriented | ||
| if G.order() < 3 or not G.is_biconnected(): | ||
| if G.order() < 3 or not G.is_connected() or any(G.bridges(labels=False)): | ||
| return | ||
|
|
||
| V = G.vertices(sort=False) | ||
| Dg = DiGraph([V, G.edges(sort=False)], pos=G.get_pos()) | ||
| V = list(G) | ||
|
|
||
| # compute an arbitrary spanning tree of the undirected graph | ||
| te = G.min_spanning_tree() | ||
| treeEdges = [(u, v) for u, v, _ in te] | ||
| tree_edges_set = set(treeEdges) | ||
| A = [edge for edge in G.edge_iterator(labels=False) if edge not in tree_edges_set] | ||
| T = G.subgraph(vertices=G, edges=G.min_spanning_tree(), inplace=False) | ||
| treeEdges = list(T.edges(labels=False, sort=False)) | ||
| A = [edge for edge in G.edge_iterator(labels=False) if not T.has_edge(edge)] | ||
|
|
||
| # Initialize a digraph with the edges of the spanning tree doubly oriented | ||
| Dg = T.to_directed(sparse=True) | ||
| Dg.add_edges(A) | ||
|
|
||
| # initialization of the first binary word 00...0 | ||
| # corresponding to the current orientation of the non-tree edges | ||
| existingAedges = [0] * len(A) | ||
|
|
||
| # Make the edges of the spanning tree doubly oriented | ||
| for e in treeEdges: | ||
| if Dg.has_edge(e): | ||
| Dg.add_edge(e[1], e[0]) | ||
| else: | ||
| Dg.add_edge(e) | ||
|
|
||
| # Generate all orientations for non-tree edges (using Gray code) | ||
| # Each of these orientations can be extended to a strong orientation | ||
| # of G by orienting properly the tree-edges | ||
| previousWord = 0 | ||
| i = 0 | ||
|
|
||
| # the orientation of one edge is fixed so we consider one edge less | ||
| nr = 2**(len(A) - 1) | ||
| while i < nr: | ||
| for i in range(nr): | ||
| word = (i >> 1) ^ i | ||
| bitChanged = word ^ previousWord | ||
|
|
||
|
|
@@ -169,9 +176,7 @@ def strong_orientations_iterator(G): | |
| Dg.reverse_edge(A[bit][1], A[bit][0]) | ||
| existingAedges[bit] = 0 | ||
| # launch the algorithm for enumeration of the solutions | ||
| for sol in _strong_orientations_of_a_mixed_graph(Dg, V, treeEdges): | ||
| yield sol | ||
| i = i + 1 | ||
| yield from _strong_orientations_of_a_mixed_graph(Dg, V, treeEdges) | ||
|
|
||
|
|
||
| def _strong_orientations_of_a_mixed_graph(Dg, V, E): | ||
|
|
@@ -201,9 +206,9 @@ def _strong_orientations_of_a_mixed_graph(Dg, V, E): | |
|
|
||
| sage: from sage.graphs.orientations import _strong_orientations_of_a_mixed_graph | ||
| sage: g = graphs.CycleGraph(5) | ||
| sage: Dg = DiGraph(g) # all edges of g will be doubly oriented | ||
| sage: Dg = DiGraph(g) # all edges of g will be doubly oriented | ||
| sage: it = _strong_orientations_of_a_mixed_graph(Dg, list(g), list(g.edges(labels=False, sort=False))) | ||
| sage: len(list(it)) # there are two orientations of this multigraph | ||
| sage: len(list(it)) # there are two orientations of this multigraph | ||
| 2 | ||
| """ | ||
| length = len(E) | ||
|
|
@@ -213,7 +218,9 @@ def _strong_orientations_of_a_mixed_graph(Dg, V, E): | |
| u, v = E[i] | ||
| Dg.delete_edge(u, v) | ||
| if not (v in Dg.depth_first_search(u)): | ||
| del E[i] | ||
| # del E[i] in constant time | ||
| E[i] = E[-1] | ||
| E.pop() | ||
| length -= 1 | ||
| Dg.add_edge(u, v) | ||
| Dg.delete_edge(v, u) | ||
|
|
@@ -222,7 +229,9 @@ def _strong_orientations_of_a_mixed_graph(Dg, V, E): | |
| Dg.add_edge(u, v) | ||
| Dg.delete_edge(v, u) | ||
| if not (u in Dg.depth_first_search(v)): | ||
| del E[i] | ||
| # del E[i] in constant time | ||
| E[i] = E[-1] | ||
| E.pop() | ||
| length -= 1 | ||
| boundEdges.append((u, v)) | ||
| Dg.delete_edge(u, v) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.