Difference between revisions of "Code:networkx"

From Predictive Chemistry
Jump to: navigation, search
(Created page with "The [https://networkx.github.io/ networkx] library is a great tool for quick graph operations in python put together by the wonderful folks at Los Alamos National Labs. It uses …")
 
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
G = {1: {2:1,3:1}, 2: {1:1,3:1}, 3: {1:1,2:1} } # dict-of-dicts
 
G = {1: {2:1,3:1}, 2: {1:1,3:1}, 3: {1:1,2:1} } # dict-of-dicts
 
</source>
 
</source>
The '''{}''' notation means create a set.
+
The '''{1, 2, 3}''' notation means create a set, while '''{1:1, 2:2, 3:3}''' means create a dictionary.
   
 
To test whether a node, '''u''' is parent to a node '''v''', we can just write,
 
To test whether a node, '''u''' is parent to a node '''v''', we can just write,
Line 18: Line 18:
 
<source lang="python">
 
<source lang="python">
 
def add_node(G, u):
 
def add_node(G, u):
assert u not in G.keys()
+
assert u not in G
 
G[u] = set() # dict-of-sets
 
G[u] = set() # dict-of-sets
  +
  +
def add_node(G, u):
  +
assert u not in G
 
G[u] = {} # dict-of-dicts
 
G[u] = {} # dict-of-dicts
 
</source>
 
</source>
Line 27: Line 30:
 
def add_children(G, u, kids):
 
def add_children(G, u, kids):
 
G[u] += kids # dict-of-set
 
G[u] += kids # dict-of-set
[G[u][k] = v for k,v in kids.iteritems()] # dict-of-dict
 
  +
  +
def add_children(G, u, kids):
 
k,v in kids.iteritems(): # dict-of-dict
  +
G[u][k] = v
   
 
# e.g.
 
# e.g.

Latest revision as of 16:45, 19 September 2014

The networkx library is a great tool for quick graph operations in python put together by the wonderful folks at Los Alamos National Labs. It uses a representation based on dictionaries of dictionaries.

A dictionary of sets is simpler, so I'll show that too. For both representations, each node is a dictionary key, and the value stores the names of its child nodes. So, a graph with nodes 1,2,3 connected in a triangle would look like:

<source lang="python"> G = {1: {2,3}, 2: {1,3}, 3: {1,2} } # dict-of-sets G = {1: {2:1,3:1}, 2: {1:1,3:1}, 3: {1:1,2:1} } # dict-of-dicts </source> The {1, 2, 3} notation means create a set, while {1:1, 2:2, 3:3} means create a dictionary.

To test whether a node, u is parent to a node v, we can just write, <source lang="python"> def is_child(G, u, v):

   return v in G[u] # both formats

</source>

To add a node to a tree, I would need to create a new key in the dictionary -- like so: <source lang="python"> def add_node(G, u):

   assert u not in G
   G[u] = set() # dict-of-sets

def add_node(G, u):

   assert u not in G
   G[u] = {} # dict-of-dicts

</source>

To put in parent-child edges, I have to add members to the right set, <source lang="python"> def add_children(G, u, kids):

   G[u] += kids # dict-of-set

def add_children(G, u, kids):

   k,v in kids.iteritems(): # dict-of-dict

G[u][k] = v

  1. e.g.

add_node(G, 4) add_children(G, 4, {1,2}) </source> This version of add_children makes a directed graph, since child to parent edges were not added.

Amongst other things, networkx can visualize graphs:

<source lang="python"> import matplotlib.pyplot as plt import networkx as nx

nG=nx.from_dict_of_lists(G) # maybe works for dict-of-set nG=nx.from_dict_of_dicts(G) # for dict-of-dict nx.draw(nG) plt.show() </source>