title: How to create a map of settlement for a game of poker algorithmically containing the least number of transactions?
body: <p>Poker is a zero-sum game. This means that the amount of money going out of the table is equal to the amount going in. I mentioned poker here because that is what I need the solution for, but the question applies to any zero-sum game.</p>
  <p>Let's say that I play poker for money. The format is the cash game. This means that anyone can buy in again after he loses all his money. Anyone can buy in as many times as he wants with any amount of money. I distribute poker chips in amounts corresponding to the amount of buy-in to every player. At any time the chips can be counted to know how much money one owes/is owed.</p>
  <p>There are two arbitrary assumptions.</p>
  <p><strong>1. We don't use cash for buying in. We just write it down on paper and plan to settle it after the game (when winning/losing amounts are known).</strong></p>
  <p><strong>2. No one will act as a bank - keeping all money and redistributing it to winners after a game</strong></p>
  <p>Let's see a manual solution on two examples.</p>
  <h1>Sample game</h1>
  <p>Let's say that there are 4 players: user A, user B, user C, user D, and User E.</p>
  <p>At the end of the game, the balance looks as follows.</p>
  <pre><code>User A lost $30
  User B lost $5
  User C lost $20
  User D won $42
  User E won $13
  </code></pre>
  <h2>Solution .1</h2>
  <p>The first solution coming to my mind is:
  User A sends $30 to user D.</p>
  <p>User B sends $2 to user D and user B sends $3 to user E.</p>
  <p>User C Sends $10 to user D and user C sends $10 to user E.</p>
  <p>This solves the settlement using 5 transactions.</p>
  <h2>Solution .2</h2>
  <p>User B sends $5 to user A.</p>
  <p>User A sends $13 to user E.</p>
  <p>User A sends $22 to user D.</p>
  <p>User C sends $20 to user D.</p>
  <p>This solves the settlement using 4 transactions (better than solution .1).</p>
  <h1>My question is: How to create a map of settlement for a game of poker algorithmically containing the least number of transactions (optimal)?</h1>
  <p>Note that the number of players increases and the ratio of the number of winners to losers changes this makes the solutions differ a lot.
  It may be pretty hard to find an optimal one by hand (subjectively).</p>
  <p>What is a generic approach that will always lead me to an optimal number of transactions?</p>
  <p>I am sure this problem is generalized and it is named somehow but I never managed to find it.</p>