Skip to content

Commit

Permalink
Merge pull request #1714 from stsouko/master
Browse files Browse the repository at this point in the history
in compose() function, merge data of G and H instead of replacing G with H
  • Loading branch information
dschult committed Aug 13, 2015
2 parents 342e421 + cd25882 commit dd209dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 4 additions & 5 deletions networkx/algorithms/operators/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,10 @@ def compose(G, H, name=None):
name = "compose( %s, %s )" % (G.name, H.name)
R = G.__class__()
R.name = name
R.add_nodes_from(H.nodes())
R.add_nodes_from(G.nodes())

R.add_nodes_from(G.nodes(data=True))
R.add_nodes_from(H.nodes(data=True))

if G.is_multigraph():
R.add_edges_from(G.edges(keys=True, data=True))
else:
Expand All @@ -335,9 +337,6 @@ def compose(G, H, name=None):
else:
R.add_edges_from(H.edges(data=True))

# add node attributes, H attributes take precedent over G attributes
R.node.update(G.node)
R.node.update(H.node)
# add graph attributes, H attributes take precedent over G attributes
R.graph.update(G.graph)
R.graph.update(H.graph)
Expand Down
7 changes: 7 additions & 0 deletions networkx/algorithms/operators/tests/test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ def test_union_and_compose():
E=disjoint_union(G1,G2)
assert_equal(sorted(E.nodes()),[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

G = nx.Graph()
H = nx.Graph()
G.add_nodes_from([(1, {'a1': 1})])
H.add_nodes_from([(1, {'b1': 1})])
R = compose(G, H)
assert_equal(R.node, {1: {'a1': 1, 'b1': 1}})


def test_union_multigraph():
G=nx.MultiGraph()
Expand Down

0 comments on commit dd209dc

Please sign in to comment.