# How do I color edges on a graph using a pattern?

I have the following code, an attempt to color the even branches of a tree... but it’s not working correctly:

CompleteKaryTree[3,EdgeStyle->{_<->_?EvenQ ->Green},EdgeLabels->"Name"]

It’s coloring the 2<->5 edge whilst I thought it would only color the edges which ended in an even number?! What am I doing wrong?

CompleteKaryTree[3, EdgeStyle -> {e_?( EvenQ[#[[2]]]&)-> Green}, EdgeLabels -> "Name"]

or

CompleteKaryTree[3, EdgeStyle -> {e_/;EvenQ[e[[2]]]-> Green}, EdgeLabels -> "Name"]

or

CompleteKaryTree[3, DirectedEdges -> True,
EdgeStyle -> {DirectedEdge[_, _?EvenQ] -> Green},
EdgeLabels -> "Name", EdgeShapeFunction -> "Line"]

or

CompleteKaryTree[3,
EdgeStyle -> {UndirectedEdge[a__] /; EvenQ[Max[{a}]]-> Green}, EdgeLabels -> "Name"]

all give

Note: The last one works with CompleteKaryTree; it doesn't work in general (thanks: Szabolcs).

• Cool... anyway I can add Green for Odd and Red for even? Commented Oct 21, 2019 at 20:46
• @CuriousDudeFromEgypt, try CompleteKaryTree[3, EdgeStyle -> {_ -> Green, e_?(EvenQ[#[[2]]] &) -> Red}, EdgeLabels -> "Name"]
– kglr
Commented Oct 21, 2019 at 20:53
• @Szabolcs, OP wants: "only color the edges which ended in an even number".
– kglr
Commented Oct 22, 2019 at 10:16
• But undirected edges do not have an endpoint, nor is there any guarantee about what ordering they may show up with in a given context. Also, in OP's example 2<->5 is green. @CuriousDudeFromEgypt should clarify. Commented Oct 22, 2019 at 12:38
• @Szabolcs, good point re ordering not working in general. I added a note. Re " in OP's example 2<->5 is green"; that's right, but my reading of the question was that CuriousDude does not want that. I think based on the fact that Cases[EdgeList[CompleteKaryTree[3]],_<->_?EvenQ, All] gives {1<->2,2<->4,3<->6} (similarly, {#,MatchQ[_<->_?EvenQ]@#}&/@EdgeList[CompleteKaryTree[3]] gives {{1<->2,True},{1<->3,False},{2<->4,True},{2<->5,False},{3<->6,True},{3<->7,False}} ) one is lead to expect EdgeStyle->{v1_<-> v2_?EvenQ ->Green} to color an edge Green only when v2 is even.
– kglr
Commented Oct 22, 2019 at 18:10