<?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>Steven She at woggie.net &#187; groovy</title>
	<atom:link href="http://www.woggie.net/tag/groovy/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.woggie.net</link>
	<description>The life of a PhD Candidate in Software Engineering</description>
	<lastBuildDate>Wed, 16 Jun 2010 22:12:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Groovy Combinations Generator</title>
		<link>http://www.woggie.net/2008/12/13/groovy-combinations-generator/</link>
		<comments>http://www.woggie.net/2008/12/13/groovy-combinations-generator/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 20:14:49 +0000</pubDate>
		<dc:creator>Steven She</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[choose]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[combinations]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://www.woggie.net/?p=106</guid>
		<description><![CDATA[Ever needed to find all  k-combinations of a set? Of course! I&#8217;m pretty sure everybody has run into this problem one way or another, as part of your development work, a combinatorics assignment (eek!) or in every day life. For me, I needed to implement this for generating association rules. What better way to prototype [...]]]></description>
			<content:encoded><![CDATA[<p>Ever needed to find all  k-combinations of a set? Of course! I&#8217;m pretty sure everybody has run into this problem one way or another, as part of your development work, a combinatorics assignment (eek!) or in every day life. For me, I needed to implement this for generating association rules. What better way to prototype my eventual Java implementation than to use <a href="http://groovy.codehaus.org/">Groovy</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> choose<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">def</span> itemset, <span style="color: #993333;">int</span> choose<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> choose<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">def</span> itemset, <span style="color: #993333;">int</span> choose<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> results <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">//Initialize indices</span>
    <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> indices <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span>choose<span style="color: #66cc66;">&#93;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #b1b100;">in</span> 0..<span style="color: #66cc66;">&lt;</span>choose<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        indices<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> i
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">boolean</span> hasMore <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>hasMore<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">def</span> combo <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #b1b100;">in</span> 0..<span style="color: #66cc66;">&lt;</span>indices.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            combo <span style="color: #66cc66;">&lt;&lt;</span> itemset<span style="color: #66cc66;">&#91;</span>indices<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #66cc66;">&#125;</span>
        results <span style="color: #66cc66;">&lt;&lt;</span> combo
        hasMore <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">/* Closure to move the right-most index */</span>
            <span style="color: #993333;">int</span> rightMostIndex <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">/* Closure to find the right-most index */</span>
                    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #b1b100;">in</span> choose<span style="color: #66cc66;">-</span>1..0<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
                        <span style="color: #993333;">int</span> bounds <span style="color: #66cc66;">=</span> itemset.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> choose <span style="color: #66cc66;">+</span> i
                        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>indices<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&lt;</span> bounds<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> i
                    <span style="color: #66cc66;">&#125;</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>
            <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// execute closure</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">// increment all indices</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>rightMostIndex <span style="color: #66cc66;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                indices<span style="color: #66cc66;">&#91;</span>rightMostIndex<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">++</span>
                <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #b1b100;">in</span> rightMostIndex<span style="color: #66cc66;">+</span>1..<span style="color: #66cc66;">&lt;</span>choose<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    indices<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> indices<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">;</span>
                <span style="color: #66cc66;">&#125;</span>
                <span style="color: #808080; font-style: italic;">// there are still more combinations</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span> 
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #808080; font-style: italic;">// reached the end, no more combinations</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// execute closure</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> results
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I&#8217;ve based my implementation off one from <a href="http://www.amazon.ca/Applied-Combinatorics-Alan-Tucker/dp/0471735078">Applied Combinatorics by Alan Tucker</a>. First, there is an <code>indice</code> array that stores the k positions in the itemset. The items at these index locations are the k-combinations. The algorithm increases the right-most index until it reaches the last element of the itemset, then increases, the second right-most index and so on.</p>
<p>I wouldn&#8217;t recommend this implementation when dealing with large itemsets. A deficiency with this one-method approach is that a single list is constructed containing all of the combinations. This list can grow to be very large, very fast. It can be easily adapted to provide one combination at a time by refactoring the <code>hasMore</code> check into a separate method. This way, it would act like an iterator.  It&#8217;s too bad that Groovy doesn&#8217;t have support for the do-while loop as well, otherwise the <code>hasMore</code> closure could have been factored out into a really cool while check. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.woggie.net/2008/12/13/groovy-combinations-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
