Pedro Pinheiro
Pedro Pinheiro

Reputation: 1069

How do I make graphviz repeat the nodes in the graph to create a tree-like image?

I have the following graph on graphviz:

Original graph with converging paths

How do I make graphviz repeat the nodes in the graph so I can get a tree-like image like this:

Derived tree with repeated labels

Upvotes: 3

Views: 1783

Answers (1)

MvG
MvG

Reputation: 60868

I don't think that you can make this modification of the graph within graphviz. You should feed graphviz a different graph, with the structure you desire. That graph can be obtained by a process like the following (pseudocode):

function visit(path: a list of nodes):
  let n be the last node on the path
  for every child c of n:
    write (a copy of) c to output
    if c is not in path:
      visit(path + [c])
write root to output
visit([root])

Instead of this list path you can also mark nodes as visited before the recursive call and remove that flag after the call:

function visit(n: a node):
  mark n as visited
  for every child c of n:
    write (a copy of) c to output
    if c is not marked as visited:
      visit(c)
  mark n as not visited
write root to output
visit(root)

Upvotes: 1

Related Questions