Popular recipes tagged "algorithms" but not "algorithm"http://code.activestate.com/recipes/tags/algorithms-algorithm/2015-07-30T13:40:41-07:00ActiveState Code RecipesPython 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> 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> 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> 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> Compare algorithms for heapq.smallest (Python) 2011-12-25T23:41:23-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577573-compare-algorithms-for-heapqsmallest/ <p style="color: grey"> Python recipe 577573 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/heaps/">heaps</a>, <a href="/recipes/tags/performance/">performance</a>). Revision 3. </p> <p>General purpose technique for counting comparisons in various searching and sorting applications.</p> Python Binary Search Tree (Python) 2011-08-08T00:15:09-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577830-python-binary-search-tree/ <p style="color: grey"> Python recipe 577830 by <a href="/recipes/users/4178890/">sivarama sarma</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/maximum/">maximum</a>, <a href="/recipes/tags/minimum/">minimum</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>A data structure that holds a sorted collection of values, and supports efficient insertion, deletion, sorted iteration, and min/max finding. Values may sorted either based on their natural ordering, or on a key function (specified as an argument to the search tree's constructor). The search tree may contain duplicate values (or multiple values with equal keys) -- the ordering of such values is undefined.</p> <p>This implementation was made with efficiency in mind. In particular, it is more than twice as fast as the other native-Python implementations I tried (which all use objects to store search tree nodes).</p> <p>See also: <a href="http://en.wikipedia.org/wiki/Binary_search_tree">http://en.wikipedia.org/wiki/Binary_search_tree</a>, <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">http://en.wikipedia.org/wiki/A*_search_algorithm</a></p> SleepSort (Timer version) (Python) 2011-06-18T05:37:06-07:00wong2http://code.activestate.com/recipes/users/4178345/http://code.activestate.com/recipes/577762-sleepsort-timer-version/ <p style="color: grey"> Python recipe 577762 by <a href="/recipes/users/4178345/">wong2</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/interest/">interest</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>Python version sleep sort by using threading.Timer</p> Python Binary Search Tree (Python) 2011-01-10T02:27:08-08:00Edward Loperhttp://code.activestate.com/recipes/users/2637812/http://code.activestate.com/recipes/577540-python-binary-search-tree/ <p style="color: grey"> Python recipe 577540 by <a href="/recipes/users/2637812/">Edward Loper</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/maximum/">maximum</a>, <a href="/recipes/tags/minimum/">minimum</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/sort/">sort</a>). Revision 2. </p> <p>A data structure that holds a sorted collection of values, and supports efficient insertion, deletion, sorted iteration, and min/max finding. Values may sorted either based on their natural ordering, or on a key function (specified as an argument to the search tree's constructor). The search tree may contain duplicate values (or multiple values with equal keys) -- the ordering of such values is undefined.</p> <p>This implementation was made with efficiency in mind. In particular, it is more than twice as fast as the other native-Python implementations I tried (which all use objects to store search tree nodes).</p> <p>See also: <a href="http://en.wikipedia.org/wiki/Binary_search_tree">http://en.wikipedia.org/wiki/Binary_search_tree</a>, <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">http://en.wikipedia.org/wiki/A*_search_algorithm</a></p> Simple graph algorithms with a modular design (Python) 2011-04-21T13:40:32-07:00jimmy2timeshttp://code.activestate.com/recipes/users/4177690/http://code.activestate.com/recipes/577668-simple-graph-algorithms-with-a-modular-design/ <p style="color: grey"> Python recipe 577668 by <a href="/recipes/users/4177690/">jimmy2times</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/breadth/">breadth</a>, <a href="/recipes/tags/depth/">depth</a>, <a href="/recipes/tags/directed/">directed</a>, <a href="/recipes/tags/first/">first</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/oriented/">oriented</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/theory/">theory</a>, <a href="/recipes/tags/undirected/">undirected</a>, <a href="/recipes/tags/visit/">visit</a>). Revision 7. </p> <p>The purpose of this recipe is to look at algorithmic graph theory from an object-oriented perspective.</p> <p>A graph is built on top of a dictionary indexed by its vertices, each item being the set of neighbours of the key vertex. This ensures that iterating through the neighbours of a vertex is still efficient in sparse graphs (as with adjacency lists) while at the same time checking for adjacency is expected constant-time (as with the adjacency matrix).</p> <p>Any valid class of graph must implement the interface defined by AbstractGraph.</p> <p>A generic search algorithm takes as input a graph, source and target vertices and a queue. A queue must implement the methods Q.get(), Q.put() and Q.empty() in such a way to get the desired order in visiting the vertices.</p> <p>Given this pattern, breadth-first and depth-first search are essentially defined by the corresponding expansion policies: the first one uses an actual FIFO queue, the second one a LIFO queue (or stack).</p> Aggregates using groupby, defaultdict and Counter (Python) 2011-02-07T15:38:09-08:00N Nhttp://code.activestate.com/recipes/users/1639254/http://code.activestate.com/recipes/577535-aggregates-using-groupby-defaultdict-and-counter/ <p style="color: grey"> Python recipe 577535 by <a href="/recipes/users/1639254/">N N</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>How to emulate SQL aggregate functions AVG, COUNT, MAX, MIN and SUM on csv type data files using tools from the standard library.</p> An interval mapping data structure (Python) 2010-12-23T12:51:27-08:00Matteo Dell'Amicohttp://code.activestate.com/recipes/users/2433284/http://code.activestate.com/recipes/577515-an-interval-mapping-data-structure/ <p style="color: grey"> Python recipe 577515 by <a href="/recipes/users/2433284/">Matteo Dell'Amico</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/interval/">interval</a>, <a href="/recipes/tags/mapping/">mapping</a>). </p> <p>This structure is a kind of dictionary which allows you to map data intervals to values. You can then query the structure for a given point, and it returns the value associated to the interval which contains the point. Boundary values don't need to be an integer.</p> <p>In this version, the excellent <a href="http://pypi.python.org/pypi/blist/">blist</a> library by Daniel Stutzbach is used for efficiency. By using the collections.MutableMapping abstract base class, the whole signature of mappings is supported.</p> Eight queen problem (Python) 2013-05-10T03:44:56-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/577438-eight-queen-problem/ <p style="color: grey"> Python recipe 577438 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/queen/">queen</a>). Revision 5. </p> <p><strong>Task</strong>:</p> <ul> <li>Think of a chess field 8x8.</li> <li>Place 8 queens in a way that no one threaten another one.</li> </ul> <p><strong>Intention</strong>:</p> <ul> <li>Writing an algorithm to provide all solutions</li> <li>Adjusting the algorithm in a way that it can handle chess fields by other sizes n x n.</li> </ul> <p><strong>Changes</strong>:</p> <ul> <li><strong>Revision 5</strong>: <ul> <li>can run providing "n" via command line parameter and </li> <li>run half of board adding the other solutions by mirroring (12x12 takes 4 seconds instead of 8 on my slow notebook)</li> </ul></li> <li>...</li> </ul> <p><strong>Other languages</strong>:</p> <ul> <li>My <a href="http://code.activestate.com/recipes/578497/">recipe 578497</a> for JavaScript</li> <li>...</li> </ul> Dijkstra's algorithm for shortest paths (Python) 2010-08-02T13:03:45-07:00poromenoshttp://code.activestate.com/recipes/users/4174550/http://code.activestate.com/recipes/577343-dijkstras-algorithm-for-shortest-paths/ <p style="color: grey"> Python recipe 577343 by <a href="/recipes/users/4174550/">poromenos</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Dijkstra(G,s) finds all shortest paths from s to each other vertex in the graph, and shortestPath(G,s,t) uses Dijkstra to find the shortest path from s to t. Uses the priorityDictionary data structure (<a href="http://code.activestate.com/recipes/117228/">Recipe 117228</a>) to keep track of estimated distances to each vertex.</p>