title: Algorithm to simplify a weighted directed graph of debts
body: <p>I've been using a little python script I wrote to manage debt amongst my roommates. It works, but there are some missing features, one of which is simplifying unnecessarily complicated debt structures. For example, if the following weighted directed graph represents some people and the arrows represent debts between them (Alice owes Bob $20 and Charlie $5, Bob owes Charlie $10, etc.):</p>
  
  <p><img src="https://i.stack.imgur.com/K17Xq.gif" alt="graph1"></p>
  
  <p>It is clear that this graph should be simplified to the following graph:</p>
  
  <p><img src="https://i.stack.imgur.com/UKLKd.gif" alt="graph1-simplified"></p>
  
  <p>There's no sense in $10 making its way from Alice to Bob and then from Bob to Charlie if Alice could just give it to Charlie directly.</p>
  
  <p>The goal, then, in the general case is to take a debt graph and simplify it (i.e. produce a new graph with the same nodes but different edges) such that</p>
  
  <ol>
  <li>No node has edges pointing both in and out of it (no useless money changing hands)</li>
  <li>All nodes have the same "flow" through them as they did in the original graph (it is identical in terms of where the money ends up).</li>
  </ol>
  
  <p>By "flow", I mean the value of all inputs minus all outputs (is there a technical term for this? I am no graph theory expert). So in the example above, the flow values for each node are:</p>
  
  <ul>
  <li>Bob: +10</li>
  <li>Alice: -25</li>
  <li>Charlie: +15</li>
  </ul>
  
  <p>You can see that the first and second graphs have the same flow through each node, so this is a good solution. There are some other easy cases, for example, any cycle can be simplified by removing the lowest valued edge and subtracting its value from all other edges.</p>
  
  <p>This:</p>
  
  <p><img src="https://i.stack.imgur.com/hsWOg.gif" alt="graph2"></p>
  
  <p>should be simplified into this:</p>
  
  <p><img src="https://i.stack.imgur.com/1GRkO.gif" alt="graph2-simplified"></p>
  
  <p>I can't imagine that no one has studied this problem; I just don't know what terms to search for to find info on it (again, not a graph theory expert). I've been looking for several hours to no avail, so my question is this: what is an algorithm that will produce a simplification (new graph) according to the conditions specified above for any weighted directed graph?</p>