(infor_value, costs, capacity)
return dp[num][capacity], flag
Knapsack-0/1 problem using dynamic porgramming.

    Parameters:
    -----------
    infor_value: array-like
        The value corresponding to each item.

    costs: array-like
        The cost corresponding to each item.

    capacity: float
        The capacity of the knapsack.
    Returns:
    -----------
    max_value: float
        The greatest value the knapsack can bring.

    select_index: array-like
        The result of whether to select the item or not,
        1 reprent this item is selected,0 is not.
Knapsack-0/1 problem using dynamic porgramming.
Knapsack - 0 / 1 problem using dynamic porgramming .
def select_Knapsack_01(infor_value, costs, capacity):
    """
    Knapsack-0/1 problem using dynamic porgramming.

    Parameters:
    -----------
    infor_value: array-like
        The value corresponding to each item.

    costs: array-like
        The cost corresponding to each item.

    capacity: float
        The capacity of the knapsack.
    Returns:
    -----------
    max_value: float
        The greatest value the knapsack can bring.

    select_index: array-like
        The result of whether to select the item or not,
        1 reprent this item is selected,0 is not.
    """
    assert(len(infor_value) == len(costs))
    num = len(infor_value)
    dp = np.zeros((num + 1, capacity + 1))
    flag = np.zeros(num)
    for i in range(num):
        for j in range(capacity+1):
            if (j - costs[i]) < 0:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = max(dp[i][j], dp[i][j - costs[i]] + infor_value[i])
    j = capacity
    for i in np.arange(num - 1, -1, -1):
        if (j - costs[i] >= 0) and (dp[i+1][j] == (dp[i][j - costs[i]] + infor_value[i])):
            flag[i] = 1
            j -= costs[i]
    return dp[num][capacity], flag
def select_Knapsack_01 ( infor_value , costs , capacity ) : assert ( len ( infor_value ) == len ( costs ) ) num = len ( infor_value ) dp = np . zeros ( ( num + 1 , capacity + 1 ) ) flag = np . zeros ( num ) for i in range ( num ) : for j in range ( capacity + 1 ) : if ( j - costs [ i ] ) < 0 : dp [ i + 1 ] [ j ] = dp [ i ] [ j ] else : dp [ i + 1 ] [ j ] = max ( dp [ i ] [ j ] , dp [ i ] [ j - costs [ i ] ] + infor_value [ i ] ) j = capacity for i in np . arange ( num - 1 , - 1 , - 1 ) : if ( j - costs [ i ] >= 0 ) and ( dp [ i + 1 ] [ j ] == ( dp [ i ] [ j - costs [ i ] ] + infor_value [ i ] ) ) : flag [ i ] = 1 j -= costs [ i ] return dp [ num ] [ capacity ] , flag