Skip to content

Commit 4ec294c

Browse files
dfs: attempt to fix x 2
1 parent 74d9575 commit 4ec294c

File tree

4 files changed

+37
-34
lines changed

4 files changed

+37
-34
lines changed

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ lib_LTLIBRARIES = digraphs.la
2323

2424
pkginclude_HEADERS = src/bitarray.h
2525
pkginclude_HEADERS += src/conditions.h
26+
pkginclude_HEADERS += src/dfs.h
2627
pkginclude_HEADERS += src/digraphs-debug.h
2728
pkginclude_HEADERS += src/digraphs.h
2829
pkginclude_HEADERS += src/homos-graphs.h
@@ -31,7 +32,6 @@ pkginclude_HEADERS += src/cliques.h
3132
pkginclude_HEADERS += src/perms.h
3233
pkginclude_HEADERS += src/planar.h
3334
pkginclude_HEADERS += src/schreier-sims.h
34-
pkginclude_HEADERS += src/dfs.h
3535

3636
if WITH_INCLUDED_BLISS
3737
pkginclude_HEADERS += extern/bliss-0.73/bignum.hh
@@ -49,6 +49,7 @@ if WITH_INCLUDED_BLISS
4949
endif
5050

5151
digraphs_la_SOURCES = src/digraphs.c
52+
digraphs_la_SOURCES += src/dfs.c
5253
digraphs_la_SOURCES += src/bitarray.c
5354
digraphs_la_SOURCES += src/conditions.c
5455
digraphs_la_SOURCES += src/homos.c
@@ -57,7 +58,6 @@ digraphs_la_SOURCES += src/homos-graphs.c
5758
digraphs_la_SOURCES += src/perms.c
5859
digraphs_la_SOURCES += src/planar.c
5960
digraphs_la_SOURCES += src/schreier-sims.c
60-
digraphs_la_SOURCES += src/dfs.c
6161

6262
if WITH_INCLUDED_BLISS
6363
digraphs_la_SOURCES += extern/bliss-0.73/defs.cc

gap/attr.gi

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -684,35 +684,40 @@ end);
684684
InstallMethod(DigraphTopologicalSort, "for a digraph by out-neighbours",
685685
[IsDigraphByOutNeighboursRep],
686686
function(D)
687-
local i, record, num_vertices, data, AncestorFunc, PostOrderFunc;
688-
if DigraphNrVertices(D) = 0 then
687+
local N, record, count, out, PostOrderFunc, AncestorFunc, i;
688+
689+
N := DigraphNrVertices(D);
690+
if N = 0 then
689691
return [];
690692
fi;
691693
record := NewDFSRecord(D);
692-
num_vertices := DigraphNrVertices(D);
693-
data := rec(count := 0,
694-
out := ListWithIdenticalEntries(num_vertices, 0));
694+
count := 0;
695+
out := [];
696+
PostOrderFunc := function(record, data)
697+
count := count + 1;
698+
out[count] := record.child;
699+
end;
695700
AncestorFunc := function(record, data)
696701
if record.current <> record.child then
697702
record.stop := true;
698703
fi;
699704
end;
700-
PostOrderFunc := function(record, data)
701-
data.count := data.count + 1;
702-
data.out[data.count] := record.child;
703-
end;
704705
for i in DigraphVertices(D) do
705-
if not IsBound(record.preorder[i]) then
706+
if IsBound(record.preorder[i]) then
706707
continue;
707708
fi;
708-
ExecuteDFS(record, data, i, DFSDefault,
709-
PostOrderFunc, AncestorFunc,
710-
DFSDefault);
709+
ExecuteDFS(record,
710+
fail,
711+
i,
712+
DFSDefault,
713+
PostOrderFunc,
714+
AncestorFunc,
715+
DFSDefault);
711716
if record.stop then
712717
return fail;
713718
fi;
714719
od;
715-
return data.out;
720+
return out;
716721
end);
717722

718723
InstallMethod(DigraphStronglyConnectedComponents,

gap/oper.gi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,10 +2273,10 @@ function(graph)
22732273
record.child := -1;
22742274
record.current := -1;
22752275
record.stop := false;
2276-
record.parent := [fail];
2277-
record.preorder := [fail];
2278-
record.postorder := [fail];
2279-
record.edge := [fail];
2276+
record.parent := [];
2277+
record.preorder := [];
2278+
record.postorder := [];
2279+
record.edge := [];
22802280
return record;
22812281
end);
22822282

src/dfs.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ Obj ExecuteDFS(Obj self, Obj args) {
6464
Obj preorder = ElmPRec(record, RNamName("preorder"));
6565
Obj edge = ElmPRec(record, RNamName("edge"));
6666

67+
// FIXME edge needs to be off by 1, so that the first entry is bound
68+
6769
DIGRAPHS_ASSERT(LEN_PLIST(parent) == 1);
6870
DIGRAPHS_ASSERT(LEN_PLIST(postorder) == 1);
6971
DIGRAPHS_ASSERT(LEN_PLIST(preorder) == 1);
7072
DIGRAPHS_ASSERT(LEN_PLIST(edge) == 1);
7173

72-
AssPlist(parent, INT_INTOBJ(start), start);
74+
ASS_LIST(parent, INT_INTOBJ(start), start);
7375

7476
Obj neighbors = FuncOutNeighbours(self, D);
7577
DIGRAPHS_ASSERT(IS_PLIST(neighbors));
@@ -89,19 +91,18 @@ Obj ExecuteDFS(Obj self, Obj args) {
8991
AssPRec(record, RNamChild, INTOBJ_INT(child));
9092
AssPRec(record, RNamCurrent, ELM_PLIST(parent, child));
9193
CALL_2ARGS(PostOrderFunc, record, data);
92-
AssPlist(postorder, child, INTOBJ_INT(++postorder_num));
94+
ASS_LIST(postorder, child, INTOBJ_INT(++postorder_num));
9395
CHANGED_BAG(record);
9496
continue;
9597
} else if (current <= LEN_PLIST(preorder)
96-
&& ELM_PLIST(preorder, current) != 0
97-
&& ELM_PLIST(preorder, current) != Fail) {
98+
&& ELM_PLIST(preorder, current) != 0) {
9899
continue;
99100
} else {
100101
AssPRec(record, RNamCurrent, INTOBJ_INT(current));
101102
CALL_2ARGS(PreorderFunc, record, data);
102-
AssPlist(preorder, current, INTOBJ_INT(++preorder_num));
103+
ASS_LIST(preorder, current, INTOBJ_INT(++preorder_num));
103104
CHANGED_BAG(record);
104-
AssPlist(stack, ++top, INTOBJ_INT(-1 * current));
105+
ASS_LIST(stack, ++top, INTOBJ_INT(-1 * current));
105106
}
106107

107108
if (ElmPRec(record, RNamStop) == True) {
@@ -112,15 +113,12 @@ Obj ExecuteDFS(Obj self, Obj args) {
112113
for (UInt j = 0; j < LEN_LIST(succ); ++j) {
113114
UInt v = INT_INTOBJ(ELM_LIST(succ, LEN_LIST(succ) - j));
114115
AssPRec(record, RNamChild, INTOBJ_INT(v));
115-
if (v > LEN_PLIST(preorder) || ELM_PLIST(preorder, v) == 0
116-
|| ELM_PLIST(preorder, v) == Fail) {
117-
AssPlist(parent, v, INTOBJ_INT(current));
118-
AssPlist(edge, v, INTOBJ_INT(LEN_LIST(succ) - j));
116+
if (v > LEN_PLIST(preorder) || ELM_PLIST(preorder, v) == 0) {
117+
ASS_LIST(parent, v, INTOBJ_INT(current));
118+
ASS_LIST(edge, v, INTOBJ_INT(LEN_LIST(succ) - j));
119119
CHANGED_BAG(record);
120-
AssPlist(stack, ++top, INTOBJ_INT(v));
121-
} else if (current > LEN_PLIST(preorder)
122-
|| ELM_PLIST(preorder, current) != 0
123-
|| ELM_PLIST(preorder, current) != Fail) {
120+
ASS_LIST(stack, ++top, INTOBJ_INT(v));
121+
} else if (v > LEN_PLIST(postorder) || ELM_PLIST(postorder, v) == 0) {
124122
CALL_2ARGS(AncestorFunc, record, data);
125123
} else if (INT_INTOBJ(ELM_PLIST(preorder, v))
126124
< INT_INTOBJ(ELM_PLIST(preorder, current))) {

0 commit comments

Comments
 (0)