title: grouping nodes in networkx
body: <p>I don't know the proper terminology, but I have a digraph in networkx and I want to merge related nodes using a mapping function, maintaining a link to the original nodes</p>
  
  <p>For example, if I had this graph:</p>
  
  <pre><code>a1 -&gt; b1
  a2 -&gt; b2
  a1 -&gt; c1
  a2 -&gt; d2
  b1 -&gt; d2
  c1 -&gt; f1
  c2 -&gt; f2
  b1 -&gt; c2
  </code></pre>
  
  <p>and this function</p>
  
  <pre><code>def mymap(node):
      return node[0]
  </code></pre>
  
  <p>then I would want to get this graph as a result:</p>
  
  <pre><code>a -&gt; b
  a -&gt; c
  a -&gt; d
  b -&gt; d
  c -&gt; f
  b -&gt; c
  </code></pre>
  
  <p>with node data</p>
  
  <pre><code>a: original_nodes = [a1, a2]
  b: original_nodes = [b1, b2]
  c: original_nodes = [c1, c2]
  d: original_nodes = [d2]
  f: original_nodes = [f1, f2]
  </code></pre>
  
  <p>Is there a way to do this? (without manually iterating over nodes and edges myself; I can do that, but I would think this would be a common task)</p>