18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DynamicProgrammingKnapsackSolver.cs 3.76 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2023-12-30 10:33 . Switch to file-scoped namespaces (#430)
using System;
using System.Collections.Generic;
namespace Algorithms.Knapsack;
/// <summary>
/// Dynamic Programming Knapsack solver.
/// </summary>
/// <typeparam name="T">Type of items in knapsack.</typeparam>
public class DynamicProgrammingKnapsackSolver<T>
{
/// <summary>
/// Returns the knapsack containing the items that
/// maximize value while not exceeding weight capacity.
/// </summary>
/// <param name="items">The list of items from which we select ones to be in the knapsack.</param>
/// <param name="capacity">
/// The maximum weight capacity of the knapsack
/// to be filled. Only integer values of this capacity are tried. If
/// a greater resolution is needed, multiply the
/// weights/capacity by a factor of 10.
/// </param>
/// <param name="weightSelector">
/// A function that returns the value of the specified item
/// from the <paramref name="items">items</paramref> list.
/// </param>
/// <param name="valueSelector">
/// A function that returns the weight of the specified item
/// from the <paramref name="items">items</paramref> list.
/// </param>
/// <returns>
/// The array of items that provides the maximum value of the
/// knapsack without exceeding the specified weight <paramref name="capacity">capacity</paramref>.
/// </returns>
public T[] Solve(T[] items, int capacity, Func<T, int> weightSelector, Func<T, double> valueSelector)
{
var cache = Tabulate(items, weightSelector, valueSelector, capacity);
return GetOptimalItems(items, weightSelector, cache, capacity);
}
private static T[] GetOptimalItems(T[] items, Func<T, int> weightSelector, double[,] cache, int capacity)
{
var currentCapacity = capacity;
var result = new List<T>();
for (var i = items.Length - 1; i >= 0; i--)
{
if (cache[i + 1, currentCapacity] > cache[i, currentCapacity])
{
var item = items[i];
result.Add(item);
currentCapacity -= weightSelector(item);
}
}
result.Reverse(); // we added items back to front
return result.ToArray();
}
private static double[,] Tabulate(
T[] items,
Func<T, int> weightSelector,
Func<T, double> valueSelector,
int maxCapacity)
{
// Store the incremental results in a bottom up manner
var n = items.Length;
var results = new double[n + 1, maxCapacity + 1];
for (var i = 0; i <= n; i++)
{
for (var w = 0; w <= maxCapacity; w++)
{
if (i == 0 || w == 0)
{
// If we have no items to take, or
// if we have no capacity in our knapsack
// we cannot possibly have any value
results[i, w] = 0;
}
else if (weightSelector(items[i - 1]) <= w)
{
// Decide if it is better to take or not take this item
var iut = items[i - 1]; // iut = Item under test
var vut = valueSelector(iut); // vut = Value of item under test
var wut = weightSelector(iut); // wut = Weight of item under test
var valueIfTaken = vut + results[i - 1, w - wut];
var valueIfNotTaken = results[i - 1, w];
results[i, w] = Math.Max(valueIfTaken, valueIfNotTaken);
}
else
{
// There is not enough room to take this item
results[i, w] = results[i - 1, w];
}
}
}
return results;
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891