如何从graphviz获得平衡图表?

时间:2010-06-23 22:06:29

标签: graphviz

graphviz中是否有设置生成如下平衡图:

correct diagram http://img3.imageshack.us/img3/6423/testaah.png

当图表比下面更复杂时 - 它不像上面那样平衡(4低于**)。

not correctly balanced 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"];
}

3 个答案:

答案 0 :(得分:2)

您可以“引入新的不可见节点来重新平衡布局。” (见http://www.graphviz.org/content/FaqBalanceTree)。所以你的代码变成了:

graph
{
  n1 [label="+"];
  n2 [label="/"];
  n1 -- n2;
  n1b1 [label="", width=.1, style=invis]
  n1 -- n1b1 [style=invis]  
  n1b2 [label="", width=.1, style=invis]
  n1 -- n1b2 [style=invis]  
  n1b3 [label="", width=.1, style=invis]
  n1 -- n1b3 [style=invis]  
  n7 [label="**"];
  n1 -- n7;
  { rank=same n2 -- n1b1 -- n1b2 -- n1b3 -- n7 [style=invis] }

  n3 [label="*"];
  n2 -- n3;
  n2b1 [label="", width=.1, style=invis]
  n2 -- n2b1 [style=invis]
  n6 [label="3"];
  n2 -- n6;
  { rank=same n3 -- n2b1 -- n6 [style=invis] }

  n8 [label="4"];
  n7 -- n8;
  n7b1 [label="", width=.1, style=invis]
  n7 -- n7b1 [style=invis]
  n9 [label="5"];
  n7 -- n9;
  { rank=same n8 -- n7b1 -- n9 [style=invis] }

  n3 -- n4;
  n4 [label="1"];
  n3 -- n5;
  n5 [label="2"];
}

使用此输出:

alt text

答案 1 :(得分:1)

对于这个特殊的例子,你只需要放松前3个边缘之间边缘的重量

graph g{    
  n1 [label="+"];
  n1 -- n2 [weight=0];
  n2 [label="/"];
  n2 -- n3;
  n3 [label="*"];
  n3 -- n4;
  n4 [label="1"];
  n3 -- n5;
  n5 [label="2"];
  n2 -- n6;
  n6 [label="3"];
  n1 -- n7 [weight=0];
  n7 [label="**"];
  n7 -- n8;
  n8 [label="4"];
  n7 -- n9;
  n9 [label="5"];
}

输出:

graphviz output

在更复杂的情况下,隐形节点和边缘以及设置权重难以管理,您可以尝试使用Emden R. Gansner的 gvpr 脚本来布置二叉树,如answer to this question中所述。

答案 2 :(得分:0)

@greg's answer的变体,它更紧凑,更少依赖图中语句的顺序。

<强>上升空间:

  1. 根本不使用rank=same
  2. 通常,源组织更像“树状”,这使编辑变得容易。
  3. 如果需要,只显示/隐藏所有节点的2次微小更改。
  4. 只要父和子都在同一行上,声明任何边的顺序都无关紧要。
  5. <强>缺点:

    FAQ一样,它仍然依赖于隐藏的节点来平衡树。

    graph calc {
      graph[nodesep=0.25, ranksep=0.3, splines=line];
      node [fontname = "Bitstream Vera Sans", fontsize=14,
            style=filled, fillcolor=lightblue,
            shape=circle, fixedsize=true, width=0.3];
    
      // layout all nodes 1 row at a time
      // order matters on each line, but not the order of lines
      "+";
      "/", am, "**";
    
      // or can be more spread out if you need to . . .
      n1 [label="1"];
      dm;
      n2 [label="2"];
      "*", bm, "3", "4", cm, "5";
    
      // make 'mid' nodes invisible
      am, bm, cm, dm [style=dotted, label=""];
    
      // layout all visible edges as parent -> left_child, right_child
      "+" -- "/","**";
      "/" -- "*","3"
      "**"-- "4","5";
      "*" -- n1, n2;
    
      // link mid nodes with a larger weight:
      edge [style=dotted, weight=10];
      "+" -- am;
      "/" -- bm;
      "**"-- cm;
      "*" -- dm;
    }
    

    结果是:

    balanced tree graph

    这是我在需要绘制树时通常使用的技术。

    我不经常使用gvpr,因为它在我经常要使用图表(sphinxdoxygen等)的环境中效果不佳。 但是如果你可以使用标准管道来制作你的图形并且你不关心生成的图形源是什么样的,那么gvpr就是你的朋友。

相关问题