Hamza Yerlikaya
Hamza Yerlikaya

Reputation: 49339

Preventing graphviz from rearranging nodes

I am trying to visualize a tree structure using graphviz, problem is as the graph gets bigger graphviz starts to rearrange the order of the nodes. Say I have the following,

  A
/ | \
B C D

it becomes,

  A
/ | \
B D C

it probably does this to save space but in my context order of the nodes matter I have tried adding,

graph [ordering="out"];

but it did not change the output.

EDIT:

digraph bt {
graph [ordering="out"];
node [style="rounded", shape=box]
N_2386 -> N_2387
N_2387 -> N_2388
N_2388 -> N_2389
N_2388 -> N_2390
N_2387 -> N_2391
N_2386 -> N_2392
subgraph cluster_2393 {
labeljust = "l";
style=dashed;color="#B0B0B0"
N_2392 -> N_2394
N_2394 -> N_2395
N_2395 -> N_2396
N_2396 -> N_2397
N_2397 -> N_2398
N_2397 -> N_2399
N_2396 -> N_2400
N_2400 -> N_2401
N_2400 -> N_2402
N_2395 -> N_2403
N_2403 -> N_2404
N_2404 -> N_2405
N_2405 -> N_2406
N_2403 -> N_2407
N_2407 -> N_2408
N_2408 -> N_2409
N_2409 -> N_2410
N_2410 -> N_2411
N_2411 -> N_2412
N_2412 -> N_2413
N_2412 -> N_2414
N_2412 -> N_2415
N_2411 -> N_2416
N_2416 -> N_2417
N_2416 -> N_2418
N_2416 -> N_2419
N_2408 -> N_2420
N_2408 -> N_2421
N_2403 -> N_2422
N_2395 -> N_2423
N_2392 -> N_2424
}
}

graph in question What I need is, N_2387 should be on the right N_2392 should be on the left. Which is the order I insert them.

Upvotes: 29

Views: 15875

Answers (5)

Ivan
Ivan

Reputation: 41

I sometimes have an issue like this with manually created files where there are links between the B, C and D type features and they get rearranged.

A solution to this in my case, which may be useful to you is to do

B -> C [weight=10;style=invis]
C -> D [weight=10;style=invis]

You may not need to use weights indicating importance of the connection.

Generally, this should prefer the order B C D (or D C B) over others, and should prevent muddling. I don't remember ever having the sequence reversed.

enter image description here

Recently I had a problem where version 4.7.5 was showing before version 4.7.4 in an image I drew (showing the versions and changes in some software).

I solved this by adding the style=invis line below.

"4.7.3" -> fix121_on_4_7 
fix121_on_4_7 -> "4.7.4"
"4.7.4" -> fix132 [style=invis]
"4.7.3" -> fix132 
fix132 -> "4.7.5" 

enter image description here

Upvotes: 0

Pekka
Pekka

Reputation: 3654

Graphviz is designed to generate graphs with pleasing appearance. Trying to force layout defeats much of the value of the tool.

As noted, the invisible edges can be used to enforce some layout, and lexicographic ordering (earlier nodes tend to be placed more left) can also indicate a preference.

In general, adding more constraints to a layout produces perverse layouts that are incredibly difficult to troubleshoot.

Some deviation from an expected layout should not automatically be cause to try forcing the result. Fewer constraints makes generated graphs remain good looking as they change over time, even if they can radically change in appearance.

Upvotes: -1

Andy Dent
Andy Dent

Reputation: 17981

If you want to control ordering of specific items use an invisible edge between them. Combining this with the rank directive gives you a lot of control.

eg: here's a sample tagcloud layout from Graphviz:

/*
Using a graph and relationships just to push things onto different lines.
Two layout rules:
1) all items on a given line go into a "rank=same" phrase 
2) a relationship is needed between the first word on each line and the next line down to
   force the vertical alignment.
*/
digraph {
    edge[style=invisible]
    node[shape=none]

    fred [fontsize=18]
    harry [fontsize=8]
    jack [fontsize=12]
    sally [fontsize=12]
    mika
    amy
    jan
    jack -> fred
    fred -> mika
    {rank=same;fred;harry}
    {rank=same;mika amy; jan}
}

Upvotes: 6

dgw
dgw

Reputation: 13666

If you change the first branch to a subgraph the two subgraphs will be ordered as written in the file.

It seems that the subgraph as a higher precedence as a normal node and therefor the ordering=out seems not to be honored.

This works:

digraph bt {
  graph [ordering="out"];
  node [style="rounded", shape=box]
  N_2386 -> N_2387
  subgraph cluster_first {
    N_2387 -> N_2388
    N_2388 -> N_2389
    N_2388 -> N_2390
    N_2387 -> N_2391
  }
  N_2386 -> N_2392
  subgraph cluster_2393 {
    labeljust = "l";
    style=dashed;color="#B0B0B0"
    N_2392 -> N_2394
    N_2394 -> N_2395
    N_2395 -> N_2396
    N_2396 -> N_2397
    N_2397 -> N_2398
    N_2397 -> N_2399
    N_2396 -> N_2400
    N_2400 -> N_2401
    N_2400 -> N_2402
    N_2395 -> N_2403
    N_2403 -> N_2404
    N_2404 -> N_2405
    N_2405 -> N_2406
    N_2403 -> N_2407
    N_2407 -> N_2408
    N_2408 -> N_2409
    N_2409 -> N_2410
    N_2410 -> N_2411
    N_2411 -> N_2412
    N_2412 -> N_2413
    N_2412 -> N_2414
    N_2412 -> N_2415
    N_2411 -> N_2416
    N_2416 -> N_2417
    N_2416 -> N_2418
    N_2416 -> N_2419
    N_2408 -> N_2420
    N_2408 -> N_2421
    N_2403 -> N_2422
    N_2395 -> N_2423
    N_2392 -> N_2424
  }
}

Upvotes: 1

dgw
dgw

Reputation: 13666

I tried the following

digraph g {
  ordering=out ;
  node [shape=box] ;

  a -> b ;  a -> c ;  a -> d ;  a -> e ;  a -> f ;
  a -> g ;  a -> h ;  a -> i ;  a -> j ;  a -> k ;
  a -> l ;  a -> m ;  a -> n ;  a -> o ;  a -> p ;
  a -> q ;  a -> r ;  a -> s ;  a -> t ;  a -> u ;
  a -> v ;  a -> w ;  a -> x ;  a -> y ;  a -> z ;
}

and all nodes b-z are on the same level in the correct order. What version are you using?

Upvotes: 9

Related Questions