<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document"><title>combinatorial algorithm</title>

<style>
<!--
table.MsoTableGrid
	{border:1.0pt solid windowtext;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.0pt;
	font-family:"Times New Roman";
	}
 li.MsoNormal
	{mso-style-parent:"";
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";
	margin-left:0cm; margin-right:0cm; margin-top:0cm}
-->
</style></head><body rightmargin="1" bottommargin="1" marginheight="1" marginwidth="1">



<p align="left"><span lang="en-ca"><font color="#ff0000" size="6"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Trotter-Johnson Algorithm</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First </span>Edition</font></b></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><b>This is the programming assignment 4 of comp6661 and we are requested to use generate all the permutation pattern by order</b></span></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><b>of minimal change. There is well-known algorithm called Trotter-Johnson which Knuth refused to call it this way. Instead he</b></span></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><b>claimed that some people in church who ring bell discovered it long time ago.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <span lang="en-ca">Question 1: what is rank and successor of permutation 
	[2,4,6,7,5,3,1] in both lexicographic order and minimal change order?</span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca">Question 3:</span></div>
<div align="left">
  　</div>
<div align="left">
  (a) (6 marks) Implement a recursive version of the Trotter-Johnson algorithm 
	to<br>
	generate all permutations of 1, . . . , n in the minimal change order. The 
	iterative<br>
	version can be found as Alg. 2.20 on page 63 of the textbook, or as Alg. P 
	on<br>
	page 4 of Knuth¨s 2B.<br>
	As usual, print out a list of permutations generated for small values of n, 
	say, up<br>
	to n = 4. For large values of n, just count the number of permutations 
	generated,<br>
	and report the number.<br>
	What is the largest value of n that you program can handle if you are 
	limited to<br>
	at most 5 minutes of CPU time.<br>
	(b) (3 marks) For this part, we use the permutations generated in part (a) 
	to count the<br>
	number of permutations with no adjacent values. A permutation p = [p1, . . . 
	, pn]<br>
	of 1, . . . , n is said to have the property of no adjacent values if there 
	exists no<br>
	pairs of adjacent elements pi and pi+1 such that the difference of pi &#8722; pi+1 
	is 1 or<br>
	-1. For example, [2, 4, 1, 3] is a permutation with no adjacent values, but 
	[1, 2, 4, 3]<br>
	is not, because of the pairs (1, 2) and the (4, 3).<br>
	Use your program from part (a) as a starting point, and count the number of<br>
	permutations of 1, . . . , n which have the no adjacent values property. 
	Report the<br>
	numbers in a table.<br>
	(c) (1 mark) To count the number of permutations with no adjacent values, 
	would<br>
	it be better to start with a recursive program that generates permutations 
	in<br>
	lexicographical order, instead of the Trotter-Johnson algorithm? Why?<br>
	This is a thinking question. You do not have to implement a recursive 
	lexicographical<br>
	permutation generating program.</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font color="#ff0000" size="5"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<p><span lang="en-ca">The most interesting part of assignment of comb. algo. is 
that you can finish all questions by writing a program. The most</span></p>
<p><span lang="en-ca">boring part of assignment of comb. algo. is that all 
program are based on some well-defined algorithms which makes me feel </span></p>
<p><span lang="en-ca">following recipe in cooking. This may be the reason I 
always postpone to finish my assignment till last moment. </span></p>
<p><span lang="en-ca">As for question 3-c, I use a simple backtrack to prove it 
is a better way to solve this kind of searching problem. However,</span></p>
<p><span lang="en-ca">the result is not so encouraging.</span></p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><font size="3">Actually when prof. demoed his program as recursive version of Trotter-Johnson, I feel a bit sad as I didn't do like that.</font></span></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><font size="3">What I did is simply following the intuitive algorithm described in textbook which is a much easy job. And to make things</font></span></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><font size="3">worse is that I discovered that most of my classmates are doing like that way.</font></span>
</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font color="#ff0000" size="5"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font color="#ff0000" size="5"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div><br>
<br>
1. permutation.cpp<br>
<br>
　<br>
<br>
file name: permutation.cpp<br>
<br>
#include &lt;iostream&gt;<br>
#include &lt;ctime&gt;<br>
<br>
using namespace std;<br>
<br>
const int MaxNumber=16;<br>
<br>
//defines a callback function pointer type<br>
typedef void (*VisitFuncType)(int*, int);<br>
<br>
void question3_a_callback(int* array, int size);<br>
<br>
int permSize;<br>
<br>
int counter=0;<br>
int non_adjacent=0;<br>
<br>
void question3_a();<br>
void visit(int* array, int size, bool left2right, VisitFuncType callback);<br>
void displayArray(int* array, int size);<br>
<br>
<br>
int permLexRank(int* array, int size);<br>
<br>
int factor(int n);<br>
<br>
<br>
<br>
int factor(int n)<br>
{<br>
&nbsp;&nbsp;&nbsp; int result=1;<br>
&nbsp;&nbsp;&nbsp; for (int i=1; i&lt;=n; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; result*=i;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; return result;<br>
}<br>
<br>
void question3_b_callback(int* array, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; bool found=false;<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;size-1; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (abs(array[i]-array[i+1])==1)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; found=true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; if (!found)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; non_adjacent++;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //this is used for testing<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /*<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (permSize&lt;=6)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; displayArray(array, size);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; */<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void question3_b()<br>
{<br>
&nbsp;&nbsp;&nbsp; int data[1];<br>
&nbsp;&nbsp;&nbsp; time_t start, finish;<br>
<br>
&nbsp;&nbsp;&nbsp; for (int i=1; i&lt;=12; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //cout&lt;&lt;"now test permutation of size of "&lt;&lt;i&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; non_adjacent=0;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; permSize=i;<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; data[0]=1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; start=time(0);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; visit(data, 1, false, question3_b_callback);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; finish=time(0);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;i&lt;&lt;"&nbsp;
"&lt;&lt;non_adjacent&lt;&lt;"&nbsp; "&lt;&lt;finish-start&lt;&lt;"
seconds"&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //cout&lt;&lt;"in size of
"&lt;&lt;i&lt;&lt;"the total number of non_adjacent permutation is:"<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp; &lt;&lt;non_adjacent&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //cout&lt;&lt;"the total running time in seconds is:"&lt;&lt;finish-start&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void question3_a()<br>
{<br>
&nbsp;&nbsp;&nbsp; time_t start, finish;<br>
&nbsp;&nbsp;&nbsp; int data[1]={1};<br>
<br>
&nbsp;&nbsp;&nbsp; for (int i=1; i&lt;=12; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"now test permutation of size of "&lt;&lt;i&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; permSize=i;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; data[0]=1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; start=time(0);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; visit(data, 1, false, question3_a_callback);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; finish=time(0);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"\nthe total number of permutation is:"&lt;&lt;counter&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"the total running time in seconds is:"&lt;&lt;finish-start&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void insert(int* array, int size, int pos, int value)<br>
{<br>
&nbsp;&nbsp;&nbsp; //right shift by one from pos<br>
&nbsp;&nbsp;&nbsp; for (int i=size; i&gt;pos; i--)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[i]=array[i-1];<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; array[pos]=value;<br>
}<br>
<br>
void displayArray(int* array, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;"[";<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;size; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;array[i];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (i!=size-1)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;",";<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;"]\n";<br>
}<br>
<br>
void question3_a_callback(int* array, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; counter++;<br>
&nbsp;&nbsp;&nbsp; if (permSize&lt;6)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; displayArray(array, size);<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
<br>
void visit(int* array, int size, bool left2right, VisitFuncType callback)<br>
{<br>
&nbsp;&nbsp;&nbsp; if (size==permSize)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; callback(array, size);<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return ;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; int local[MaxNumber];<br>
&nbsp;&nbsp;&nbsp; int pos=left2right?0:size;<br>
&nbsp;&nbsp;&nbsp; int shift=left2right?1:-1;<br>
&nbsp;&nbsp;&nbsp; int boundary=left2right?size+1:-1;<br>
&nbsp;&nbsp;&nbsp; //this is the most tricky part and I cannot figure it out for one hour<br>
&nbsp;&nbsp;&nbsp; //finally I tried and tested and get this unexplanable format<br>
&nbsp;&nbsp;&nbsp; bool temp=size%2==0?left2right:false;<br>
<br>
&nbsp;&nbsp;&nbsp; do<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; memcpy(local, array, size*sizeof(int));<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; insert(local, size, pos, size+1);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //visit(local, size+1, (size+pos)%2==1);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; visit(local, size+1, temp, callback);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; temp=!temp;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pos+=shift;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; while (pos!=boundary);<br>
&nbsp;&nbsp;&nbsp; /*<br>
&nbsp;&nbsp;&nbsp; if (size==MaxNumber-1)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"\n";<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; */<br>
}<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
int permRank(int* array, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; int r=0;<br>
&nbsp;&nbsp;&nbsp; int i, j, k;<br>
&nbsp;&nbsp;&nbsp; for (j=2; j&lt;=size; j++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; k=0; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i=0;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (array[i]!=j)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (array[i]&lt;j)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; k++;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i++;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (r%2==0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r=r*j+j-k-1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r=j*r+k;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; return r;<br>
}<br>
<br>
void permRankDemo()<br>
{<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber]={1,3,2};<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;permRank(array, MaxNumber)&lt;&lt;endl;<br>
}<br>
<br>
void permUnRank(int rank, int size, int*array)<br>
{<br>
&nbsp;&nbsp;&nbsp; int r1=0, r2=0, r=rank, i, j, k;<br>
&nbsp;&nbsp;&nbsp; array[0]=1;<br>
&nbsp;&nbsp;&nbsp; for (j=2; j&lt;=size; j++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r1=r*factor(j)/factor(size);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; k=r1-j*r2;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (r2%2==0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i=j-1; i&gt;=j-k; i--)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[i]=array[i-1];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[j-k-1]=j;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i=j-1; i&gt;=k+1; i--)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[i]=array[i-1];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[k]=j;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r2=r1;<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void permUnRankDemo()<br>
{<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber];<br>
&nbsp;&nbsp;&nbsp; permUnRank(13, 4, array);<br>
&nbsp;&nbsp;&nbsp; displayArray(array, 4);<br>
}<br>
<br>
int permParity(int* array, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; bool flags[MaxNumber];<br>
&nbsp;&nbsp;&nbsp; int i, j, c=0;<br>
&nbsp;&nbsp;&nbsp; for (i=0; i&lt;size; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; flags[i]=false;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; for (j=1; j&lt;=size; j++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (!flags[j-1])<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; c++;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; flags[j-1]=true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i=j;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (array[i-1]!=j)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i=array[i-1];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; flags[i-1]=true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; return (size-c)%2;<br>
}<br>
<br>
void permSuccessor(int* array, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; int st=0, m, d, parity;<br>
&nbsp;&nbsp;&nbsp; bool done=false;<br>
&nbsp;&nbsp;&nbsp; int local[MaxNumber];<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;size; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; local[i]=array[i];<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; m=size;<br>
&nbsp;&nbsp;&nbsp; while (m&gt;1&amp;&amp;!done)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; d=1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (local[d-1]!=m)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; d++;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i=d; i&lt;m; i++)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; local[i-1]=local[i];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; parity=permParity(local, m-1);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (parity==1)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (d==m)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; m--;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int temp=array[st+d-1];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[st+d-1]=array[st+d];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[st+d]=temp;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; done=true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (d==1)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; m--;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; st++;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int temp=array[st+d-1];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[st+d-1]=array[st+d-2];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[st+d-2]=temp;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; done=true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; if (m==1)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"undefined\n";<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return ;<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void permSuccessorDemo()<br>
{<br>
&nbsp;&nbsp;&nbsp; int size=4;<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber]={1,2,3,4};<br>
&nbsp;&nbsp;&nbsp; int bound=factor(size);<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;bound-1; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; permSuccessor(array, size);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; displayArray(array, size);<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
//lexico part <br>
void permLexSuccessor(int* array, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; int i, j, t;<br>
&nbsp;&nbsp;&nbsp; i=size-2;<br>
&nbsp;&nbsp;&nbsp; while (array[i+1]&lt;array[i])<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i--;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; if (i&lt;0)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"undefined\n";<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return ;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; j=size-1;<br>
&nbsp;&nbsp;&nbsp; while (array[j]&lt;array[i])<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; j--;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; t=array[j];<br>
&nbsp;&nbsp;&nbsp; array[j]=array[i];<br>
&nbsp;&nbsp;&nbsp; array[i]=t;<br>
<br>
&nbsp;&nbsp;&nbsp; int bound=(size-i-1)/2;<br>
&nbsp;&nbsp;&nbsp; for (int k=0; k&lt;bound; k++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; t=array[i+1+k];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[i+1+k]=array[size-1-k];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[size-1-k]=t;<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void lexSuccessorDemo()<br>
{<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber]={1, 2, 3, 4};<br>
&nbsp;&nbsp;&nbsp; int size=factor(MaxNumber);<br>
<br>
&nbsp;&nbsp;&nbsp; displayArray(array, MaxNumber);<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;size-1; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; permLexSuccessor(array, MaxNumber);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; displayArray(array, MaxNumber);<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
//assume contents in array starts from 1 instead of 0<br>
int permLexRank(int* array, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; int local[MaxNumber];<br>
&nbsp;&nbsp;&nbsp; int result=0;<br>
&nbsp;&nbsp;&nbsp; memcpy(local, array, sizeof(int)*size);<br>
&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;size; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; result+=(local[i]-1)*factor(size-i-1);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int j=i+1; j&lt;size; j++)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (local[j]&gt;local[i])<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; local[j]--;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; return result;<br>
}<br>
<br>
<br>
<br>
void question1_a()<br>
{<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber]={2,4,6,7,5,3,1};<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;"the old array is:\n";<br>
&nbsp;&nbsp;&nbsp; displayArray(array, 7);<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;"the lex rank is:"&lt;&lt;permLexRank(array, 7)&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;"the lex successor is:\n";<br>
&nbsp;&nbsp;&nbsp; permLexSuccessor(array, 7);<br>
&nbsp;&nbsp;&nbsp; displayArray(array, 7);<br>
}<br>
<br>
void test1_a()<br>
{<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber]={1,2,3,4,5,6,7};<br>
&nbsp;&nbsp;&nbsp; for (int i=1; i&lt;=1055; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"i="&lt;&lt;i&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; permLexSuccessor(array, 7);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; displayArray(array, 7);<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
<br>
void question1_b()<br>
{<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber]={2,4,6,7,5,3,1};<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;"the old array is:\n";<br>
&nbsp;&nbsp;&nbsp; displayArray(array, 7);<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;"the Trotter-Johnson rank is:"&lt;&lt;permRank(array, 7)&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; cout&lt;&lt;"the Trotter-Johnson successor is:\n";<br>
&nbsp;&nbsp;&nbsp; permSuccessor(array, 7);<br>
&nbsp;&nbsp;&nbsp; displayArray(array, 7);<br>
}<br>
<br>
void test1_b()<br>
{<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber]={1,2,3,4,5,6,7};<br>
&nbsp;&nbsp;&nbsp; displayArray(array, 7);<br>
&nbsp;&nbsp;&nbsp; for (int i=1; i&lt;=3888; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"i="&lt;&lt;i&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; permSuccessor(array, 7);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; displayArray(array, 7);<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void backtrack(int* array, int index, int size)<br>
{<br>
&nbsp;&nbsp;&nbsp; if (index==size)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //displayArray(array, size);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; non_adjacent++;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; bool noGood;<br>
&nbsp;&nbsp;&nbsp; for (int i=1; i&lt;=size; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; noGood=abs(array[index-1]-i)==1;<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (!noGood)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int j=0; j&lt;index; j++)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (array[j]==i)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; noGood=true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (!noGood)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[index]=i;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; backtrack(array, index+1, size);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
void question3_c()<br>
{<br>
&nbsp;&nbsp;&nbsp; time_t start, finish;<br>
<br>
&nbsp;&nbsp;&nbsp; int array[MaxNumber];<br>
&nbsp;&nbsp;&nbsp; for (int i=1; i&lt;=12; i++)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; non_adjacent=0;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;i&lt;&lt;" ";<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; start=time(0);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int j=1; j&lt;=i; j++)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[0]=j;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; backtrack(array, 1, i);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; finish=time(0);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;" "&lt;&lt;non_adjacent&lt;&lt;" "&lt;&lt;finish-start&lt;&lt;endl;<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
<br>
<br>
<br>
<br>
int main()<br>
{<br>
&nbsp;&nbsp;&nbsp; question1_a();<br>
&nbsp;&nbsp;&nbsp; question1_b();<br>
&nbsp;&nbsp;&nbsp; question3_a();<br>
&nbsp;&nbsp;&nbsp; question3_b();<br>
&nbsp;&nbsp;&nbsp; question3_c();<br>
<br>
<br>
<br>
<br>
&nbsp;&nbsp;&nbsp; return 0;<br>
}<br>
<br>
<br>
<pre>

<b><font color="#0000ff" size="3"><span lang="en-ca">A snapshot of running automated testing:</span></font></b></pre>
<pre>the old array is:<br>[2,4,6,7,5,3,1]<br>the lex rank is:1055<br>the lex successor is:<br>[2,4,7,1,3,5,6]<br>the old array is:<br>[2,4,6,7,5,3,1]<br>the Trotter-Johnson rank is:3888<br>the Trotter-Johnson successor is:<br>[2,4,6,5,7,3,1]<br>now test permutation of size of 1<br>[1]<br><br>the total number of permutation is:1<br>the total running time in seconds is:0<br>now test permutation of size of 2<br>[1,2]<br>[2,1]<br><br>the total number of permutation is:3<br>the total running time in seconds is:0<br>now test permutation of size of 3<br>[1,2,3]<br>[1,3,2]<br>[3,1,2]<br>[3,2,1]<br>[2,3,1]<br>[2,1,3]<br><br>the total number of permutation is:9<br>the total running time in seconds is:0<br>now test permutation of size of 4<br>[1,2,3,4]<br>[1,2,4,3]<br>[1,4,2,3]<br>[4,1,2,3]<br>[4,1,3,2]<br>[1,4,3,2]<br>[1,3,4,2]<br>[1,3,2,4]<br>[3,1,2,4]<br>[3,1,4,2]<br>[3,4,1,2]<br>[4,3,1,2]<br>[4,3,2,1]<br>[3,4,2,1]<br>[3,2,4,1]<br>[3,2,1,4]<br>[2,3,1,4]<br>[2,3,4,1]<br>[2,4,3,1]<br>[4,2,3,1]<br>[4,2,1,3]<br>[2,4,1,3]<br>[2,1,4,3]<br>[2,1,3,4]<br><br>the total number of permutation is:33<br>the total running time in seconds is:0<br>now test permutation of size of 5<br>[1,2,3,4,5]<br>[1,2,3,5,4]<br>[1,2,5,3,4]<br>[1,5,2,3,4]<br>[5,1,2,3,4]<br>[5,1,2,4,3]<br>[1,5,2,4,3]<br>[1,2,5,4,3]<br>[1,2,4,5,3]<br>[1,2,4,3,5]<br>[1,4,2,3,5]<br>[1,4,2,5,3]<br>[1,4,5,2,3]<br>[1,5,4,2,3]<br>[5,1,4,2,3]<br>[5,4,1,2,3]<br>[4,5,1,2,3]<br>[4,1,5,2,3]<br>[4,1,2,5,3]<br>[4,1,2,3,5]<br>[4,1,3,2,5]<br>[4,1,3,5,2]<br>[4,1,5,3,2]<br>[4,5,1,3,2]<br>[5,4,1,3,2]<br>[5,1,4,3,2]<br>[1,5,4,3,2]<br>[1,4,5,3,2]<br>[1,4,3,5,2]<br>[1,4,3,2,5]<br>[1,3,4,2,5]<br>[1,3,4,5,2]<br>[1,3,5,4,2]<br>[1,5,3,4,2]<br>[5,1,3,4,2]<br>[5,1,3,2,4]<br>[1,5,3,2,4]<br>[1,3,5,2,4]<br>[1,3,2,5,4]<br>[1,3,2,4,5]<br>[3,1,2,4,5]<br>[3,1,2,5,4]<br>[3,1,5,2,4]<br>[3,5,1,2,4]<br>[5,3,1,2,4]<br>[5,3,1,4,2]<br>[3,5,1,4,2]<br>[3,1,5,4,2]<br>[3,1,4,5,2]<br>[3,1,4,2,5]<br>[3,4,1,2,5]<br>[3,4,1,5,2]<br>[3,4,5,1,2]<br>[3,5,4,1,2]<br>[5,3,4,1,2]<br>[5,4,3,1,2]<br>[4,5,3,1,2]<br>[4,3,5,1,2]<br>[4,3,1,5,2]<br>[4,3,1,2,5]<br>[4,3,2,1,5]<br>[4,3,2,5,1]<br>[4,3,5,2,1]<br>[4,5,3,2,1]<br>[5,4,3,2,1]<br>[5,3,4,2,1]<br>[3,5,4,2,1]<br>[3,4,5,2,1]<br>[3,4,2,5,1]<br>[3,4,2,1,5]<br>[3,2,4,1,5]<br>[3,2,4,5,1]<br>[3,2,5,4,1]<br>[3,5,2,4,1]<br>[5,3,2,4,1]<br>[5,3,2,1,4]<br>[3,5,2,1,4]<br>[3,2,5,1,4]<br>[3,2,1,5,4]<br>[3,2,1,4,5]<br>[2,3,1,4,5]<br>[2,3,1,5,4]<br>[2,3,5,1,4]<br>[2,5,3,1,4]<br>[5,2,3,1,4]<br>[5,2,3,4,1]<br>[2,5,3,4,1]<br>[2,3,5,4,1]<br>[2,3,4,5,1]<br>[2,3,4,1,5]<br>[2,4,3,1,5]<br>[2,4,3,5,1]<br>[2,4,5,3,1]<br>[2,5,4,3,1]<br>[5,2,4,3,1]<br>[5,4,2,3,1]<br>[4,5,2,3,1]<br>[4,2,5,3,1]<br>[4,2,3,5,1]<br>[4,2,3,1,5]<br>[4,2,1,3,5]<br>[4,2,1,5,3]<br>[4,2,5,1,3]<br>[4,5,2,1,3]<br>[5,4,2,1,3]<br>[5,2,4,1,3]<br>[2,5,4,1,3]<br>[2,4,5,1,3]<br>[2,4,1,5,3]<br>[2,4,1,3,5]<br>[2,1,4,3,5]<br>[2,1,4,5,3]<br>[2,1,5,4,3]<br>[2,5,1,4,3]<br>[5,2,1,4,3]<br>[5,2,1,3,4]<br>[2,5,1,3,4]<br>[2,1,5,3,4]<br>[2,1,3,5,4]<br>[2,1,3,4,5]<br><br>the total number of permutation is:153<br>the total running time in seconds is:0<br>now test permutation of size of 6<br><br>the total number of permutation is:873<br>the total running time in seconds is:0<br>now test permutation of size of 7<br><br>the total number of permutation is:5913<br>the total running time in seconds is:0<br>now test permutation of size of 8<br><br>the total number of permutation is:46233<br>the total running time in seconds is:0<br>now test permutation of size of 9<br><br>the total number of permutation is:409113<br>the total running time in seconds is:0<br>now test permutation of size of 10<br><br>the total number of permutation is:4037913<br>the total running time in seconds is:1<br>now test permutation of size of 11<br><br>the total number of permutation is:43954713<br>the total running time in seconds is:11<br>now test permutation of size of 12<br><br>the total number of permutation is:522956313<br>the total running time in seconds is:149<br>1 1 0 seconds<br>2 0 0 seconds<br>3 0 0 seconds<br>4 2 0 seconds<br>5 14 0 seconds<br>6 90 0 seconds<br>7 646 0 seconds<br>8 5242 0 seconds<br>9 47622 1 seconds<br>10 479306 1 seconds<br>11 5296790 15 seconds<br>12 63779034 187 seconds<br>1 1 0<br>2 0 0<br>3 0 0<br>4 2 0<br>5 14 0<br>6 90 0<br>7 646 0<br>8 5242 0<br>9 47622 0<br>10 479306 1<br>11 5296790 14<br>12 63779034 281<br>Press any key to continue<br></pre>

<pre><span lang="en-ca">			</span></pre>

<pre><span lang="en-ca">				</span> <a href="game24.htm"><img src="picture/back.gif" style="border: medium none ;" alt="back.gif (341 bytes)" height="35" width="32"></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)" height="32" width="35"></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="picture/next.gif" style="border: medium none ;" alt="next.gif (337 bytes)" height="35" width="32"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<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;</p>

</body></html>