Skip to content

Commit

Permalink
gh-37835: Optimize DisjointSet
Browse files Browse the repository at this point in the history
    
The methods `find` and `union` of the class `DisjointSet_of_hashables`
pass through an unnecessary layer of input checks that slow down the
code. I avoided these checks, cythonized more of the code, and edited
the docstrings.

I can attest to the fact that these changes improve speed, and in a
follow-up PR (#37839) I mention a test that confirms this.
    
URL: #37835
Reported by: gmou3
Reviewer(s): gmou3, Martin Rubey, Travis Scrimshaw, Vincent Delecroix
  • Loading branch information
Release Manager committed May 1, 2024
2 parents 6e58d87 + e24a492 commit ac9c1b0
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 182 deletions.
2 changes: 1 addition & 1 deletion src/sage/groups/perm_gps/partn_ref/data_structures.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ cdef inline int OP_find(OrbitPartition *OP, int n) noexcept:
OP.parent[n] = OP_find(OP, OP.parent[n])
return OP.parent[n]

cdef inline int OP_join(OrbitPartition *OP, int m, int n) noexcept:
cdef inline void OP_join(OrbitPartition *OP, int m, int n) noexcept:
"""
Join the cells containing m and n, if they are different.
"""
Expand Down
16 changes: 14 additions & 2 deletions src/sage/sets/disjoint_set.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,25 @@
from sage.groups.perm_gps.partn_ref.data_structures cimport OrbitPartition
from sage.structure.sage_object cimport SageObject

cpdef DisjointSet(arg)

cdef class DisjointSet_class(SageObject):
cdef OrbitPartition *_nodes
cpdef cardinality(self)
cpdef number_of_subsets(self)

cdef class DisjointSet_of_integers(DisjointSet_class):
pass
cpdef int find(self, int i)
cpdef void union(self, int i, int j)
cpdef root_to_elements_dict(self)
cpdef element_to_root_dict(self)
cpdef to_digraph(self)

cdef class DisjointSet_of_hashables(DisjointSet_class):
cdef list _int_to_el
cdef dict _el_to_int
cdef DisjointSet_of_integers _d
cpdef find(self, e)
cpdef void union(self, e, f)
cpdef root_to_elements_dict(self)
cpdef element_to_root_dict(self)
cpdef to_digraph(self)
Loading

0 comments on commit ac9c1b0

Please sign in to comment.