How to set the first node green color and the last node red color


As shown in the picture,I may have many paths,every path may have different length

But I need to draw the frist node in the green color and the last node in the red color

How to do that?

my dot file:
color.dot (86 Bytes)

I suggest looking at https://graphviz.org/pdf/dotguide.pdf, at least page 5 (fancy graph).
Also style | Graphviz

One solution:

strict digraph{
  4 [style=filled color=red]
  f [style=filled color=red]
  {
    node [style=filled color=green]
    1 a
  }
  "1" -> "2" ->"3" ->"4";
   "a" -> "b" -> "c" -> "d" -> "e" -> "f" 
}

Giving:

Or are you asking how to identify nodes with no incoming edges (green) or no outgoing edges (red)??

thank you for your answer!

I don’t need to identify nodes.But I need paint to all the no incoming nodes to green color and all the no outgoing nodes to red color.

your code seems that I need to specify the no incoming node is “1”(“a”) and the no outgoing node is “4”(“f”),but I fact the dot file is generated automaticly, I don’t know what is the no incoming node and what is the no incoming node

Here is a (Linux) shell script that takes your automatically-generated file & modifies it to meet your needs:

f=yourFileName.gv
T=png
F=`basename $f .gv`  # assumes .gv, change for .dot
gvpr -c '
N{
 if($.indegree==0&&$.outdegree>0){$.style="filled";$.color="green";}
 if($.indegree>0&&$.outdegree==0){$.style="filled";$.color="red";}
}' $f  |
dot -T$T >$F.$T

GVPR is part of the Graphviz package. It is documented here: https://www.graphviz.org/pdf/gvpr.1.pdf

  • indegree is the count of inbound edges
  • outdegree is the count of outbound edges

Result:

strict digraph {
        1       [color=green,
                style=filled];
        1 -> 2;
        2 -> 3;
        4       [color=red,
                style=filled];
        3 -> 4;
        a       [color=green,
                style=filled];
        a -> b;
        b -> c;
        c -> d;
        d -> e;
        f       [color=red,
                style=filled];
        e -> f;
}

Thank you very much!

I will have a test