Skip to content
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

improve method cycle_basis for graphs with multiple edges #36493

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5150,13 +5150,13 @@ def cycle_basis(self, output='vertex'):
sage: G = Graph([(0, 2, 'a'), (0, 2, 'b'), (0, 1, 'c'), (1, 2, 'd')],
....: multiedges=True)
sage: G.cycle_basis() # needs networkx
[[0, 2], [2, 1, 0]]
[[2, 0], [2, 0, 1]]
sage: G.cycle_basis(output='edge') # needs networkx
[[(0, 2, 'b'), (2, 0, 'a')], [(2, 1, 'd'), (1, 0, 'c'), (0, 2, 'a')]]
[[(0, 2, 'b'), (2, 0, 'a')], [(1, 2, 'd'), (2, 0, 'a'), (0, 1, 'c')]]
sage: H = Graph([(1, 2), (2, 3), (2, 3), (3, 4), (1, 4),
....: (1, 4), (4, 5), (5, 6), (4, 6), (6, 7)], multiedges=True)
sage: H.cycle_basis() # needs networkx
[[1, 4], [2, 3], [4, 3, 2, 1], [6, 5, 4]]
[[4, 1], [3, 2], [4, 1, 2, 3], [6, 4, 5]]

Disconnected graph::

Expand All @@ -5168,14 +5168,14 @@ def cycle_basis(self, output='vertex'):
('Really ?', 'Wuuhuu', None),
('Wuuhuu', 'Hey', None)],
[(0, 2, 'a'), (2, 0, 'b')],
[(0, 2, 'b'), (1, 0, 'c'), (2, 1, 'd')]]
[(0, 1, 'c'), (1, 2, 'd'), (2, 0, 'b')]]

Graph that allows multiple edges but does not contain any::

sage: G = graphs.CycleGraph(3)
sage: G.allow_multiple_edges(True)
sage: G.cycle_basis() # needs networkx
[[2, 1, 0]]
[[2, 0, 1]]

Not yet implemented for directed graphs::

Expand All @@ -5192,11 +5192,11 @@ def cycle_basis(self, output='vertex'):
sage: G = Graph([(1, 2, 'a'), (2, 3, 'b'), (2, 3, 'c'),
....: (3, 4, 'd'), (3, 4, 'e'), (4, 1, 'f')], multiedges=True)
sage: G.cycle_basis() # needs networkx
[[2, 3], [4, 3, 2, 1], [4, 3, 2, 1]]
[[3, 2], [4, 1, 2, 3], [4, 1, 2, 3]]
sage: G.cycle_basis(output='edge') # needs networkx
[[(2, 3, 'c'), (3, 2, 'b')],
[(4, 3, 'd'), (3, 2, 'b'), (2, 1, 'a'), (1, 4, 'f')],
[(4, 3, 'e'), (3, 2, 'b'), (2, 1, 'a'), (1, 4, 'f')]]
[(3, 4, 'd'), (4, 1, 'f'), (1, 2, 'a'), (2, 3, 'b')],
[(3, 4, 'e'), (4, 1, 'f'), (1, 2, 'a'), (2, 3, 'b')]]
"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then for these cases, you may remove # needs networkx?

if output not in ['vertex', 'edge']:
raise ValueError('output must be either vertex or edge')
Expand All @@ -5211,14 +5211,32 @@ def cycle_basis(self, output='vertex'):
[])

from sage.graphs.graph import Graph
from itertools import pairwise
T = Graph(self.min_spanning_tree(), multiedges=True, format='list_of_edges')
H = self.copy()
H.delete_edges(T.edge_iterator())
root = next(T.vertex_iterator())
rank = dict(T.breadth_first_search(root, report_distance=True))
parent = {v: u for u, v in T.breadth_first_search(root, edges=True)}
L = []
for e in H.edge_iterator():
T.add_edge(e)
L.append(T.is_tree(certificate=True, output=output)[1])
T.delete_edge(e)
# Search for the nearest common ancestor of e[0] and e[1] in T
P = [e[0]]
Q = [e[1]]
while P[-1] != Q[-1]:
# If rank[P[-1]] > rank[Q[-1]], we extend the path P.
# If rank[P[-1]] < rank[Q[-1]], we extend the path Q.
# In case of equality, we extend both paths.
diff = rank[P[-1]] - rank[Q[-1]]
if diff >= 0:
P.append(parent[P[-1]])
if diff <= 0:
Q.append(parent[Q[-1]])

cycle = Q + P[-2::-1]
if output == 'edge':
cycle = [e] + [(x, y, T.edge_label(x, y)[0]) for x, y in pairwise(cycle)]
L.append(cycle)
return L

# second case: there are no multiple edges
Expand Down Expand Up @@ -5289,7 +5307,7 @@ def minimum_cycle_basis(self, algorithm=None, weight_function=None, by_weight=Fa
TESTS::

sage: g = Graph([(0, 1, 1), (1, 2, 'a')])
sage: g.min_spanning_tree(by_weight=True)
sage: g.minimum_cycle_basis(by_weight=True)
Traceback (most recent call last):
...
ValueError: the weight function cannot find the weight of (1, 2, 'a')
Expand Down