<?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>Equivalence &#187; Algorithms</title>
	<atom:link href="http://www.equivalence.co.uk/archives/category/algorithms/feed" rel="self" type="application/rss+xml" />
	<link>http://www.equivalence.co.uk</link>
	<description>Technical Blog for Software Developers</description>
	<lastBuildDate>Wed, 21 Apr 2010 22:30:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>generating a unique range of numbers</title>
		<link>http://www.equivalence.co.uk/archives/719</link>
		<comments>http://www.equivalence.co.uk/archives/719#comments</comments>
		<pubDate>Thu, 26 Feb 2009 11:29:00 +0000</pubDate>
		<dc:creator>Gregg</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[random number]]></category>
		<category><![CDATA[set]]></category>
		<category><![CDATA[unique ids]]></category>

		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=719</guid>
		<description><![CDATA[Often at work I find the need to generate a set of n unique integers in a specified range. In order to do this as efficiently and as easily as possible, I created a small C# class, which a colleague thought may be of general interest. Hence, I&#8217;m posting it here. I&#8217;m sure someone else [...]]]></description>
			<content:encoded><![CDATA[<p>Often at work I find the need to generate a set of <code>n</code> unique integers in a specified range. In order to do this as efficiently and as easily as possible, I created a small C# class, which a colleague thought may be of general interest. Hence, I&#8217;m posting it here. I&#8217;m sure someone else has come up with a similar (or better) way to do this in the past, but I&#8217;m sharing my way regardless <img src='http://www.equivalence.co.uk/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' />  .</p>
<p>The code is shown below (and you <a href='http://www.equivalence.co.uk/wp-content/uploads/2009/02/uniquesetgenerator.cs'>download the C# class here</a>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #FF0000;">class</span> UniqueSetGenerator
 <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> store_<span style="color: #008000;">;</span>
      <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> size_<span style="color: #008000;">;</span>
      <span style="color: #0600FF;">private</span> Random random_<span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF;">public</span> UniqueSetGenerator<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> size, <span style="color: #FF0000;">int</span> start<span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
          size_ <span style="color: #008000;">=</span> size<span style="color: #008000;">;</span>
          store_ <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span>size<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
          random_ <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Random<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          PopulateArray<span style="color: #000000;">&#40;</span>start<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> PopulateArray<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> start<span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> size_<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
              store_<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> start<span style="color: #008000;">++;</span>
      <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> Delete<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> pos<span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
          <span style="color: #FF0000;">int</span> val <span style="color: #008000;">=</span> store_<span style="color: #000000;">&#91;</span>pos<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
          store_<span style="color: #000000;">&#91;</span>pos<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> store_<span style="color: #000000;">&#91;</span><span style="color: #008000;">--</span>size_<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
          <span style="color: #0600FF;">return</span> val<span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> GetRandomNumber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>size_ <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
             <span style="color: #0600FF;">return</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
          <span style="color: #0600FF;">return</span> Delete<span style="color: #000000;">&#40;</span>random_.<span style="color: #0000FF;">Next</span><span style="color: #000000;">&#40;</span>size_<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
 <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I think it&#8217;s pretty easy to see how this works, so I will not go into it in much detail discussing it, but the sequence of figures below shows the basic operations.</p>
<div id="attachment_735" class="wp-caption aligncenter" style="width: 460px"><img src="http://www.equivalence.co.uk/wp-content/uploads/2009/02/unique1.jpg" alt="First populate the array with the values." title="Example part 1" width="450" height="57" class="size-full wp-image-735" /><p class="wp-caption-text">First populate the array with the values.</p></div><br />
<div id="attachment_737" class="wp-caption aligncenter" style="width: 460px"><img src="http://www.equivalence.co.uk/wp-content/uploads/2009/02/unique2.jpg" alt="Use the Random class to obtain our first random number - in this case 6. Now copy the value at position size_ in the array to position 6, and decrement size_. Note not we don&#039;t actually delete anything from the array." title="Example part 2" width="450" height="57" class="size-full wp-image-737" /><p class="wp-caption-text">Use the <code>Random</code> class to obtain our first random number - in this case 6. Now copy the value at position <code>size_</code> in the array to position 6, and decrement <code>size_</code>. <em>Note</em>: not we don't actually delete anything from the array.</p></div><br />
<div id="attachment_740" class="wp-caption aligncenter" style="width: 460px"><img src="http://www.equivalence.co.uk/wp-content/uploads/2009/02/unique3.jpg" alt="We then use the Random class to generate another random number in our reduced range - in this case 3. We then copy the value at position size_ (i.e. 9) to position 3, and then decrement the size_ count as before. This process continues until size_ is reduced to 0." title="Example part 3" width="450" height="60" class="size-full wp-image-740" /><p class="wp-caption-text">We then use the <code>Random </code>class to generate another random number in our reduced range - in this case 3. We then copy the value at position <code>size_ </code>(i.e. 9) to position 3, and then decrement the <code>size_ </code>count as before. This process continues until size_ is reduced to 0.</p></div>
<p>So when would you require something like this? Well, say you need to generate unique random IDs with values 1 to 10, then you can use the class as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">    UniqueSetGenerator uniqueSet <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UniqueSetGenerator<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">9</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">int</span> id <span style="color: #008000;">=</span> uniqueSet.<span style="color: #0000FF;">GetRandomNumber</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Now do something with the id....</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>For the moment I have only included a C# version but I will update this post with a Java version soon.  Hope some of you find this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.equivalence.co.uk/archives/719/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>does not being aware of np-completeness make you dumb?</title>
		<link>http://www.equivalence.co.uk/archives/200</link>
		<comments>http://www.equivalence.co.uk/archives/200#comments</comments>
		<pubDate>Tue, 06 Jan 2009 15:59:59 +0000</pubDate>
		<dc:creator>Gregg</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Complexity]]></category>
		<category><![CDATA[Observations]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[jobs]]></category>
		<category><![CDATA[software developers]]></category>

		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=200</guid>
		<description><![CDATA[I have recently been reading a series of posts ([1][2][3]) from authors about the pros and cons of having or not having a degree in CS.  I&#8217;m sure the argument goes both ways, but my thoughts are focused on how does someone who has not spent 4 years obtaining a degree, accrue the knowledge they may need. [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been reading a series of posts ([<a href="http://www.billthelizard.com/2008/12/education-vs-experience.html">1</a>][<a href="http://blog.uncommons.org/2008/12/31/the-value-of-a-degree/">2</a>][<a href="http://www.danielgpratt.com/2009/01/confession-i-dont-have-degree.html">3</a>]) from authors about the pros and cons of having or not having a degree in CS.  I&#8217;m sure the argument goes both ways, but my thoughts are focused on how does someone who has not spent 4 years obtaining a degree, <span>accrue </span>the knowledge they may need.</p>
<p>For the purpose of this post I will use the concept of <a href="http://www.equivalence.co.uk/archives/63">NP-completeness</a> (or in fact algorithmics in general) as an example.  I think from <span>experience </span>I have learned that NP-completeness is a topic that quite a few developers are not familiar with despite its importance in the field.  Many may be aware of it through <a href="http://www.codinghorror.com/blog/">Jeff Atwood</a>&#8216;s <a href="http://www.codinghorror.com/blog/archives/001187.html">post </a>over at <a href="http://www.codinghorror.com/blog/">codinghorror</a>, where he suffered at the hands of the topic&#8217;s rigour.</p>
<p>So how does someone who has never obtained a CS degree find out about such a topic?  My feeling is that they don&#8217;t.  OK some people will go out their way to find such topics but I haven&#8217;t met too many that do.  Some may even point out that NP-completeness did (does) not appear on their course curriculum, in which case it probably boils down to <a href="http://blog.uncommons.org/2008/12/31/the-value-of-a-degree/">Dan Dyer</a>&#8216;s point about the virtues of choosing a good university course.  What I&#8217;m trying to say is that someone who goes to a decent university and obtains a good degree has spent (possibly) 4 years learning about the foundations of computing, giving them a good base to make important technical decisions.</p>
<p>For example.  You are hit with a problem at work, let&#8217;s say some sort of job scheduling problem or even <span><a href="http://www.equivalence.co.uk/archives/172">allocation </a></span><a href="http://www.equivalence.co.uk/archives/172">of people to resources</a>.  The problem seems easy when described and the agile developer takes over you and you start churning out code.  However, if this person had only taken a simple algorithms course at university, he might have thought twice about jumping into such a problem &#8211; as many variants of such problems are known to be NP-hard.</p>
<p>This brings me back round to my question, where do such developers learn about these things?  They may read articles on sites like dZone or reddit, but c&#8217;mon, let&#8217;s face it, these sites are pretty good at giving you the latest articles about Java, .NET, jQuery, but it&#8217;s rare that you see an article about algorithmic issues rise to the top of the popularity list.  I mean who wants to read the details of what a <a href="http://en.wikipedia.org/wiki/B%2B_tree">B+ Tree</a>, <a href="http://en.wikipedia.org/wiki/Trie">Trie</a>, <a href="http://en.wikipedia.org/wiki/Boyer–Moore_string_search_algorithm">Boyer-Moore string matching algorithm</a>, etc are, because it&#8217;s hard going, instead most will take the easy option of reading guff like <a href="http://www.equivalence.co.uk/archives/101">7 ways to write beautiful code</a>.  However, if you attend university you are forced to know many of the finer details of CS, as you are examined on them. OK, you may not need them for a simple web app, but an employer is certainly going to be happier that you have the details in your head should you need them.  The point is that you never know when you are going to need such things, and knowing when you do is maybe what you spend 4 years gaining.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.equivalence.co.uk/archives/200/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
