Graph Union in Networkx
I have two graphs:
G.nodes() = [0,3]
H.nodes() = [1,2,3,4]
I am trying to merge the graphs together while only relabeling the nodes of H and maintaining the same labels for G so the resulting graph would have the following nodes:
U.nodes() = [0,3,1,2,5,4]
Where the first two elements are from G and everything else is from H given that there is a conflict of names at node 3 it gets renamed to the next available integer.
disjoint_union from networkx doesn't work because G.nodes() get relabeled to [0,1].
Any help is appreciated!
python-3.x union networkx
add a comment |
I have two graphs:
G.nodes() = [0,3]
H.nodes() = [1,2,3,4]
I am trying to merge the graphs together while only relabeling the nodes of H and maintaining the same labels for G so the resulting graph would have the following nodes:
U.nodes() = [0,3,1,2,5,4]
Where the first two elements are from G and everything else is from H given that there is a conflict of names at node 3 it gets renamed to the next available integer.
disjoint_union from networkx doesn't work because G.nodes() get relabeled to [0,1].
Any help is appreciated!
python-3.x union networkx
1
take a look atnx.union
with therename
optional argument and thenconvert_node_labels_to_integers
. (anyone interested - feel free to build on this to give a full answer)
– Joel
Nov 13 '18 at 17:51
add a comment |
I have two graphs:
G.nodes() = [0,3]
H.nodes() = [1,2,3,4]
I am trying to merge the graphs together while only relabeling the nodes of H and maintaining the same labels for G so the resulting graph would have the following nodes:
U.nodes() = [0,3,1,2,5,4]
Where the first two elements are from G and everything else is from H given that there is a conflict of names at node 3 it gets renamed to the next available integer.
disjoint_union from networkx doesn't work because G.nodes() get relabeled to [0,1].
Any help is appreciated!
python-3.x union networkx
I have two graphs:
G.nodes() = [0,3]
H.nodes() = [1,2,3,4]
I am trying to merge the graphs together while only relabeling the nodes of H and maintaining the same labels for G so the resulting graph would have the following nodes:
U.nodes() = [0,3,1,2,5,4]
Where the first two elements are from G and everything else is from H given that there is a conflict of names at node 3 it gets renamed to the next available integer.
disjoint_union from networkx doesn't work because G.nodes() get relabeled to [0,1].
Any help is appreciated!
python-3.x union networkx
python-3.x union networkx
asked Nov 13 '18 at 17:34
mb567mb567
351211
351211
1
take a look atnx.union
with therename
optional argument and thenconvert_node_labels_to_integers
. (anyone interested - feel free to build on this to give a full answer)
– Joel
Nov 13 '18 at 17:51
add a comment |
1
take a look atnx.union
with therename
optional argument and thenconvert_node_labels_to_integers
. (anyone interested - feel free to build on this to give a full answer)
– Joel
Nov 13 '18 at 17:51
1
1
take a look at
nx.union
with the rename
optional argument and then convert_node_labels_to_integers
. (anyone interested - feel free to build on this to give a full answer)– Joel
Nov 13 '18 at 17:51
take a look at
nx.union
with the rename
optional argument and then convert_node_labels_to_integers
. (anyone interested - feel free to build on this to give a full answer)– Joel
Nov 13 '18 at 17:51
add a comment |
1 Answer
1
active
oldest
votes
Like @Joel states:
import networkx as nx
import matplotlib.pyplot as plt
_,ax = plt.subplots(1,3, figsize=(10,8))
G = nx.Graph()
G.add_nodes_from([0,3])
nx.draw_networkx(G, ax=ax[0], title='G')
H = nx.Graph()
H.add_nodes_from([1,2,3,4])
nx.draw_networkx(H, ax=ax[1])
U = nx.union(G, H, rename=('G-','H-'))
nx.draw_networkx(U, ax=ax[2])
print(U.nodes())
Output:
NodeView(('G-0', 'G-3', 'H-1', 'H-2', 'H-3', 'H-4'))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286610%2fgraph-union-in-networkx%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Like @Joel states:
import networkx as nx
import matplotlib.pyplot as plt
_,ax = plt.subplots(1,3, figsize=(10,8))
G = nx.Graph()
G.add_nodes_from([0,3])
nx.draw_networkx(G, ax=ax[0], title='G')
H = nx.Graph()
H.add_nodes_from([1,2,3,4])
nx.draw_networkx(H, ax=ax[1])
U = nx.union(G, H, rename=('G-','H-'))
nx.draw_networkx(U, ax=ax[2])
print(U.nodes())
Output:
NodeView(('G-0', 'G-3', 'H-1', 'H-2', 'H-3', 'H-4'))
add a comment |
Like @Joel states:
import networkx as nx
import matplotlib.pyplot as plt
_,ax = plt.subplots(1,3, figsize=(10,8))
G = nx.Graph()
G.add_nodes_from([0,3])
nx.draw_networkx(G, ax=ax[0], title='G')
H = nx.Graph()
H.add_nodes_from([1,2,3,4])
nx.draw_networkx(H, ax=ax[1])
U = nx.union(G, H, rename=('G-','H-'))
nx.draw_networkx(U, ax=ax[2])
print(U.nodes())
Output:
NodeView(('G-0', 'G-3', 'H-1', 'H-2', 'H-3', 'H-4'))
add a comment |
Like @Joel states:
import networkx as nx
import matplotlib.pyplot as plt
_,ax = plt.subplots(1,3, figsize=(10,8))
G = nx.Graph()
G.add_nodes_from([0,3])
nx.draw_networkx(G, ax=ax[0], title='G')
H = nx.Graph()
H.add_nodes_from([1,2,3,4])
nx.draw_networkx(H, ax=ax[1])
U = nx.union(G, H, rename=('G-','H-'))
nx.draw_networkx(U, ax=ax[2])
print(U.nodes())
Output:
NodeView(('G-0', 'G-3', 'H-1', 'H-2', 'H-3', 'H-4'))
Like @Joel states:
import networkx as nx
import matplotlib.pyplot as plt
_,ax = plt.subplots(1,3, figsize=(10,8))
G = nx.Graph()
G.add_nodes_from([0,3])
nx.draw_networkx(G, ax=ax[0], title='G')
H = nx.Graph()
H.add_nodes_from([1,2,3,4])
nx.draw_networkx(H, ax=ax[1])
U = nx.union(G, H, rename=('G-','H-'))
nx.draw_networkx(U, ax=ax[2])
print(U.nodes())
Output:
NodeView(('G-0', 'G-3', 'H-1', 'H-2', 'H-3', 'H-4'))
answered Nov 13 '18 at 20:36
Scott BostonScott Boston
52.9k72955
52.9k72955
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286610%2fgraph-union-in-networkx%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
take a look at
nx.union
with therename
optional argument and thenconvert_node_labels_to_integers
. (anyone interested - feel free to build on this to give a full answer)– Joel
Nov 13 '18 at 17:51