Skip to content

Commit

Permalink
dfs: attempt to fix x 2
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-mitchell committed Sep 15, 2021
1 parent 74d9575 commit 4ec294c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lib_LTLIBRARIES = digraphs.la

pkginclude_HEADERS = src/bitarray.h
pkginclude_HEADERS += src/conditions.h
pkginclude_HEADERS += src/dfs.h
pkginclude_HEADERS += src/digraphs-debug.h
pkginclude_HEADERS += src/digraphs.h
pkginclude_HEADERS += src/homos-graphs.h
Expand All @@ -31,7 +32,6 @@ pkginclude_HEADERS += src/cliques.h
pkginclude_HEADERS += src/perms.h
pkginclude_HEADERS += src/planar.h
pkginclude_HEADERS += src/schreier-sims.h
pkginclude_HEADERS += src/dfs.h

if WITH_INCLUDED_BLISS
pkginclude_HEADERS += extern/bliss-0.73/bignum.hh
Expand All @@ -49,6 +49,7 @@ if WITH_INCLUDED_BLISS
endif

digraphs_la_SOURCES = src/digraphs.c
digraphs_la_SOURCES += src/dfs.c
digraphs_la_SOURCES += src/bitarray.c
digraphs_la_SOURCES += src/conditions.c
digraphs_la_SOURCES += src/homos.c
Expand All @@ -57,7 +58,6 @@ digraphs_la_SOURCES += src/homos-graphs.c
digraphs_la_SOURCES += src/perms.c
digraphs_la_SOURCES += src/planar.c
digraphs_la_SOURCES += src/schreier-sims.c
digraphs_la_SOURCES += src/dfs.c

if WITH_INCLUDED_BLISS
digraphs_la_SOURCES += extern/bliss-0.73/defs.cc
Expand Down
33 changes: 19 additions & 14 deletions gap/attr.gi
Original file line number Diff line number Diff line change
Expand Up @@ -684,35 +684,40 @@ end);
InstallMethod(DigraphTopologicalSort, "for a digraph by out-neighbours",
[IsDigraphByOutNeighboursRep],
function(D)
local i, record, num_vertices, data, AncestorFunc, PostOrderFunc;
if DigraphNrVertices(D) = 0 then
local N, record, count, out, PostOrderFunc, AncestorFunc, i;

N := DigraphNrVertices(D);
if N = 0 then
return [];
fi;
record := NewDFSRecord(D);
num_vertices := DigraphNrVertices(D);
data := rec(count := 0,
out := ListWithIdenticalEntries(num_vertices, 0));
count := 0;
out := [];
PostOrderFunc := function(record, data)
count := count + 1;
out[count] := record.child;
end;
AncestorFunc := function(record, data)
if record.current <> record.child then
record.stop := true;
fi;
end;
PostOrderFunc := function(record, data)
data.count := data.count + 1;
data.out[data.count] := record.child;
end;
for i in DigraphVertices(D) do
if not IsBound(record.preorder[i]) then
if IsBound(record.preorder[i]) then
continue;
fi;
ExecuteDFS(record, data, i, DFSDefault,
PostOrderFunc, AncestorFunc,
DFSDefault);
ExecuteDFS(record,
fail,
i,
DFSDefault,
PostOrderFunc,
AncestorFunc,
DFSDefault);
if record.stop then
return fail;
fi;
od;
return data.out;
return out;
end);

InstallMethod(DigraphStronglyConnectedComponents,
Expand Down
8 changes: 4 additions & 4 deletions gap/oper.gi
Original file line number Diff line number Diff line change
Expand Up @@ -2273,10 +2273,10 @@ function(graph)
record.child := -1;
record.current := -1;
record.stop := false;
record.parent := [fail];
record.preorder := [fail];
record.postorder := [fail];
record.edge := [fail];
record.parent := [];
record.preorder := [];
record.postorder := [];
record.edge := [];
return record;
end);

Expand Down
26 changes: 12 additions & 14 deletions src/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ Obj ExecuteDFS(Obj self, Obj args) {
Obj preorder = ElmPRec(record, RNamName("preorder"));
Obj edge = ElmPRec(record, RNamName("edge"));

// FIXME edge needs to be off by 1, so that the first entry is bound

DIGRAPHS_ASSERT(LEN_PLIST(parent) == 1);
DIGRAPHS_ASSERT(LEN_PLIST(postorder) == 1);
DIGRAPHS_ASSERT(LEN_PLIST(preorder) == 1);
DIGRAPHS_ASSERT(LEN_PLIST(edge) == 1);

AssPlist(parent, INT_INTOBJ(start), start);
ASS_LIST(parent, INT_INTOBJ(start), start);

Obj neighbors = FuncOutNeighbours(self, D);
DIGRAPHS_ASSERT(IS_PLIST(neighbors));
Expand All @@ -89,19 +91,18 @@ Obj ExecuteDFS(Obj self, Obj args) {
AssPRec(record, RNamChild, INTOBJ_INT(child));
AssPRec(record, RNamCurrent, ELM_PLIST(parent, child));
CALL_2ARGS(PostOrderFunc, record, data);
AssPlist(postorder, child, INTOBJ_INT(++postorder_num));
ASS_LIST(postorder, child, INTOBJ_INT(++postorder_num));
CHANGED_BAG(record);
continue;
} else if (current <= LEN_PLIST(preorder)
&& ELM_PLIST(preorder, current) != 0
&& ELM_PLIST(preorder, current) != Fail) {
&& ELM_PLIST(preorder, current) != 0) {
continue;
} else {
AssPRec(record, RNamCurrent, INTOBJ_INT(current));
CALL_2ARGS(PreorderFunc, record, data);
AssPlist(preorder, current, INTOBJ_INT(++preorder_num));
ASS_LIST(preorder, current, INTOBJ_INT(++preorder_num));
CHANGED_BAG(record);
AssPlist(stack, ++top, INTOBJ_INT(-1 * current));
ASS_LIST(stack, ++top, INTOBJ_INT(-1 * current));
}

if (ElmPRec(record, RNamStop) == True) {
Expand All @@ -112,15 +113,12 @@ Obj ExecuteDFS(Obj self, Obj args) {
for (UInt j = 0; j < LEN_LIST(succ); ++j) {
UInt v = INT_INTOBJ(ELM_LIST(succ, LEN_LIST(succ) - j));
AssPRec(record, RNamChild, INTOBJ_INT(v));
if (v > LEN_PLIST(preorder) || ELM_PLIST(preorder, v) == 0
|| ELM_PLIST(preorder, v) == Fail) {
AssPlist(parent, v, INTOBJ_INT(current));
AssPlist(edge, v, INTOBJ_INT(LEN_LIST(succ) - j));
if (v > LEN_PLIST(preorder) || ELM_PLIST(preorder, v) == 0) {
ASS_LIST(parent, v, INTOBJ_INT(current));
ASS_LIST(edge, v, INTOBJ_INT(LEN_LIST(succ) - j));
CHANGED_BAG(record);
AssPlist(stack, ++top, INTOBJ_INT(v));
} else if (current > LEN_PLIST(preorder)
|| ELM_PLIST(preorder, current) != 0
|| ELM_PLIST(preorder, current) != Fail) {
ASS_LIST(stack, ++top, INTOBJ_INT(v));
} else if (v > LEN_PLIST(postorder) || ELM_PLIST(postorder, v) == 0) {
CALL_2ARGS(AncestorFunc, record, data);
} else if (INT_INTOBJ(ELM_PLIST(preorder, v))
< INT_INTOBJ(ELM_PLIST(preorder, current))) {
Expand Down

0 comments on commit 4ec294c

Please sign in to comment.