@@ -663,15 +663,15 @@ cdef class SubgraphSearch:
663
663
sage: SubgraphSearch( Graph( 5) , Graph( 1)) # optional - sage. modules
664
664
Traceback ( most recent call last) :
665
665
...
666
- ValueError: Searched graph should have at least 2 vertices.
666
+ ValueError: searched graph should have at least 2 vertices
667
667
sage: SubgraphSearch( Graph( 5) , Graph( 2)) # optional - sage. modules
668
668
<sage. graphs. generic_graph_pyx. SubgraphSearch ... >
669
669
"""
670
670
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" )
672
672
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" )
675
675
676
676
G._scream_if_not_simple(allow_loops = True )
677
677
H._scream_if_not_simple(allow_loops = True )
@@ -724,6 +724,18 @@ cdef class SubgraphSearch:
724
724
sage: S = SubgraphSearch( g, h) # optional - sage. modules
725
725
sage: S. cardinality( ) # optional - sage. modules
726
726
6
727
+
728
+ Check that the method is working even when vertices or edges are of
729
+ incomparable types::
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
727
739
"""
728
740
if self .nh > self .ng:
729
741
return 0
@@ -812,7 +824,8 @@ cdef class SubgraphSearch:
812
824
self .nh = H.order()
813
825
814
826
# 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)
816
829
817
830
# Are the graphs directed (in __init__(), we check
818
831
# whether both are of the same type)
@@ -846,15 +859,22 @@ cdef class SubgraphSearch:
846
859
self .h = DenseGraph(self .nh)
847
860
848
861
# 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)
858
878
859
879
# vertices is equal to range(nh), as an int *variable
860
880
for i in range (self .nh):
0 commit comments