3

I am very new to networkx and graphing in general and wanted your help.

I am designing a graph where each node has a node_id (which is just the number of points i am iterating through to make my graph) and five attributes.

I.e.

G.add_node(node_id)
G[id]['Coord1'] = 3
G[id]['Coord2'] = 7
G[id]['Coord3'] = None
G[id]['Coord4'] = None
G[id]['Coord5'] = None

So now i want to consider adding a new node, with some coordinates.

Now before adding a new node to my graph i want to check that there isn't already a node which has an attribute (one of the coords) which is the same as the node i am intending to add.

If there already exists a node which has a coord which is shared, then i will not add a new node but simply make some changes to the existing node.

The only way i could think of doing this was to loop through all existing nodes in the graph and checking if one of the attributes is the same as the node i am contemplating adding. E.g.

find_node = [attrdict for n,attrdict in G.node.items() if attrdict['coord1'] == tempcoord ]    

Where tempcoord is the coordinate of interest on the node i am intending adding.

This is clearly very inefficient to do this everytime i want to add node to have check all existing nodes, my question is: Is there a better way?

I understand that networkx will not add a new node which has the same id, but that doesnt extend to nodes which share the same attribute correct?

Alternatively, is it better to build the graph with all these "duplicate" nodes and then condense all nodes which have some shared attributes down into one node after i have added all nodes?

Sorry for the somewhat convoluted explaination, i hope that it makes some sense.

AHawks
  • 155
  • 1
  • 2
  • 5
  • Have these answers not been pertinent? http://stackoverflow.com/questions/23233127/how-to-get-values-from-specific-node-attributes-in-networkx-with-python?rq=1 or http://stackoverflow.com/questions/15644684/best-practices-for-querying-graphs-by-edge-and-node-attributes-in-networkx?rq=1 – zezollo Mar 22 '16 at 04:58
  • Hi Zezollo, thanks for pointing towards those threads. Whilst they are definitely related they didn't really answer my question. I know how to get the list of attribute values and or there indices, and just select all nodes withe certain attributes. What i was wondering was whether you can access a node based on a unique attribute rather than a node index. – AHawks Mar 22 '16 at 21:55
  • In essence i want to be able to access a node by multiple hashable attributes rather than just the node index. Is there a way to do this? – AHawks Mar 22 '16 at 21:56

1 Answers1

2

Create a set which contains all of the observed node attributes. When you want to add a node with a given attribute, check if that attribute is there. If so, do nothing but if not, add it and put the attribute in the set. May be worth defining a function to do these steps if it's done in different parts of the code.

seen_attributes = set()

# code here adding nodes

node = node_to_add
attribute = attribute_of_node

if attribute not in seen_attributes:
    G.add_node(node, coord=attribute)
    seen.add(attribute)
Joel
  • 22,598
  • 6
  • 69
  • 93
  • That is a good idea and would certainly work, i managed to resolve my issue by also including a dictionary where the key was the attribute and the value the node index. That way i could check the dictionary for a key and if it was found i know that the node already exists. – AHawks Mar 23 '16 at 03:31