Skip to content

Commit

Permalink
Trac #31129: Improve Depth First Search in c_graph.pyx
Browse files Browse the repository at this point in the history
This ticket simply applies the improvements of #31117 to depth first
search as well.

Before:

{{{
sage: def comp():
....:     for n in [5, 10, 50, 100, 500, 1000]:
....:         G = graphs.Grid2dGraph(n, n)
....:         print(G)
....:         %timeit _ = list(G.depth_first_search(start=(0, 0)))
....:
sage: comp()
2D Grid Graph for [5, 5]
8.11 µs ± 115 ns per loop (mean ± std. dev. of 7 runs, 100000 loops
each)
2D Grid Graph for [10, 10]
26.9 µs ± 64.3 ns per loop (mean ± std. dev. of 7 runs, 10000 loops
each)
2D Grid Graph for [50, 50]
827 µs ± 1.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2D Grid Graph for [100, 100]
3.35 ms ± 5.38 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2D Grid Graph for [500, 500]
115 ms ± 218 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
2D Grid Graph for [1000, 1000]
470 ms ± 882 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
}}}

After:
{{{
sage: def comp():
....:     for n in [5, 10, 50, 100, 500, 1000]:
....:         G = graphs.Grid2dGraph(n, n)
....:         print(G)
....:         %timeit _ = list(G.depth_first_search(start=(0, 0)))
....:
sage: comp()
2D Grid Graph for [5, 5]
4.41 µs ± 12.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops
each)
2D Grid Graph for [10, 10]
13.2 µs ± 27.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops
each)
2D Grid Graph for [50, 50]
409 µs ± 225 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2D Grid Graph for [100, 100]
1.67 ms ± 7.46 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2D Grid Graph for [500, 500]
65.8 ms ± 54.2 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
2D Grid Graph for [1000, 1000]
264 ms ± 331 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
}}}

URL: https://trac.sagemath.org/31129
Reported by: gh-kliem
Ticket author(s): Jonathan Kliem
Reviewer(s): David Coudert
  • Loading branch information
Release Manager committed Jan 3, 2021
2 parents f69f8ca + 78d5536 commit a623e05
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/sage/graphs/base/c_graph.pyx
Expand Up @@ -47,6 +47,7 @@ from sage.data_structures.bitset_base cimport *
from sage.rings.integer cimport Integer
from sage.arith.long cimport pyobject_to_long
from libcpp.queue cimport priority_queue, queue
from libcpp.stack cimport stack
from libcpp.pair cimport pair
from sage.rings.integer_ring import ZZ
from cysignals.memory cimport check_allocarray, sig_free
Expand Down Expand Up @@ -4715,7 +4716,7 @@ cdef class Search_iterator:

cdef CGraphBackend graph
cdef int direction
cdef list stack
cdef stack[int] lifo
cdef queue[int] fifo
cdef int n
cdef bitset_t seen
Expand Down Expand Up @@ -4796,7 +4797,7 @@ cdef class Search_iterator:
self.fifo.push(v_id)
bitset_add(self.seen, v_id)
else:
self.stack = [v_id]
self.lifo.push(v_id)

if not self.graph._directed:
ignore_direction = False
Expand Down Expand Up @@ -4867,7 +4868,7 @@ cdef class Search_iterator:

return value

def next_depth_first_search(self):
cdef inline next_depth_first_search(self):
r"""
Return the next vertex in a depth first search traversal of a graph.
Expand All @@ -4880,20 +4881,30 @@ cdef class Search_iterator:
0
"""
cdef int v_int
cdef int w_int
cdef int l
cdef CGraph cg = self.graph.cg()

while self.stack:
v_int = self.stack.pop()
while not self.lifo.empty():
v_int = self.lifo.top()
self.lifo.pop()

if bitset_not_in(self.seen, v_int):
value = self.graph.vertex_label(v_int)
bitset_add(self.seen, v_int)

if self.test_out:
self.stack.extend(self.graph.cg().out_neighbors(v_int))
w_int = cg.next_out_neighbor_unsafe(v_int, -1, &l)
while w_int != -1:
self.lifo.push(w_int)
w_int = cg.next_out_neighbor_unsafe(v_int, w_int, &l)
if self.test_in:
self.stack.extend(self.in_neighbors(v_int))

w_int = cg.next_in_neighbor_unsafe(v_int, -1, &l)
while w_int != -1:
self.lifo.push(w_int)
w_int = cg.next_in_neighbor_unsafe(v_int, w_int, &l)
break

else:
raise StopIteration

Expand Down

0 comments on commit a623e05

Please sign in to comment.