Not getting the output as expected in a Python program that uses dynamic programming

Muhammad Daud

I've written this program that uses dynamic programming:

def count_partitions(n, k):
    if n < k:
        return 0
    elif n == k == 3:
        return 1
    else:
        # Initialize table with base cases
        table = [[0] * (k+1) for _ in range(n+1)]
        table[3][3] = 1

        # Fill in table using recurrence relation
        for i in range(4, n+1):
            for j in range(3, k+1):
                table[i][j] = (j-2) * table[i-1][j] + (j-1) * table[i-1][j-1]

        return table[n][k]

The problem is for the following senario:

A student care center partitions students into groups and supervises them for self-study. However, there is a triumph of triplets from the same family who always talk to one another, center staff will not want to place any two of them in the same group. Suppose there are n students including the triplets, how many ways can the student care center partition these n students into k groups? It is allowed to have only one student in a group, but not allowed to have an empty group. The order of the groups does not matter, so it is considered as the same way if one grouping can be converted to another by rearranging the sequence of the group. G(n, k) = G(n-1, k-1) + k × G(n-1, k)

I'm making this program that can be used to solve a specific instance of the partitioning problem by providing the values of "n" and "k" as input.

The "count_partitions" function is used to calculate the number of ways to partition "n" distinct items into "k" non-empty sets. The "count_partitions" function in the given program also contains a specific case for when both "n" and "k" are equal to 3. In this case, the function returns the value 1, indicating that there is only one way to partition 3 distinct items into 3 non-empty sets.

My code runs, but I don't get the expected results. I would expect these outputs:

# Outputs 1, should output 3
count_partitions(4, 3)
# Outputs 1, should output 9
count_partitions(5, 3)
# Outputs 21, should output 37
count_partitions(6, 4)
Welbog

Since you haven't provided a description of the problem, we can't help you find the right solution, but we can get you started on understanding why your current program behaves the way it does.

The simplest one is the inputs n=4, k=3, which outputs 1. This is because you initialize the table like this:

    table = [[0] * (k+1) for _ in range(n+1)]
    table[3][3] = 1

So your table, after this executes will be a 5x4 matrix of 0s, except for position [3,3], which is 1:

# k=0 1 2 3
  [ 0 0 0 0 ] # n=0
  [ 0 0 0 0 ] #   1
  [ 0 0 0 0 ] #   2
  [ 0 0 0 1 ] #   3
  [ 0 0 0 0 ] #   4

Then your loops start at i=4 (in the n-axis) and j=3 (in the k-axis). It also stops at i=4, j=3 as well.

for i in range(4, n+1):
    for j in range(3, k+1):

Then you compute a single value (since the loops only run once):

# i=4, j=3
# table[3][3] = 1
# all other table[x][y] = 0

table[i][j] = (j-2) * table[i-1][j] + (j-1) * table[i-1][j-1]

# This reduces to
table[4][3] = 1 * 1 + 2 * 0 = 1

Then you return table[n][k], again with n=4, k=3, so this returns 1, which was set in the loop.

What you need to do is look at what's happening for this example and explain what's supposed to happen instead. Maybe in explaining it, you will find the problem.


Based on your edit, you are trying to implement this recurrence:

G(n, k) = G(n-1, k-1) + k × G(n-1, k)

What you have is

table[i][j] = (j-2) * table[i-1][j] + (j-1) * table[i-1][j-1]

So, to start, you've learned why variable names are important. It's hard to see whether these expressions line up. Let's start by renaming your target recurrence (from G to T, n to i, k to j), and renaming your code from table to t:

T(i, j) = T(i-1, j-1) + j × T(i-1, j)
t[i][j] = (j-2) * t[i-1][j] + (j-1) * t[i-1][j-1]

Now the order is different, too. Let's re-arrange terms and line them up:

T(i, j) = j     × T(i-1, j)   +           T(i-1, j-1)
t[i][j] = (j-2) * t[i-1][j]   +   (j-1) * t[i-1][j-1]

Do you see now what the problem is? The recurrence you've coded isn't the recurrence you're trying to make.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Not getting the expected output in Java

Getting no output after running python program

Two Dimensional Array Not Getting Expected Output

Not getting expected output for threaded program

not getting the expected output of intent

Not getting expected output in scrapy

Not getting my expected output in mapreduce using python code

jq not getting expected output

Program printing % after expected output

Why the output of this program is not as expected

getting unwanted output in cobol program

Getting different output in Python programming

Explanation for this simple looking haskell program (dynamic programming)

Program doesn't show expected output

what will be the time complexity of the following program for fibonacci series? code uses dynamic programming

Not getting the output as expected in Swing

Not getting correct expected output

Sine rule not getting expected output

Saving the output of a dynamic query that uses refcursor into a table

The program doesn't give the expected output

C programming in Linux: not getting correct output for program that finds number of occurrences of substring in file

Paramiko ssh output not getting output as expected

C++ - Not Getting Expected Output to Console

What's the expected output for this program?

Not getting a desired output in JAVA as expected

Not getting expected output in the Multithreading question

looping with if and for but not getting expected output

Not getting expected output for some reason?

Not getting expected output in descending order