<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Generator(2)</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">Second</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the <span lang="en-ca"> second </span>edition of my </b><span lang="en-ca"><b>Generator class. It now generates partition solution to a set. This costs me </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>a lot of time to figour out. </b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Idea of program</b></font></span></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: </b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>How to partition an array? I originally want to use recursive method to do the job. It is coming from the </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b><a href="emptySet.htm#formulae">recursive formula </a>which I solve for the number of partition. But it turned out too complicated. After torture of</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>several days, I remember that separator idea can solve the partition and it is the simplest idea! Suppose there is</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>n elements in an array, then there are n-1 space between each two adjacent elements. Imagine one separator is put</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>on any these n-1 position will divide array into two partitions. And up to n-1 separators could be put to get </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>up to n subsets. Then the ways of all these partitions together are all the partitions of this array. Of course, </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>don't forget one last partition which are the set itself. </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>So, based on this idea, I internally use the combination generating method to do the length-1 combinations. Or</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>C(n-1, 1), C(n-1, 2), ... C(n-1, n-1).  And finally I separately output all elements in set to give the whole as</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>a partition as my combination method cannot work with 0.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>Before call chooseNumber method, I changed length to length-1, and set isInternal true which allow partition </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>output. These are not elegant!!! But I cannot give a better solution.</b></span></pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function</b></pre>
  <pre><span lang="en-ca">A. </span>void partition();</pre>
  <pre><span lang="en-ca">It first set length to length-1 and isInternal to be true. At end of function, set them back.</span></pre>
  <blockquote>
    <blockquote>
  <pre><b> </b></pre>
    </blockquote>
  </blockquote>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><span lang="en-ca"><b>All methods need to design a better user interface for people to use it easily.</b></span></pre>
  <pre><span lang="en-ca"><b>	</b></span></pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;

const int MaxNumber =100;

class Generator
{
private:
	bool isCombination;
	bool isInternal;
	int array[MaxNumber];
	int length;
	int nextDigit();
	int findSmallest(int next);
	void swapIndex(int a, int b);
	void formTail(int next);
	void output();
	void initialize(int number);
	void chooseNumber(int number, int start = 0);	
	void plugNumber(int start, int number);
	bool advance(int index);
	void outputPartition();
protected:
	void doGenerator();
	void doCombination();
public:
	Generator(int number =10, bool combination=false);
	void setLength(int number);
	int getLength() { return length;}	
	void slotMachine();
	void partition();
	void choose(int number);
};


int main()
{
	Generator P(5, true);

	P.partition();

	return 0;
}


void Generator::outputPartition()
{
	for (int i=0;i&lt;length;i++)
	{
		if (array[i]==1)
		{
			cout&lt;&lt;i&lt;&lt;&quot;, &quot;;
		}
	}
	cout&lt;&lt;endl;
}

void Generator::partition()
{
	setLength(length-1);
	isInternal = true;
	for (int i=1; i&lt;length;i++)   //the length is length-1;
	{
		chooseNumber(i);
	}
	for (i =0; i&lt; length; i++)
	{
		cout&lt;&lt;i&lt;&lt;&quot;, &quot;;
	}
	cout&lt;&lt;endl;
	setLength(length+1);
	isInternal = false;	
	
}


void Generator::plugNumber(int start, int number)
{
	for (int i=start; i&lt; length; i++)
	{
		array[i]= number;
	}
}

void Generator::chooseNumber(int number, int start)
{
	if ((number&lt; length - start) &amp;&amp; number&gt;0&amp;&amp; start&lt; length)
	{
		array[start] = 0;
		chooseNumber(number, start +1);
		array[start] = 1;
		chooseNumber(number -1, start+1);
	}
	else
	{
		if (number&gt;=length - start)
		{
			plugNumber(start, 1);
			output();
			plugNumber(start, 0); //restore
		}
		else
		{
			if (number==0)
			{
				plugNumber(start, 0);
				output();
			}	
		}
	}
}




void Generator::choose(int number)
{
	chooseNumber(number);
}

void Generator::doCombination()
{
	int index =0;
	while (true)
	{
		while (array[index])
		{
			array[index] = 0;
			index++;
			if (index==length)
			{
				return;
			}
		}
		array[index] = 1;

		output();	

		index =0;
	}
}

void Generator::doGenerator()
{
	int next =0;
	int small =0;
	while ((next = nextDigit()) != -1)
	{
		small = findSmallest(next);
		swapIndex(next, small);
		formTail(next);
		output();
	}
}

Generator::Generator(int number, bool combination)
{
	setLength(number);
	isInternal = false;
	isCombination = combination;
	initialize(number);
}


void Generator::output()
{
	if (isInternal)
	{
		outputPartition();
		return;
	}
	if (!isCombination)
	{
		cout&lt;&lt;&quot;\nThis is the Permutation:\n&quot;;
	
	}
	else
	{
		cout&lt;&lt;&quot;\nThis is the Combination:\n&quot;;
	}
	for (int i=0; i&lt; length; i++)
	{
		cout&lt;&lt;array[i]&lt;&lt;(i==length - 1?&quot;;&quot;:&quot;,&quot;);
	}
}


void Generator::slotMachine()
{
	output();
	if (!isCombination)
	{
		doGenerator();
	}
	else
	{
		doCombination();
	}
}



void Generator::formTail(int next)
{
	int start = next +1;
	int end = length -1;
	while (end &gt; start)
	{
		swapIndex(start, end);
		start++;
		end--;
	}
}

void Generator::swapIndex(int a, int b)
{
	int temp;
	temp = array[a];
	array[a] = array[b];
	array[b] = temp;
}

int Generator::findSmallest(int next)
{
	int small = length -1;
	while (array[next]&gt; array[small])
	{
		small--;
	}
	return small;
}


void Generator::setLength(int number)
{
	if (number&gt;1)
	{
		 length = number;		 
	}
}




int Generator::nextDigit()
{
	int next = length -2;
	while (array[next]&gt; array[next+1])
	{
		next--;
		if (next&lt;0)
		{
			return -1;
		}
	}
	return next;
}


void Generator::initialize(int number)
{
	if (!isCombination)
	{
		for (int i=0; i&lt; length; i++)
		{
			array[i]= i;
		}	
	}
	else
	{
		for (int i=0; i&lt; length; i++)
		{
			array[i]= 0;
		}
	}
}

</pre>

<pre>This is the permutation:
0,1,2,3;
This is the permutation:
0,1,3,2;
This is the permutation:
0,2,1,3;
This is the permutation:
0,2,3,1;
This is the permutation:
0,3,1,2;
This is the permutation:
0,3,2,1;
This is the permutation:
1,0,2,3;
This is the permutation:
1,0,3,2;
This is the permutation:
1,2,0,3;
This is the permutation:
1,2,3,0;
This is the permutation:
1,3,0,2;
This is the permutation:
1,3,2,0;
This is the permutation:
2,0,1,3;
This is the permutation:
2,0,3,1;
This is the permutation:
2,1,0,3;
This is the permutation:
2,1,3,0;
This is the permutation:
2,3,0,1;
This is the permutation:
2,3,1,0;
This is the permutation:
3,0,1,2;
This is the permutation:
3,0,2,1;
This is the permutation:
3,1,0,2;
This is the permutation:
3,1,2,0;
This is the permutation:
3,2,0,1;
This is the permutation:
3,2,1,0;</pre>

<p>¡¡</p>

<p><br>
This is the Combination:<br>
0,0,0,0;<br>
This is the Combination:<br>
1,0,0,0;<br>
This is the Combination:<br>
0,1,0,0;<br>
This is the Combination:<br>
1,1,0,0;<br>
This is the Combination:<br>
0,0,1,0;<br>
This is the Combination:<br>
1,0,1,0;<br>
This is the Combination:<br>
0,1,1,0;<br>
This is the Combination:<br>
1,1,1,0;<br>
This is the Combination:<br>
0,0,0,1;<br>
This is the Combination:<br>
1,0,0,1;<br>
This is the Combination:<br>
0,1,0,1;<br>
This is the Combination:<br>
1,1,0,1;<br>
This is the Combination:<br>
0,0,1,1;<br>
This is the Combination:<br>
1,0,1,1;<br>
This is the Combination:<br>
0,1,1,1;<br>
This is the Combination:<br>
1,1,1,1;</p>

<p><span lang="en-ca"><font color="#FF0000"><a name="result"></a>The following 
is the partition:</font>( How to interpret it? You see that in an array of 5 
elements, each line is one</span></p>

<p><span lang="en-ca">kind of partition. The number in each line represents that 
counting <font color="#FF0000">from no.0 elements, up to</font> which subscript 
of</span></p>

<p><span lang="en-ca">elements are included in a subset. For example, &quot;2,3&quot; 
means that 0,1,2 elements in array is in first subset, 3 </span></p>

<p><span lang="en-ca">is in second subset, 4 is in third subset. Why is this? 
Because the number &quot;2,3&quot; means put a separator at place</span></p>

<p><span lang="en-ca">after element 2(subscript 2 equal to number 3 element), 
and a separator after element 3(equal number 4 element).</span></p>

<p><span lang="en-ca">So, you see the number is really <b><font color="#FF0000">
the position of separators</font></b> which do the job of partition of an array.</span></p>

<p>¡¡</p>

<p>3, <br>
2, <br>
1, <br>
0, <br>
2, 3, <br>
1, 3, <br>
1, 2, <br>
0, 3, <br>
0, 2, <br>
0, 1, <br>
1, 2, 3, <br>
0, 2, 3, <br>
0, 1, 3, <br>
0, 1, 2, <br>
0, 1, 2, 3, <br>
¡¡</p>

<p>¡¡</p>

<p><span lang="en-ca"> <br>
¡¡</span></p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="Monopoly.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>