Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guarantees on order of nodes #261

Open
acsor opened this issue Apr 24, 2021 · 2 comments
Open

Guarantees on order of nodes #261

acsor opened this issue Apr 24, 2021 · 2 comments

Comments

@acsor
Copy link

acsor commented Apr 24, 2021

For a project I'm working on, I use pydot to handle directed graphs as trees, and it is vital for me that the order of "children" of a node (speaking in the language of trees) is respected. I know that for dot, and therefore pydot, there's no such a thing as a tree as I mean it. Therefore the order of "children" of a node depends on how edges are internally represented by pydot.

Does this library make any guarantees on the order of its nodes? I'm building some programmatically, as pictured below

graph2

and so far, the nodes order reflected the order of insertion, which is what I expected and wanted. However, there's no statement in the documentation nor elsewhere that assures this is always the case. As far as I know, it could just depend by my own setup.

@ferdnyc
Copy link
Member

ferdnyc commented Jun 17, 2024

@acsor

I know it's three years later, but for the record (meaning, more for anyone else who comes across this issue in the future): Yes, Pydot does guarantee that nodes — and all other entities — will be output in the order they were inserted into a graph with add_node(), add_edge(), add_subgraph(), etc.

A monotonically-increasing sequence number is attached to each element at the time of insertion. When the graph source is generated, all elements are output in that sequence order. Deleted nodes are simply left as gaps in the sequence. If the same element is added twice, or deleted and re-added, the second addition will be placed at the end of the sequence.

(The reason those guarantees are made is, the ordering is critical for the creation of proper graphviz source code. For example, default attribute statements — node, edge, or graph nodes — will only affect the elements that come after them in the source.)

So, there's a big difference between this:

graph G {
    node [shape=box];
    edge [color=red];
    A -- B;
    edge [color=green];
    B -- C;
}

and, say, this:

graph G {
    A -- B;
    B -- C;
    edge [color=green];
    node [shape=box];
    edge [color=red];
}

@ferdnyc
Copy link
Member

ferdnyc commented Jun 17, 2024

@lkk7: (And with that, I think this can probably be closed as answered?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants