Skip to content

Commit

Permalink
sagemathgh-39124: unify and simplify orientation methods
Browse files Browse the repository at this point in the history
    
Fixes sagemath#38758.

This PR follows sagemath#38758, sagemath#38757, sagemath#38778, sagemath#38809. it adds a helper method
that initialize a directed copy of a graph with the same set of
vertices. It copies all attributes. It helps simplifying the code of
several orientation methods and avoids code duplication.



### 📝 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#39124
Reported by: David Coudert
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Jan 25, 2025
2 parents debb3ea + 86342f3 commit b9b0109
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 170 deletions.
42 changes: 6 additions & 36 deletions src/sage/graphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5271,42 +5271,12 @@ def to_directed(self, data_structure=None, sparse=None):
sage: D.get_vertices()
{0: 'foo', 1: None, 2: None}
"""
if sparse is not None:
if data_structure is not None:
raise ValueError("The 'sparse' argument is an alias for "
"'data_structure'. Please do not define both.")
data_structure = "sparse" if sparse else "dense"

if data_structure is None:
from sage.graphs.base.dense_graph import DenseGraphBackend
from sage.graphs.base.sparse_graph import SparseGraphBackend
if isinstance(self._backend, DenseGraphBackend):
data_structure = "dense"
elif isinstance(self._backend, SparseGraphBackend):
data_structure = "sparse"
else:
data_structure = "static_sparse"
from sage.graphs.digraph import DiGraph
D = DiGraph(name=self.name(),
pos=self.get_pos(),
multiedges=self.allows_multiple_edges(),
loops=self.allows_loops(),
data_structure=(data_structure if data_structure != "static_sparse"
else "sparse")) # we need a mutable copy

D.add_vertices(self.vertex_iterator())
D.set_vertices(self.get_vertices())
for u, v, l in self.edge_iterator():
D.add_edge(u, v, l)
D.add_edge(v, u, l)
if hasattr(self, '_embedding'):
D._embedding = copy(self._embedding)
D._weighted = self._weighted

if data_structure == "static_sparse":
D = D.copy(data_structure=data_structure)

return D
from sage.graphs.orientations import _initialize_digraph
from itertools import chain
edges = chain(self.edge_iterator(),
((v, u, l) for u, v, l in self.edge_iterator()))
return _initialize_digraph(self, edges, name=self.name(),
data_structure=data_structure, sparse=sparse)

@doc_index("Basic methods")
def to_undirected(self):
Expand Down
Loading

0 comments on commit b9b0109

Please sign in to comment.