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

**URL:** <https://forum.graphviz.org/t/how-to-set-the-first-node-green-color-and-the-last-node-red-color/917>\
**Category:** Uncategorized\
**Created:** [November 20, 2021, 1:47am UTC](https://forum.graphviz.org/t/how-to-set-the-first-node-green-color-and-the-last-node-red-color/917 "2021-11-20T01:47:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@lee](https://forum.graphviz.org/u/lee)\
**Post date:** [November 20, 2021, 1:47am UTC](https://forum.graphviz.org/t/how-to-set-the-first-node-green-color-and-the-last-node-red-color/917/1 "2021-11-20T01:47:04Z")

</div>

![image](https://global.discourse-cdn.com/graphviz/original/1X/627f8fef5c2a3740f383266bd99967085767dec6.png)  
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](https://forum.graphviz.org/uploads/short-url/6lJ00XwU0nVUeHoJewXjPTyriT2.dot) (86 Bytes)

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [November 20, 2021, 10:31pm UTC](https://forum.graphviz.org/t/how-to-set-the-first-node-green-color-and-the-last-node-red-color/917/2 "2021-11-20T22:31:22Z")

</div>

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

One solution:

```auto
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:

 ![anotherColor1](https://global.discourse-cdn.com/graphviz/original/1X/8c8630efac4b6208d5f5841fa91121a05366e3c0.png)

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

---

<div class="post-metadata">

**Author:** ![lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@lee](https://forum.graphviz.org/u/lee)\
**Post date:** [November 21, 2021, 12:51am UTC](https://forum.graphviz.org/t/how-to-set-the-first-node-green-color-and-the-last-node-red-color/917/3 "2021-11-21T00:51:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [November 21, 2021, 1:17am UTC](https://forum.graphviz.org/t/how-to-set-the-first-node-green-color-and-the-last-node-red-color/917/4 "2021-11-21T01:17:26Z")

</div>

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

```auto
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](https://www.graphviz.org/pdf/gvpr.1.pdf)

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

Result:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@lee](https://forum.graphviz.org/u/lee)\
**Post date:** [November 21, 2021, 2:05am UTC](https://forum.graphviz.org/t/how-to-set-the-first-node-green-color-and-the-last-node-red-color/917/5 "2021-11-21T02:05:36Z")

</div>

Thank you very much!

I will have a test
