Milk problem - a graphs solution

GONZALO GARCÍA ALARCÓN ESTRADA,  **********
Nov / 16 / 2017
University of Toronto, Shameless Mathematica

Milk problem:
A milkman has an 8 liter jar, a 5 liter jar and 3 liter jar. The 8 liter jar is full of milk. Can the milkman measure out 4 liters? He has no other way to measure besides using these jars.

Challenge:
Draw the state graph of this problem (no spilling allowed!)

S = Solve[(x+y+z==8)&&(8≥ x≥ 0)∧(5≥ y≥0)∧(3≥ z≥ 0),{x,y,z},Integers];

States = {x,y,z}/.S

(*compare .... -1 out, 0 equal, +1 in*)
lzg[x_,y_]:=If[x>y,-1,
If[x==y,0,
If[x<y,+1]]]

(* now for sets.... actually lists *)
lzgSet[A_,B_]:=Map[(lzg[A[[#]],B[[#]]])&,Range[Min[Length[A],Length[B]]]]

(* RELATION A->B i.e. B is pourable from A *)
Pourable[A_,B_]:=Module[{change,full={8,5,3}},
change = lzgSet[A,B];
If[MemberQ[change,0],
If[(Length[Position[change,-1]]==1)∧(Length[Position[change,+1]]==1),
out= Position[change,-1][[1]][[1]];in=Position[change,+1][[1]][[1]];
If[B[[out]]==0∨B[[in]]==full[[in]],True,False],False],False]]

(*tests*)
a={5,3,0};b={3,5,0};

Pourable[a,b]

Pourable[b,a]

(* apply relation to states *)
SxS=Pair@@@Tuples[States,2];
SxS2 = SxS/.Pair[i_,j_]:> i\[DirectedEdge]j/;Pourable[i,j];
SxS3 = Select[SxS2,Head[#]==DirectedEdge&]

(* ----- PREPARATION FOR GRAPH ------ *)

ver = P@@@States;

edg = SxS3/.a_\[DirectedEdge]b_:> P@@a\[DirectedEdge]P@@b;

Start = {P[8,0,0]};

Obj = Select[ver,MemberQ[List@@#,4]&]

G=Graph[ver,edg,VertexLabels->"Name",ImageSize ->900,EdgeShapeFunction->GraphElementData["ShortCarvedArrow","ArrowSize"->0.02]];

HighlightGraph[G,{Style[Start,Orange],Style[Obj,Green]}]

(* find reachable objectives *)
reachPaths =Map[FindShortestPath[G,P[8,0,0],#]&,Obj];

NotObj = Select[Obj,!MemberQ[Flatten[reachPaths],#]&]
YesObj=Complement[Obj,NotObj]

HighlightGraph[G,{Style[Start,Blue],Style[YesObj,Green],Style[NotObj,LightGreen]}⋃Map[PathGraph[#,DirectedEdges->True]&,reachPaths]]