Some nodes on wrong layer


I am creating some graphs (random edges on each run) with the help of a python library, however I get some graphs with a layout I do not want.

I need a three layered graph, the green triangles in the top row, the orange ellipses in the middle and the blue rectangles on the bottom row.
However, sometimes there are some orange ones in the bottom row, and I can not understand why this is happening or how I can prevent this.

Thanks and kind regards

How some graphs look like:

How they should look like:

Working example: digraph%20{ node%20[height%3D.1] rankdir%3DTB node%20[shape%3Dtriangle] { rank%3Dsame a%20[fillcolor%3D"%23c7f867"%20style%3Dfilled] b%20[fillcolor%3D"%23c7f867"%20style%3Dfilled] } node%20[shape%3Dellipse] { 0%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 1%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 2%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 3%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 4%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 5%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 6%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 7%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 8%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 9%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] } node%20[shape%3Dbox] { rank%3Dsame x1%20[fillcolor%3D"%2360f3ff"%20style%3Dfilled] x2%20[fillcolor%3D"%2360f3ff"%20style%3Dfilled] y1%20[fillcolor%3D"%2360f3ff"%20style%3Dfilled] y2%20[fillcolor%3D"%2360f3ff"%20style%3Dfilled] } a%20->%200 b%20->%200 a%20->%201 b%20->%201 1%20->%202 1%20->%202 1%20->%203 0%20->%203 0%20->%204 3%20->%204 b%20->%205 a%20->%205 4%20->%206 a%20->%206 4%20->%207 0%20->%207 b%20->%208 5%20->%208 3%20->%209 8%20->%209 2%20->%20x1 5%20->%20x2 2%20->%20y1 4%20->%20y2 }

Your Graphviz program is unreadable. Please consult this “PSA” (public service announcement) Center a node above a group of other nodes - #4 by smattr and “fix” the readability of the program. Thanks

Thanks for your answer, it’s a clickable link to graphviz online. I am not sure if I can shorten links in a post?

There’s some option to put nodes on the highest possible level (at least if there is slack in the assignment) if someone can find it or if it was ever documented :slight_smile:

In your third subgraph with the box nodes, change the statement:




and you will get the effect you seek.

[fyi, layers means something else to Graphviz (FAQ | Graphviz), but we’ll ignore that]

Dot tries to minimize the number of ranks in a graph. While rank=same forces a set of nodes to appear on the same rank, it does not prevent dot from placing other nodes on that same rank. You created a set of nodes, but not an exclusive set of nodes.
Since you are setting the top and bottom “layers” to be single ranks, all you need to do is set the top layer to rank=source and the bottom layer to rank=sink (rank | Graphviz) This will make the sets exclusive.
Like so:

digraph {
	node [height=.1]
	node [shape=triangle]
		rank=source  // changed
		a [fillcolor="#c7f867" style=filled]
		b [fillcolor="#c7f867" style=filled]
	node [shape=ellipse]
		0 [fillcolor="#ff8945" style=filled]
		1 [fillcolor="#ff8945" style=filled]
		2 [fillcolor="#ff8945" style=filled]
		3 [fillcolor="#ff8945" style=filled]
		4 [fillcolor="#ff8945" style=filled]
		5 [fillcolor="#ff8945" style=filled]
		6 [fillcolor="#ff8945" style=filled]
		7 [fillcolor="#ff8945" style=filled]
		8 [fillcolor="#ff8945" style=filled]
		9 [fillcolor="#ff8945" style=filled]
	node [shape=box]
		rank=sink   // changed
		x1 [fillcolor="#60f3ff" style=filled]
		x2 [fillcolor="#60f3ff" style=filled]
		y1 [fillcolor="#60f3ff" style=filled]
		y2 [fillcolor="#60f3ff" style=filled]
	a -> 0
	b -> 0
	a -> 1
	b -> 1
	1 -> 2
	1 -> 2
	1 -> 3
	0 -> 3
	0 -> 4
	3 -> 4
	b -> 5
	a -> 5
	4 -> 6
	a -> 6
	4 -> 7
	0 -> 7
	b -> 8
	5 -> 8
	3 -> 9
	8 -> 9
	2 -> x1
	5 -> x2
	2 -> y1
	4 -> y2


Thanks, using “sink” and “source” solved my issue. :slight_smile:

A note in the docs which says that those make the nodes exclusive would be nice for beginners like me :slight_smile: