5
$\begingroup$

Is there a more efficient way to code the specifying of selected vertices and edges than listing them separately as shown below?

enter image description here

edges = {1 -> 2, 1 -> 3, 1 -> 4, 1 -> 6, 2 -> 3, 2 -> 4, 3 -> 4, 3 -> 6, 4 -> 5, 4 -> 6, 
   4 -> 7, 5 -> 7, 5 -> 8, 5 -> 9, 6 -> 7, 6 -> 9, 7 -> 9, 8 -> 9};
Graph[edges, GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left}, 
 VertexLabels -> Placed["Name", Center], VertexSize -> 0.45, 
 VertexStyle -> {1 | 2 | 3 | 5 | 7 | 8 | 9 -> Lighter[Lighter[Lighter[Lighter[Yellow]]]], 
   4 | 6 -> Lighter[Lighter[Red]]}, 
 EdgeStyle -> {1 \[DirectedEdge] 3 | 1 \[DirectedEdge] 2 | 1 \[DirectedEdge] 4 | 
     2 \[DirectedEdge] 3 | 2 \[DirectedEdge] 4 | 4 \[DirectedEdge] 6 | 
     3 \[DirectedEdge] 6 | 4 \[DirectedEdge] 5 | 4 \[DirectedEdge] 7 | 
     5 \[DirectedEdge] 9 | 5 \[DirectedEdge] 8 | 5 \[DirectedEdge] 7 | 
     8 \[DirectedEdge] 9 | 7 \[DirectedEdge] 9 | 3 \[DirectedEdge] 4 -> Blue, 
   1 \[DirectedEdge] 6 | 6 \[DirectedEdge] 9 -> Red}, 
 VertexLabelStyle -> Directive[Black, Bold, 13]]
$\endgroup$
3
$\begingroup$

You can specify a default color using blank pattern _ -> color or just color first in the list followed by exceptions as follows :

VertexStyle -> {Lighter[Yellow, 1 - (2/3)^4], 4 | 6 -> Lighter[Lighter[Red]]}

EdgeStyle -> {Blue,   1\[DirectedEdge] 6 | 6\[DirectedEdge] 9 -> Red}

alternatively

VertexStyle -> {_ -> Lighter[Yellow, 1 - (2/3)^4],  4 | 6 -> Lighter[Lighter[Red]]}

EdgeStyle -> {_ -> Blue,   1\[DirectedEdge] 6 | 6\[DirectedEdge] 9 -> Red}
$\endgroup$
  • $\begingroup$ thank you very much kglr!! $\endgroup$ – PRG Sep 4 at 1:04
1
$\begingroup$

Yet another alternative is to use MapAt[] to apply Style[] directly to the "special" vertices and edges, and leave the default colors in EdgeStyle/VertexStyle. For instance,

 Graph[MapAt[Style[#, Nest[Lighter, Red, 2]] &, Range[9], {{4}, {6}}],
       MapAt[Style[#, Red] &, edges, Position[edges, (1 -> 6) | (6 -> 9)]],
       EdgeStyle -> Blue, 
       GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left}, 
       VertexLabels -> Placed["Name", Center], VertexLabelStyle -> Directive[Black, Bold, 13],
       VertexSize -> 0.45, VertexStyle -> Nest[Lighter, Yellow, 4]]

colored graph

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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