26

I make graphs that have huge width ratio: they are 51706 x 503 pixels in size. How can I tell GraphViz to optimize width ?

Note 1: the graph is in fact a tree with each node having a lot of children. Here is a sample.

Note 2: I think I use dot :)

Note 3: Here is the Ruby code

def graph_node(n, parent=nil, depth=0)
  #print n, " "
  gn = @g.add_node(n.object_id.to_s, :label=>n.to_graphviz, :shape=>"Mrecord")
  if parent
    e = @g.add_edge(parent, gn)
  end
  if n == @current_pos_node
    gn[:color] = "brown3"
    gn[:style] = "filled"
  elsif @s.tree.pv(@current_pos_node).include?(n)
    gn[:color] = "cadetblue"
    gn[:style] = "filled"
  elsif @s.tree.pv(@root).include?(n)
    gn[:color] = "yellow"
    gn[:style] = "filled"
  end
  return if !n.children # or depth == 2
  i = 0
  for c in n.children
    graph_node(c, gn, depth+1)
    i += 1
    #break if i > 2
  end
end

def graph(name="tree", root_node=@current_pos_node)
  @g = GraphViz::new("G")
  #@g['sep'] = "10,100"
  #@g["overlap"] = "compress"
  #@g["rankdir"] = "BT"
  #@g["ratio"] = "0.9"
  @g["size"] = "350,500"
  graph_node(root_node)
  @g.output(:svg => "#{name}.svg")
end
John Bachir
  • 22,495
  • 29
  • 154
  • 227
MickaelFM
  • 897
  • 1
  • 8
  • 19
  • Which layout - dot or neato? Do you happen to have a sample ? – marapet Jun 21 '11 at 12:01
  • dot I think :) [Here is a sample](https://9099831083452856398-a-1802744773732722657-s-sites.googlegroups.com/site/faivrem/home/myteacher/best_node.svg?attachauth=ANoY7cqLGm936eOGjRks95aqfrt-Da0peR4y1OfybiKGH6PN-oJgyA2G239UTtpqbW0Sm_3fgrWbNtKSQBine-dirDQPIJG8uNsCpW1BSUXVOcAP3Cni3Jewgz5mdfV9g7FToe3sA6R1YUGcXscy4w8MTpdB_C_G4O51yBwwwAN4r9bqk4Ph08O2KIoPZIeKcGJqV2guNFCLRuwh5wc9jre0ST7dPxWNXw%3D%3D&attredirects=1) – MickaelFM Jul 02 '11 at 15:58
  • I was thinking of a sample of the code which generates the image (the dot file). But from the picture linked to, `ratio="compress"` together with a `size` should yield a smaller result. Also, did you try an other layout (neato)? The result with neato may be more interesting. – marapet Jul 02 '11 at 16:49
  • I'm using GraphViz through Ruby. I do not have the corresponding GraphViz code. That' why I didn't know if I was really using dot. Now I'm pretty sure I'm using dot. – MickaelFM Jul 03 '11 at 06:32
  • Here is the Ruby code: https://gist.github.com/1062005 – MickaelFM Jul 03 '11 at 06:33

3 Answers3

17

In case the graph consists of several trees which are not connected, you could split them up (as mentioned in Graphviz: break flat but sparsely connected graph into multiple rows?)

Depending on your particular graph, you may obtain a smaller graph when using

ratio="compress"

(You'll have to specify size though)

For detailed optimizations on a specific graph, you may add rank attributes and distribute the nodes manually on different ranks.


Edit:

There is a graphviz tool called unflatten which seems to exist exactly for this purpose :

unflatten is a preprocessor to dot that is used to improve the aspect ratio of graphs having many leaves or disconnected nodes. The usual layout for such a graph is generally very wide or tall. unflatten inserts invisible edges or adjusts the minlen on edges to improve layout compaction.

Never had the need to use it, but I think it's worth a try.

Community
  • 1
  • 1
marapet
  • 54,856
  • 12
  • 170
  • 184
  • It seems to work ([output here](https://9099831083452856398-a-1802744773732722657-s-sites.googlegroups.com/site/faivrem/home/myteacher/compress.svg?attachauth=ANoY7coiJU97n6-GgU0je_wHj1D7OeYs5ITEXaEldXpyAN9ch2uhaF62qAMFZubDjZAt1iS8OVZH_cnRNuJKfx6qgSnGoKp-iG9FjuGYZjJhR5MsxrUy7HwCslcQLn9d7EEAHkGzGvwxP7KJ9ix5crQGByeITYL9tcT6r78aiwF4hfuD-f5uc5W_j1szBxSXsuz4VTRyRHPMntarIruzDicYyCppipJ7NQ%3D%3D&attredirects=0)) but some children nodes are mixed up. Do you know how I could increase the height between parent and children nodes ? – MickaelFM Jul 03 '11 at 06:47
  • The vertical distance between two ranks can be adjusted by setting the `ranksep` attribute. – marapet Jul 03 '11 at 17:12
  • **unflatten** is great. Really worth trying, also the only tool here that helps solve the problem. – fiatjaf Aug 10 '15 at 03:00
  • 1
    @fiatjaf I agree, unflatten is great. Here are some unflatten [examples](http://stackoverflow.com/questions/11888462/graphviz-compress-automatically-generated-graph/11891175#11891175) in [other](http://stackoverflow.com/questions/11134946/nodes-y-various-lines/11136488#11136488) [answers](http://stackoverflow.com/questions/17150667/more-compact-hierarchical-layout-for-dot-graphviz/17157811#17157811) – marapet Aug 10 '15 at 08:10
7

I also had the problem that neato kept lots of space between the boxes. Finally I achieved a reasonable result using following graph settings in addition:

overlap=prism, overlap_scaling=0.01, ratio=0.7

It's especially useful testing different values for overlap_scaling and ratio. It's the quickest to use smaller examples (<50 nodes) and use gvedit.exe to see the effects.

It took me quite a while to identify these settings, which are documented at http://www.graphviz.org/doc/info/attrs.html

CHN
  • 71
  • 1
  • 1
3

You can try to play with the ratio parameter.

Michael Spector
  • 36,723
  • 6
  • 60
  • 88
  • I wasn't able to do it... I tried many parameters. I edited my post with a link to a sample. I'd like to either increase the vertical space between parent and children or either that children's position are alternated, so width is reduced. – MickaelFM Jul 02 '11 at 16:04