Top-rated recipes tagged "algorithm"http://code.activestate.com/recipes/tags/algorithm/top/2017-05-12T10:40:58-07:00ActiveState Code RecipesA-star Shortest Path Algorithm (C++)
2010-12-25T23:36:35-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577457-a-star-shortest-path-algorithm/
<p style="color: grey">
C++
recipe 577457
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/graphs/">graphs</a>, <a href="/recipes/tags/routes/">routes</a>).
Revision 4.
</p>
<p>A-star (A*) is a shortest path algorithm widely used for RTS games, GPS navigation etc.</p>
Rsync Algorithm (Python)
2011-01-09T16:32:22-08:00Eric Pruitthttp://code.activestate.com/recipes/users/4170757/http://code.activestate.com/recipes/577518-rsync-algorithm/
<p style="color: grey">
Python
recipe 577518
by <a href="/recipes/users/4170757/">Eric Pruitt</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/delta/">delta</a>, <a href="/recipes/tags/diff/">diff</a>, <a href="/recipes/tags/patch/">patch</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/rsync/">rsync</a>).
Revision 4.
</p>
<p>This is a pure Python implementation of the <a href="http://samba.anu.edu.au/rsync/">rsync algorithm</a>. On my desktop (3.0GHz dual core, 7200RPM), best case throughput for target file hash generation and delta generation is around 2.9MB/s. Absolute worst case scenario (no blocks in common) throughput for delta generation is 200KB/s to 300KB/s on the same system.</p>
<p>Tested in Python 2.5, 2.6, and 3.1. In 2.7, io.BufferedReader should yield the best throughput. On all other versions use __builtin__.open.</p>
groupby() For Unsorted Input (Python)
2017-05-12T10:40:58-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580800-groupby-for-unsorted-input/
<p style="color: grey">
Python
recipe 580800
by <a href="/recipes/users/4182236/">Alfe</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/grouping/">grouping</a>, <a href="/recipes/tags/lazy/">lazy</a>).
</p>
<p>We all know the <code>groupby()</code> which is available in the <code>itertools</code> standard module. This one yields groups of consecutive elements in the input which are meant to be together in one group. For non-consecutive elements this will yield more than one group for the same key.</p>
<p>So effectively, <code>groupby()</code> only reformats a flat list into bunches of elements from that list without reordering anything. In practice this means that for input sorted by key this works perfect, but for unsorted input it might yield several groups for the same key (with groups for other keys in between). Typically needed, though, is a grouping with reordering if necessary.</p>
<p>I implemented a likewise lazy function (yielding generators) which also accepts ungrouped input.</p>
topological sorting again (Python)
2013-03-06T19:21:11-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578406-topological-sorting-again/
<p style="color: grey">
Python
recipe 578406
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/topological/">topological</a>).
Revision 9.
</p>
<p>Topological sorting is the answer to the following question : in a direct acyclic graph, how can I pick up nodes "in order", such as upstream nodes are always before downstream ones ? Many solutions may exists, many algorithms too.</p>
<p>Alas, it seems I'm too stupid to understand already proposed recipes on topological sorting. Hopefully I do grasp the "write once, read many" concept.</p>
<p>Here, you will find a plain algorithm, optimized only for code clarity, of a topological sorting for direct acyclic graphs, implemented in python from the pseudo code found on <a href="http://en.wikipedia.org/wiki/Topological_sorting">wikipedia</a>:</p>
<pre class="prettyprint"><code>L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
insert n into L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
</code></pre>
<p>Only tested with python3.2, should work with other versions. Be careful, code indented with tabs, since space is evil è_é</p>
Topological Sort (Python)
2012-09-27T12:21:23-07:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578272-topological-sort/
<p style="color: grey">
Python
recipe 578272
by <a href="/recipes/users/4172262/">Sam Denton</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/topological/">topological</a>, <a href="/recipes/tags/toposort/">toposort</a>).
</p>
<p>A topological sort (sometimes abbreviated topsort or toposort) or topological ordering of a directed graph is a linear ordering of its vertices such that, for every edge uv, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG).</p>
SPOJ backup script (Python)
2010-02-09T08:50:44-08:00Shashwat Anandhttp://code.activestate.com/recipes/users/4172995/http://code.activestate.com/recipes/577036-spoj-backup-script/
<p style="color: grey">
Python
recipe 577036
by <a href="/recipes/users/4172995/">Shashwat Anand</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/spoj/">spoj</a>, <a href="/recipes/tags/tools/">tools</a>).
Revision 3.
</p>
<h4>Introduction</h4>
<p>At Sphere Online Judge (<a href="http://www.spoj.pl" rel="nofollow">http://www.spoj.pl</a>) you are given the capability of trying out the
challenging problems given. It also gives you the capability of viewing
and downloading your own solution.</p>
<p>The tool spojbackup tends to automatically backup all your Accepted
submissions and save them on the desired location of your computer. The
basic idea is to automate the process which can be used as a backup and
an offline reference tool of your own codes.</p>
<h4>Features</h4>
<ul>
<li><p>Resume downloads.
spojbackup currently supports resuming of the solutions if internet
connection is disrupted</p></li>
<li><p>Incremental backup facility
it'll not download the code already present on your machine. Only newer
code added in your signedlist will be downloaded</p></li>
<li><p>User-defined destination
all codes are saved at user-defined destination
if no option is given by user it saves in the folder from where command is
run</p></li>
<li><p>Proxy support
Proxy support is provided as SPOJ users are generally university students
and they are generally behind a proxy and university firewall.</p></li>
</ul>
<h4>Bugs</h4>
<p>In case of finding a bug please drop a mail to the authors. We will try to sort
out the problems.</p>
Huffman coding, Encoder/Deconder (Python)
2009-01-04T04:11:37-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576603-huffman-coding-encoderdeconder/
<p style="color: grey">
Python
recipe 576603
by <a href="/recipes/users/4168519/">Shao-chuan Wang</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/compression/">compression</a>, <a href="/recipes/tags/decompression/">decompression</a>, <a href="/recipes/tags/huffman_code/">huffman_code</a>).
Revision 2.
</p>
<p>Please refer to wikipedia: <a href="http://en.wikipedia.org/wiki/Huffman_coding" rel="nofollow">http://en.wikipedia.org/wiki/Huffman_coding</a></p>
<p>Huffman coding is an entropy encoding algorithm used for lossless data compression. The term refers to the use of a variable-length code table for encoding a source symbol (such as a character in a file) where the variable-length code table has been derived in a particular way based on the estimated probability of occurrence for each possible value of the source symbol. It was developed by David A. Huffman while he was a Ph.D. student at MIT, and published in the 1952 paper "A Method for the Construction of Minimum-Redundancy Codes".</p>
Reversi Othello (Python)
2016-09-19T18:03:09-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580698-reversi-othello/
<p style="color: grey">
Python
recipe 580698
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/games/">games</a>).
</p>
<p>Reversi/Othello Board Game using Minimax, Alpha-Beta Pruning, Negamax, Negascout algorithms.</p>
Infix Expression Evaluation (Python)
2015-11-08T05:08:26-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579123-infix-expression-evaluation/
<p style="color: grey">
Python
recipe 579123
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/stack/">stack</a>).
</p>
<p>Infix expression evaluation using two stacks.</p>
Genetic Algorithm in Python source code - AI-Junkie tutorial (Python)
2012-06-19T12:59:13-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578128-genetic-algorithm-in-python-source-code-ai-junkie-/
<p style="color: grey">
Python
recipe 578128
by <a href="/recipes/users/4182015/">David Adler</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/artificial/">artificial</a>, <a href="/recipes/tags/genetic/">genetic</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/python/">python</a>).
Revision 5.
</p>
<p>A simple genetic algorithm program. I followed this tutorial to make the program <a href="http://www.ai-junkie.com/ga/intro/gat1.html." rel="nofollow">http://www.ai-junkie.com/ga/intro/gat1.html.</a></p>
<p>The objective of the code is to evolve a mathematical expression which calculates a user-defined target integer.</p>
<hr />
<p>KEY:</p>
<p>chromosome = binary list (this is translated/decoded into a protein in the format number --> operator --> number etc, any genes (chromosome is read in blocks of four) which do not conform to this are ignored.</p>
<p>protein = mathematical expression (this is evaluated from left to right in number + operator blocks of two)</p>
<p>output = output of protein (mathematical expression)</p>
<p>error = inverse of difference between output and target</p>
<p>fitness score = a fraction of sum of of total errors</p>
<hr />
<p>OTHER:</p>
<p>One-point crossover is used.</p>
<p>I have incorporated <strong>elitism</strong> in my code, which somewhat deviates from the tutorial but made my code more efficient (top ~7% of population are carried through to next generation)</p>
Chess Notation Player (Python)
2011-05-25T18:41:33-07:00Stijn de Graafhttp://code.activestate.com/recipes/users/4178055/http://code.activestate.com/recipes/577719-chess-notation-player/
<p style="color: grey">
Python
recipe 577719
by <a href="/recipes/users/4178055/">Stijn de Graaf</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/chess/">chess</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/notation/">notation</a>, <a href="/recipes/tags/viewer/">viewer</a>).
</p>
<p>This allows you to input the algebraic chess notation of a game, move by move, and the position of the pieces will be shown on the screen. The upper case letters represent Black pieces and the lower case letters represent White pieces. Most notations are accepted, including Castling and Disambiguating. For details on Algebraic Chess notation see: <a href="http://en.wikipedia.org/wiki/Algebraic_chess_notation." rel="nofollow">http://en.wikipedia.org/wiki/Algebraic_chess_notation.</a></p>
Simple Sudoku (Python)
2011-06-02T14:15:02-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577716-simple-sudoku/
<p style="color: grey">
Python
recipe 577716
by <a href="/recipes/users/4177294/">amir naghavi</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/oo/">oo</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sudoku/">sudoku</a>).
Revision 10.
</p>
<p>This is a simple sudoku game.</p>
converting numbers to their alphabetical style (Python)
2011-03-17T06:57:06-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577607-converting-numbers-to-their-alphabetical-style/
<p style="color: grey">
Python
recipe 577607
by <a href="/recipes/users/4177294/">amir naghavi</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/string/">string</a>).
Revision 4.
</p>
<p>converts numbers to alphabetical numbers </p>
Fibonacci Data Compression (Python)
2011-01-28T04:18:30-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577554-fibonacci-data-compression/
<p style="color: grey">
Python
recipe 577554
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/data_compression/">data_compression</a>).
</p>
<p>Data compression via Fibonacci encoding.</p>
Shannon-Fano Data Compression (Python)
2010-12-18T22:09:40-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577502-shannon-fano-data-compression/
<p style="color: grey">
Python
recipe 577502
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/data_compression/">data_compression</a>).
Revision 3.
</p>
<p>Shannon-Fano Data Compression.
It can compress any kind of file up to 4 GB.</p>
<p>(But trying to compress an already compressed file like zip, jpg etc. can produce a (slightly) larger file though. Shannon-Fano is not the best data compression algorithm anyway.)</p>
Huffman Data Compression (C++)
2010-12-01T02:47:09-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577480-huffman-data-compression/
<p style="color: grey">
C++
recipe 577480
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/data_compression/">data_compression</a>).
Revision 2.
</p>
<p>Huffman encoding (data compression).
Usage: huffman -i [input file name] -o [output file name] [-e|d]</p>
Fast Running Median using an Indexable Skiplist (Fast version) (Python)
2010-03-03T11:06:29-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577073-fast-running-median-using-an-indexable-skiplist-fa/
<p style="color: grey">
Python
recipe 577073
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/indexable/">indexable</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/running/">running</a>, <a href="/recipes/tags/skiplist/">skiplist</a>, <a href="/recipes/tags/statistics/">statistics</a>).
Revision 5.
</p>
<p>Fast version of r576930 reimplemented using a list of lists instead of a node class. </p>
Simple Permutations (Python)
2010-02-07T06:47:14-08:00manchesterboyhttp://code.activestate.com/recipes/users/4172234/http://code.activestate.com/recipes/577031-simple-permutations/
<p style="color: grey">
Python
recipe 577031
by <a href="/recipes/users/4172234/">manchesterboy</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/combinations/">combinations</a>, <a href="/recipes/tags/permutations/">permutations</a>).
Revision 2.
</p>
<p>Given a string of chars and a length, returns permutations of the specified length using the char string given in order. For example, given a string of "01" and a length of 3 returns 000, 001, 010, 011 ... 111</p>
Floyd-Steinberg dithering algorithm (Python)
2009-06-02T01:28:46-07:00Alan Holthttp://code.activestate.com/recipes/users/4170480/http://code.activestate.com/recipes/576788-floyd-steinberg-dithering-algorithm/
<p style="color: grey">
Python
recipe 576788
by <a href="/recipes/users/4170480/">Alan Holt</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/dithering/">dithering</a>, <a href="/recipes/tags/floyd_steinberg/">floyd_steinberg</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/manipulation/">manipulation</a>).
</p>
<p>Floyd-Steinberg dithering is an image dithering algorithm (see <a href="http://en.wikipedia.org/wiki/Floyd-Steinberg" rel="nofollow">http://en.wikipedia.org/wiki/Floyd-Steinberg</a> for more details). While the algorithm is mainly for image manipulation, I use it to create random locations for sensor networt devices.</p>
dfs and bfs graph traversal (Python)
2013-06-28T09:59:38-07:00bruce wernickhttp://code.activestate.com/recipes/users/4169952/http://code.activestate.com/recipes/576723-dfs-and-bfs-graph-traversal/
<p style="color: grey">
Python
recipe 576723
by <a href="/recipes/users/4169952/">bruce wernick</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/graph/">graph</a>).
</p>
<p>Very simple depth first search and breath first graph traversal. This is based on the find_path written by Guido van Rossum.</p>