1

I'm writing some python code to create an edge between two nodes, and then randomly remove 2 edges from one of those nodes but with other nodes (not the original two).

Right now I'm stuck (and have gone over all networkx documentation multiple times).

I called/named one of the nodes "bad_apple_Bad_apple" - the other one I captured by it being the most central node in "self.most_central_node" using a max function to identify it. I can't seem to be able to select the node object itself so that I can create the edge using add_edge().

I'm pretty sure I'll need to explain more, but below is the code. I can post more of the code if need be, but I tried to go a little further up the code file to give some background on what I'm doing.

    self.most_central_node = max(nx.closeness_centrality(self.combined_network))
    print("The node with the max value for closeness centrality in the combined network is: ", self.most_central_node)

    self.bad_apple_node_stuff = self.combined_network['bad_apple_Bad_apple']

    print("This is the bad apple test", self.bad_apple_node_stuff) #This is not printing the node object which means I'm not selecting it. It's printing it's attributes, I think. This is where the issue is.

    self.combined_network.add_edge(self.combined_network.node['bad_apple_Bad_apple'], self.combined_network.node['closeness'==self.most_central_node]) #<<-- this doesnt work, no specific error though
5
  • I'm having some trouble figuring out exactly what you want to do. Can you give an example input and what you would like as output? Commented Dec 1, 2015 at 15:04
  • I'd like to identify the node called "bad_apple_Bad_apple" in my network called "combined_network" and create an edge between it and the most central node (by closeness centrality) which I've called "most_central_node" using add_edge(). Basically, the last line in the code isnt working (the one that starts with self.combined_network.add_edge Commented Dec 1, 2015 at 16:23
  • You seem to have identified the line that isn't working. Can you strip out the parts that don't work and create an MCVE? Presumably you can use some simpler attribute. I think once I see that it'll be clear enough. Commented Dec 2, 2015 at 0:35
  • I think I edited the way you asked. I got rid of all the code except the code that is necessary to understand. Also, I changed the notes in the code to clarify where I need help. Is this close enough for you to understand what I need help with? Thanks so much for your help in advance Joel. Commented Dec 2, 2015 at 3:48
  • It has something to do with being able to create that edge based on the "keys" of the node. The nodes are apparently stored as objects in a dictionary. I'm not good with dictionaries. All I need is to figure out how to create an edge from "bad_apple_Bad_apple" to "most_central_node". Commented Dec 2, 2015 at 6:00

1 Answer 1

1

Your code has a few problems. In the line:

self.most_central_node = max(nx.closeness_centrality(self.combined_network))

closeness_centrality actually returns a dictionary where the nodes are keys and their centralities are the values. Your line simply chose the maximum node by either the nodes being integers or longest string or whatever. You should change that to:

close = nx.closeness_centrality(self.combined_network)
from operator import itemgetter
self.most_central_node = max(close.items(), key=itemgetter(1))[0]

That last line compares all (node, closeness) pairs by their closeness value and then simply stores the associated node in the variable.

Now, if you already know that your other node is 'bad_apple_Bad_apple', then you can simply issue the statement:

self.combined_network.add_edge(self.most_central_node, 'bad_apple_Bad_apple')

Please also note that you have accessed dictionaries in your code that store attributes:

network.graph

is where graph attributes are stored, for example, name.

network.node

is where node attributes are stored, for example,

network.node['bad_apple_Bad_apple']

will return a dict with the names of the attributes and their values.

You say that you have read the documentation multiple times but I strongly suggest that you go through the tutorial.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. That works. You should rename yourself midnighter genius. So my problem was that I didn't know how to call the object item itself in the node dictionary I guess. Time to go over dictionary modules again!
Thanks for the praises, glad to be of help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.