<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Algorithms · Tools</title><link>https://tools.eknath.dev/tags/algorithms/</link><description>Algorithms updates from Tools</description><language>en-us</language><atom:link href="https://tools.eknath.dev/tags/algorithms/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Mon, 01 Jan 0001 00:00:00 +0000</lastBuildDate><item><title>What running time can I afford?</title><link>https://tools.eknath.dev/tools/running-time/</link><guid isPermaLink="true">https://tools.eknath.dev/tools/running-time/</guid><category>tool</category><category>algorithms</category><description>Type how big your input can get and see the running time you can afford, from the row it falls in.</description><content:encoded><![CDATA[<h2 id="what-this-is-for">What this is for</h2>
<p>You are solving an algorithm question and the input can get large. This tool tells you which running time is fast enough, so you do not try an answer that will be too slow.</p>
<p><strong>Use it</strong> at the start, when you set a target. <strong>Skip it</strong> when the input is tiny, because then anything works.</p>
<p>As a rough guide, a computer does about 10^8 simple steps per second.</p>
<h2 id="try-it">Try it</h2>
<p>Type the largest value n can have. The row that fits is shown with the reason.</p>
<form class="tool" data-tool="running-time" data-logic="/tools/running-time/logic.mjs" data-data="/tools/running-time/data.json" hidden>
  <p class="tool-loading">Loading the tool…</p>
</form>

<h2 id="the-table">The table</h2>
<p>This is the same data the tool uses. If the tool does not load, read your answer from here.</p>
<table>
  <thead>
      <tr>
          <th>Limit on n</th>
          <th>Target running time</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>up to 12</td>
          <td><code>O(n!)</code>, permutations</td>
      </tr>
      <tr>
          <td>up to 25</td>
          <td><code>O(2^n)</code>, subsets, bitmask</td>
      </tr>
      <tr>
          <td>up to 500</td>
          <td><code>O(n^3)</code></td>
      </tr>
      <tr>
          <td>up to 5,000</td>
          <td><code>O(n^2)</code></td>
      </tr>
      <tr>
          <td>up to 10^6</td>
          <td><code>O(n log n)</code> or <code>O(n)</code></td>
      </tr>
      <tr>
          <td>up to 10^8</td>
          <td><code>O(n)</code>: one simple pass, nothing heavier</td>
      </tr>
      <tr>
          <td>more than 10^8</td>
          <td><code>O(log n)</code> or <code>O(1)</code>, usually a formula or a binary search</td>
      </tr>
  </tbody>
</table>
<h2 id="for-agents">For agents</h2>
<p>Run it from the repository, with no browser:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>node cli.mjs running-time <span style="color:#e6db74">&#39;{&#34;n&#34;: &#34;10^5&#34;}&#39;</span>
</span></span></code></pre></div><p>The result is JSON: <code>ok</code>, <code>summary</code>, and <code>matches</code>, a list of <code>{id, label, detail}</code>. Here <code>id</code> is the row it matched and <code>detail</code> says why. Accepted forms of the number: <code>100000</code>, <code>100,000</code>, <code>10^5</code>, <code>1e5</code>. A value that is not a number gives <code>ok: false</code> and no matches.</p>
<p>The logic is <code>/tools/running-time/logic.mjs</code> and the data is <code>/tools/running-time/data.json</code>. Every example in the data file is checked by <code>npm test</code>.</p>
<h2 id="related">Related</h2>
<ul>
<li><a href="https://qref.eknath.dev/skills/problem-solving/">Problem Solving</a>: the method this tool belongs to (step 2, set the target).</li>
</ul>
]]></content:encoded></item><item><title>What do you notice about the problem?</title><link>https://tools.eknath.dev/tools/technique-finder/</link><guid isPermaLink="true">https://tools.eknath.dev/tools/technique-finder/</guid><category>tool</category><category>algorithms</category><description>Tick what you notice about a problem and get the techniques to try, best match first.</description><content:encoded><![CDATA[<h2 id="what-this-is-for">What this is for</h2>
<p>You have a problem and an answer that is too slow, and you do not know which idea to reach for. Tick what you notice. Each thing you notice points at techniques, and the ones pointed at most often come first.</p>
<p><strong>Use it</strong> when you are stuck on how to speed something up. <strong>Skip it</strong> when you already know the technique.</p>
<h2 id="try-it">Try it</h2>
<p>Tick everything that applies.</p>
<form class="tool" data-tool="technique-finder" data-logic="/tools/technique-finder/logic.mjs" data-data="/tools/technique-finder/data.json" hidden>
  <p class="tool-loading">Loading the tool…</p>
</form>

<h2 id="the-table">The table</h2>
<p>This is the same data the tool uses. If the tool does not load, find your row here.</p>
<table>
  <thead>
      <tr>
          <th>If you notice</th>
          <th>Try</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>The same smaller problem comes up again and again</td>
          <td>Memoisation, dynamic programming</td>
      </tr>
      <tr>
          <td>The order of the items matters or could help</td>
          <td>Sort first</td>
      </tr>
      <tr>
          <td>You keep asking &ldquo;have I seen this before?&rdquo;</td>
          <td>Hash set or map</td>
      </tr>
      <tr>
          <td>You need sums or counts over ranges</td>
          <td>Prefix sums, difference array</td>
      </tr>
      <tr>
          <td>You are looking for pairs in sorted data</td>
          <td>Two pointers</td>
      </tr>
      <tr>
          <td>You are looking at a run of items that must meet a condition</td>
          <td>Sliding window</td>
      </tr>
      <tr>
          <td>The answer is feasible up to a point and never after</td>
          <td>Binary search on the answer</td>
      </tr>
      <tr>
          <td>The locally best choice seems safe</td>
          <td>Greedy, then check the exchange argument</td>
      </tr>
      <tr>
          <td>There are links, dependencies or reachability</td>
          <td>Graph model: BFS or DFS, topological sort, union-find</td>
      </tr>
      <tr>
          <td>The structure is nested or hierarchical</td>
          <td>Tree, recursion, stack</td>
      </tr>
      <tr>
          <td>n is small and there are choices to make</td>
          <td>Backtracking, bitmask</td>
      </tr>
      <tr>
          <td>You always need the smallest or largest so far</td>
          <td>Heap, monotonic stack or queue</td>
      </tr>
      <tr>
          <td>The problem splits into independent halves</td>
          <td>Divide and conquer</td>
      </tr>
  </tbody>
</table>
<h2 id="for-agents">For agents</h2>
<p>Run it from the repository, with no browser. The input lists the ids of the signals that apply (see <code>signals</code> in the data file):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>node cli.mjs technique-finder <span style="color:#e6db74">&#39;{&#34;signals&#34;: [&#34;order&#34;, &#34;pairs&#34;]}&#39;</span>
</span></span></code></pre></div><p>The result is JSON: <code>ok</code>, <code>summary</code>, and <code>matches</code>, a list of <code>{id, label, detail}</code>. Each match is a technique, ranked by how many of your signals point at it; <code>detail</code> names those signals. Ticking nothing, or only unknown ids, gives <code>ok: false</code>.</p>
<p>The logic is <code>/tools/technique-finder/logic.mjs</code> and the data is <code>/tools/technique-finder/data.json</code>. Every example in the data file is checked by <code>npm test</code>.</p>
<h2 id="related">Related</h2>
<ul>
<li><a href="https://qref.eknath.dev/skills/problem-solving/">Problem Solving</a>: the method this tool belongs to (step 4, find the lever).</li>
</ul>
]]></content:encoded></item></channel></rss>