mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-01 00:39:05 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -2053,8 +2053,8 @@
|
||||
<p>From a backtracking perspective, <strong>we can view a permutation as the result of a sequence of choices</strong>. For input <span class="arithmatex">\([1, 2, 3]\)</span>, choosing <span class="arithmatex">\(1\)</span>, then <span class="arithmatex">\(3\)</span>, then <span class="arithmatex">\(2\)</span> produces <span class="arithmatex">\([1, 3, 2]\)</span>. Backtracking undoes a choice so that other choices can be tried.</p>
|
||||
<p>In the code, <code>choices</code> contains all the input elements, and <code>state</code> contains the elements chosen so far. Each element may be chosen only once, <strong>so every element in <code>state</code> must be unique</strong>.</p>
|
||||
<p>As shown in Figure 13-5, we can expand the search into a recursion tree, where each node represents the current <code>state</code>. Starting from the root, three rounds of choices lead to a leaf, and each leaf corresponds to one permutation.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_i.png" data-dark-src="/assets/figure-svg/figures/af05616151f302905d4f8c4d.json" data-src="/assets/figure-svg/figures/0bde23e48cadb7c3682fef37.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_i.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recursion tree for permutations</object><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_i.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recursion tree for permutations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-5 Recursion tree for permutations </p>
|
||||
<p><ha-svg-figure aria-label="Recursive choice tree for permutations of distinct elements" class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_i.png" data-dark-src="/assets/figure-svg/figures/af05616151f302905d4f8c4d.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/0bde23e48cadb7c3682fef37.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_i.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Recursion tree for permutations</object><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_i.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Recursion tree for permutations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-5 Recursion tree for permutations </p><div class="ha-sr-only figure-description"><h3>Description of Figure 13-5: Recursive choice tree for permutations of distinct elements</h3><p>Starting from an empty prefix, choose one unused element from [1, 2, 3] in each round and append it to state, producing 6 permutations after three rounds.</p><p>The candidate array is choices = [1, 2, 3], whose three elements are distinct and may each be used only once per permutation. The root node is drawn as three blank slots representing the empty prefix [] before any element is chosen; the blank slots themselves are not element values.</p><p>The figure expands all recursive branches simultaneously; in actual backtracking execution, after copying and saving state at a leaf node, the code pops the last element and marks it unselected again before trying sibling branches.</p><p>The choice tree has four levels, from the empty prefix at level 0 to the leaf nodes at level 3. The two tables below look up available next choices by prefix and list all six permutations by index.</p><table><caption>Available next choices by prefix</caption><thead><tr><th scope="col">Current prefix</th><th scope="col">Available elements</th><th scope="col">Next-level prefixes or leaf result</th></tr></thead><tbody><tr><th scope="row">[]</th><td>1, 2, 3</td><td>[1]; [2]; [3]</td></tr><tr><th scope="row">[1]</th><td>2, 3</td><td>[1, 2]; [1, 3]</td></tr><tr><th scope="row">[2]</th><td>1, 3</td><td>[2, 1]; [2, 3]</td></tr><tr><th scope="row">[3]</th><td>1, 2</td><td>[3, 1]; [3, 2]</td></tr><tr><th scope="row">[1, 2]</th><td>3</td><td>[1, 2, 3]</td></tr><tr><th scope="row">[1, 3]</th><td>2</td><td>[1, 3, 2]</td></tr><tr><th scope="row">[2, 1]</th><td>3</td><td>[2, 1, 3]</td></tr><tr><th scope="row">[2, 3]</th><td>1</td><td>[2, 3, 1]</td></tr><tr><th scope="row">[3, 1]</th><td>2</td><td>[3, 1, 2]</td></tr><tr><th scope="row">[3, 2]</th><td>1</td><td>[3, 2, 1]</td></tr></tbody></table><table><caption>All six permutations</caption><thead><tr><th scope="col">Result number</th><th scope="col">Complete permutation</th></tr></thead><tbody><tr><th scope="row">1</th><td>[1, 2, 3]</td></tr><tr><th scope="row">2</th><td>[1, 3, 2]</td></tr><tr><th scope="row">3</th><td>[2, 1, 3]</td></tr><tr><th scope="row">4</th><td>[2, 3, 1]</td></tr><tr><th scope="row">5</th><td>[3, 1, 2]</td></tr><tr><th scope="row">6</th><td>[3, 2, 1]</td></tr></tbody></table><p>Across the first, second, and third rounds, each node has 3, 2, and 1 available elements respectively, generating 3 × 2 × 1 = 6 leaf nodes corresponding to all 6 permutations of [1, 2, 3].</p></div>
|
||||
<h3 id="1-pruning-repeated-selections">1. Pruning repeated selections<a class="headerlink" href="#1-pruning-repeated-selections" title="Permanent link">¶</a></h3>
|
||||
<p>To ensure that each element is chosen only once, introduce a Boolean array <code>selected</code>, where <code>selected[i]</code> indicates whether <code>choices[i]</code> has been chosen. Use it for the following pruning steps.</p>
|
||||
<ul>
|
||||
@@ -2062,8 +2062,8 @@
|
||||
<li>When iterating over <code>choices</code>, skip all selected elements, pruning their branches.</li>
|
||||
</ul>
|
||||
<p>As shown in Figure 13-6, if we choose 1 first, 3 second, and 2 third, we must prune the branch for 1 in the second round and the branches for 1 and 3 in the third round.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_i_pruning.png" data-dark-src="/assets/figure-svg/figures/b325f3cc606bb833f225e3bf.json" data-src="/assets/figure-svg/figures/a83aed448b186989c0971a7a.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_i_pruning.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Example of pruning permutations</object><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_i_pruning.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Example of pruning permutations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-6 Example of pruning permutations </p>
|
||||
<p><ha-svg-figure aria-label="Pruning already chosen elements in permutations" class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_i_pruning.png" data-dark-src="/assets/figure-svg/figures/b325f3cc606bb833f225e3bf.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/a83aed448b186989c0971a7a.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_i_pruning.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Example of pruning permutations</object><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_i_pruning.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Example of pruning permutations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-6 Example of pruning permutations </p><div class="ha-sr-only figure-description"><h4>Description of Figure 13-6: Pruning already chosen elements in permutations</h4><p>Along the choice path 1 → 3 → 2, use selected to prune element indices already used in the current permutation prefix.</p><p>The input is choices = [1, 2, 3] (indices 0, 1, 2), with distinct elements, and each element may be used at most once in a permutation. Here state holds the current permutation prefix, while the Boolean array selected tracks by input index whether each element has already been chosen on the current path.</p><p>The selected array only prevents reusing an index along the current recursive path: descending skips marked indices, and backtracking unmarks them on return, so choosing 1 does not disable 1 permanently—it remains available in other permutation branches.</p><p>The figure fully expands only the path [] → [1] → [1, 3] → [1, 3, 2]. Pruning marks denote repeated selections that are not entered, such as [1, 1] for choosing element 1 at index 0 again under prefix [1]; ellipses mark valid branches whose continuations are left unexpanded. The table below summarizes choice restrictions and both kinds of branches by prefix.</p><table><caption>Choice restrictions along the highlighted path</caption><thead><tr><th scope="col">Current prefix</th><th scope="col">Used indices (with values)</th><th scope="col">Remaining available values</th><th scope="col">Value chosen next in figure</th><th scope="col">Pruned and unexpanded branches</th></tr></thead><tbody><tr><th scope="row">[]</th><td>None</td><td>1, 2, 3</td><td>1</td><td>None pruned; [2] and [3] are valid, continuations unexpanded</td></tr><tr><th scope="row">[1]</th><td>0 (value 1)</td><td>2, 3</td><td>3</td><td>Prune [1, 1]; [1, 2] is valid, continuation unexpanded</td></tr><tr><th scope="row">[1, 3]</th><td>0 (value 1), 2 (value 3)</td><td>2</td><td>2</td><td>Prune [1, 3, 1] and [1, 3, 3]; no unexpanded branches</td></tr></tbody></table><p>The highlighted path ultimately produces permutation [1, 3, 2].</p></div>
|
||||
<p>This pruning reduces the search space from <span class="arithmatex">\(O(n^n)\)</span> to <span class="arithmatex">\(O(n!)\)</span>.</p>
|
||||
<h3 id="2-code-implementation">2. Code implementation<a class="headerlink" href="#2-code-implementation" title="Permanent link">¶</a></h3>
|
||||
<p>With these details established, we can fill in the backtracking template. To keep the code short, we inline the template's helper functions into <code>backtrack()</code>:</p>
|
||||
@@ -2540,15 +2540,15 @@
|
||||
</div>
|
||||
<p>Suppose the input is <span class="arithmatex">\([1, 1, 2]\)</span>. To distinguish the two occurrences of <span class="arithmatex">\(1\)</span>, denote the second <span class="arithmatex">\(1\)</span> by <span class="arithmatex">\(\hat{1}\)</span>.</p>
|
||||
<p>As shown in Figure 13-7, half the permutations generated by the previous method are duplicates.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_ii.png" data-dark-src="/assets/figure-svg/figures/49f23b2c3a1aebf43f4cf937.json" data-src="/assets/figure-svg/figures/5e439d7929764620bc154bb3.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Duplicate permutations</object><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Duplicate permutations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-7 Duplicate permutations </p>
|
||||
<p><ha-svg-figure aria-label="Equal elements cause duplicate permutations" class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_ii.png" data-dark-src="/assets/figure-svg/figures/49f23b2c3a1aebf43f4cf937.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/5e439d7929764620bc154bb3.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Duplicate permutations</object><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Duplicate permutations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-7 Duplicate permutations </p><div class="ha-sr-only figure-description"><h3>Description of Figure 13-7: Equal elements cause duplicate permutations</h3><p>Expanding two elements of value 1 as separate candidates produces six paths representing only three distinct numeric permutations.</p><p>The input array is choices = [1, 1, 2] at indices 0, 1, and 2. Below, "first 1" and "second 1" distinguish indices 0 and 1; the figure places a hat over the second 1 as an identity marker, though both have numeric value 1. Each index is used at most once along a path, yet preventing index reuse with selected alone still yields numerically identical permutations.</p><p>At the top, state has three empty slots. Blue squares denote the first 1, cyan denotes the second 1, and purple denotes 2. Three rounds of choices fill the slots with values from unused indices, expanding into the six leaf nodes in the table below. Entries 3, 4, and 6 have orange borders pointing to "Duplicate permutations to remove". Leaf numbers run from left to right, and the kept-versus-duplicate pairings follow from the orange borders and numeric values.</p><table><caption>Six leaf nodes and duplicate pairings</caption><thead><tr><th scope="col">Leaf number (left to right)</th><th scope="col">Permutation distinguishing element identities</th><th scope="col">Marking in figure and summary</th></tr></thead><tbody><tr><th scope="row">1</th><td>[first 1, second 1, 2]</td><td>No orange border, kept</td></tr><tr><th scope="row">2</th><td>[first 1, 2, second 1]</td><td>No orange border, kept</td></tr><tr><th scope="row">3</th><td>[second 1, first 1, 2]</td><td>Orange border, duplicates entry 1</td></tr><tr><th scope="row">4</th><td>[second 1, 2, first 1]</td><td>Orange border, duplicates entry 2</td></tr><tr><th scope="row">5</th><td>[2, first 1, second 1]</td><td>No orange border, kept</td></tr><tr><th scope="row">6</th><td>[2, second 1, first 1]</td><td>Orange border, duplicates entry 5</td></tr></tbody></table><p>Once the element-identity markers are ignored, the six leaf nodes correspond to just three distinct numeric permutations: [1, 1, 2], [1, 2, 1], and [2, 1, 1].</p></div>
|
||||
<p>How can we remove these duplicates? A straightforward approach is to use a hash set to deduplicate the results. However, this is inelegant: <strong>branches that generate duplicate permutations are unnecessary and should be identified and pruned in advance</strong>, improving efficiency.</p>
|
||||
<h3 id="1-pruning-equal-elements">1. Pruning equal elements<a class="headerlink" href="#1-pruning-equal-elements" title="Permanent link">¶</a></h3>
|
||||
<p>In Figure 13-8, choosing <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(\hat{1}\)</span> in the first round is equivalent: every permutation produced under one choice is duplicated under the other. We should therefore prune <span class="arithmatex">\(\hat{1}\)</span>.</p>
|
||||
<p>Similarly, after choosing <span class="arithmatex">\(2\)</span> in the first round, choosing either <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(\hat{1}\)</span> in the second produces duplicate branches. Prune the second-round choice of <span class="arithmatex">\(\hat{1}\)</span> as well.</p>
|
||||
<p>In essence, <strong>within any one round of choices, we want to try only one of the elements with the same value</strong>.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning.png" data-dark-src="/assets/figure-svg/figures/95efecc68b9e7e213abda8cd.json" data-src="/assets/figure-svg/figures/ef0da466ff05de57d93fc36e.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Pruning duplicate permutations</object><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Pruning duplicate permutations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-8 Pruning duplicate permutations </p>
|
||||
<p><ha-svg-figure aria-label="Pruning equal-element branches within a round" class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning.png" data-dark-src="/assets/figure-svg/figures/95efecc68b9e7e213abda8cd.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/ef0da466ff05de57d93fc36e.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Pruning duplicate permutations</object><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Pruning duplicate permutations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-8 Pruning duplicate permutations </p><div class="ha-sr-only figure-description"><h4>Description of Figure 13-8: Pruning equal-element branches within a round</h4><p>Try the repeated value 1 only once per round of choices, pruning duplicate search subtrees in advance.</p><p>The input array is choices = [1, 1, 2] at indices 0, 1, and 2. Below, "first 1" and "second 1" distinguish indices 0 and 1; the figure places a hat over the second 1 as an identity marker, though both have numeric value 1.</p><p>Two constraints govern indices and values separately: selected tracks indices already used on the current path, while in the code below a per-round deduplication set duplicated records values not skipped by selected and actually tried in the current round. Each recursive call creates its own duplicated set; backtracking restores selected and state without removing values already recorded in that call's set.</p><p>The figure explicitly marks only the two same-round prunings that skip the second 1 under the root [] and under prefix [2]. Below, the "One round of choices for each prefix" table completes all six nonterminal prefixes, where the "Skipped due to used index" column follows from the code rules rather than explicit pruning icons in this figure; the "Three unique permutations" table lists each leaf's identity order alongside its numeric values.</p><table><caption>One round of choices for each prefix</caption><thead><tr><th scope="col">Current prefix</th><th scope="col">Kept candidates</th><th scope="col">Skipped due to used index</th><th scope="col">Skipped due to duplicate value in round</th></tr></thead><tbody><tr><th scope="row">[]</th><td>first 1, 2</td><td>None</td><td>second 1: value 1 already tried in this round</td></tr><tr><th scope="row">[first 1]</th><td>second 1, 2</td><td>first 1: index 0 already on path</td><td>None</td></tr><tr><th scope="row">[first 1, second 1]</th><td>2</td><td>first 1, second 1: corresponding indices already used</td><td>None</td></tr><tr><th scope="row">[first 1, 2]</th><td>second 1</td><td>first 1, 2: corresponding indices already used</td><td>None</td></tr><tr><th scope="row">[2]</th><td>first 1</td><td>2: index 2 already on path</td><td>second 1: value 1 already tried in this round</td></tr><tr><th scope="row">[2, first 1]</th><td>second 1</td><td>2, first 1: corresponding indices already used</td><td>None</td></tr></tbody></table><table><caption>Three unique permutations</caption><thead><tr><th scope="col">Result number</th><th scope="col">Identity notation distinguishing both 1s</th><th scope="col">Actual numeric result</th></tr></thead><tbody><tr><th scope="row">1</th><td>[first 1, second 1, 2]</td><td>[1,1,2]</td></tr><tr><th scope="row">2</th><td>[first 1, 2, second 1]</td><td>[1,2,1]</td></tr><tr><th scope="row">3</th><td>[2, first 1, second 1]</td><td>[2,1,1]</td></tr></tbody></table><p>A single path may still pick both 1s at their two different indices in sequence; within any single round, only one branch is expanded for value 1.</p></div>
|
||||
<h3 id="2-code-implementation_1">2. Code implementation<a class="headerlink" href="#2-code-implementation_1" title="Permanent link">¶</a></h3>
|
||||
<p>Building on the previous solution, create a hash set <code>duplicated</code> in each round to record the values already tried in that round and prune repeated values:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio"/><input id="__tabbed_2_2" name="__tabbed_2" type="radio"/><input id="__tabbed_2_3" name="__tabbed_2" type="radio"/><input id="__tabbed_2_4" name="__tabbed_2" type="radio"/><input id="__tabbed_2_5" name="__tabbed_2" type="radio"/><input id="__tabbed_2_6" name="__tabbed_2" type="radio"/><input id="__tabbed_2_7" name="__tabbed_2" type="radio"/><input id="__tabbed_2_8" name="__tabbed_2" type="radio"/><input id="__tabbed_2_9" name="__tabbed_2" type="radio"/><input id="__tabbed_2_10" name="__tabbed_2" type="radio"/><input id="__tabbed_2_11" name="__tabbed_2" type="radio"/><input id="__tabbed_2_12" name="__tabbed_2" type="radio"/><input id="__tabbed_2_13" name="__tabbed_2" type="radio"/><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label></div>
|
||||
@@ -3053,8 +3053,8 @@
|
||||
<li><strong>Pruning equal elements</strong>: Each round of choices (each call to <code>backtrack</code>) has its own <code>duplicated</code> set. It records the values already chosen in that round's <code>for</code> loop, ensuring that equal elements are tried only once in that round.</li>
|
||||
</ul>
|
||||
<p>Figure 13-9 shows the scope of each pruning condition. Each node represents a choice, and the nodes along a path from root to leaf form a permutation.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning_summary.png" data-dark-src="/assets/figure-svg/figures/7c6b3320e838fa0a368539a8.json" data-src="/assets/figure-svg/figures/db2fc04b5c4736b3fa8429d9.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning_summary.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Scope of the two pruning conditions</object><object data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning_summary.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Scope of the two pruning conditions</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-9 Scope of the two pruning conditions </p>
|
||||
<p><ha-svg-figure aria-label="Two permutation pruning rules restricting indices and values" class="animation-figure" data-book-key="en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning_summary.png" data-dark-src="/assets/figure-svg/figures/7c6b3320e838fa0a368539a8.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/db2fc04b5c4736b3fa8429d9.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning_summary.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Scope of the two pruning conditions</object><object aria-hidden="true" data="/en/chapter_backtracking/permutations_problem.assets/permutations_ii_pruning_summary.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Scope of the two pruning conditions</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 13-9 Scope of the two pruning conditions </p><div class="ha-sr-only figure-description"><h4>Description of Figure 13-9: Two permutation pruning rules restricting indices and values</h4><p>Compare side by side the two pruning conditions: no repeated indices along a path (selected) and no repeated values within a round (duplicated).</p><p>The input array is choices = [1, 1, 2] at indices 0, 1, and 2. Below, "first 1" and "second 1" distinguish indices 0 and 1; the figure places a hat over the second 1 as an identity marker, though both have numeric value 1.</p><p>Black scissors mark path-index pruning, which forbids reusing an index already on the current path until backtracking makes that index available again. Red scissors mark same-round value pruning: duplicated records only values not skipped by the black scissors and actually chosen during the current call's round, and each recursive call creates a fresh set whose scope differs from selected.</p><p>Each square node here represents a single choice, the prefix in the table concatenates the choices along the path from the root to that node, and a complete root-to-leaf path forms a permutation. The first table below separates the black and red scissors shown in the figure into two columns; a cell listing multiple candidates corresponds to multiple pruned branches. The second 1 under the root [] and under prefix [2] is pruned by red scissors, while all branches with already used indices are pruned by black scissors. The second table lists the three unique permutations in order.</p><table><caption>One round of choices for each prefix</caption><thead><tr><th scope="col">Current prefix</th><th scope="col">Kept candidates</th><th scope="col">Black scissors: skipped due to used index</th><th scope="col">Red scissors: skipped due to duplicate value in round</th></tr></thead><tbody><tr><th scope="row">[]</th><td>first 1, 2</td><td>None</td><td>second 1: value 1 already tried in this round</td></tr><tr><th scope="row">[first 1]</th><td>second 1, 2</td><td>first 1: index 0 already on path</td><td>None</td></tr><tr><th scope="row">[first 1, second 1]</th><td>2</td><td>first 1, second 1: corresponding indices already used</td><td>None</td></tr><tr><th scope="row">[first 1, 2]</th><td>second 1</td><td>first 1, 2: corresponding indices already used</td><td>None</td></tr><tr><th scope="row">[2]</th><td>first 1</td><td>2: index 2 already on path</td><td>second 1: value 1 already tried in this round</td></tr><tr><th scope="row">[2, first 1]</th><td>second 1</td><td>2, first 1: corresponding indices already used</td><td>None</td></tr></tbody></table><table><caption>Three unique permutations</caption><thead><tr><th scope="col">Result number</th><th scope="col">Identity notation distinguishing both 1s</th><th scope="col">Actual numeric result</th></tr></thead><tbody><tr><th scope="row">1</th><td>[first 1, second 1, 2]</td><td>[1,1,2]</td></tr><tr><th scope="row">2</th><td>[first 1, 2, second 1]</td><td>[1,2,1]</td></tr><tr><th scope="row">3</th><td>[2, first 1, second 1]</td><td>[2,1,1]</td></tr></tbody></table><p>A single path may still pick both 1s at their two different indices in sequence; within any single round, only one branch is expanded for value 1.</p></div>
|
||||
<!-- Source file information -->
|
||||
<!-- Was this page helpful? -->
|
||||
<!-- Previous and next pages link -->
|
||||
@@ -3176,5 +3176,5 @@
|
||||
<script src="../../javascripts/mathjax.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
|
||||
<script src="../../javascripts/animation_player.js"></script>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/00174ff861e190f5c1f09e34.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/70ae82c6406c9bdbda8e8362.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user