Skip to content

Commit 4eb3fcb

Browse files
author
Release Manager
committed
gh-35904: Make SubgraphSearch robust to vertex labels Part of #35902. ### 📚 Description We ensure that the method operates properly even when vertices and edges are of incomparable types. On the way, we also avoid a call to `adjacency_matrix`. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x ]`. --> - [x] The title is concise, informative, and self-explanatory. - [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 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: #35904 Reported by: David Coudert Reviewer(s): Matthias Köppe
2 parents f85e7b8 + d12921a commit 4eb3fcb

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/sage/graphs/generic_graph_pyx.pyx

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -663,15 +663,15 @@ cdef class SubgraphSearch:
663663
sage: SubgraphSearch(Graph(5), Graph(1)) # optional - sage.modules
664664
Traceback (most recent call last):
665665
...
666-
ValueError: Searched graph should have at least 2 vertices.
666+
ValueError: searched graph should have at least 2 vertices
667667
sage: SubgraphSearch(Graph(5), Graph(2)) # optional - sage.modules
668668
<sage.graphs.generic_graph_pyx.SubgraphSearch ...>
669669
"""
670670
if H.order() <= 1:
671-
raise ValueError("Searched graph should have at least 2 vertices.")
671+
raise ValueError("searched graph should have at least 2 vertices")
672672

673-
if sum([G.is_directed(), H.is_directed()]) == 1:
674-
raise ValueError("One graph cannot be directed while the other is not.")
673+
if G.is_directed() != H.is_directed():
674+
raise ValueError("one graph cannot be directed while the other is not")
675675

676676
G._scream_if_not_simple(allow_loops=True)
677677
H._scream_if_not_simple(allow_loops=True)
@@ -724,6 +724,18 @@ cdef class SubgraphSearch:
724724
sage: S = SubgraphSearch(g, h) # optional - sage.modules
725725
sage: S.cardinality() # optional - sage.modules
726726
6
727+
728+
Check that the method is working even when vertices or edges are of
729+
incomparable types (see :trac:`35904`)::
730+
731+
sage: from sage.graphs.generic_graph_pyx import SubgraphSearch
732+
sage: G = Graph()
733+
sage: G.add_cycle(['A', 1, 2, 3, ('a', 1)])
734+
sage: H = Graph()
735+
sage: H.add_path("xyz")
736+
sage: S = SubgraphSearch(G, H) # optional - sage.modules
737+
sage: S.cardinality() # optional - sage.modules
738+
10
727739
"""
728740
if self.nh > self.ng:
729741
return 0
@@ -812,7 +824,8 @@ cdef class SubgraphSearch:
812824
self.nh = H.order()
813825

814826
# Storing the list of vertices
815-
self.g_vertices = G.vertices(sort=True)
827+
self.g_vertices = list(G)
828+
cdef list h_vertices = list(H)
816829

817830
# Are the graphs directed (in __init__(), we check
818831
# whether both are of the same type)
@@ -846,15 +859,22 @@ cdef class SubgraphSearch:
846859
self.h = DenseGraph(self.nh)
847860

848861
# copying the adjacency relations in both G and H
849-
for i, row in enumerate(G.adjacency_matrix()):
850-
for j, k in enumerate(row):
851-
if k:
852-
self.g.add_arc(i, j)
853-
854-
for i, row in enumerate(H.adjacency_matrix()):
855-
for j, k in enumerate(row):
856-
if k:
857-
self.h.add_arc(i, j)
862+
cdef dict vertex_to_int = {v: i for i, v in enumerate(self.g_vertices)}
863+
cdef bint undirected = not G.is_directed()
864+
for u, v in G.edge_iterator(labels=False):
865+
i = vertex_to_int[u]
866+
j = vertex_to_int[v]
867+
self.g.add_arc(i, j)
868+
if undirected:
869+
self.g.add_arc(j, i)
870+
871+
vertex_to_int = {v: i for i, v in enumerate(h_vertices)}
872+
for u, v in H.edge_iterator(labels=False):
873+
i = vertex_to_int[u]
874+
j = vertex_to_int[v]
875+
self.h.add_arc(i, j)
876+
if undirected:
877+
self.h.add_arc(j, i)
858878

859879
# vertices is equal to range(nh), as an int *variable
860880
for i in range(self.nh):

0 commit comments

Comments
 (0)