From fa9a391b40531f1c40e41786ab6ecfc6cfbf404d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Tavares?= Date: Fri, 13 Mar 2020 22:20:39 +0000 Subject: [PATCH] Added support for DiGraph + Fixed bug concerning the doublesweep Also added a couple of very simples tests --- src/sage/graphs/base/static_sparse_graph.pyx | 48 ++++++++++++-------- src/sage/graphs/digraph.py | 1 + 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/sage/graphs/base/static_sparse_graph.pyx b/src/sage/graphs/base/static_sparse_graph.pyx index 4333520ba77..f61cd80794e 100644 --- a/src/sage/graphs/base/static_sparse_graph.pyx +++ b/src/sage/graphs/base/static_sparse_graph.pyx @@ -758,11 +758,11 @@ cdef uint32_t TYY_diameter_C(short_digraph g): cdef uint32_t u, v cdef uint32_t D = 0, V = g.n - cdef uint32_t idx, aux, aux_ecc + cdef uint32_t idx, aux_min, aux_max, aux_ecc - cdef uint32_t *ecc = mem.malloc(V * sizeof(uint32_t)) - for idx in range(V): - ecc[idx] = INT_MAX + cdef int *scc = mem.malloc(V * sizeof(int)) + cdef int nscc = tarjan_strongly_connected_components_C(g, scc) + cdef uint32_t *ecc = mem.malloc(V * sizeof(uint32_t)) cdef bitset_t seen bitset_init(seen, V) @@ -770,21 +770,31 @@ cdef uint32_t TYY_diameter_C(short_digraph g): cdef uint32_t *distance_from = mem.malloc(V * sizeof(uint32_t)) cdef uint32_t *distance_back = mem.malloc(V * sizeof(uint32_t)) + # Initialize distances + for idx in range(V): + ecc[idx] = INT_MAX + # DoubleSweep + v = randint(0, V-1) + simple_BFS(g, v, distance_from, NULL, BFS_order, seen) + u = BFS_order[V-1] + simple_BFS(g, u, distance_back, NULL, BFS_order, seen) + D = distance_back[BFS_order[V-1]] + for v in range(V): - aux = INT_MAX - for idx in range(g.neighbors[v+1]-g.neighbors[v+1]): - u = g.neighbors[v][idx] - aux = min(ecc[u] + 1, aux) - - ecc[v] = min(ecc[v], aux) - - # DoubleSweep - v = randint(0, V-1) - simple_BFS(g, v, distance_from, NULL, BFS_order, seen) - u = BFS_order[V-1] - simple_BFS(g, u, distance_back, NULL, BFS_order, seen) - D = distance_back[BFS_order[V-1]] + for u in range(V): + print(ecc[u]) + + aux_max = 0 + aux_min = INT_MAX + for comp in range(nscc): + for idx in range(g.neighbors[v+1]-g.neighbors[v+1]): + u = g.neighbors[v][idx] + if scc[v] == scc[u]: + aux_min = min(ecc[u] + 1, aux_min) + aux_max = max(aux_max, aux_min) + + ecc[v] = min(ecc[v], aux_max) if ecc[v] <= D: continue @@ -804,6 +814,7 @@ cdef uint32_t TYY_diameter_C(short_digraph g): for u in range(V): ecc[u] = min(ecc[u], distance_back[u] + aux_ecc) + return D def TYY_diameter(G): @@ -818,8 +829,9 @@ def TYY_diameter(G): sage: assert(TYY_diameter(G) == 3) """ from sage.graphs.graph import Graph + from sage.graphs.digraph import DiGraph - if not isinstance(G, Graph): + if not (isinstance(G, Graph) or isinstance(G, DiGraph)): raise ValueError("Not yet") cdef MemoryAllocator mem = MemoryAllocator() diff --git a/src/sage/graphs/digraph.py b/src/sage/graphs/digraph.py index 00d852010d6..51578abad47 100644 --- a/src/sage/graphs/digraph.py +++ b/src/sage/graphs/digraph.py @@ -3674,6 +3674,7 @@ def _singleton_in_branching(): # Aliases to functions defined in other modules from sage.graphs.comparability import is_transitive from sage.graphs.base.static_sparse_graph import tarjan_strongly_connected_components as strongly_connected_components + from sage.graphs.base.static_sparse_graph import TYY_diameter as diameter from sage.graphs.connectivity import is_strongly_connected from sage.graphs.connectivity import strongly_connected_components_digraph from sage.graphs.connectivity import strongly_connected_components_subgraphs