<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nklein software &#187; recurrence relations</title>
	<atom:link href="http://nklein.com/tags/recurrence-relations/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Thu, 22 Dec 2011 04:42:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>A Recurrence of Constrained Shuffles</title>
		<link>http://nklein.com/2009/06/a-recurrence-of-constrained-shuffles/</link>
		<comments>http://nklein.com/2009/06/a-recurrence-of-constrained-shuffles/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 05:52:51 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[recurrence relations]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=478</guid>
		<description><![CDATA[Earlier, I posted about finding the Fibonacci numbers when counting a very simple constrained shuffle. In that article, I was concerned with the question: How many permutations are there in such that for all ? For that problem, the answer was . The number of permutations on items where no item moves more than one [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier, I <a href="http://nklein.com/2009/05/i-discovered-the-fibonacci-numbers/">posted about finding the Fibonacci numbers</a> when counting a very simple constrained shuffle.  In that article, I was concerned with the question:  <q>How many permutations are there in <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1aba8e8186e3ec0beefc6db1f718e449.png" title="\Sigma_n" style="vertical-align:-20%;" class="tex" alt="\Sigma_n" /> such that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_186426730ead7ce2dac6ae5849e841e9.png" title="\left| \pi(i) - i \right| \le 1" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le 1" /> for all <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e8e7c9058db45694b72005d4f4b41bb0.png" title="i \in [1,n]" style="vertical-align:-20%;" class="tex" alt="i \in [1,n]" />?</q></p>
<p>For that problem, the answer was <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e47ea81a9fb42594de930f03d9c022d7.png" title="F_{n+1}" style="vertical-align:-20%;" class="tex" alt="F_{n+1}" />.  The number of permutations on <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> items where no item moves more than one space from where it started is the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ad29876a0c7f492a05327ad527cf8d4a.png" title="(n+1)" style="vertical-align:-20%;" class="tex" alt="(n+1)" />-th Fibonacci number.  Now, the obvious question is: <q>How many permutations are there in <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1aba8e8186e3ec0beefc6db1f718e449.png" title="\Sigma_n" style="vertical-align:-20%;" class="tex" alt="\Sigma_n" /> such that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_043f5e06c892696b1a2d68676957086a.png" title="\left| \pi(i) - i \right| \le k" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le k" /> for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e8e7c9058db45694b72005d4f4b41bb0.png" title="i \in [1,n]" style="vertical-align:-20%;" class="tex" alt="i \in [1,n]" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_98d86ac06986d02b6e40f483093b61e0.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> is an arbitrary positive integer?</q></p>
<h3>Let&#8217;s not start so arbitrary just yet though&#8230;</h3>
<p>Let&#8217;s just look for a moment at how much trickier things become when we let <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e6a29db9de076157e600ff201406e059.png" title="k = 2" style="vertical-align:-20%;" class="tex" alt="k = 2" />.  It&#8217;s easy enough for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_63d46ba0a78b01e8ff176ed16ce409ae.png" title="n = 1" style="vertical-align:-20%;" class="tex" alt="n = 1" />.  There&#8217;s only one shuffle possible and it satisfies the constraint.  For that matter, the same is (conventionally) true for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_401e3786eabc9ae0c19764a05040a35e.png" title="n = 0" style="vertical-align:-20%;" class="tex" alt="n = 0" />.  When you have zero items, there is only one (very Zen) way to shuffle them, and it certainly does not violate the constraints.</p>
<p>For <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_15a5e27cb7797287a56125149455e6d0.png" title="n = 2" style="vertical-align:-20%;" class="tex" alt="n = 2" />, there are two shuffles.  Both satisfy the constraints.  Similarly, with <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_57adc49a1855f637d336bb87f20451ff.png" title="n = 3" style="vertical-align:-20%;" class="tex" alt="n = 3" />, there are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0f284b9717afa2fc3d73feb2507df8e9.png" title="3! = 6" style="vertical-align:-20%;" class="tex" alt="3! = 6" /> shuffles.  All of them satisfy the constraints.</p>
<p>For <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ff17260025ecb747696ef4b00e0dfd8b.png" title="n = 4" style="vertical-align:-20%;" class="tex" alt="n = 4" />, we get to the first interesting case.  Here, some of the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_27b0f6cfd9c75f6235cdc9ceeeb5f5ff.png" title=" 4! = 24 " style="vertical-align:-20%;" class="tex" alt=" 4! = 24 " /> shuffles violate the constraints.  So, how many shuffles are still possible?  To tackle this, we are going to use an <a href="http://en.wikipedia.org/wiki/Inclusion-exclusion_principle">inclusion-exclusion</a> counting.  The only items that could be out of place are the first and the last.  So, how many of the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fb15860410c3c88df6c2ffb26dd8ea34.png" title=" 4! " style="vertical-align:-20%;" class="tex" alt=" 4! " /> shuffles have either the first element move to the back or the last element move to the front?</p>
<p>There are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_dede0797e910bf3bafdde772dd82bfc9.png" title=" 3! " style="vertical-align:-20%;" class="tex" alt=" 3! " /> permutations that send the first element to the back.  There are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_dede0797e910bf3bafdde772dd82bfc9.png" title=" 3! " style="vertical-align:-20%;" class="tex" alt=" 3! " /> permutations that send the last element to the front.  We might then think that there are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_f1345b8099a3e44b970e9534f11d1694.png" title=" 4! - 2\cdot 3! " style="vertical-align:-20%;" class="tex" alt=" 4! - 2\cdot 3! " /> valid shuffles.  Alas, there are some shuffles that move both the first element to the back and the last element to the front.  We have counted those twice.  There are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_7e12d4921e8fdcd3a3caf34ec399ca47.png" title=" 2! = 2" style="vertical-align:-20%;" class="tex" alt=" 2! = 2" /> of those.</p>
<p>So, for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ff17260025ecb747696ef4b00e0dfd8b.png" title="n = 4" style="vertical-align:-20%;" class="tex" alt="n = 4" />, there are <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_aa6ecb4d3355c6192df380a00633c6d7.png" title=" 4! - 2\cdot 3! + 2! = 24 - 12 + 2 = 14" style="vertical-align:-20%;" class="tex" alt=" 4! - 2\cdot 3! + 2! = 24 - 12 + 2 = 14" /> shuffles satisfying our constraint that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e69aa45a64575b9175a80e0f3e183c73.png" title="\left| \pi(i) - i \right| \le 2" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le 2" />.</p>
<p>What then of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fdff41d5733ef6dab92131c8eebc1a38.png" title="n = 5" style="vertical-align:-20%;" class="tex" alt="n = 5" />?  Well, it&#8217;s bedtime here.  This is gonna have to wait.</p>
<p>Goodnight.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/06/a-recurrence-of-constrained-shuffles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I Discovered The Fibonacci Numbers</title>
		<link>http://nklein.com/2009/05/i-discovered-the-fibonacci-numbers/</link>
		<comments>http://nklein.com/2009/05/i-discovered-the-fibonacci-numbers/#comments</comments>
		<pubDate>Sun, 31 May 2009 06:19:22 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[recurrence relations]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=469</guid>
		<description><![CDATA[Alright, I wasn&#8217;t the first. But, I wasn&#8217;t particularly expecting to find them either. One nascent game concept that I have in my head requires me to do some constrained shuffling. As an example constraint, imagine that you need to shuffle a deck of cards while ensuring that no card moved more than five spots [...]]]></description>
			<content:encoded><![CDATA[<p>Alright, I wasn&#8217;t the first.  But, I wasn&#8217;t particularly expecting to find them either.</p>
<p>One nascent game concept that I have in my head requires me to do some constrained shuffling.  As an example constraint, imagine that you need to shuffle a deck of cards while ensuring that no card moved more than five spots away from where it started.  Or, imagine that you had to shuffle the deck so that a card of rank <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_de3adc820dbc4655c45b5555765fe84b.png" title="r" style="vertical-align:-20%;" class="tex" alt="r" /> could only move to spot that had been held by a card of rank <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_07981036a9403915ca58421b5035d3a4.png" title="r-1" style="vertical-align:-20%;" class="tex" alt="r-1" />, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_de3adc820dbc4655c45b5555765fe84b.png" title="r" style="vertical-align:-20%;" class="tex" alt="r" />, or <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0320f7d96f69a29b9d070d6e6203d522.png" title="r+1" style="vertical-align:-20%;" class="tex" alt="r+1" />.</p>
<p>In the general case, you have a directed graph with vertexes <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e4144550fde1519669847e2542ddb78.png" title="\left\{ v_1, v_2, v_3, \ldots, v_n \right\}" style="vertical-align:-20%;" class="tex" alt="\left\{ v_1, v_2, v_3, \ldots, v_n \right\}" /> and a permutation <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_45e9944b76bbb5b9e0dc513417fadf63.png" title="\pi" style="vertical-align:-20%;" class="tex" alt="\pi" /> such that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_6dbb0f5b8fd6dc5ebabc4b018ba28420.png" title="\pi(i)" style="vertical-align:-20%;" class="tex" alt="\pi(i)" /> can only equal <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_33c9ab784156bc3b75c894b91ea4ecf8.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" /> if there is an edge in the graph from <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_ad2c9ff366d76710bea05a69e6c3a857.png" title="v_i" style="vertical-align:-20%;" class="tex" alt="v_i" /> to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e3728333578d04c1d0065fcbc6f3187.png" title="v_j" style="vertical-align:-20%;" class="tex" alt="v_j" />.</p>
<p>Well, as this is for a game, it would get pretty boring if there were only a few possible shuffles.  On the other hand, if there are a large number of possible shuffles, then I haven&#8217;t really restricted the shuffling at all for all practical purposes.  So, then the question is:  for a given directed graph, how many possible shuffles are there?  Can I generate them without generating any of the invalid permutations, too?</p>
<p>Today, I started with the <q>How many?</q>-question.  I started with a very simple graph.  The vertexes are spaced evenly along an east-west line.  Each vertex is connected only to itself, its nearest neighbor to the east, and its nearest neighbor to the west.  Put another way, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_186426730ead7ce2dac6ae5849e841e9.png" title="\left| \pi(i) - i \right| \le 1" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le 1" /> for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e8e7c9058db45694b72005d4f4b41bb0.png" title="i \in [1,n]" style="vertical-align:-20%;" class="tex" alt="i \in [1,n]" />.</p>
<p>Let&#8217;s say we already had a row of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4782f2b0264558004f0824db5700cf6d.png" title="n-1" style="vertical-align:-20%;" class="tex" alt="n-1" /> vertexes.  If we add another vertex at the beginning of the row, then there are two things that a shuffle can do to that new vertex:  leave it where it is or swap it with what had been the first vertex in the row.  Now, suppose that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_53316fc356252c4cc4c536e64db68f2c.png" title="c(n)" style="vertical-align:-20%;" class="tex" alt="c(n)" /> is the count of valid shuffles for <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> vertexes in this configuration.  The case where our new vertex stays where it is leaves <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fa393d0c91ed7fde639a1aedc30f4381.png" title="c(n-1)" style="vertical-align:-20%;" class="tex" alt="c(n-1)" /> possibilities for the rest of the row.  The case where our new vertex swaps places with the old first vertex leaves <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_092d8f19093c611fb58af36f06cf78f3.png" title="c(n-2)" style="vertical-align:-20%;" class="tex" alt="c(n-2)" /> possibilities.  As such, <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1503d16977135f2e1a8c55f6f0131b6d.png" title="c(n) = c(n-1) + c(n-2)" style="vertical-align:-20%;" class="tex" alt="c(n) = c(n-1) + c(n-2)" />.</p>
<p>Cha-ching.  Note that <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1182f6f5e262beb08f3cc2a73fae5b6a.png" title="c(1) = 1" style="vertical-align:-20%;" class="tex" alt="c(1) = 1" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5601b17e73c45f0a3335669744079a38.png" title="c(2) = 2" style="vertical-align:-20%;" class="tex" alt="c(2) = 2" />, then <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_9fd81997a1905e65456c3a87a0085178.png" title="c(n) = F_{n+1}" style="vertical-align:-20%;" class="tex" alt="c(n) = F_{n+1}" /> where <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_6bcb9f483df51a4c41c69caa991e3e4c.png" title="F_i" style="vertical-align:-20%;" class="tex" alt="F_i" /> are the <a href="http://en.wikipedia.org/wiki/Fibonacci_number">Fibonacci numbers</a>.</p>
<p>Things get a great deal messier if we move up to <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e69aa45a64575b9175a80e0f3e183c73.png" title="\left| \pi(i) - i \right| \le 2" style="vertical-align:-20%;" class="tex" alt="\left| \pi(i) - i \right| \le 2" />.  More on that some other time&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/i-discovered-the-fibonacci-numbers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

