19 lines
782 B
Text
19 lines
782 B
Text
fcn colorGraph(nodeStr){ // "0-1 1-2 2-0 3"
|
|
numEdges,graph := 0,Dictionary(); // ( 0:(1,2), 1:L(0,2), 2:(1,0), 3:() )
|
|
foreach n in (nodeStr.split(" ")){ // parse string to graph
|
|
n=n - " ";
|
|
if(n.holds("-")){
|
|
a,b := n.split("-"); // keep as string
|
|
graph.appendV(a,b); graph.appendV(b,a);
|
|
numEdges+=1;
|
|
}
|
|
else graph[n]=T; // island
|
|
}
|
|
colors,colorPool := Dictionary(), ["A".."Z"].walk();
|
|
graph.pump(Void,'wrap([(node,nbrs)]){ // ( "1",(0,2), "3",() )
|
|
clrs:=colorPool.copy(); // all colors are available, then remove neighbours
|
|
foreach i in (nbrs){ clrs.remove(colors.find(i)) } // if nbr has color, color not available
|
|
colors[node] = clrs[0]; // first available remaining color
|
|
});
|
|
return(graph,colors,numEdges)
|
|
}
|