2010-06-23 38 views
1

是否有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"]; 
} 

回答

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

+0

謝謝,它看起來不錯。 – user360872 2010-07-04 19:23:30

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

在更復雜的情況下,看不見的節點和邊和設置權變得難以管理,您可以嘗試埃姆登R. Gansner的gvpr腳本在answer to this question描述佈局二叉樹。

0

@greg's answer上的變體,它更緊湊一些,並且對圖表中語句順序的依賴性更小。

上升空間:

  1. 它不使用rank=same在所有
  2. 一般源組織是一個比較「樹狀」,這讓編輯很容易。
  3. 只有2個小的更改才能顯示/隱藏所有節點(如果需要)。
  4. 只要父項和子項全部位於同一行,任何邊的聲明順序都沒有關係。

缺點:

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就是您的朋友。