Skip to content

Commit

Permalink
gh-36682: src/sage/graphs: some care with # optional
Browse files Browse the repository at this point in the history
    
Unify the syntax and ensure that we have 2 spaces before `# optional`.

### 📝 Checklist

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

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on
- #12345: short description why this is a dependency
- #34567: ...
-->

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: #36682
Reported by: David Coudert
Reviewer(s): Frédéric Chapoton
  • Loading branch information
Release Manager committed Dec 9, 2023
2 parents e204ca8 + c287566 commit 168719b
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 101 deletions.
15 changes: 8 additions & 7 deletions src/sage/graphs/digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ class DiGraph(GenericGraph):
#. An igraph directed Graph (see also
:meth:`~sage.graphs.generic_graph.GenericGraph.igraph_graph`)::
sage: import igraph # optional - python_igraph
sage: g = igraph.Graph([(0,1),(0,2)], directed=True) # optional - python_igraph
sage: DiGraph(g) # optional - python_igraph
sage: import igraph # optional - python_igraph
sage: g = igraph.Graph([(0,1),(0,2)], directed=True) # optional - python_igraph
sage: DiGraph(g) # optional - python_igraph
Digraph on 3 vertices
If ``vertex_labels`` is ``True``, the names of the vertices are given by
Expand All @@ -462,8 +462,9 @@ class DiGraph(GenericGraph):
If the igraph Graph has edge attributes, they are used as edge labels::
sage: g = igraph.Graph([(0,1),(0,2)], directed=True, edge_attrs={'name':['a','b'], 'weight':[1,3]}) # optional - python_igraph
sage: DiGraph(g).edges(sort=True) # optional - python_igraph
sage: g = igraph.Graph([(0, 1), (0, 2)], directed=True, # optional - python_igraph
....: edge_attrs={'name':['a', 'b'], 'weight':[1, 3]})
sage: DiGraph(g).edges(sort=True) # optional - python_igraph
[(0, 1, {'name': 'a', 'weight': 1}), (0, 2, {'name': 'b', 'weight': 3})]
Expand Down Expand Up @@ -621,8 +622,8 @@ def __init__(self, data=None, pos=None, loops=None, format=None,
Sage DiGraph from igraph undirected graph::
sage: import igraph # optional - python_igraph
sage: DiGraph(igraph.Graph()) # optional - python_igraph
sage: import igraph # optional - python_igraph
sage: DiGraph(igraph.Graph()) # optional - python_igraph
Traceback (most recent call last):
...
ValueError: a *directed* igraph graph was expected. To build an undirected graph, call the Graph constructor
Expand Down
4 changes: 2 additions & 2 deletions src/sage/graphs/dot2tex_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def have_dot2tex():
EXAMPLES::
sage: sage.graphs.dot2tex_utils.have_dot2tex() # optional - dot2tex graphviz
sage: sage.graphs.dot2tex_utils.have_dot2tex() # optional - dot2tex graphviz
True
sage: sage.graphs.dot2tex_utils.have_dot2tex() in [True, False]
True
Expand All @@ -41,7 +41,7 @@ def assert_have_dot2tex():
EXAMPLES::
sage: sage.graphs.dot2tex_utils.assert_have_dot2tex() # optional - dot2tex graphviz
sage: sage.graphs.dot2tex_utils.assert_have_dot2tex() # optional - dot2tex graphviz
"""
check_error_string = """
An error occurs while testing the dot2tex installation.
Expand Down
68 changes: 35 additions & 33 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,8 +1622,8 @@ def igraph_graph(self, vertex_list=None, vertex_attrs={}, edge_attrs={}):

sage: G = Graph([(1, 2, 'a'), (2, 3, 'b')])
sage: E = list(G.edge_iterator())
sage: H = G.igraph_graph(edge_attrs={'label': [e[2] for e in E]}) # optional - python_igraph
sage: H.es['label'] # optional - python_igraph
sage: H = G.igraph_graph(edge_attrs={'label': [e[2] for e in E]}) # optional - python_igraph
sage: H.es['label'] # optional - python_igraph
['a', 'b']


Expand All @@ -1632,84 +1632,86 @@ def igraph_graph(self, vertex_list=None, vertex_attrs={}, edge_attrs={}):

sage: G = Graph([(1, 2, {'weight': 1}), (2, 3, {'weight': 2})])
sage: E = list(G.edge_iterator())
sage: H = G.igraph_graph(edge_attrs={'weight': [e[2]['weight'] for e in E]}) # optional - python_igraph
sage: H.is_weighted() # optional - python_igraph
sage: H = G.igraph_graph(edge_attrs={'weight': [e[2]['weight'] for e in E]}) # optional - python_igraph
sage: H.is_weighted() # optional - python_igraph
True
sage: H.es['weight'] # optional - python_igraph
sage: H.es['weight'] # optional - python_igraph
[1, 2]

Adding vertex attributes::

sage: G = graphs.GridGraph([2, 2])
sage: H = G.igraph_graph(vertex_attrs={'name': G.vertices(sort=True)}) # optional - python_igraph
sage: H.vs()['name'] # optional - python_igraph
sage: H = G.igraph_graph(vertex_attrs={'name': G.vertices(sort=True)}) # optional - python_igraph
sage: H.vs()['name'] # optional - python_igraph
[(0, 0), (0, 1), (1, 0), (1, 1)]

Providing a mapping from vertices to consecutive integers::

sage: G = graphs.GridGraph([2, 2])
sage: V = list(G)
sage: H = G.igraph_graph(vertex_list=V, vertex_attrs={'name': V}) # optional - python_igraph
sage: H.vs()['name'] == V # optional - python_igraph
sage: H = G.igraph_graph(vertex_list=V, vertex_attrs={'name': V}) # optional - python_igraph
sage: H.vs()['name'] == V # optional - python_igraph
True

Sometimes, Sage integer/floats are not compatible with igraph::

sage: G = Graph([(0, 1, 2)])
sage: E = list(G.edge_iterator())
sage: H = G.igraph_graph(edge_attrs={'capacity': [e[2] for e in E]}) # optional - python_igraph
sage: H.maxflow_value(0, 1, 'capacity') # optional - python_igraph
sage: H = G.igraph_graph(edge_attrs={'capacity': [e[2] for e in E]}) # optional - python_igraph
sage: H.maxflow_value(0, 1, 'capacity') # optional - python_igraph
1.0
sage: H = G.igraph_graph(edge_attrs={'capacity': [float(e[2]) for e in E]}) # optional - python_igraph
sage: H.maxflow_value(0, 1, 'capacity') # optional - python_igraph
sage: H = G.igraph_graph(edge_attrs={'capacity': [float(e[2]) for e in E]}) # optional - python_igraph
sage: H.maxflow_value(0, 1, 'capacity') # optional - python_igraph
2.0

TESTS:

Converting a DiGraph back and forth::

sage: # optional - python_igraph
sage: G = DiGraph([('a', 'b', {'w': 1}), ('b', 'c', {'w': 2})])
sage: vertex_attrs = {'name': G.vertices(sort=False)}
sage: E = list(G.edge_iterator())
sage: edge_attrs = {'w': [e[2]['w'] for e in E]}
sage: H = DiGraph(G.igraph_graph(vertex_attrs=vertex_attrs, edge_attrs=edge_attrs)) # optional - python_igraph
sage: G == H # optional - python_igraph
sage: H = DiGraph(G.igraph_graph(vertex_attrs=vertex_attrs, edge_attrs=edge_attrs))
sage: G == H
True
sage: G.edges(sort=True) == H.edges(sort=True) # optional - python_igraph
sage: G.edges(sort=True) == H.edges(sort=True)
True
sage: H = DiGraph(G.igraph_graph(edge_attrs=edge_attrs)) # optional - python_igraph
sage: G == H # optional - python_igraph
sage: H = DiGraph(G.igraph_graph(edge_attrs=edge_attrs))
sage: G == H
False

When checking for equality, edge labels are not taken into account::

sage: H = DiGraph(G.igraph_graph(vertex_attrs=vertex_attrs)) # optional - python_igraph
sage: G == H # optional - python_igraph
sage: H = DiGraph(G.igraph_graph(vertex_attrs=vertex_attrs)) # optional - python_igraph
sage: G == H # optional - python_igraph
True
sage: G.edges(sort=True) == H.edges(sort=True) # optional - python_igraph
sage: G.edges(sort=True) == H.edges(sort=True) # optional - python_igraph
False

Converting a Graph back and forth::

sage: # optional - python_igraph
sage: G = Graph([('a', 'b', {'w': 1}), ('b', 'c', {'w': 2})])
sage: vertex_attrs = {'name': G.vertices(sort=False)}
sage: E = list(G.edge_iterator())
sage: edge_attrs = {'w': [e[2]['w'] for e in E]}
sage: H = Graph(G.igraph_graph(vertex_attrs=vertex_attrs, edge_attrs=edge_attrs)) # optional - python_igraph
sage: G == H # optional - python_igraph
sage: H = Graph(G.igraph_graph(vertex_attrs=vertex_attrs, edge_attrs=edge_attrs))
sage: G == H
True
sage: G.edges(sort=True) == H.edges(sort=True) # optional - python_igraph
sage: G.edges(sort=True) == H.edges(sort=True)
True
sage: H = Graph(G.igraph_graph(edge_attrs=edge_attrs)) # optional - python_igraph
sage: G == H # optional - python_igraph
sage: H = Graph(G.igraph_graph(edge_attrs=edge_attrs))
sage: G == H
False

When checking for equality, edge labels are not taken into account::

sage: H = Graph(G.igraph_graph(vertex_attrs=vertex_attrs)) # optional - python_igraph
sage: G == H # optional - python_igraph
sage: H = Graph(G.igraph_graph(vertex_attrs=vertex_attrs)) # optional - python_igraph
sage: G == H # optional - python_igraph
True
sage: G.edges(sort=True) == H.edges(sort=True) # optional - python_igraph
sage: G.edges(sort=True) == H.edges(sort=True) # optional - python_igraph
False

Check input parameter ``vertex_list``::
Expand Down Expand Up @@ -7331,7 +7333,7 @@ def edge_cut(self, s, t, value_only=True, use_edge_labels=False, vertices=False,
....: g.set_edge_label(u,v,round(random(),5))
sage: g.edge_cut(0, 1, algorithm="FF") == g.edge_cut(0, 1, algorithm="LP") # needs sage.numerical.mip
True
sage: g.edge_cut(0, 1, algorithm="FF") == g.edge_cut(0, 1, algorithm="igraph") # optional - python_igraph
sage: g.edge_cut(0, 1, algorithm="FF") == g.edge_cut(0, 1, algorithm="igraph") # optional - python_igraph
True

Rounded return value when using the LP method::
Expand Down Expand Up @@ -9583,8 +9585,8 @@ def flow(self, x, y, value_only=True, integer=False, use_edge_labels=True,
sage: flow_lp = g.flow(0, 1, algorithm="LP") # needs sage.numerical.mip
sage: abs(flow_ff - flow_lp) < 0.01 # needs sage.numerical.mip
True
sage: flow_igraph = g.flow(0, 1, algorithm="igraph") # optional python_igraph
sage: abs(flow_ff - flow_igraph) < 0.00001 # optional python_igraph
sage: flow_igraph = g.flow(0, 1, algorithm="igraph") # optional - python_igraph
sage: abs(flow_ff - flow_igraph) < 0.00001 # optional - python_igraph
True
"""
self._scream_if_not_simple(allow_loops=True)
Expand Down Expand Up @@ -23268,7 +23270,7 @@ def automorphism_group(self, partition=None, verbosity=0,
sage: G.automorphism_group(edge_labels=True) # needs sage.groups
Permutation Group with generators [(1,4)(2,3)]

sage: G.automorphism_group(edge_labels=True, algorithm="bliss") # optional - bliss
sage: G.automorphism_group(edge_labels=True, algorithm="bliss") # optional - bliss
Permutation Group with generators [(1,4)(2,3)]

sage: G.automorphism_group(edge_labels=True, algorithm="sage") # needs sage.groups
Expand Down
20 changes: 10 additions & 10 deletions src/sage/graphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@
- an igraph Graph::
sage: import igraph # optional - python_igraph
sage: g = Graph(igraph.Graph([(1,3),(3,2),(0,2)])) # optional - python_igraph
sage: g # optional - python_igraph
sage: import igraph # optional - python_igraph
sage: g = Graph(igraph.Graph([(1,3),(3,2),(0,2)])) # optional - python_igraph
sage: g # optional - python_igraph
Graph on 4 vertices
Generators
Expand Down Expand Up @@ -820,9 +820,9 @@ class Graph(GenericGraph):
#. An igraph Graph (see also
:meth:`~sage.graphs.generic_graph.GenericGraph.igraph_graph`)::
sage: import igraph # optional - python_igraph
sage: g = igraph.Graph([(0, 1), (0, 2)]) # optional - python_igraph
sage: Graph(g) # optional - python_igraph
sage: import igraph # optional - python_igraph
sage: g = igraph.Graph([(0, 1), (0, 2)]) # optional - python_igraph
sage: Graph(g) # optional - python_igraph
Graph on 3 vertices
If ``vertex_labels`` is ``True``, the names of the vertices are given by
Expand All @@ -838,8 +838,8 @@ class Graph(GenericGraph):
If the igraph Graph has edge attributes, they are used as edge labels::
sage: g = igraph.Graph([(0,1),(0,2)], edge_attrs={'name':['a','b'], 'weight':[1,3]}) # optional - python_igraph
sage: Graph(g).edges(sort=True) # optional - python_igraph
sage: g = igraph.Graph([(0,1),(0,2)], edge_attrs={'name':['a','b'], 'weight':[1,3]}) # optional - python_igraph
sage: Graph(g).edges(sort=True) # optional - python_igraph
[(0, 1, {'name': 'a', 'weight': 1}), (0, 2, {'name': 'b', 'weight': 3})]
Expand Down Expand Up @@ -883,7 +883,7 @@ class Graph(GenericGraph):
...
ValueError: Unknown input format 'HeyHeyHey'
sage: Graph(igraph.Graph(directed=True)) # optional - python_igraph
sage: Graph(igraph.Graph(directed=True)) # optional - python_igraph
Traceback (most recent call last):
...
ValueError: An *undirected* igraph graph was expected. To build an directed graph, call the DiGraph constructor.
Expand Down Expand Up @@ -7175,7 +7175,7 @@ def vertex_cover(self, algorithm="Cliquer", value_only=False,
Testing mcqd::
sage: graphs.PetersenGraph().vertex_cover(algorithm="mcqd", value_only=True) # optional - mcqd
sage: graphs.PetersenGraph().vertex_cover(algorithm="mcqd", value_only=True) # optional - mcqd
6
Given a wrong algorithm::
Expand Down
Loading

0 comments on commit 168719b

Please sign in to comment.