Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Graph-colouring/Zkl/graph-colouring-1.zkl
Normal file
19
Task/Graph-colouring/Zkl/graph-colouring-1.zkl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
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)
|
||||
}
|
||||
18
Task/Graph-colouring/Zkl/graph-colouring-2.zkl
Normal file
18
Task/Graph-colouring/Zkl/graph-colouring-2.zkl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
fcn printColoredGraph(graphStr){
|
||||
graph,colors,numEdges := colorGraph(graphStr);
|
||||
nodes:=graph.keys.sort();
|
||||
println("Graph: ",graphStr);
|
||||
println("Node/color: ",
|
||||
nodes.pump(List,'wrap(v){ String(v,"/",colors[v]) }).concat(", "));
|
||||
println("Node : neighbours --> colors:");
|
||||
foreach node in (nodes){
|
||||
ns:=graph[node];
|
||||
println(node," : ",ns.concat(" ")," --> ",
|
||||
colors[node]," : ",ns.apply(colors.get).concat(" "));
|
||||
}
|
||||
println("Number nodes: ",nodes.len());
|
||||
println("Number edges: ",numEdges);
|
||||
println("Number colors: ",
|
||||
colors.values.pump(Dictionary().add.fp1(Void)).len()); // create set, count
|
||||
println();
|
||||
}
|
||||
7
Task/Graph-colouring/Zkl/graph-colouring-3.zkl
Normal file
7
Task/Graph-colouring/Zkl/graph-colouring-3.zkl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
graphs:=T(
|
||||
"0-1 1-2 2-0 3",
|
||||
"1-6 1-7 1-8 2-5 2-7 2-8 3-5 3-6 3-8 4-5 4-6 4-7",
|
||||
"1-4 1-6 1-8 3-2 3-6 3-8 5-2 5-4 5-8 7-2 7-4 7-6",
|
||||
"1-6 7-1 8-1 5-2 2-7 2-8 3-5 6-3 3-8 4-5 4-6 4-7"
|
||||
);
|
||||
graphs.apply2(printColoredGraph);
|
||||
Loading…
Add table
Add a link
Reference in a new issue