Popular recipes tagged "graph"http://code.activestate.com/recipes/tags/graph/2015-12-22T10:49:59-08:00ActiveState Code RecipesSimple Graph library (Python) 2015-12-22T10:49:59-08:00Narayana Chikkamhttp://code.activestate.com/recipes/users/4174427/http://code.activestate.com/recipes/579141-simple-graph-library/ <p style="color: grey"> Python recipe 579141 by <a href="/recipes/users/4174427/">Narayana Chikkam</a> (<a href="/recipes/tags/graph/">graph</a>). Revision 2. </p> <p>A collection of generally used Graph Algorithms in one place with simple constructs.</p> Simple Bash Text Mode Sine Curve Generator. (Bash) 2014-08-12T20:57:39-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578921-simple-bash-text-mode-sine-curve-generator/ <p style="color: grey"> Bash recipe 578921 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/cygwin/">cygwin</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>This bash script is a taster for a kids level, audio, text mode, sweep generator. The code just creates a single cycle of a quantised sine curve inside an 80 x 24 bash terminal. This will be the calculator for a sinewave sweep generator from about 50Hz the 12KHz... The code tells you more and the display is in comments at the end...</p> Strongly connected components of a directed graph. (Python) 2013-04-03T19:30:32-07:00Mark Dickinsonhttp://code.activestate.com/recipes/users/4172683/http://code.activestate.com/recipes/578507-strongly-connected-components-of-a-directed-graph/ <p style="color: grey"> Python recipe 578507 by <a href="/recipes/users/4172683/">Mark Dickinson</a> (<a href="/recipes/tags/connected/">connected</a>, <a href="/recipes/tags/directed/">directed</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/strong/">strong</a>, <a href="/recipes/tags/tarjan/">tarjan</a>). Revision 3. </p> <p>Two linear-time algorithms for finding the strongly connected components of a directed graph. <code>strongly_connected_components_tree</code> implements (a variant of) Tarjan's well-known algorithm for finding strongly connected components, while <code>strongly_connected_components_path</code> implements a path-based algorithm due (in this form) to Gabow.</p> <p>Edit: I added an iterative function <code>strongly_connected_components_iterative</code>; this is a direct conversion of <code>strongly_connected_components_path</code> into iterative form. It's therefore safe to use on high-depth graphs, without risk of running into Python's recursion limit.</p> graph (Python) 2015-10-15T21:33:32-07:00userhttp://code.activestate.com/recipes/users/4187240/http://code.activestate.com/recipes/578615-graph/ <p style="color: grey"> Python recipe 578615 by <a href="/recipes/users/4187240/">user</a> (<a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/graph/">graph</a>). Revision 4. </p> <p>Abstract Graph Class: A Graph is a very abstract and powerful data structure to hold many different kinds of data relationships. </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> 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> Simple Dijkstra Algorithm (Python) 2012-06-08T02:20:18-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578156-simple-dijkstra-algorithm/ <p style="color: grey"> Python recipe 578156 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/graph/">graph</a>). Revision 3. </p> <p>The algorithm uses the priority queue version of Dijkstra and return the distance between the source node and the others nodes d(s,i). </p> Experiment with Kaprekar's routine (Python) 2011-06-12T03:29:01-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577749-experiment-with-kaprekars-routine/ <p style="color: grey"> Python recipe 577749 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/6174/">6174</a>, <a href="/recipes/tags/dot/">dot</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/kaprekar/">kaprekar</a>). Revision 2. </p> <p>Explore the story behind the mysterious number 6174. Generate all possible chains leading to 6174, show their length and their patterns of convergence.</p> A-star Shortest Path Algorithm (Python) 2010-12-26T08:31:16-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577519-a-star-shortest-path-algorithm/ <p style="color: grey"> Python recipe 577519 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/graph/">graph</a>, <a href="/recipes/tags/routes/">routes</a>). Revision 3. </p> <p>A-star (A*) Shortest Path Algorithm</p> Dijkstra Shortest Path Algorithm (Python) 2011-10-29T18:46:53-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577506-dijkstra-shortest-path-algorithm/ <p style="color: grey"> Python recipe 577506 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/graph/">graph</a>, <a href="/recipes/tags/routes/">routes</a>). Revision 5. </p> <p>Dijkstra shortest path algorithm.</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> Mandelbrot trajectories (Python) 2011-04-06T18:18:48-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577642-mandelbrot-trajectories/ <p style="color: grey"> Python recipe 577642 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/interactive/">interactive</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>, <a href="/recipes/tags/widget/">widget</a>). </p> <p>An interactive graph to plot the trajectory of points on and off the mandelbrot set. Illustrates the use of sliders in matplotlib</p> Chaotic Function Analysis Graph (Python) 2010-12-10T03:31:50-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577487-chaotic-function-analysis-graph/ <p style="color: grey"> Python recipe 577487 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/chaos/">chaos</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). Revision 2. </p> <p>There are 3 chaotic functions to graph as examples.</p> <p>Graph of function (0) is a curve. Graph of function (1) is a group of lines. (Both are Xn+1 = f(Xn) type functions.)</p> <p>Graph of function (2) on the other hand is a plane. (It is Xn+1 = f(Xn, Xn-1) type function.)</p> <p>These mean there is a simple relationship between the previous and next X values in (0) and (1). (Next X value can always be predicted from the previous X value by using the graph of the function w/o knowing the function itself.) But (2) does not have a discernible relationship. (No prediction possible!) So (2) is clearly more chaotic than others. (I think it could be used as a Pseudo-Random Number Generator (PRNG).)</p> tgraph - Simple ASCII graphing utility (Python) 2011-07-28T21:13:23-07:00Drew Gulinohttp://code.activestate.com/recipes/users/4119417/http://code.activestate.com/recipes/577077-tgraph-simple-ascii-graphing-utility/ <p style="color: grey"> Python recipe 577077 by <a href="/recipes/users/4119417/">Drew Gulino</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/log/">log</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/tail/">tail</a>, <a href="/recipes/tags/unix/">unix</a>). </p> <p>Takes a stream of numbers and outputs simple ASCII graphs of those numbers</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> Creating fake data using numpy (Python) 2009-08-07T09:22:34-07:00tat.wrighthttp://code.activestate.com/recipes/users/4171357/http://code.activestate.com/recipes/576870-creating-fake-data-using-numpy/ <p style="color: grey"> Python recipe 576870 by <a href="/recipes/users/4171357/">tat.wright</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/fake/">fake</a>, <a href="/recipes/tags/gaussian/">gaussian</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/histogram/">histogram</a>, <a href="/recipes/tags/normal/">normal</a>, <a href="/recipes/tags/sample_data/">sample_data</a>). </p> <p>Create a deliberately bad normal distribution for sample data - varying the badness, mean, standard deviation and range of results shown.</p> Covert color space from HSV to RGB and RGB to HSV (Python) 2008-11-04T02:13:01-08:00Victor Linhttp://code.activestate.com/recipes/users/4167779/http://code.activestate.com/recipes/576554-covert-color-space-from-hsv-to-rgb-and-rgb-to-hsv/ <p style="color: grey"> Python recipe 576554 by <a href="/recipes/users/4167779/">Victor Lin</a> (<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/color_space/">color_space</a>, <a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/graph/">graph</a>). </p> <p>Functions for converting bwtween RGB and HSV color space.</p>