Advertisement
nikolov_k

Knapsack Problem

Jun 23rd, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _01KnapsackProblem
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<Item> items = new List<Item>();
  14.             items.Add(new Item("NoCost", int.MaxValue, 0));
  15.             items.Add(new Item("beer", 3, 2));
  16.             items.Add(new Item("vodka", 8, 12));
  17.             items.Add(new Item("cheese", 4, 5));
  18.             items.Add(new Item("nuts", 1, 4));
  19.             items.Add(new Item("ham", 2, 3));
  20.             items.Add(new Item("whiskey", 8, 13));
  21.  
  22.  
  23.             int totalCost = 0;
  24.             List<int> result = Calculate(items, 10, out totalCost);
  25.             Console.WriteLine("Optimal Solution");
  26.             for (int i = 0; i < result.Count; i++)
  27.             {
  28.                 Console.WriteLine(items[result[i]].Name);
  29.             }
  30.             Console.WriteLine("Cost = {0}", totalCost);
  31.         }
  32.  
  33.         private static List<int> Calculate(List<Item> items, int maxWeight, out int cost)
  34.         {
  35.             int N = items.Count;
  36.             List<int>[,] optimalValues = new List<int>[N+1, maxWeight + 1];
  37.             int[,] optimalCost = new int[N+1, maxWeight + 1];
  38.             for (int i = 0; i < items.Count; i++)
  39.             {
  40.                 optimalValues[i, 0] = new List<int>();
  41.             }
  42.  
  43.             for (int i = 1; i < N; i++)
  44.             {
  45.                 for (int weight = 1; weight <= maxWeight; weight++)
  46.                 {
  47.                     if (weight >= items[i].Weight)
  48.                     {
  49.                         if (items[i].Cost + optimalCost[i - 1, weight - items[i].Weight]
  50.                             > optimalCost[i - 1, weight])
  51.                         {
  52.                             optimalCost[i, weight] = items[i].Cost +
  53.                                 optimalCost[i - 1, weight - items[i].Weight];
  54.                             optimalValues[i, weight] = new List<int>();
  55.                             optimalValues[i, weight].Add(i);
  56.                             if (optimalValues[i - 1, weight - items[i].Weight] != null)
  57.                             {
  58.                                 optimalValues[i, weight].AddRange(optimalValues[i - 1, weight - items[i].Weight]);
  59.                             }
  60.                         }
  61.                         else
  62.                         {
  63.                             optimalCost[i, weight] = optimalCost[i - 1, weight];
  64.                             optimalValues[i, weight] = optimalValues[i - 1, weight];
  65.                         }
  66.                     }
  67.                     else
  68.                     {
  69.                         optimalCost[i, weight] = optimalCost[i - 1, weight];
  70.                         optimalValues[i, weight] = optimalValues[i - 1, weight];
  71.                     }
  72.                 }
  73.             }
  74.             cost = optimalCost[N - 1, maxWeight];
  75.  
  76.             return optimalValues[N - 1, maxWeight];
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement