算导3(背包问题)

1. Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem both as fractional knapsack and 0/1 knapsack.

分数背包问题:

#include <cstdlib>
#include <iostream>
using namespace std;
int value[5] = {20, 30, 65, 40, 60};
int weight[5] = {10, 20, 30, 40, 50};
double valueperweight[5] = {2, 1.5, 2.1, 1, 1.2};
int x[5];
int flag[5];
double lastv;
int lastw;
int find()
{
    double max = 0;
    int v;
    for(int i = 0; i < 5; i++)
    {
            if(valueperweight[i] > max && flag[i] == 0)
            {
                                 max = valueperweight[i];
                                 v = i;
            }
    }
    flag[v] = 1;
    return v;
}
void FractionalKnapsack()
{
    int c = 100, k;
    for(int i = 0; i < 5; i++)
    {
            k = find();
            cout << k <<endl;
            if(weight[k] < c)
            {
                         x[k] = 1;
                         c -= weight[k];
            }
            else break;
    }
    if(weight[k] != c)
    {
                 x[k] = 2;
                 lastv = c * valueperweight[k];
                 lastw = c;
   
}
void print()
{
     double sum = 0;
     for(int i = 0; i < 5; i++)
     {
             if(x[i] == 1)
             {
                     cout << "value:" << value[i] << " " << "weight:" << weight[i] << " " << "num:" << i;
                     sum += value[i];
                     cout << endl;
             }
             else if(x[i] == 2)
             {
                  cout << "value:" << lastv << " " << "weight:" << lastw << " " << "num:" << i;
                  sum += lastv;
                  cout << endl;
             }
     }
     cout << "sum of value:" << sum <<endl;
}
int main(int argc, char *argv[])
{
    memset(flag, 0, sizeof(flag));
    cout << "Fractional Knapsack:" << endl;
    FractionalKnapsack();
    print();
    system("PAUSE");
    return EXIT_SUCCESS;
}

算导3(背包问题)


0/1背包问题:

  对于背包问题,通常使用的处理方法是搜索。用递归来完成。当背包剩余空间可以放入第i件物品,则第i件物品放入和不放入所能得到的价值取最大。这个算法的时间复杂度是O(2^n),我们可以做一些简单的优化。

  由于本题的所有物品的体积均是整数,经过几次的选择后背包的剩余空间可能会相等,在搜索中会重复计算这些结点。所以,如果我们把搜索过程中计算过的结点的值记录下来,以保证不重复计算的话,速度就会提高很多。这是简单的“以空间换时间”。由于计算过程中会出现重叠的结点,符合动态规划中子问题重叠的性质。并且如果通过第N次选择得到的是一个最优解的话,那么第N-1次选择的结果一定也是一个最优解。符合动态规划最有子结构的性质。

  DP的三个步骤:

1. 阶段:在前N件物品中,选取若干件放入背包中;

2. 状态:在前N件物品中,选取若干件物品放入所剩空间为W的背包中的所获得的最大价值;

3. 决策:第N件物品放或者不放;

  我们可以由此写出动态转移方程:用f[i,j]表示在前i件物品中选择若干件放在所剩空间为j的背包里所能获得的最大价值。f[i,j] = max{f[i - 1, j - wi] + Pi, f[i - 1, j]}。这个

  由于是一个二重循环,这个算法的时间复杂度是O(n*w)。

#include <cstdlib>
#include <iostream>
using namespace std;
int value[6] = {0, 20, 30, 65, 40, 60};
int weight[6] = {0, 10, 20, 30, 40, 50};
int c[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int flag[6][11];
int map[6][11];
int w = 0, sum = 0;
void Knapsack()
{
     int t;
     for(int j = 0; j < 10; j++)
             map[0][j] = 0;
     for(int i = 0; i < 6; i++)
             map[i][0] = 0;
     for(int i = 1; i < 6; i++)
             for(int j = 1; j < 11; j++)
             {
                     if(c[j] >= weight[i])
                     {
                                t = (c[j] - weight[i])/10;
                                if((value[i] + map[i - 1][t]) > map[i - 1][j])
                                {
                                             map[i][j] = value[i] + map[i - 1][t];
                                             flag[i][j] = 1;
                                }
                                else map[i][j] = map[i - 1][j];
                     }
                     else map[i][j] = map[i - 1][j];
             }
     for(int i = 0; i < 6; i++)
     {
             for(int j = 0; j < 11; j++)
                     cout << map[i][j] << " ";
             cout << endl;
     }
     cout << endl;
     for(int i = 0; i < 6; i++)
     {
             for(int j = 0; j < 11; j++)
                     cout << flag[i][j] << " ";
             cout << endl;
     }
}
int print(int i, int j)
{
     if(i == 0 || j == 0) 
     {
          cout << "weight:" << w << " " << "sum:" << sum << endl;
          system("pause");
          return 0;
     }
     if(flag[i][j] == 0)
                  print(i - 1, j);
     else
     {
          cout << "weight:" << weight[i] << " " << "value:" << value[i] << " " <<endl;
          sum += value[i];
          w += weight[i];
     }
     int t = (c[j] - weight[i])/10;
     print(i-1, t);
}
int main(int argc, char *argv[])
{
    memset(flag, 0, sizeof(flag));
    cout << "0-1 Knapsack:" << endl;
    Knapsack();
    cout << map[5][10] << endl;
    print(5, 10);
    system("PAUSE");
    return EXIT_SUCCESS;
}
算导3(背包问题)

Practice 1 Date: Monday, March 18th, 2013 We highly encourage being environment friendly and trying all problems on your own. Implement exercise 2.3-7. Implement priority queue. Implement Quicksort and answer the following questions. (1) How many comparisons will Quicksort do on a list of n elements that all have the same value? (2) What are the maximum and minimum number of comparisons will Quicksort do on a list of n elements, give an instance for maximum and minimum case respectively. Give a divide and conquer algorithm for the following problem: you are given two sorted lists of size m and n, and are allowed unit time access to the ith element of each list. Give an O(lg m + lgn) time algorithm for computing the kth largest element in the union of the two lists. (For simplicity, you can assume that the elements of the two lists are distinct). Practice 2 Date: Monday, April 1st, 2013 We highly encourage being environment friendly and trying all problems on your own. Matrix-chain product. The following are some instances. Longest Common Subsequence (LCS). The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Longest Common Substring. The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Max Sum. The following is an instance. (-2,11,-4,13,-5,-2) Shortest path in multistage graphs. Find the shortest path from 0 to 15 for the following graph.   A multistage graph is a graph (1) G=(V,E) with V partitioned into K >= 2 disjoint subsets such that if (a,b) is in E, then a is in Vi , and b is in Vi+1 for some subsets in the partition; and (2) | V1 | = | VK | = 1.     Practice 3 Date: Monday, April 15th, 2013 We highly encourage being environment friendly and trying all problems on your own. Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem both as fractional knapsack and 0/1 knapsack. A simple scheduling problem. We are given jobs j1, j2… jn, all with known running times t1, t2… tn, respectively. We have a single processor. What is the best way to schedule these jobs in order to minimize the average completion time. Assume that it is a nonpreemptive scheduling: once a job is started, it must run to completion. The following is an instance. (j1, j2, j3, j4) : (15,8,3,10) Single-source shortest paths. The following is the adjacency matrix, vertex A is the source.  A B C D E A -1 3 B 3 2 2 C D 1 5 E -3 All-pairs shortest paths. The adjacency matrix is as same as that of problem 3.(Use Floyd or Johnson’s algorithm)     Practice 4 Date: Monday, May 8th, 2013 We highly encourage being environment friendly and trying all problems on your own. 0/1 Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem using back-tracking algorithm and try to draw the tree generated. Solve the 8-Queen problem using back-tracking algorithm.    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值