private static int[,] FindMaxCostInKnapsack(Product[] products)
        {
            int numberOfProducts = products.Length;
            var arr = new int[numberOfProducts + 1, maxCapacity];
            for (int j = 0; j < maxCapacity; j++)
            {
                arr[0, j] = 0;
            }

            for (int i = 1; i <= products.Length; i++)
            {
                for (int j = 0; j < maxCapacity; j++)
                {
                    if (products[i - 1].Weight <= j)
                    {
                        arr[i, j] = Math.Max(arr[i - 1, j], arr[i - 1, j - products[i - 1].Weight] + products[i - 1].Cost);
                    }
                    else
                    {
                        arr[i, j] = arr[i - 1, j];
                    }
                }
            }
            Console.WriteLine(arr[numberOfProducts, maxCapacity - 1]);
            return arr;
        }
        static void Main()
        {
            Product beer = new Product("beer", 3, 2);
            Product vodka = new Product("vodka", 8, 12);
            Product cheese = new Product("cheese", 4, 5);
            Product nuts = new Product("nuts", 1, 4);
            Product ham = new Product("ham", 2, 3);
            Product whiskey = new Product("whiskey", 8, 13);

            List<Product> products = new List<Product>();
            products.Add(beer);
            products.Add(vodka);
            products.Add(cheese);
            products.Add(nuts);
            products.Add(ham);
            products.Add(whiskey);

            int bagCapacity = 10;

            Knapsack sack = new Knapsack();

            List<Product> productsInSack = sack.FindBestItems(products, bagCapacity);
            
            Console.WriteLine(ListToString(productsInSack));
        }
예제 #3
0
        /// <summary>
        /// Prints all products using template with simple style :)
        /// </summary>
        /// <param name="products">Products to be printed</param>
        public static void PrintAllProducts(Product[] products)
        {
            Console.WriteLine("------- =|= All products =|= -------");

            foreach (var product in products)
            {
                Console.WriteLine("- {0}", product);
            }

            Console.WriteLine("-----------------------------\n");
        }
예제 #4
0
        static void Main()
        {
            int capacity = 10;
            int n = 6;

            Product[] products = new Product[]
            {

                new Product("beer",2,3),
                new Product("vodka ",12,8),
                new Product("cheese",5,4),
                new Product("nuts",4,1),
                 new Product("whiskey",13,8),
                new Product("ham",3,2),

            };

            int[,] table = new int[n + 1,capacity + 1];

            for (int row = 1; row <= products.Length; row++)
            {
                var item = products[row - 1];
                for (int col = 0; col <= capacity; col++)
                {
                    if (item.Weight > col)
                    {
                        table[row, col] = table[row - 1, col];
                    }
                    else
                    {
                        table[row, col] = Math.Max(
                            table[row - 1, col],
                            table[row - 1, col - item.Weight] + item.Cost);
                    }
                }
            }

            var productsToPrint = new List<Product>();
            for (int row = products.Length, col = capacity; row > 0; row--)
            {
                if (table[row, col] != table[row - 1, col])
                {
                    productsToPrint.Add(products[row - 1]);
                    col -= products[row - 1].Weight;
                }
            }

            productsToPrint.ForEach(p => Console.WriteLine(p.Name + " Cost " + p.Cost + " Weight " + p.Weight));
        }
        static void Main()
        {
            var beer = new Product("Beer", 3, 2);
            var vodka = new Product("Vodka", 8, 12);
            var cheese = new Product("Cheese", 4, 5);
            var nuts = new Product("Nuts", 1, 4);
            var ham = new Product("Ham", 2, 3);
            var whiskey = new Product("Whiskey", 8, 13);

            Product[] products = { beer, vodka, cheese, nuts, ham, whiskey };
            var array = FindMaxCostInKnapsack(products);

            PrintArr(array);
            Console.WriteLine("Result:");
            PrintProducts(products.Length, maxCapacity, array, products);
        }
 private static void PrintProducts(int i, int j, int[,] array, Product[] products)
 {
     while (i != 0 && j != 0)
     {
         if (array[i, j - 1] != array[i - 1, j - 1])
         {
             Console.WriteLine(products[i - 1]);
             var currentcost = array[i, j - 1] - products[i - 1].Cost;
             while (array[i, j - 1] != currentcost)
             {
                 j--;
             }
         }
         else
         {
             i--;
         }
     }
 }
예제 #7
0
        public static void Main()
        {
            int knapsackCapacity = 10;
            int numberOfProducts = 6;
            Product beer = new Product("Beer", 3, 2);
            Product vodka = new Product("Vodka", 8, 12);
            Product cheese = new Product("Cheese", 4, 5);
            Product nuts = new Product("Nuts", 1, 4);
            Product ham = new Product("Ham", 2, 3);
            Product whiskey = new Product("Whiskey", 8, 13);
            Product[] products = new Product[] { beer, vodka, cheese, nuts, ham, whiskey };

            int[,] results = new int[numberOfProducts, knapsackCapacity];
            int[,] keepProducts = new int[numberOfProducts, knapsackCapacity];

            for (int i = 1; i < numberOfProducts; i++)
            {
                for (int j = 0; j < knapsackCapacity; j++)
                {
                    if (j >= products[i].Weight)
                    {
                        results[i, j] = Math.Max(results[i - 1, j], results[i - 1, j - products[i].Weight] + products[i].Cost);
                        keepProducts[i, j] = 1;
                    }
                    else
                    {
                        results[i, j] = results[i - 1, j];
                    }
                }
            }

            int currentKnapsackCapacity = knapsackCapacity;

            for (int i = numberOfProducts  - 1; i > 0; i--)
            {
                if (keepProducts[i, currentKnapsackCapacity - 1] == 1)
                {
                    Console.WriteLine(products[i]);
                    currentKnapsackCapacity = currentKnapsackCapacity - products[i].Weight;
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Parse input using Regular Expressions - Regex.Matches() method
        /// </summary>
        public static void ParseInput(out Product[] products, out int maximalWeight)
        {
            maximalWeight = int.Parse(Console.ReadLine());
            int numberOfProducts = int.Parse(Console.ReadLine());
            products = new Product[numberOfProducts];

            for (int i = 0; i < numberOfProducts; i++)
            {
                var line = Console.ReadLine();
                var productName = Regex.Matches(line, @"(\w+).-")
                                       .Cast<Match>()
                                       .First()
                                       .Groups[1].Value;

                var productCharacteristics = Regex.Matches(line, @"(\w+=)+(\d+)")
                                                  .Cast<Match>()
                                                  .Select(a => int.Parse(a.Groups[2].Value))
                                                  .ToArray();

                products[i] = new Product(productName, productCharacteristics[0], productCharacteristics[1]);
            }
        }
        static void Main(string[] args)
        {
            Product[] store = new Product[] {
                new Product("beer",2,3),
                new Product("vodka ",12,8),
                new Product("cheese",5,4),
                new Product("nuts",4,1),
                new Product("ham",3,2),
                new Product("whiskey",13,8)
            };

            int maxWeight = 10;
            List<int> allSums = new List<int>();
            List<int> allWeight = new List<int>();
            List<List<Product>> allSets = new List<List<Product>>();
            allSums.Add(0);
            allWeight.Add(0);
            allSets.Add(new List<Product>());

            for (int i = 0; i < store.Length; i++)
            {
                List<int> tempSum = new List<int>();
                List<int> tempWeight = new List<int>();
                List<List<Product>> tempSets = new List<List<Product>>();
                for (int j = 0; j < allSums.Count; j++)
                {
                    if (allWeight[j] + store[i].Weight <= maxWeight)
                    {
                        tempSum.Add(allSums[j] + store[i].Price);
                        tempWeight.Add(allWeight[j] + store[i].Weight);
                        List<Product> newSet = new List<Product>();
                        if (allSets[j].Count > 0)
                        {
                            foreach (var item in allSets[j])
                            {
                                newSet.Add(item);
                            }
                        }
                        newSet.Add(store[i]);
                        tempSets.Add(newSet);
                    }
                }

                for (int k = 0; k < tempSum.Count; k++)
                {
                    allSums.Add(tempSum[k]);
                    allWeight.Add(tempWeight[k]);
                    allSets.Add(tempSets[k]);
                }

            }

            int maxSum = int.MinValue;
            int index = 0;

            for (int i = 0; i < allSums.Count; i++)
            {
                if (allSums[i] > maxSum)
                {
                    maxSum = allSums[i];
                    index = i;
                }
            }
            foreach (var item in allSets[index])
            {
                Console.WriteLine("{0} {1} {2}", item.Name, item.Price, item.Weight);
            }

            Console.WriteLine("Best weight {0}, best price {1}", allWeight[index], allSums[index]);
        }