diff --git a/gap/attr.gd b/gap/attr.gd index d0c1732ff..cc011d274 100644 --- a/gap/attr.gd +++ b/gap/attr.gd @@ -14,6 +14,7 @@ DeclareAttribute("DigraphVertices", IsDigraph); DeclareAttribute("DigraphNrVertices", IsDigraph); DeclareAttribute("DigraphEdges", IsDigraph); DeclareAttribute("DigraphNrEdges", IsDigraph); +DeclareAttribute("DigraphNrAdjacencies", IsDigraph); DeclareAttribute("DigraphNrLoops", IsDigraph); DeclareAttribute("DigraphHash", IsDigraph); diff --git a/gap/attr.gi b/gap/attr.gi index c2fde5582..83a0d68c7 100644 --- a/gap/attr.gi +++ b/gap/attr.gi @@ -701,6 +701,13 @@ function(D) return m; end); +InstallMethod(DigraphNrAdjacencies, "for a digraph", [IsDigraphByOutNeighboursRep], +function(D) + local m; + m := DIGRAPH_NRADJACENCIES(D); + return m; +end); + InstallMethod(DigraphNrLoops, "for a digraph by out-neighbours", [IsDigraphByOutNeighboursRep], diff --git a/src/digraphs.c b/src/digraphs.c index 53ea3f390..2a397b7d5 100644 --- a/src/digraphs.c +++ b/src/digraphs.c @@ -138,6 +138,34 @@ static Obj FuncDIGRAPH_NREDGES(Obj self, Obj D) { return INTOBJ_INT(DigraphNrEdges(D)); } +Int DigraphNrAdjacencies(Obj D) { + Int nr = 0; + if (IsbPRec(D, RNamName("DigraphNrAdjacencies"))) { + return INT_INTOBJ(ElmPRec(D, RNamName("DigraphNrAdjacencies"))); + } else { + Obj const out = FuncOutNeighbours(0L, D); + for (Int v = 1; v <= LEN_LIST(out); ++v) { + Obj const out_v = ELM_LIST(out, v); + for (Int w = 1; w <= LEN_LIST(out_v); ++w) { + Int u = INT_INTOBJ(ELM_LIST(out_v, w)); + if (v < u + || CALL_3ARGS(IsDigraphEdge, D, INTOBJ_INT(u), INTOBJ_INT(v)) + == False) { + ++nr; + } + } + } + } + if (IsAttributeStoringRep(D)) { + AssPRec(D, RNamName("DigraphNrAdjacencies"), INTOBJ_INT(nr)); + } + return nr; +} + +static Obj FuncDIGRAPH_NRADJACENCIES(Obj self, Obj D) { + return INTOBJ_INT(DigraphNrAdjacencies(D)); +} + /**************************************************************************** ** *F FuncGABOW_SCC @@ -2113,6 +2141,7 @@ FuncMULTIDIGRAPH_CANONICAL_LABELLING(Obj self, Obj digraph, Obj colours) { static StructGVarFunc GVarFuncs[] = { GVAR_FUNC(DIGRAPH_NREDGES, 1, "digraph"), + GVAR_FUNC(DIGRAPH_NRADJACENCIES, 1, "digraph"), GVAR_FUNC(GABOW_SCC, 1, "adj"), GVAR_FUNC(DIGRAPH_CONNECTED_COMPONENTS, 1, "digraph"), GVAR_FUNC(IS_ACYCLIC_DIGRAPH, 1, "adj"), diff --git a/src/digraphs.h b/src/digraphs.h index f7b1f1ff8..4017a16a3 100644 --- a/src/digraphs.h +++ b/src/digraphs.h @@ -24,6 +24,7 @@ Obj FuncIS_ANTISYMMETRIC_DIGRAPH(Obj self, Obj D); Obj FuncADJACENCY_MATRIX(Obj self, Obj D); Int DigraphNrEdges(Obj digraph); +Int DigraphNrAdjacencies(Obj digraph); Obj DigraphSource(Obj digraph); Obj DigraphRange(Obj digraph);