Settling Multiple Debts Efﬁciently: An Invitation to Computing Science

I present and solve several problems related to the settling of multiple debts. The solutions are documented in much detail, with (bright) high-school students in mind. One of the variants has a simple solution, though it is not so easy to code concisely. Another variant is an elegant NP-hard problem. Theproblemleadsintoimportantareasofmathematicsandcomputingscience, making it suitable as an invitation to these subjects.


Introduction
Many informatics curricula for secondary education suffer from the 'encyclopedia syndrome'. They try to cover every topic, but in view of the limited time can offer almost nothing about each topic. The author believes that it is much better to select a few topics and treat them enthousiastically in more depth. This article presents such a topic.
The problem I am about to present looks pretty innocent. It can be explained to anyone with some common (money) sense. Do not be misled, however. I found it embarrassingly instructive to fill in all details. I suggest that you work on the problem ( §2) yourself before reading my analysis ( §3) and solutions ( §4 to §6).

The Problem
A group of friends lend each other money throughout the year. They carefully record each transaction and at the end of the year wish to settle their debts. How should they transfer money so as to settle all debts? How difficult is it to find an appropriate settling scheme? How efficient is that scheme? Try to minimize the number of transfers and the total amount transferred.
To be a bit more precise, let us number the N friends from 0 to N −1. At the end of the year, the record shows for each pair i, j with 0 ≤ i, j < N and i = j how much, in total, friend i owes to friend j, say amount a i, j ≥ 0.
The context where I encountered this problem is a joint practice for physical therapy. Each therapist treats her patients in a number of sessions. Afterwards she sends them an invoice and collects the fee. Occasionally, therapist i calls on therapist j to take over a session for a patient. The patient pays to therapist i the entire bill, for all sessions, including those sessions handled by therapist j. Thus, a debt is created from i to j. These debts are settled at the end of the year. In this context, there are amounts a i, j ≥ 0 that therapist i collected for therapist j, with possibly i = j. The amounts a i,i , however, are irrelevant for settling of the debts.
Read no further if you want to have a go at this problem yourself.

Analysis
For N ≤ 2, the problem is trivial. In fact, for N = 0 and N = 1 there is no problem, because there are no debts to settle; thus, zero transfers is the optimal solution. For N = 2, the optimal solution is well known: Friend 0 pays a 0,1 − a 1,0 to friend 1, if a 0,1 > a 1,0 No transfers are needed, if a 0,1 = a 1,0 Friend 1 pays a 1,0 − a 0,1 to friend 0, if a 0,1 < a 1,0 That is, the key is a 0,1 − a 1,0 . How about N = 3? Maybe you see how to do that right away. I didn't. The debts can be conveniently presented in a matrix, which I will call the debt matrix: col. col. col. 0 1 2 row 0 · a 0,1 a 0,2 row 1 a 1,0 · a 1,2 row 2 a 2,0 a 2,1 · Amount a i, j appears in row i and column j. Irrelevant entries a i,i contain a dot '·'. These debts can obviously be settled by at most six transfers and, in general, by N 2 − N = N (N −1) transfers: i pays a i, j to j for all relevant pairs i, j. I call this the trivial solution. But one can do better, as already shown for N = 2. For i = j, the debts a i, j and a j,i are mirror images along the main (dotted) diagonal in the debt matrix. These two mutual debts can be settled by a single transfer involving the amount |a i, j −a j,i |. This way, all debts can be settled by at most three transfers and, in general, by 1 2 N (N −1) transfers. This I call the paired solution.
Is the paired solution optimal? No! Consider the matrix · 1 1 0 · 1 2 0 · (4) which corresponds to example (1) above, disregarding a factor ten. The paired solution involves three 1-unit transfers in a cycle: friend 0 pays to friend 1, who pays to friend 2, who pays again to friend 0. Observe that each friend also receives 1 unit. Consequently, there is no need to transfer any money at all. 1 Apparently, the total amounts p i to be paid by i are of importance: and the total amounts r j to be received by j: In terms of the debt matrix, p i is the sum of the amounts in row i, and r j is the sum of the amounts in column j: All that matters for friend i is her balance b i at the end of the year: If b i > 0, then she borrowed more than she lent out, and she needs to pay some others. The question remains whom to pay how much. If b i = 0, then she borrowed as much as she lent out, and she is even. If b i < 0, then she lent out more than she borrowed, and she needs to receive from some others. In example (4), we have the following row and column sums: · 1 1 2 0 · 1 1 2 0 · 2 2 1 2 and, hence, all b i = 0, that is, there effectively are no debts to settle. Before giving an optimal pay-back scheme for N = 3, I make the following observations. The grand total amount to be paid equals the grand total amount to be received: Consequently, the grand total balance equals zero: Hence, there exists a friend with a positive balance, if and only if there exists a friend with a negative balance: For N = 3, there are only four possible combinations of the three b i -signs (+, 0, or −), if we abstract from friend identity, that is, from permutations of friends: 1 Provided these friends are just interested in the total amount of money and not, for instance, in the finger prints of their debtors.

3
The first combination (all 0, the leftmost column in (13)) is equivalent to N = 0 and N = 1, requiring zero transfers. The second combination (one 0) is equivalent to N = 2, requiring one transfer. The third combination (two +) is settled by two transfers: from each of the two positive b i to the negative b i . The fourth combination (two −) is also settled by two transfers: from the positive b i to each of the two negative b i . Note that the latter two cases are related by sign reversal. In general, reversing the directions of all transfers in a solution for some case, yields a solution to the case with all balance signs reversed. Thus, we can also abstract from sign reversal.
The optimal transfers for each of the four sign combinations are depicted in Figure 1. Each dot represents a friend, each arrow a transfer. For N = 3, the worst case apparently involves two transfers. Next, consider case N = 4. These seven combinations of balance signs can occur: The four leftmost combinations reduce to cases with N < 4. Of the remaining three combinations, the two with a unique positive or negative balance are optimally solved by three transfers, as depicted in Figure  Finally, the case with two positive and two negative balances requires further analysis. Without loss of generality, assume Distinguish three cases depending on the sign of b 0 + b 3 (also see Figure 3): , on account of (11); the optimal solution involves just two transfers, from 0 to 3, and from 1 to 2; ; the optimal solution involves three transfers, from 0 to 2 and 3, and from 1 to 2; ; the optimal solution involves three transfers, from 0 to 3, and from 1 to 2 and 3.
For N = 4, the worst case apparently involves three transfers. How about N > 4? Can the debts always be settled in at most N −1 transfers?

Practical Solutions
There is a practical solution that requires at most N transfers. It is used in the partnership of therapists mentioned at the end of Section 2: All fees are collected in a central account, and at the end of the year, therapist i collects r i from the central account (this includes a i,i ).
The N friends can do this as follows. First, all friends i with b i > 0 put b i on the table (the central account). Next, all friends i with b i < 0 take −b i from the table. There is no surplus or deficit on the table, on account of (11): A slight modification of this scheme uses a friend as central account, say friend j. All others with b i > 0 transfer b i to j, and then those with b i < 0 collect −b i from j. Friend j has to provide b j herself if b j > 0, or gains −b j if b j < 0. This scheme requires no more than N −1 transfers.
Is the scheme with a friend as central account optimal? No, not in general! It might involve unnecessary transfers, or unnecessarily large amounts transferred. For example, if someone with b i = 0 plays the role of central account, both inefficiencies will be the case. Consider N = 3 with b 0 = 0 < b 1 = −b 2 . Using friend 0 as central account involves two transfers instead of one, and, in total, twice as much money changes hands as necessary. Even if the central account is with someone whose b i = 0, it need not be optimal. Consider again N = 3, now with b 0 , b 1 > 0 and b 2 < 0. If the central account is with friend 0, then the number of transfers is indeed optimal (two), but the total amount transferred would be unnecessarily large (b 0 + 2b 1 instead of b 0 + b 1 , see left in Figure 4). In this case, using friend 2 as center works optimally. For N = 4 with 0 < b 0 = −b 3 and 0 < b 1 = −b 2 , any center is suboptimal. The optimal solution (see left in Figure 3) involves two 'independent' transfers. Introduction of a center makes all transfers 'dependent'. Thus, one of the independent transfers of the optimal solution is then handled indirectly: the amount is transferred twice by way of the center. This gives rise to an unnecessary transfer and an unnecessarily large total amount transferred. How can one construct better solutions?

Transformed Solutions
There is another way to see that no more than N −1 transfers are required; a way that eventually leads to optimal solutions. I have already been drawing pictures with friends and transfers. Let us formalize such a picture as a mathematical graph, more precisely a labeled directed graph. A directed graph is a pair (P, A), where P is a set of points and A is a set of arrows. Each arrow starts in a point and ends in a point. The points and the arrows can be labeled, for instance, with numbers. The statement 'there is an arrow from point i to point j labeled with amount a' is denoted by Our problem can be rephrased as the following graph problem. We are given N points (friends), namely the numbers 0 to N −1. Each point i is labeled by a number b i (balance), such that (also see (11)): We are to find a set of arrows (transfers) labeled by positive numbers (amounts), such that all balances are evened out: For every point i, the sum of the labels on its outgoing arrows minus the sum of the labels on its incoming arrows equals b i .
This property, which I call the balancing relation, can be formalized as follows. Let in i be the sum of the labels on the incoming arrows of node i, and, similarly, out i the sum of the labels on the outgoing arrows, that is, The balancing relation (19) is then captured by A graph with this property is called a transfer graph for the numbers b i . The sum of the numbers on all arrows (total amount transferred) is called the weight of the graph. We are looking for a transfer graph with minimum number of arrows and minimum weight. (Why is this problem not solvable when (18) does not hold?) The trivial solution, presented in Section 3, consists of all arrows with a i, j > 0. In general, it is not optimal. I now investigate some graph transformations to reduce the number of arrows and/or the weight, while preserving the balancing relation (19). In the remainder of this section, (P, A) is a transfer graph for b i (0 ≤ i < N ).

Directed-cycle transformation
Consider a directed cycle in the graph (P, A) involving k points and k arrows: Let m be the minimum amount on the arrows in the cycle, that is, Decrease each t i in the cycle by m, yielding Arrows whose amount has become 0 are omitted. There is at least one such arrow, hence the cycle is 'broken'. This transformation preserves the balancing relation (19), because for each of the affected points i both the outgoing sum out i and incoming sum in i are decreased by m, leaving their difference invariant. Consequently, the resulting graph is a transfer graph for the same problem, and it has fewer arrows and its weight has been decreased by k * m.
As an example, consider the problem given by (1). Its trivial solution is depicted by the transfer graph on the left in Figure 5. I have omitted the b i -labels, to avoid cluttering the picture. In general, reducing all cycles of length two in the trivial solution yields the paired solution. Next, observe that what remains is a directed cycle of length three, which disappears altogether under the directed-cycle transformation, because all amounts equal the minimum amount. We already knew the resulting solution shown on the right.
Repeated application of this transformation yields a transfer graph without directed cycles, a so-called directed acyclic graph, or dag.
The resulting dag depends on the order in which cycles are broken, as illustrated by the following example with N = 4 and debt matrix i\ j 0 1 2 3 p i 0 · 3 0 6 9 1 1 · 5 0 6 2 2 4 · 0 6 3 0 0 3 · 3 r j 3 7 8 6 The transfer graph for the paired solution is shown on the left in Figure  We already know the optimal transfer graph for N = 4 and type +−−− from Section 3. It is shown on the right, having three arrows and weight 6. Note that a lower bound on the weight of a transfer graph is because all these balances need to be evened out. On account of (16), it equals Concerning the number of arrows, the following can be remarked. Each positive balance must have at least one outgoing arrow and each negative balance must have at least one incoming arrow. Therefore, a lower bound on the number of arrows is where (#i :: B.i) is the number of i's for which B.i holds, and a ↑ b is the maximum of a and b. The optimal graph in Fig. 6 attains these lower bounds.

Directed-path transformation
The directed-cycle transformation is not enough to reduce the transfer graph to at most N −1 arrows. Here is another transformation. Consider two 'parallel' directed paths with the same start point p 0 and end point p k : involving k arrows and involving arrows. Let m be the minimum amount on the arrows in the first path, that is, Decrease each t i in the first path by m, and increase each t i in the second path by m.
Arrows whose amount has become 0 are omitted (there is at least one such arrow). This transformation preserves the balancing relation (why?) and the resulting graph is a transfer graph for the same problem. It has fewer arrows and its weight has been 'decreased' by (k − ) * m.
Note that the directed-cycle transformation discussed earlier is a special case of this transformation by taking p 0 = p k and = 0.
In the example above for N = 4, the second graph from the left in Figure 6 contains no directed cycles, but it does contain two directed paths from 0 to 2. It is shown again on the left in Figure 7. Decreasing the upper path by 1 and increasing the lower path by 1 yields the second graph from the left (also shown in Figure 6). This graph can be reduced to the optimal graph on the right by first applying the transformation in 'reverse'. The second graph from the right can be transformed to its left neighbor by decreasing the upper path from 0 to 2 by 1 and increasing the lower path by 1. Thus, these two graphs solve the same problem. That same graph can also be transformed to its right neighbor, the optimal solution, by decreasing the lower path by 1 and increasing the upper path by 1.

Arbitrary cycle transformation
The 'reverse' application of transformations hints at a further generalization. First of all, there is no reason to decrease a directed-cycle by exactly the minimum amount that occurs on any of its arrows. Decreasing by less also works fine: it reduces the weight but not the number of arrows. However, decreasing by more than the minimum can also be made to work. This still preserves the balancing relation (19). The only trouble is the introduction of arrows labeled with negative amounts. Such arrows can be 'eliminated' by reversing their direction and sign: Furthermore, any pair of points p, q not connected by an arrow may be connected by an arrow labeled with amount 0: In this new light, the directed-path transformation is actually a special case of the directed-cycle transformation: In the second path, reverse the arrows and the signs on their amounts. Together with the first path this yields a directed cycle. Next, decrease all amounts by m, and, finally, reverse arrows and amounts in the second path again. You do not explicitly have to create a directed cycle by reversing arrows, as long as you keep in mind that when amounts on an arbitrary cycle are decreased, the amounts on arrows pointing in the reverse direction need to be increased.
As an example consider the transfer graph for N = 4 depicted on the left in Figure 8. The balances (not shown) are +3, +7, −4, −6. Neither the directed-cycle nor the directed-path transformation applies, but the graph consists of one (undirected) cycle. Walking around in the direction of the arrow with amount 1, decreasing all forward arrows by 1 and increasing all reverse arrows by 1, yields the graph in the middle. It has one fewer arrow and the same weight. Walking around in the direction of the arrow with amount 2, decreasing all forward arrows by 2 and increasing all reverse arrows by 2, yields the graph shown on the right. Both these resulting graphs are optimal, as we know from Section 3. By the way, this example also illustrates that the optimal solution need not be unique. This transformation applies to any cycle in the transfer graph, possibly including zero-labeled arrows and arrows in reverse. Consequently, any cycle of arrows with non-zero amounts can be 'broken': just decrease along the cycle by an amount that occurs on some forward arrow. Remember to increase reverse arrows, to drop out zerolabeled arrows, and to reverse arrows that got negative amounts. This way any transfer graph can be put into a form without cycles. An acyclic graph on N points has no more than N −1 arrows, because if you walk along k arrows (ignoring their direction) without encountering the same point twice, you have seen k+1 distinct points. Thus, I have shown, once more, how to settle the debts by no more than N −1 transfers. Note that the solution with a friend as central account has an acyclic transfer graph.

Optimized Solutions
A transfer graph with no more than N −1 arrows is not a bad solution, certainly when compared to the trivial solution that may have as many as N (N −1) arrows. But it is not necessarily optimal.
I mentioned two items to minimize: the number of arrows (transfers) and the weight (total amount transferred). Is it the case that minimality of one item implies minimality of the other? No! You can see in Figure 6 that transfer graphs with the minimum number of arrows are not necessarily of minimum weight, and in Figure 8 that transfer graphs with minimum weight do not necessarily have the minimum number of arrows.

Minimum weight
In a transfer graph, any directed path of length two or more can be shortened. Consider a directed path of length two, as shown on the left in Figure 9, from p via q to r with amounts t and u. Let m be the minimum of t and u, that is, m = t↓u. Imagine a zerolabeled arrow from p to r , and decrease the cycle p, q, r by m, as shown in the second graph from the left (the zero-labeled arrow was increased because it is reversed). At least one of the resulting arrows gets a zero label. Which one(s), depends on how t and u compare (see the three graphs on the right). In all cases the weight has decreased by m. In the case of t = u, the number of arrows has decreased by one, whereas in the other cases it has remained the same.  Figure 9: Shortening a path and decreasing the weight By applying this transformation repeatedly, a transfer graph is obtained in which each point has either no outgoing arrows or no incoming arrows. Such a graph is known as a (directed) bipartite graph, in which the points are partitioned into two groups, and all arrows go from one group to the other and not back or within groups. Points with positive balance (b i > 0) have outgoing arrows only, points with negative balance (b i < 0) have incoming arrows only. Points with zero balance (b i = 0) have no arrows at all. All graphs in Figures 1, 2, 3, and 8 are bipartite. Observe that a transfer graph with minimum weight must be bipartite, because the weight of a graph that is not bipartite (as defined above) contains a path of length two and can thus be reduced in weight.
The weight of a bipartite transfer graph is because each arrow starts in a point with positive balance and all balances are evened out. On account of lower bound (28), this is the least possible weight. Thus, all bipartite transfer graphs have minimum weight. Together with the observation above, it has been shown that the solutions of minimum weight are exactly the bipartite solutions.
Here is an algorithm to construct a bipartite transfer graph: 1. Start with the empty graph: just N points and no arrows. Upon termination, the resulting transfer graph is bipartite (only arrows from positive to negative balances) and it evens out all balances. Whenever a transfer i t −→ j is added, the balance at i or j is decreased to zero (or both are decreased to zero). Hence, no transfer cycles are created. Because an acyclic graph on N points has at most N −1 arrows (see end of Section 5), the graph contains no more than N −1 transfers. This also shows that the algorithm indeed terminates. In Section 8, I present a Pascal program for this algorithm.

Minimum number of arrows
By now it is clear that points with balance zero can be omitted, because they need not be involved in any transfers. For if they would be then they would have incoming and outgoing arrows, which can be removed by path shortening. In the remainder, I assume b i = 0.
The algorithm presented above produces an acyclic bipartite graph, which minimizes the weight. We have already seen in Figure 8 that minimum-weight graphs need not have a minimum number of arrows. However, in that example, there is still a cycle that can be broken. But even an acyclic bipartite graph need not be optimal, as illustrated by Figure 10 for N = 4 and balances +5, +3, −5, −3.
The algorithm presented above is greedy, because the greatest possible amount is transferred between every selected pair i, j with b i > 0 > b j . There are various ways to modify it, in an attempt to minimize the number of transfers. One approach is to make it less greedy. But then how should one choose an appropriate amount to transfer? Another approach-which I analyzed-is to select the pairs i, j in a careful order. My first attempt considers pairs i, j with b i > 0 > b j in decreasing order of absolute value, that is, to match the greatest balance differences first. That would indeed avoid the suboptimal graph in Figure 10, because balances 5 and −5 (difference 10) would be matched first. This works well for N ≤ 4, but not in general: Consider the case with N = 5 and balances +9, +8, −3, −6, −8. My proposal would first match +9 and −8 (difference 17), yielding a graph with four transfers instead of the optimal three (verify!). Figure 11 shows the four classes of acyclic bipartite graph for N = 5, without isolated points, modulo sign reversal. For the type + + − − −, the three-transfer solution is preferred. It can only occur if there are two opposite balances that can cancel each other in one transfer. My second attempt considers pairs i, j with b i > 0 > b j in order of increasing sum b i + b j , that is, to match closest opposite balances first. That would indeed avoid suboptimal graphs for the cases with N = 5. In the example above, balances +8 and −8 (sum 0) would be matched first. This works well for N ≤ 5, but not in general. A counterexample for N = 6 is provided by the balances +9, +8, +2, −4, −5, −10. Matching +9 and −10 results in a solution with five transfers instead of the optimal four.
Is it always best to match opposite balances? Consider a solution where two points i and j, having opposite balances b i = −b j = B > 0, are not connected by an arrow. There must be some arrows going from i, say p > 0 of them in total, and some other arrows going to j, say q > 0 in total. Note that the sum of the amounts on the p arrows from i equals the sum of the amounts on the q arrows to j, both sums being equal to B. If the p + q arrows are removed and an arrow i B −→ j is added, then we still have to settle the remaining imbalance at the p sources and q sinks. Since these imbalances are opposite, they can be evened out by at most p+q−1 arrows. Thus, the total number of arrows in the new solution involving i B −→ j is at most the number of arrows in the original solution.
Exercise: Give an example showing that opposite matching is not nec-essary for optimality.
Matching opposites, however, never prohibits optimality, and an algorithm to construct an optimal transfer graph could start by eliminating all opposites. As remarked before and evident from Figure 11, this approach takes care of all cases with N ≤ 5. But it leaves the harder part of the problem unsolved: What to do when there are no opposites? Exercise: Give an example showing that it is not always best to 'match triples'. Figure 12 shows the eleven classes of acyclic bipartite graphs for N = 6, without isolated points, modulo sign reversal. It seems evident from these graphs that acyclic graphs with fewer arrows consist of more 'disconnected clusters', and vice versa. There is an easy explanation for this, but it calls for more graph terminology. Figure 12: Acyclic bipartite graphs for N = 6 A graph is called connected if there is a path between every pair of points. The direction of arrows in a path is considered irrelevant. Every graph consists of a number of connected subgraphs (possibly one) that are not connected to each other. These connected subgraphs are called the components of the graph. For example, in Figure 12 there are six graphs with one component, four with two components, and one with three.
All components of an acyclic graph are acyclic. A connected acyclic graph is called a tree. It has one more point than arrows; that is, if it has k points, then it has k−1 arrows. Adding an arrow would introduce a cycle, and upon removal of an arrow the graph would no longer be connected. Therefore, an acyclic graph is a collection of trees, also known as a forest. The number of arrows in a forest equals the number of points minus the number of components (trees). Consequently, minimizing the number of arrows in an acyclic graph, is equivalent to maximizing the number of components.
The sum of the balances in a component of a transfer graph equals zero, because each arrow i t −→ j adds t to the balance sum via out i and subtracts t from the balance sum via in j . Therefore, to minimize the number of arrows one needs to find a partition of the points such that each part has balance sum zero and the number of parts is maximized.
Consider the special case of this problem, where N −2 distinct balances are positive and two are negative, say b 0 , b 1 < 0. On account of lower bound (30) on the number of arrows in a transfer graph, this special case requires at least N −2 transfers (each of the positive balances needs an outgoing arrow). Since no more than N −1 13 transfers are needed, the question is whether N −2 is feasible. In terms of partitioning, this boils down to the existence of a partition of the balances into two parts, both with balance sum zero. Obviously, each part must contain a negative balance. Therefore, the problem is to find a subset of the positive balances b i (those with 2 ≤ i < N ) whose sum equals −b 0 , the complement then automatically sums to −b 1 . This problem is equivalent to the subset-sum problem, sometimes also called the (simplified) knapsack problem (Garey and Johnson, 1979, SP13): For a given positive integer K and set S of items x with positive integer size s(x), does there exists a subset R of S whose total size x∈R s(x) equals K ?
Here, K is the size of the knapsack, S contains the items to pack, and s gives their sizes. The question is whether the knapsack can be filled exactly with a suitable selection R of the items.
It is easy to verify that a given subset R has indeed total size K . However, deciding whether a suitable subset R exists is believed to be a hard problem. As far as we know, one can, in general, not do much better than trying out all possibilities. The number of possibilities is an exponential function of the number of items in the input set S: there are 2 n subsets of an n-element set. If you discover a shortcut, such as a solution requiring no more work than a polynomial function of n, then you'll be famous instantly.
Finding a transfer graph with minimum number of arrows is at least as difficult as solving the subset-sum problem.

Programs
There are many ways to present the problem of efficiently settling multiple debts as a programming problem. In the preceding section, it turned out that the problem of finding a minimum weight transfer graph is relatively easy, whereas that of finding a transfer graph with minimum number of arrows is hard.
The input to the program can be given in various ways: 1. A list of loans a i, j from i to j. This is the format used in (1). More precisely: The input to your program consists of a text file. On the first line is an integer N , N min ≤ N ≤ N max , the number of friends. On the second line is an integer K , K min ≤ K ≤ K max , the number of loans. On each of the K following lines are three integers i, j, a with 0 ≤ i, j < N , i = j, and 0 < a = a i, j ≤ A max . The pairs i, j that appear are distinct and ordered lexicographically.
Either of the two conditions on the pairs i, j (distinctness, lexicographic order) can be dropped. When dropping the distinctness condition, the input can be interpreted to give the raw data of all the loaning transactions that took place over the year and that need to be settled at the end of the year. Zero could be allowed as loan.
2. A debt matrix a i, j . This is the format used in (4). More precisely: The input to your program consists of a text file. On the first line is an integer N , N min ≤ N ≤ N max , the number of friends. On each of the N following lines are N −1 integers. Line i+2 contains, in order of increasing j, the debts a i, j with i = j, and 0 ≤ a i, j ≤ A max .
'Debts' a i,i might be included as well.
3. A list of balances b i . More precisely: The input to your program consists of a text file. On the first line is an integer N , 2 ≤ N ≤ N max , the number of friends. On each of the N following lines is an integer Zero balances could be excluded.
N.B. Lower bounds N min and K min , and upper bounds N max , K max , A max , and B max need to be chosen carefully. Input format 3 takes away half of the problem, because for formats 1 and 2 the relevance of the balances still needs to be discovered. The output of the program must list all transfers needed to settle the input debts. More precisely: The output of your program is a text file. Each line contains three numbers i, j, t, encoding a transfer of amount t from i to j, with 0 ≤ i, j < N , i = j, and 0 < t, such that all pairs i, j are distinct and all debts in the input file are settled. The order of the lines is irrelevant.
There are various ways to impose extra constraints: 1. Minimize the total amount transferred (weight), that is, the sum of all t-values in the output.

15
2. Minimize the number of transfers, that is, the number of lines in the output.
3. Minimize the number of transfers and the total amount transferred. This is the original problem of §2.
Furthermore, the score of the program can depend on how close the output approximates the minimum. For the second and third form (minimizing the number of transfers), it is even possible to give several non-secret input files and only ask for corresponding output files.

Input processing
It is straightforward how input formats 1 and 2 can be transformed into each other and into input format 3. Here is a piece of Pascal program for reading input format 1 and storing it as an array of balances (format 3): program GettingEven;

Minimum total amount transferred
In Section 6.1, I have presented a greedy algorithm to construct a bipartite transfer graph that minimizes the total amount transferred. The following procedure expresses it concisely in Pascal, like a poem.
The while-loop terminates in at most 3N steps, because in each step either i increases, or j increases, or the number of non-zero balances decreases. As was already pointed out, this algorithm produces an acyclic graph, which involves no more than N −1 transfers.

Minimum number of transfers
Since minimizing the number of transfers turned out to be a hard problem in Section 6.2, one cannot do much better than try all possibilities. In this case, that means inspecting all partitions whose parts have zero balance sums, and finding the one with maximum number of parts. In view of the lower bounds (28) and (30), there is an opportunity for branch-and-bound. There are clear relationships to multiple knapsacks and bin packing.

17
One could imagine that for small values of the balances, an efficient dynamic programming solution exists, as with the knapsack problem. However, in general, even that is not possible, since the well-known problem 3-Partition (Garey and Johnson, 1979, SP15) is NP-complete in the strong sense, and it can be transformed to minimizing the number of transfers as follows. First, the definition of the 3-Partition problem: Given are a positive integer K and a set S of 3m items x with positive integer size s(x) such that K /4 < s(x) < K /2 and x∈S s(x) = m K . The question is whether S can be partitioned into m disjoint sets S i for 1 ≤ i ≤ m with x∈Si s(x) = K . Note that on account of the restrictions on s(x), each S i contains exactly three items.
This situation can be translated into the following transfer minimization problem: There are m negative balances of value −K each, and 3m positive balances of value s(x). These balances take at least m transfers to settle. If it can be done in exactly m transfers, then that solution corresponds to a partition of S into m sets S i with x∈Si s(x) = K , which is also a solution to the instance of 3-Partition. If it cannot be done in exactly m transfers (but requires more transfers), then there is no such partition, and the instance of 3-Partition cannot be solved.
Hence, minimizing the number of transfers is at least as difficult as 3-Partition.

Minimum total amount in minimum number of transfers
The combined minimization problem (minimizing the number of transfers and the total amount transferred) can be solved by first finding an appropriate partition (hard), then, for each component, minimizing the amount transferred (easy).

Conclusion
When I wrote the initial version of this article in 1998, I had not seen the problem of efficiently settling multiple debts in the literature. I must admit that I did not do much research to locate it. Nor have I discussed it with a lot of people, because I wanted to keep it secret in order to use it in a competition. The problem seemed directly related to the input-output economic model of Leontief (Leontief, 1951;HET, 2003).
An anonymous reviewer referred me to a series of articles about clearing debts in the Russian economy (Kalitkin, 1995;Anikonov et al., 1997;Kalitkin et al., 2001). Here the grand total balance is not zero (open economy; cf. (11), (18)). The objective is to clear as much of the debts as possible, given certain legal restrictions. The number of transactions and total amount transferred are not so important. The algorithmic complexity is not investigated.
Problem 96 in Steinhaus (1964) concerns minimization of the number of transfers. A further literature search reveals relationships with the minimum-cost network flow problem and special cases like the transportation problem and transshipment problem (Cook et al., 1998). Our problem of minimizing the number of transfers appears to be known as a special case of the fixed-charge transportation problem (Spielberg, 1964) and the Uncapacitated Fixed-charge Network Flow (UFNF) problem (Duhamel, 2001), with per-unit costs c i, j = 0 and fixed costs f i, j = 1. I have mostly presented the material in the order that I discovered it. Of course, this is not the only approach. Some readers may have wondered at places why I did not give the key argument immediately. Well, I simply did not see it immediately. Therefore, this story reveals more how my problem-solving brain works, than that it teaches how best to solve problems. With hindsight, some of my excursions seem unnecessarily long-winded. Possibly, I could have avoided these by concentrating more on the abstract problem, instead of working with concrete -and sometimes misleading-examples. It is, however, important to realize that most high-school students need details and they should be encouraged to follow their own path of investigation. In secondary education it is good to include many concrete examples, as I have done.
The ancient Greeks, such as Pythagoras and Euclid, could, no doubt, have understood this problem. One can only wonder whether they could have solved it. More likely they would not have appreciated the problem. Their cultural framework prevented them from asking such questions. Graph theory is a convenient ingredient for fully understanding and tackling the problem. Euler is generally recognized as the father of graph theory with his famous publication in 1736 (Euler, 1736). That may seem long ago but one must realize that Pythagoras lived 500 B.C. and Euclid 300 B.C. Clay tablets with mathematical inscriptions date back to well before 10,000 B.C. Mathematics has grown considerably, especially since Euler, and is still under turbulent development. Only in the second half of this century has graph theory become a part of 'respectable' mathematics (instead of 'just' dealing with recreational problems and games). There is ample literature on graph theory. The terminology, however, is not uniform and may differ from what I have used.
I hope that this problem can serve as an example of how a simple question leads to many important concepts in mathematics and computing science. Teachers need not use this material directly. I would rather encourage them to find their own questions that they can develop in detail and teach with enthousiasm.