如何从graphviz获得平衡图?
graphviz 中是否有设置可以生成这样的平衡图:
正确的图 http://img3 .imageshack.us/img3/6423/testaah.png
当图像下面这样更复杂时 - 它不像上面那样平衡(4 在 ** 下面)。
未正确平衡 http://img51.imageshack.us/img51/6632/test2b.png
生成第二个图的代码:
graph
{
n1 [label="+"];
n1 -- n2;
n2 [label="/"];
n2 -- n3;
n3 [label="*"];
n3 -- n4;
n4 [label="1"];
n3 -- n5;
n5 [label="2"];
n2 -- n6;
n6 [label="3"];
n1 -- n7;
n7 [label="**"];
n7 -- n8;
n8 [label="4"];
n7 -- n9;
n9 [label="5"];
}
Is there a setting in graphviz to generate balanced diagrams like this:
correct diagram http://img3.imageshack.us/img3/6423/testaah.png
When diagram is more complex like below - it isn't balanced like that above (4 is below **).
not correctly balanced http://img51.imageshack.us/img51/6632/test2b.png
Code to generate second diagram:
graph
{
n1 [label="+"];
n1 -- n2;
n2 [label="/"];
n2 -- n3;
n3 [label="*"];
n3 -- n4;
n4 [label="1"];
n3 -- n5;
n5 [label="2"];
n2 -- n6;
n6 [label="3"];
n1 -- n7;
n7 [label="**"];
n7 -- n8;
n8 [label="4"];
n7 -- n9;
n9 [label="5"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以“引入新的、不可见的节点来重新平衡布局”。 (请参阅https://graphviz.org/faq/#FaqBalanceTree)。 So your code becomes :
With this output :
You can "introduce new, invisible nodes to re-balance the layout." (see https://graphviz.org/faq/#FaqBalanceTree). So your code becomes :
With this output :
对于这个特定的示例,您所需要做的就是放松前 3 个边缘之间的边缘的权重:
输出:
在更复杂的情况下,不可见的节点和边以及设置权重变得难以管理,您可以尝试 Emden R. Gansner 的 gvpr 脚本来布局二叉树,如 < href="https://stackoverflow.com/a/10905657/63733">此问题的答案。
For this particular example, all you need is to loosen up the weight of the edges between the top 3 edges:
Output:
In more complex cases where invisible nodes and edges and setting weights gets difficult to manage, you may try Emden R. Gansner's gvpr script to layout binary trees as described in an answer to this question.
@greg 的答案的一个变体,它更加紧凑,并且更少依赖于图中语句的顺序。
优点:
rank=same
一般缺点:
与常见问题解答一样,它仍然依赖于隐藏节点来平衡树。
结果是:
这是我需要绘制树时通常使用的技术。
我不经常使用 gvpr 因为它不在我经常想使用图表的环境中表现不佳(sphinx 、doxygen 等)。
但是,如果您可以使用标准管道来制作图表并且您不关心生成的图表源是什么样子,那么 gvpr 就是您的朋友。
A variation on @greg's answer, that's a bit more compact and less dependent on the order of statements in the graph.
Upsides:
rank=same
at allDownside:
Like the FAQ, it still depends on hidden nodes to balance the tree.
Which results in:
This is the technique I usually use when I need to draw a tree.
I don't often use gvpr because it doesn't play well in environments where I often want to use a graph (sphinx, doxygen, etc.).
But if you can use a standard pipeline to make your graph and you don't care what the resulting graph source looks like, then gvpr is your friend.