Popular recipes tagged "algorithms"http://code.activestate.com/recipes/tags/algorithms/popular/2016-09-19T18:03:09-07:00ActiveState Code RecipesReversi 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>
Simple Infix Expression Evaluation (Python)
2015-11-07T18:44:05-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579122-simple-infix-expression-evaluation/
<p style="color: grey">
Python
recipe 579122
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>Simple infix expression evaluation using a stack.</p>
Hopfield Artificial Neural Network (C++)
2016-07-03T21:58:49-07:00Jay ballerhttp://code.activestate.com/recipes/users/4194368/http://code.activestate.com/recipes/580688-hopfield-artificial-neural-network/
<p style="color: grey">
C++
recipe 580688
by <a href="/recipes/users/4194368/">Jay baller</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/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/neural_network/">neural_network</a>).
</p>
<p>(Discrete (Binary)) Hopfield Artificial Neural Network (ANN).</p>
<p>For more info see:</p>
<p><a href="http://en.wikipedia.org/wiki/Hopfield_net" rel="nofollow">http://en.wikipedia.org/wiki/Hopfield_net</a></p>
<p><a href="http://www.scholarpedia.org/article/Hopfield_network" rel="nofollow">http://www.scholarpedia.org/article/Hopfield_network</a></p>
Computing simple list functions recursively (Python)
2016-03-01T20:41:13-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580617-computing-simple-list-functions-recursively/
<p style="color: grey">
Python
recipe 580617
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/recursive/">recursive</a>).
</p>
<p>This recipe shows how to compute some simple list functions (functions that operate on lists) using simple recursive algorithms. Simple non-recursive algorithms exist for these tasks; this recipe is just for illustrative purposes. However, the principles described can be used for more complex recursive algorithms, for which non-recursive algorithms might be more complex than the recursive ones.</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>
Python A* Pathfinding (With Binary Heap) (Python)
2014-08-06T10:03:01-07:00Christian Careagahttp://code.activestate.com/recipes/users/4186639/http://code.activestate.com/recipes/578919-python-a-pathfinding-with-binary-heap/
<p style="color: grey">
Python
recipe 578919
by <a href="/recipes/users/4186639/">Christian Careaga</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/binary_search/">binary_search</a>).
Revision 3.
</p>
<p>Implementation of the A-star Pathfinding algorithm in Python, using Binary heap to sort the open list</p>
edge-coloring of a bipartite graph (Python)
2015-07-30T13:40:41-07:00Praveenhttp://code.activestate.com/recipes/users/4192603/http://code.activestate.com/recipes/579088-edge-coloring-of-a-bipartite-graph/
<p style="color: grey">
Python
recipe 579088
by <a href="/recipes/users/4192603/">Praveen</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Konig's theorem tells us that every bipartite graph with maximum vertex-degree d can be edge-colored with just d colors.
This recipe relies on a previous recipe by D.Eppstein (123641) : "Hopcroft-Karp bipartite matching".</p>
Flattening an arbitrarily nested list in Python (Python)
2014-10-05T21:44:45-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578948-flattening-an-arbitrarily-nested-list-in-python/
<p style="color: grey">
Python
recipe 578948
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This is a recipe to flatten a Python list which may have nested lists as items within it. It works for lists that have a maximum depth of nesting roughly equal to the recursion depth limit of Python, which is set to 1000 by default, though it can be increased with sys.setrecursionlimit().</p>
Simple Linear Regression with Pure Python (Python)
2014-07-31T15:55:15-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578914-simple-linear-regression-with-pure-python/
<p style="color: grey">
Python
recipe 578914
by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/machine_learning/">machine_learning</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/regression/">regression</a>).
</p>
<p>Linear regression is a very useful and simple to understand way for predicting values, given a set of training data. The outcome of the regression is a best fitting line function, which, by definition, is the line that minimizes the sum of the squared errors (When plotted on a 2 dimensional coordination system, the errors are the distance between the actual Y' and predicted Y' on the line.) In machine learning, this line equation Y' = b*x + A is solved using Gradient Descent to gradually approach to it. Also, there is a statistical approach that directly solves this line equation without using an iterative algorithm.</p>
<p>This recipe is a pure Python implementation of this statistical algorithm. It has no dependencies.</p>
<p>If you have pandas and numpy, you can test its result by uncommenting the assert lines.</p>
The Bentley-Knuth problem and solutions (Python)
2014-03-15T23:46:59-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578851-the-bentley-knuth-problem-and-solutions/
<p style="color: grey">
Python
recipe 578851
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text_processing/">text_processing</a>).
</p>
<p>This is a program Jon Bentley asked Donald Knuth to write, and is one that’s become familiar to people who use languages with serious text-handling capabilities: Read a file of text, determine the n most frequently used words, and print out a sorted list of those words along with their frequencies. I wrote 2 solutions for it earlier, in Python and in Unix shell. Also see the comment by a user on the post, giving another solution.</p>
Priority queue dictionary (Python)
2013-08-25T00:23:12-07:00Nezar Abdennurhttp://code.activestate.com/recipes/users/4187557/http://code.activestate.com/recipes/578643-priority-queue-dictionary/
<p style="color: grey">
Python
recipe 578643
by <a href="/recipes/users/4187557/">Nezar Abdennur</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/heap/">heap</a>, <a href="/recipes/tags/heapq/">heapq</a>, <a href="/recipes/tags/pqdict/">pqdict</a>, <a href="/recipes/tags/priority_queue/">priority_queue</a>).
Revision 4.
</p>
<p>An indexed priority queue implemented in pure python as a dict-like class. It is a stripped-down version of <a href="https://pypi.python.org/pypi/pqdict/">pqdict</a>. A Priority Queue Dictionary maps dictionary keys (dkeys) to updatable priority keys (pkeys).</p>
<p>The priority queue is implemented as a binary heap, which supports: </p>
<ul>
<li><p>O(1) access to the top priority element </p></li>
<li><p>O(log n) removal of the top priority element </p></li>
<li><p>O(log n) insertion of a new element</p></li>
</ul>
<p>In addition, an internal dictionary or "index" maps dictionary keys to the position of their entry in the heap. This index is maintained as the heap is manipulated. As a result, a PQ-dict also supports: </p>
<ul>
<li><p>O(1) lookup of an arbitrary element's priority key </p></li>
<li><p>O(log n) removal of an arbitrary element </p></li>
<li><p>O(log n) updating of an arbitrary element's priority key</p></li>
</ul>
<p>A data structure like this can be used to drive simulations, schedulers, certain greedy algorithms, merging streams of sorted data, and other applications where priorities may need to be changed frequently.</p>
A simple KD Tree example with custom Euclidean distance ball query. (Python)
2013-01-24T10:28:24-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578434-a-simple-kd-tree-example-with-custom-euclidean-dis/
<p style="color: grey">
Python
recipe 578434
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>In computer science, a k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). k-d trees are a special case of binary space partitioning trees.</p>
<p><a href="http://en.wikipedia.org/wiki/K-d_tree" rel="nofollow">http://en.wikipedia.org/wiki/K-d_tree</a></p>
Knight's Tour using Warnsdorff Algorithm (Python)
2012-12-17T06:04:19-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578382-knights-tour-using-warnsdorff-algorithm/
<p style="color: grey">
Python
recipe 578382
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>).
</p>
<p>Knight's Tour using Warnsdorff Algorithm.</p>
Knight's Tour Map using Warnsdorff's Algorithm (Python)
2012-12-18T01:05:51-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578386-knights-tour-map-using-warnsdorffs-algorithm/
<p style="color: grey">
Python
recipe 578386
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/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Solves Knight's Tour Problem using Warnsdorff's Algorithm for every square on the chessboard of arbitrary size.</p>
<p>It colors each initial square with a color that depends on the final square coordinates.
(This is a method used to create fractals.)
Many different formulas maybe used for coloring (based on abs/rel x/y/dist/ang).</p>
<p>Calculating each tour in full creates a highly chaotic image (and takes hours).
So I added maxItPercent to cut-off tours earlier.
(Using %10 creates an image that has both ordered and chaotic regions.)</p>
Averaging Literal Numbers (Python)
2012-12-06T13:44:42-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578365-averaging-literal-numbers/
<p style="color: grey">
Python
recipe 578365
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/demonstration/">demonstration</a>, <a href="/recipes/tags/numbers/">numbers</a>).
</p>
<p>One program that a teacher may assign to beginning programming students would be to take a list of numbers and average them together. This recipe does just that and is designed to be easy to read and follow, showing what is possible with a few lines of Python.</p>
A-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>
Converts from decimal to any base ( between 2 and 26 ) (Python)
2011-02-24T00:33:07-08:00Shashwat Anandhttp://code.activestate.com/recipes/users/4172995/http://code.activestate.com/recipes/577586-converts-from-decimal-to-any-base-between-2-and-26/
<p style="color: grey">
Python
recipe 577586
by <a href="/recipes/users/4172995/">Shashwat Anand</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/base/">base</a>).
</p>
<p>This function takes in any base-10 integer and returns the string representation of that number in its specified base-n form.
The code was inspired from <a href="http://code.activestate.com/recipes/65212-convert-from-decimal-to-any-base-number/" rel="nofollow">http://code.activestate.com/recipes/65212-convert-from-decimal-to-any-base-number/</a> , thereby improving upon it.</p>
2-3 Tree (Python)
2011-10-08T21:46:03-07:00Borishttp://code.activestate.com/recipes/users/4179529/http://code.activestate.com/recipes/577898-2-3-tree/
<p style="color: grey">
Python
recipe 577898
by <a href="/recipes/users/4179529/">Boris</a>
(<a href="/recipes/tags/2_3tree/">2_3tree</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/structure/">structure</a>, <a href="/recipes/tags/tree/">tree</a>).
</p>
<p>My implementation of 2-3 Trees on python</p>
Caching decorator with timeout, selective invalidation (Python)
2005-11-05T05:47:03-08:00Greg Steffensenhttp://code.activestate.com/recipes/users/2649990/http://code.activestate.com/recipes/442513-caching-decorator-with-timeout-selective-invalidat/
<p style="color: grey">
Python
recipe 442513
by <a href="/recipes/users/2649990/">Greg Steffensen</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>A caching decorator that garbage collects in a separate thread (for performance), allows each cached function to (optionally) set a custom maximum age for entries, and allows individual cache entries to be selectively invalidated.</p>
get all possible combinations of characters given a string (Python)
2011-08-15T04:15:42-07:00Yanghttp://code.activestate.com/recipes/users/4178968/http://code.activestate.com/recipes/577842-get-all-possible-combinations-of-characters-given-/
<p style="color: grey">
Python
recipe 577842
by <a href="/recipes/users/4178968/">Yang</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/permutation/">permutation</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>This will give a result that is more than a permutation, but all possible combinations. An example is when input is 'abc', the output would be:
a,ab,abc,ac,acb,b,ba,bac,bc,bca,c,ca,cab,cb,cba</p>