This is a split from discussion on earlier question.
Suppose I need to define a function f which checks if given labeling of a graph is a proper coloring. In other words, we have an integer assigned to every node and no two adjacent nodes get the same answer. For instance, for {“Path”,3}, f[{1,2,3}] returns True and f[{1,1,2}] returns False. How would I go about creating such a function for arbitrary graph?
The following does essentially what I need, but generates Part warnings.
g[edges_] := Function @@ {{x}, And @@ (x[[First[#]]] != x[[Last[#]]] & /@ edges)}
f = g[GraphData[{"Path", 3}, "EdgeIndices"]];
f[{1, 2, 1}]==False
This is a toy instance problem I regularly come across — I need to programmatically create a multivariate function f, and end up with either 1) part warning 2) deferring evaluation of g until evaluation of f
Here’s something. When nothing else is working,
Holdand rules can usually get the job done. I’m not sure it produces the correct results w.r.t. your graph-coloring question but hopefully gives you a starting place. I ended up usingSlotinstead of named variable because there were some scoping issues (also present in my previous suggestion,x$vs.x) when I used a named variable that I didn’t spend the time trying to work around.I feel like it lacks typical Mathematica elegance, but it works. I’ll update if I’m inspired with something more beautiful.