Skip to content

Commit

Permalink
Add NrAdjacencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph-Edwards committed Sep 2, 2024
1 parent 4e2bc04 commit f4b45f0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions gap/attr.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions gap/attr.gi
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
29 changes: 29 additions & 0 deletions src/digraphs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions src/digraphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f4b45f0

Please sign in to comment.