# Integer Partition Problem

In this article, we will learn the algorithm that will solve the partition problem and coin change problem. Consider the below example:

3 = 2 + 1;

In the above example, we can observe that 3 is the addition of 2 and 1. It means that an integer is represented as an addition of two positive integers.

Consider the below table:

 n ways p(n) 1 1 1 2 2 1 + 1 2 3 3 2 + 1 1 + 1 + 1 3 4 43 + 12 + 22 + 1 + 11 + 1 + 1 + 1 5 ...

As we can observe in the above table that number 1 can be represented in a one way, i.e., 1. The number 2 can be represented in two ways, i.e., 2, and (1 + 1). The number 3 can be represented in three ways, i.e., 3, (2 + 1), and (1 + 1 + 1). The number 4 can be represented in five ways, i.e., 4, (3 + 1), (2 + 2), (2 + 1 + 1), and (1 + 1 + 1 + 1).

P(n) is the number of ways of representing an integer as addition of positive integers.

If we have to write 50 as the sum of the positive integers then it is difficult to write all the possible partitions on the paper. We require some algorithm to solve the problem.

Consider the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 3 5 5 1 1 2 3 5

Method to be used to solve the above problem:

• Excluding the new coin.
• Including the new coin

Now we have to fill the empty columns.

As we can observe in the above table that to make a 5 using coins 0 and 1, answer is 1. Mathematically, it can be written as:

Set = {0, 1}

Total = 5

To calculate the value of new cell, the new coin is also added, i.e., 2. We have to find the effect of the new coin on the number of ways.

According to the algorithm, we first exclude the new coin from the set.

Set = {0, 1}

The number of ways is 1

Now, we include the new coin in a set.

Set = {0, 1, 2}

When we include coin 2, then the remaining amount to make 5 would be 3. The sum 3 can be made with the help of coins 0, 1, and 2. The number of ways to make 3 by using the coins 0, 1, and 2 is 2. Therefore, the total number of ways to make 5 using the coins 0, 1, and 2 is (1 + 2) 3.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 3 1 1 2 3 4 4 1 1 2 3 5 5 1 1 2 3 5

Now we need to calculate the value of the next cell. In this case, new coin is added, i.e., 3 to make a total of 5.

According to the algorithm, we first exclude the coin 3 then the set can be written as:

Set = {0, 1, 2}

The number of ways to make 5 when the coins 0, 1, and 2 are included is 3. Now we include the coin, i.e., 3 in a set then the set can be written as:

Set = {0, 1, 2, 3}

When we include coin 3, then the remaining amount to make 5 would be 2. The sum 2 can be made with the help of coins 0, 1, 2 and 3. The number of ways to make 2 by using the coins 0, 1, 2 and 3 is 2. Therefore, the total number of ways to make 5 using the coins 0, 1, 2 and 3 is (3 + 2) 5.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 3 1 1 2 3 4 5 4 1 1 2 3 5 5 1 1 2 3 5

Now we need to calculate the value of the next cell. In this case, new coin is added, i.e., 4 to make a total of 5.

According to the algorithm, we first exclude the coin 4 then the set can be written as:

Set = {0, 1, 2, 3}

The number of ways to make 5 when the coins 0, 1, 2 and 3 are included is 5. Now we include the coin, i.e., 4 in a set then the set can be written as:

Set = {0, 1, 2, 3, 4}

When we include coin 4, then the remaining amount to make 5 would be 1. The sum 1 can be made with the help of coins 0, 1, 2, 3 and 4. The number of ways to make 1 by using the coins 0, 1, 2, 3 and 4 is 1. Therefore, the total number of ways to make 5 using the coins 0, 1, 2, 3 and 4 is (4 + 1) 5.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 3 1 1 2 3 4 5 4 1 1 2 3 5 5 5 1 1 2 3 5

Now we need to calculate the value of the next cell. In this case, new coin is added, i.e., 5 to make a total of 5.

According to the algorithm, we first exclude the coin 5 then the set can be written as:

Set = {0, 1, 2, 3, 4}

The number of ways to make 5 when the coins 0, 1, 2, 3 and 4 are included is 5. Now we include the coin, i.e., 5 in a set then the set can be written as:

Set = {0, 1, 2, 3, 4, 5}

When we include coin 5, then the remaining amount to make 5 would be 0. The sum 0 can be made with the help of coins 0, 1, 2, 3, 4 and 5. The number of ways to make 0 by using the coins 0, 1, 2, 3, 4 and 5 is 1. Therefore, the total number of ways to make 5 using the coins 0, 1, 2, 3, 4 and 5 is (5 + 1) 6.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 3 1 1 2 3 4 5 4 1 1 2 3 5 6 5 1 1 2 3 5 7

Let's see how all the values of a table are calculated.

When the value of coin is 0.

To calculate the value of first cell, when we have a coin of 0 and we have to calculate the number of ways to make 0 with a help of 0 coin. There is only 1 way to make 0 by using the coin 0.

 0 1 2 3 4 5 0 1 1 2 3 4 5

The value 1 cannot be made with the help of 0 coin so we put 0 in the cell shown as below:

 0 1 2 3 4 5 0 1 0 1 2 3 4 5

Similarly, the sums 2, 3, 4 and 5 cannot be made with the help of coin 0, so we put 0 as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 2 3 4 5

Now the new coin, i.e., 1 is added in the set. Now we have two coins in a set, i.e., 0, and 1. Since the newly added coin, i.e., 1 does not have any effect on the number of ways to make 0, so we copy the value of the upper cell, i.e., 1 as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 2 3 4 5

To make a total of 1, coin 1 is itself used. Therefore, there is only one way to make a total of 1 by using the coins 0 and 1 as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 2 3 4 5

To make a total of 2, two coins of 1 are used. Therefore, there is only one way to make a total of 2 by using the coins 0 and 1 as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 2 3 4 5

To make a total of 3, three coins of 1 are used. Therefore, there is only one way to make a total of 3 by using the coins 0 and 1 as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 2 3 4 5

To make a total of 4, four coins of 1 are used. Therefore, there is only one way to make a total of 4 by using the coins 0 and 1 as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 2 3 4 5

To make a total of 5, five coins of 1 are used. Therefore, there is only one way to make a total of 5 by using the coins 0 and 1 as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 3 4 5

Now the new coin is added in the set, i.e., 2. So, we have three coins in a set, i.e., 0, 1 and 2. There is only one way to make 0 by using the coins 0, 1, and 2 as shown below:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 3 4 5

The newly added coin, i.e., 2 will have no effect on the total sum of 1. The number of ways to make a sum of 1 by using the coins 0 and 1 is 1 as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 3 4 5

To make 2, we first exclude the coin from the set, i.e., 2. The number of ways to make 2 by using the coins, i.e., 0 and 1 is 1. We then include the coin 2 in a set. Therefore, the total number of ways to make 2 is (1+1) 2.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 3 4 5

To make 3, we first exclude the coin from the set, i.e., 2. The number of ways to make 3 by using the coins, i.e., 0 and 1 is 1. We then include the coin 2 in a set. Therefore, the total number of ways to make 3 is (1+1) 2.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 4 5

To make 4, we first exclude the coin from the set, i.e., 2. The number of ways to make 4 by using the coins, i.e., 0 and 1 is 1. We then include the coin 2 in a set. Therefore, the total number of ways to make 4 is (1+2) 3.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 4 5

Now the value of the coin is 3. There are 4 coins available in the set, i.e., 0, 1, 2 and 3. The number of ways to make a coin 0 by using the coins 0, 1, 2 and 3 is 1 so we add 1 in the cell as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 4 5

Now, we have four coins in a set, i.e., 0, 1, 2 and 3. Since the coins 2 and 3 are greater than 1, so there will be no effect of these coins. The number of ways to make 1 by using the coins 0, and 1 is 1. So, we add 1 in the cell as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 4 5

The coin 3 is greater than 2 so it will have no effect on sum 2. The number of ways to make 2 by using the coins 0, 1 and 2 is 2, so we add 3 in the cell as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 4 5

Now the value of sum is 3. We have four coins in a set {0, 1, 2, 3}. According to the algorithm, we first exclude the newly added coin, i.e., 3. The number of ways to make 3 by using 0, 1, and 2 is 2. When coin 3 is added in a set, the total number of ways to make 3 is
(2 + 1) 3.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 5

Now the value of sum is 4. We have four coins in a set {0, 1, 2, 3}. According to the algorithm, we first exclude the newly added coin, i.e., 3. The number of ways to make 4 by using 0, 1, and 2 is 3. When coin 3 is added in a set, the total number of ways to make 4 is
(3 + 1) 4.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 5

Now the value of sum is 0. We have five coins in a set {0, 1, 2, 3, 4}. There is only one way to make 0 by using the coins 0, 1, 2, 3 and 4, so 1 is added in the cell as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 5

Now the value of sum is 1. We have five coins in a set {0, 1, 2, 3, 4}. Since 4 is greater than 1 so it will have no effect on the value of 1. The number of ways to make 1 by using the coins 0, 1, 2 and 3 is 1. Therefore, the total number of ways to make 1 by using the coins (0, 1, 2, 3 and 4) is 1.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 5

Now the value of sum is 2. We have five coins in a set {0, 1, 2, 3, 4}. Since 4 is greater than 2 so it will have no effect on the value of 2. The number of ways to make 2 by using the coins 0, 1, 2 and 3 is 2. Therefore, the total number of ways to make 2 by using the coins (0, 1, 2, 3 and 4) is 2.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 5

Now the value of sum is 3. We have five coins in a set {0, 1, 2, 3, 4}. Since 4 is greater than 3 so it will have no effect on the value of 3. The number of ways to make 3 by using the coins 0, 1, 2 and 3 is 3. Therefore, the total number of ways to make 3 by using the coins (0, 1, 2, 3 and 4) is 3.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 3 5

Now the value of sum is 4. We have five coins in a set {0, 1, 2, 3, 4}. The newly added coin is 4. First, we exclude the coin 4 then the coins in the set are 0, 1, 2, and 3. The number of ways to make 4 using the coins 0, 1, 2 and 3 is 4. Now we include the coin 4 then the number of ways to make 4 using the coins (0, 1, 2, 3, 4) is (4 + 1) 5.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 3 5 5

Now the value of sum is 0. We have six coins in a set {0, 1, 2, 3, 4, 5}. There is only one way to make 0 by using the coins 0, 1, 2, 3, 4 and 4, so 1 is added in the cell as shown in the below table:

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 3 5 5 1

Now the value of sum is 1. We have six coins in a set {0, 1, 2, 3, 4, 5}. Since 5 is greater than 1 so it will have no effect on the value of 1. The number of ways to make 1 by using the coins 0, 1, 2, 3 and 4 is 1. Therefore, the total number of ways to make 1 by using the coins (0, 1, 2, 3, 4 and 5) is 1.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 3 5 5 1 1

Now the value of sum is 2. We have six coins in a set {0, 1, 2, 3, 4, 5}. Since 5 is greater than 2 so it will have no effect on the value of 2. The number of ways to make 2 by using the coins 0, 1, 2, 3 and 4 is 2. Therefore, the total number of ways to make 2 by using the coins (0, 1, 2, 3, 4 and 5) is 2.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 3 5 5 1 1 2

Now the value of sum is 3. We have six coins in a set {0, 1, 2, 3, 4, 5}. Since 5 is greater than 3 so it will have no effect on the value of 3. The number of ways to make 3 by using the coins 0, 1, 2, 3 and 4 is 3. Therefore, the total number of ways to make 3 by using the coins (0, 1, 2, 3, 4 and 5) is 3.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 3 5 5 1 1 2 3

Now the value of sum is 4. We have six coins in a set {0, 1, 2, 3, 4, 5}. Since 5 is greater than 4 so it will have no effect on the value of 4. The number of ways to make 4 by using the coins 0, 1, 2, 3 and 4 is 5. Therefore, the total number of ways to make 5 by using the coins (0, 1, 2, 3, 4 and 5) is 5.

 0 1 2 3 4 5 0 1 0 0 0 0 0 1 1 1 1 1 1 1 2 1 1 2 2 3 3 1 1 2 3 4 4 1 1 2 3 5 5 1 1 2 3 5

Next TopicKruskal Algorithm

### Feedback   