31

I'm starting working with graphviz and I have problem with creating new nodes with this same label. For example for word "sentence" I would like to create graph with 8 nodes: s -> e -> n -> t -> e -> n -> c -> e Now I'm receiving graph with only 5 nodes (one "e" instead of 3 and one "n" instead of 2). I need to create more nodes with this same label (value).

Example of my problem may be this image http://rdftwig.sourceforge.net/paper/diagrams/bfsdeep.png where there are 2 nodes with value "C", "E" and "D".

Is it possible? If it is possible how can I access in my example with word "sentence" first, second or third "e" node?

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Piotr0123456
  • 638
  • 3
  • 7
  • 21

2 Answers2

55

You could define your nodes explicitly and set the label for them. Then each node has an unique id, but can have the same labels. Consider this example:

strict graph G {
    1 [label="A"];
    2 [label="B"];
    3 [label="B"];
    4 [label="A"];
    1 -- 2;
    2 -- 3;
    3 -- 4;
}

which will output (with dot):

Nodes with same labels

Maehler
  • 6,111
  • 1
  • 41
  • 46
  • 2
    This worked for me, but its tedious as hell. It would be nice to have a flag like repeatnode=false, so when we do something like a -> b b -> c c -> d d -> a It will create a new 'a' automatically, –  Jan 26 '15 at 14:16
  • This is what's refreshing about dot, it's just plain simple and it works, I wish more technologies worked like dot... – Felipe Valdes Oct 23 '19 at 10:42
0

It might sound wired, but just put a whitespace at the end of the character that repeated will solve the problem.

Elinx
  • 1,196
  • 12
  • 20
  • 1
    This isn't a good idea. Instead just use the key to be unique, as @maehler said. The keys are for the programmer and not displayed in the graph. A simple whitespace is not visible, nor easy to remember or self explanatory. – Greg Hilston Nov 01 '19 at 14:58