<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Algorithms on When Moore's Law Ends</title><link>https://jimwang99.github.io/posts/algorithms/</link><description>Recent content in Algorithms on When Moore's Law Ends</description><generator>Hugo</generator><language>en-us</language><atom:link href="https://jimwang99.github.io/posts/algorithms/index.xml" rel="self" type="application/rss+xml"/><item><title>Boyer-Moore Voting Algorithm</title><link>https://jimwang99.github.io/posts/algorithms/boyer-moore-voting-algorithm/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://jimwang99.github.io/posts/algorithms/boyer-moore-voting-algorithm/</guid><description>&lt;p>#note #learn-with-chatgpt #algorithm&lt;/p>
&lt;blockquote class='book-hint '>
&lt;p>NOTE: this is a note from ChatGPT&lt;/p>&lt;/blockquote>&lt;p>The Boyer-Moore Voting Algorithm, also known simply as the Voting Algorithm, is a method for finding a majority element in a sequence of elements. This means it&amp;rsquo;s useful for determining if there&amp;rsquo;s an element in the sequence that appears more than &lt;code>n/2&lt;/code> times, where &lt;code>n&lt;/code> is the number of elements in the sequence.&lt;/p>
&lt;p>Here&amp;rsquo;s the basic idea:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Initialization&lt;/strong>: Start with an initial candidate and a count of 0.&lt;/li>
&lt;li>&lt;strong>Iteration&lt;/strong>: Go through the sequence element by element.&lt;/li>
&lt;/ol>
&lt;ul>
&lt;li>If the count is 0, set the current element as the candidate.&lt;/li>
&lt;li>If the current element is the same as the current candidate, increment the count.&lt;/li>
&lt;li>Otherwise, decrement the count.&lt;/li>
&lt;/ul>
&lt;ol start="3">
&lt;li>&lt;strong>Validation&lt;/strong>: The candidate at the end of the iteration is the potential majority element. However, one additional pass through the sequence is required to confirm if it is indeed the majority element.&lt;/li>
&lt;/ol>
&lt;p>The intuition behind this algorithm is that if there&amp;rsquo;s a majority element, it will outvote every other element combined.&lt;/p></description></item><item><title>Python `heapq` Priority queue (heap queue)</title><link>https://jimwang99.github.io/posts/algorithms/python-heapq-priority-queue-heap-queue/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://jimwang99.github.io/posts/algorithms/python-heapq-priority-queue-heap-queue/</guid><description>&lt;h2 id="attributes">Attributes&lt;a class="anchor" href="#attributes">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&amp;ldquo;Min heap&amp;rdquo;, where index 0 is the smallest item&lt;/li>
&lt;/ul>
&lt;h2 id="apis">APIs&lt;a class="anchor" href="#apis">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;code>heapq.heapify(iterable) -&amp;gt; None&lt;/code>: Create a heap queue &lt;strong>in-place&lt;/strong>&lt;/li>
&lt;li>&lt;code>heapq.heappush(heap, item) -&amp;gt; None&lt;/code>: Add a new item&lt;/li>
&lt;li>&lt;code>heapq.heappop(heap) -&amp;gt; T&lt;/code>: Pop the smallest item&lt;/li>
&lt;li>&lt;code>heapq.heappushpop(heap, item) -&amp;gt; T&lt;/code>: Push then pop the smallest&lt;/li>
&lt;li>&lt;code>heapq.heapreplace(heap, item) -&amp;gt; T&lt;/code>: Pop then push&lt;/li>
&lt;/ul></description></item><item><title>Traverse a graph</title><link>https://jimwang99.github.io/posts/algorithms/traverse-a-graph/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://jimwang99.github.io/posts/algorithms/traverse-a-graph/</guid><description>&lt;ol>
&lt;li>Mark all nodes as not visited.&lt;/li>
&lt;li>Create an empty &lt;code>ready&lt;/code> bucket to hold nodes that has been visited itself but not its neighbors yet.&lt;/li>
&lt;li>Find a starting &lt;code>node&lt;/code>, mark it as visited and put into the &lt;code>ready&lt;/code> bucket.&lt;/li>
&lt;li>Get an &lt;code>node&lt;/code> from the &lt;code>ready&lt;/code> bucket and look at its directly connected neighbors. Skip those that are visited already, mark the rest as &lt;code>visited&lt;/code> and put them into the &lt;code>ready&lt;/code> bucket.&lt;/li>
&lt;li>Repeat 4 until &lt;code>ready&lt;/code> is empty.&lt;/li>
&lt;/ol>
&lt;p>Depends on how you operate &lt;code>ready&lt;/code> bucket, you can achieve BFS (breadth first search) or DFS (depth first search).&lt;/p></description></item><item><title>Traverse a tree</title><link>https://jimwang99.github.io/posts/algorithms/traverse-a-tree/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://jimwang99.github.io/posts/algorithms/traverse-a-tree/</guid><description>&lt;p>&lt;strong>DFS (depth first search)&lt;/strong>&lt;/p>
&lt;p>DFS can be done easily with &lt;strong>recursive&lt;/strong> method, because you can treat the subtrees as new trees and use the same function to traverse them.&lt;/p>
&lt;p>&lt;strong>BFS (breadth first search)&lt;/strong>&lt;/p>
&lt;p>BFS will need extra space of a queue. When visiting a node, add all its children to this queue, before visiting its siblings by dequeue from the queue.&lt;/p></description></item></channel></rss>