Skip to content

Commit

Permalink
Prevent stack overflow in graph colouring
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed Mar 20, 2016
1 parent f5d2be9 commit 5fc7100
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
37 changes: 14 additions & 23 deletions Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -1153,13 +1153,14 @@ algorithm
colored = arrayCreate(sizeN,0);
arraysparseGraph = listArray(sparseGraph);
if debug then SimCodeFunctionUtil.execStat("generateSparsePattern -> coloring start "); end if;
colored1 = Graph.partialDistance2colorInt(sparseGraphT, forbiddenColor, nodesList, arraysparseGraph, colored);
Graph.partialDistance2colorInt(sparseGraphT, forbiddenColor, nodesList, arraysparseGraph, colored);
if debug then SimCodeFunctionUtil.execStat("generateSparsePattern -> coloring end "); end if;
// get max color used
maxColor = Array.fold(colored1, intMax, 0);
maxColor = Array.fold(colored, intMax, 0);

// map index of that array into colors
coloredArray = mapIndexColors(colored1, arrayLength(diffCompRefs), arrayCreate(maxColor, {}));
coloredArray = arrayCreate(maxColor, {});
mapIndexColors(colored, arrayLength(diffCompRefs), coloredArray);

if Flags.isSet(Flags.DUMP_SPARSE) then
print("Print Coloring Cols: \n");
Expand Down Expand Up @@ -1410,27 +1411,17 @@ protected function mapIndexColors
input array<Integer> inColors;
input Integer inMaxIndex;
input array<list<Integer>> inArray;
output array<list<Integer>> outColors;
protected
Integer index;
algorithm
outColors := matchcontinue(inColors, inMaxIndex, inArray)
local
Integer i, index;
list<Integer> lst;
case (_, 0, _) then inArray;
case (_, i, _)
equation
index = arrayGet(inColors, i);
lst = arrayGet(inArray, index);
lst = i::lst;
arrayUpdate(inArray, index, lst);
then
mapIndexColors(inColors, i-1, inArray);
else
equation
Error.addInternalError("function mapIndexColors failed", sourceInfo());
then
fail();
end matchcontinue;
try
for i in 1:inMaxIndex loop
index := arrayGet(inColors, i);
arrayUpdate(inArray, index, i::arrayGet(inArray, index));
end for;
else
Error.addInternalError("function mapIndexColors failed", sourceInfo());
end try;
end mapIndexColors;

protected function createBipartiteGraph
Expand Down
38 changes: 16 additions & 22 deletions Compiler/Util/Graph.mo
Expand Up @@ -953,29 +953,23 @@ color[ui ] <- min{c > 0 : forbiddenColors[c] = ui }
input list<Integer> inColors;
input array<tuple<Integer, list<Integer>>> inGraph;
input array<Integer> inColored;
output array<Integer> outColored;
protected
Integer node, color;
list<Integer> nodes;
array<Option<list<Integer>>> forbiddenColor;
Integer color;
list<tuple<Integer, list<Integer>>> restGraph;
algorithm
outColored := matchcontinue(inGraphT, inforbiddenColor, inColors, inGraph, inColored)
local
Integer node;
list<Integer> nodes;
array<Option<list<Integer>>> forbiddenColor;
array<Integer> colored;
Integer color;
list<tuple<Integer, list<Integer>>> restGraph;
case ({},_,_,_,_) then inColored;
case (((node,nodes))::restGraph, _, _, _, _)
equation
addForbiddenColorsInt(node, nodes, inColored, inforbiddenColor, inGraph);
color = arrayFindMinColorIndexInt(inforbiddenColor, node, 1);
colored = arrayUpdate(inColored, node, color);
then
partialDistance2colorInt(restGraph, inforbiddenColor, inColors, inGraph, colored);
else
equation
Error.addSourceMessage(Error.INTERNAL_ERROR, {"Graph.partialDistance2colorInt failed."}, sourceInfo());
then fail();
end matchcontinue;
try
for tpl in inGraphT loop
(node,nodes) := tpl;
addForbiddenColorsInt(node, nodes, inColored, inforbiddenColor, inGraph);
color := arrayFindMinColorIndexInt(inforbiddenColor, node, 1);
arrayUpdate(inColored, node, color);
end for;
else
Error.addSourceMessage(Error.INTERNAL_ERROR, {"Graph.partialDistance2colorInt failed."}, sourceInfo());
end try;
end partialDistance2colorInt;

protected function addForbiddenColorsInt
Expand Down

0 comments on commit 5fc7100

Please sign in to comment.