Edge loses attributes when creating a subgraph

Hi All,

I'm having trouble working with subgraphs in a non-strict graph. When I move an edge into subgraph, edge attributes get lost when strict is False. I work through networkX and Pygraphviz, and Aric pointed me here. The entire story can be found here:

https://github.com/pygraphviz/pygraphviz/pull/28#issuecomment-58962373

I can fix this in python, but Aric recommended that I check here first since this may be a GV bug

Thanks,
k

The output seems to make

The output seems to make sense, assuming the print G function is basically a call to agwrite(). In the output
strict graph {
    subgraph test {
        1 -- 2       [color=green];
    }
    subgraph test2 {
        1 -- 2;
    }
}
you have a graph with 2 nodes and 1 edge. The second edge is the same as the first edge; agwrite() only writes an object's attributes once. In addition, agwrite() only writes an object the minimum number of times to show the graph structure. Thus, in the output
digraph {
    subgraph sg {
        1 -> 2;
    }
    1 -> 2 [key=ab,
    myattr=55];
}
there are two different edges. If they were the same, you would only see a single edge in the subgraph, as every edge in a subgraph automatically belongs to the parent graph. That's what happened when the graph was strict:
strict digraph {
    subgraph sg {
        1 -> 2 [key=ab,
        myattr=55];
}
}
The problem is in the use of add_edge function by add_subgraph. The add_edge function calls agedge with the create flag set to true and no key. If you have a multigraph, this will create a brand new edge between 1 and 2. Instead of
        for (u, v) in self.edges():
            if u in H and v in H:
                H.add_edge(u, v)
you need something like
    for n in H.nodes():
        for e in self.out_edges([n]):
            if e[1] in H:
                gv.subedge (H.handle, e.handle, _Action.create)
(I'm sure there is correct if not better way to write this in python.) You could get away with gv.edge as you currently do, but only works if every edge has a unique, non-None key.

Recent comments