4
$\begingroup$

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"]

tree graph

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?

$\endgroup$

1 Answer 1

2
$\begingroup$
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

enter image description here

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

$\endgroup$
6
  • $\begingroup$ Cool... anyway I can add Green for Odd and Red for even? $\endgroup$ Commented Oct 21, 2019 at 20:46
  • 1
    $\begingroup$ @CuriousDudeFromEgypt, try CompleteKaryTree[3, EdgeStyle -> {_ -> Green, e_?(EvenQ[#[[2]]] &) -> Red}, EdgeLabels -> "Name"] $\endgroup$
    – kglr
    Commented Oct 21, 2019 at 20:53
  • 1
    $\begingroup$ @Szabolcs, OP wants: "only color the edges which ended in an even number". $\endgroup$
    – kglr
    Commented Oct 22, 2019 at 10:16
  • 1
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Oct 22, 2019 at 12:38
  • 1
    $\begingroup$ @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. $\endgroup$
    – kglr
    Commented Oct 22, 2019 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.