Wouter Beek
Wouter Beek

Reputation: 3407

How to represent node weight in GraphViz?

In GraphViz, the width of edges is represented by the weight attribute. In some graphs the nodes can have a weight as well, e.g., in my case the weight of a nodes indicates the number of internal edges after an aggregation step.

Unfortunately, there is no weight attribute for nodes. Nodes do have a width attribute, but that is for display purposes only (having a fixed semantics in inches).

Given the above limitations, what is the best way to represent node weights in GraphViz / the DOT language?

Upvotes: 4

Views: 6312

Answers (1)

Diomidis Spinellis
Diomidis Spinellis

Reputation: 19345

Add a weight attribute to the nodes. You can then process the graph with gvpr based on weight. As an example, consider the following graph.

digraph weighted {
    a [weight = 5];
    b [weight = 2];
    c [weight = 12];
    d [weight = 7];

    a -> b -> c -> d;
}

You can color its nodes by processing it with gvpr with a script, such as the following.

N [weight >= 5 ] {color="red"}
N [weight < 5] { color = "blue"}

Processing the graph with this script and then passing the result to dot with a command such as the following

gvpr -c -f t.gvpr foo.dot | dot -Tpng -ocolored.png

will generate the following output.

enter image description here

Upvotes: 2

Related Questions