mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-30 13:53:36 +08:00
deploy
This commit is contained in:
@ -18,7 +18,7 @@
|
||||
<link rel="prev" href="../greedy_algorithm/">
|
||||
|
||||
|
||||
<link rel="next" href="../../chapter_appendix/">
|
||||
<link rel="next" href="../max_capacity_problem/">
|
||||
|
||||
|
||||
<link rel="icon" href="../../assets/images/favicon.png">
|
||||
@ -2896,6 +2896,8 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -3013,6 +3015,34 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="../max_capacity_problem/" class="md-nav__link">
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
15.3. 最大容量问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
@ -3246,12 +3276,7 @@
|
||||
<p><img alt="分数背包的贪心策略" src="../fractional_knapsack_problem.assets/fractional_knapsack_greedy_strategy.png" /></p>
|
||||
<p align="center"> Fig. 分数背包的贪心策略 </p>
|
||||
|
||||
<p><strong>第三步:正确性证明</strong></p>
|
||||
<p>采用反证法。假设物品 <span class="arithmatex">\(x\)</span> 是单位价值最高的物品,使用某算法求得最大价值为 <span class="arithmatex">\(res\)</span> ,但该解中不包含物品 <span class="arithmatex">\(x\)</span> 。</p>
|
||||
<p>现在从背包中拿出单位重量的任意物品,并替换为单位重量的物品 <span class="arithmatex">\(x\)</span> 。由于物品 <span class="arithmatex">\(x\)</span> 的单位价值最高,因此替换后的总价值一定大于 <span class="arithmatex">\(res\)</span> 。<strong>这与 <span class="arithmatex">\(res\)</span> 是最优解矛盾,说明最优解中必须包含物品 <span class="arithmatex">\(x\)</span> 。</strong></p>
|
||||
<p>对于该解中的其他物品,我们也可以构建出上述矛盾。总而言之,<strong>单位价值更大的物品总是更优选择</strong>,这说明贪心策略是有效的。</p>
|
||||
<p><strong>实现代码</strong></p>
|
||||
<p>我们构建了一个物品类 <code>Item</code> ,以便将物品按照单位价值进行排序。在循环贪心选择中,分为放入整个物品或放入部分物品两种情况。当背包已满时,则跳出循环并返回解。</p>
|
||||
<p>我们构建了一个物品类 <code>Item</code> ,以便将物品按照单位价值进行排序。循环进行贪心选择,当背包已满时跳出并返回解。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:11"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Java</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Python</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">JavaScript</label><label for="__tabbed_1_6">TypeScript</label><label for="__tabbed_1_7">C</label><label for="__tabbed_1_8">C#</label><label for="__tabbed_1_9">Swift</label><label for="__tabbed_1_10">Zig</label><label for="__tabbed_1_11">Dart</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
@ -3410,12 +3435,15 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>如下图所示,如果将一个 2D 图表的横轴和纵轴分别看作物品重量和物品单位价值,则分数背包问题可被转化为“求在有限横轴区间下的最大围成面积”。这个类比可以帮助我们从几何角度清晰地看到贪心策略的有效性。</p>
|
||||
<p>最差情况下,需要遍历整个物品列表,<strong>因此时间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> ,其中 <span class="arithmatex">\(n\)</span> 为物品数量。由于初始化了一个 <code>Item</code> 对象列表,<strong>因此空间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> 。</p>
|
||||
<p><strong>第三步:正确性证明</strong></p>
|
||||
<p>采用反证法。假设物品 <span class="arithmatex">\(x\)</span> 是单位价值最高的物品,使用某算法求得最大价值为 <span class="arithmatex">\(res\)</span> ,但该解中不包含物品 <span class="arithmatex">\(x\)</span> 。</p>
|
||||
<p>现在从背包中拿出单位重量的任意物品,并替换为单位重量的物品 <span class="arithmatex">\(x\)</span> 。由于物品 <span class="arithmatex">\(x\)</span> 的单位价值最高,因此替换后的总价值一定大于 <span class="arithmatex">\(res\)</span> 。<strong>这与 <span class="arithmatex">\(res\)</span> 是最优解矛盾,说明最优解中必须包含物品 <span class="arithmatex">\(x\)</span> 。</strong></p>
|
||||
<p>对于该解中的其他物品,我们也可以构建出上述矛盾。总而言之,<strong>单位价值更大的物品总是更优选择</strong>,这说明贪心策略是有效的。</p>
|
||||
<p>如下图所示,如果将物品重量和物品单位价值分别看作一个 2D 图表的横轴和纵轴,则分数背包问题可被转化为“求在有限横轴区间下的最大围成面积”。这个类比可以帮助我们从几何角度清晰地看到贪心策略的有效性。</p>
|
||||
<p><img alt="分数背包问题的几何表示" src="../fractional_knapsack_problem.assets/fractional_knapsack_area_chart.png" /></p>
|
||||
<p align="center"> Fig. 分数背包问题的几何表示 </p>
|
||||
|
||||
<p>最差情况下,需要遍历整个物品列表,<strong>因此时间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> ,其中 <span class="arithmatex">\(n\)</span> 为物品数量。由于初始化了一个 <code>Item</code> 对象列表,<strong>因此空间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> 。</p>
|
||||
|
||||
|
||||
|
||||
|
||||
@ -3510,13 +3538,13 @@
|
||||
|
||||
|
||||
|
||||
<a href="../../chapter_appendix/" class="md-footer__link md-footer__link--next" aria-label="下一页: 16. &nbsp; 附录" rel="next">
|
||||
<a href="../max_capacity_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 15.3. &nbsp; 最大容量问题" rel="next">
|
||||
<div class="md-footer__title">
|
||||
<span class="md-footer__direction">
|
||||
下一页
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
16. 附录
|
||||
15.3. 最大容量问题
|
||||
</div>
|
||||
</div>
|
||||
<div class="md-footer__button md-icon">
|
||||
|
Reference in New Issue
Block a user