<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" marginwidth="1" marginheight="1">



<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; 
Max Clique</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 5 of comp6661 and we are requested to find maximum clique which has the biggest size </b></span></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><b>among all maximal clique. A maximal clique is such a clique that it cannot be enlarged any more.</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">
  1. (5 marks, from final exam of 2000)<br>
	Bounding functions for maximum clique<br>
	Consider the maximum clique problem as specified by Ex. 4.9 (p. 147) of your 
	textbook,<br>
	which is based on using Algorithm 4.19: MAXCLIQUE2(`) (p. 139) of the<br>
	textbook. Assume that your input data is the graph given in part (b) of Ex. 
	4.9<br>
	and consider the particular instance when MAXCLIQUE2 is called with ` = 1 
	and<br>
	[x0] = [8]. Assume also that GREEDYBOUND (Algorithm 4.18, p. 138) is used as<br>
	the bounding function.<br>
	What value is returned by GREEDYBOUND?</div>
<div align="left">
  　</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 size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<p><span lang="en-ca">The input part cost me a half evening before I started 
watching &quot;starcraft&quot; competition. The max clique part cost me a whole</span></p>
<p><span lang="en-ca">morning before I began to enjoy my dinner cooked by a 
visiting friend. Maybe I am becoming slower and slower, more and more</span></p>
<p><span lang="en-ca">lazy. </span></p>
<p><span lang="en-ca">Please be noted that original input data makes no need of 
matrix &quot;B&quot; which is a set of all elements behind an element. But </span></p>
<p><span lang="en-ca">you will find it is useful in &quot;greedybound&quot;. That is why I 
added a seemingly useless statement A[second].set(first); after</span></p>
<p><span lang="en-ca">statement A[first].set(second);. If you are a programmer, 
you surely understand this.</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">And as usual I tried to show off a bit by using call back functions. </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 size="5" color="#FF0000"><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 size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left" style="width: 898; height: 86">
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">1. clique.cpp</span></b></font></pre>
	<pre>　</pre>
  <pre><font size="3" color="#FF0000"><span lang="en-ca"><b>file name: clique</b></span><b><span lang="en-ca">.cpp</span></b></font></pre>
	<pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;
//#include &lt;vector&gt;

using namespace std;

const int MaxNumber=16;

class SimpleSet
{
protected:
	static int size;
	unsigned int data;
public:
	SimpleSet()
	{
		data=0;
	}
	void set(int index)
	{
		unsigned int mask=1;
		data|=(mask&lt;&lt;index);
	}
	void reset(int index)
	{
		unsigned int mask=1;
		data^=(mask&lt;&lt;index);
	}

	void clear()
	{
		data=0;
	}
	bool test(int index) const
	{
		unsigned int mask=1;
		return (data&amp;(mask&lt;&lt;index))!=0;
	}
	void intersect(const SimpleSet&amp; other)
	{
		data&amp;=other.data;		
	}
	int count() const
	{
		int result=0;
		for (int i=0; i&lt;size; i++)
		{
			if (test(i))
			{
				result++;
			}
		}
		return result;
	}
	SimpleSet&amp; operator=(const SimpleSet&amp; other)
	{
		data=other.data;
		return *this;
	}
	int first() const
	{
		int result=-1;
		for (int i=0; i&lt;size; i++)
		{
			if (test(i))
			{
				result= i;
				break;
			}
		}
		return result;
	}
	int last() const
	{
		int result=-1;
		for (int i=size-1; i&gt;=0; i--)
		{
			if (test(i))
			{
				result= i;
				break;
			}
		}
		return result;
	}


	int next(int start) const
	{
		int result=-1;
		for (int i=start+1; i&lt;size; i++)
		{
			if (test(i))
			{
				result= i;
				break;
			}
		}
		return result;
	}
	SimpleSet operator&amp;&amp;(const SimpleSet&amp; other)
	{
		SimpleSet result=*this;
		result.intersect(other);
		return result;
	}
	void display()
	{
		cout&lt;&lt;&quot;[&quot;;
		for (int i=0; i&lt;size; i++)
		{
			if (test(i))
			{
				if (i&lt;10)
				{
					cout&lt;&lt;i&lt;&lt;&quot; &quot;;
				}
				else
				{					
					cout&lt;&lt;(char)('a'+(i-10))&lt;&lt;&quot; &quot;;
				}
			}
		}
		cout&lt;&lt;&quot;]\n&quot;;
	}
	

};

int SimpleSet::size=MaxNumber;

char nameList[MaxNumber];
SimpleSet A[MaxNumber];
SimpleSet B[MaxNumber];
//SimpleSet C;
SimpleSet result;
SimpleSet current;
int currentMax=0;
int nodeCount;

int currentBound;
int color[MaxNumber];
int colorSize;


int greedyColor(const SimpleSet A[], const SimpleSet&amp; vertex, int color[])
{
	int h, k=0, index=-1, i=0;
	SimpleSet colorClass[MaxNumber];
	SimpleSet temp;

	while ((index=vertex.next(index))!=-1)
	{
		h=0;
		while (h&lt;k&amp;&amp;((colorClass[h]&amp;&amp;A[index]&amp;&amp;vertex).count()!=0))
		{
			h++;
		}
		if (h==k)
		{
			k++;
			colorClass[h].clear();
		}
		colorClass[h].set(index);
		color[index]=h;
	}
	return k;
}

void display()
{
	int i, j, count=0;
	cout&lt;&lt;&quot;the name list is:\n&quot;;
	for (i=0; i&lt;MaxNumber; i++)
	{
		cout&lt;&lt;nameList[i]&lt;&lt;&quot;,&quot;;
	}
	cout&lt;&lt;&quot;\n&quot;;
	cout&lt;&lt;&quot;the adjacent list is:\n&quot;;
	for (i=0; i&lt;MaxNumber; i++)
	{
		for (j=0; j&lt;MaxNumber; j++)
		{
			if (A[i].test(j))
			{
				cout&lt;&lt;&quot;(&quot;&lt;&lt;nameList[i]&lt;&lt;&quot;,&quot;&lt;&lt;nameList[j]&lt;&lt;&quot;),&quot;;
				count++;
				if (count%10==0)
				{
					cout&lt;&lt;&quot;\n&quot;;
				}
			}
		}
		
	}
}

int element2Index(char ch)
{
	if (ch&lt;='9'&amp;&amp;ch&gt;='0')
	{
		return ch-'0';
	}
	else
	{
		if (ch&gt;='a'&amp;&amp;ch&lt;='z')
		{
			return ch-'a'+10;
		}
		else
		{
			return -1;
		}
	}
}

void readFromFile(char* fileName)
{
	char buf[256];
	char* ptr;
	ifstream in(fileName);

	while (!in.eof())
	{
		in.getline(buf, 256);
		//first call to initialize
		ptr= strtok(buf, &quot;,&quot;);
		//to get edge one by one
		while (ptr!=NULL)
		{
			int first=-1, second=-1, i=0;
			while (*(ptr+i)!='\0')
			{
				if (*(ptr+i)!=' ')
				{
					if (first==-1)
					{
						first=element2Index(*(ptr+i));
					}
					else
					{
						second=element2Index(*(ptr+i));
					}
				}
				i++;
			}
			if (first==-1||second==-1)
			{
				cout&lt;&lt;&quot;error in reading edge\n&quot;;
				exit(1);
			}
			//get the edge
			A[first].set(second);
			A[second].set(first);
			ptr=strtok(NULL, &quot;,&quot;);
		}
		
	}
}

void initialize(char* fileName)
{	
	SimpleSet full;
	for (int i=0; i&lt;MaxNumber; i++)
	{
		full.set(i);
		if (i&lt;10)
		{
			nameList[i]='0'+i;
		}
		else
		{
			nameList[i]='a'+i-10;
		}
		for (int j=i+1; j&lt;MaxNumber; j++)
		{
			B[i].set(j);
		}
	}
	readFromFile(fileName);

	
	colorSize=greedyColor(A, full, color);
}

int samplingBound(int size, const SimpleSet&amp; choice)
{
	int index=-1, i, result=0;
	int counter[MaxNumber];

	for (i=0; i&lt;MaxNumber; i++)
	{
		counter[i]=0;
	}

	//to count the number of color used in C
	while ((index=choice.next(index))!=-1)
	{
		//flag the color used in C
		counter[color[index]]=1;
	}
	//count color used
	for (i=0; i&lt;MaxNumber; i++)
	{
		result+=counter[i];
	}
	return size+result;
}


int greedyBound(int size, const SimpleSet&amp; choice)
{
	int result;
	int local[MaxNumber];
	result=greedyColor(A, choice, local);
	return result+size;
}



int sizeBound(int size, const SimpleSet&amp; choice)
{
	return size+choice.count();
}


int noBound(int size, const SimpleSet&amp; choice)
{
	return MaxNumber;
}

//pass choice as value!!!!
void doMaxClique(int size, SimpleSet choice, int (*boundFunc)(int, const SimpleSet&amp;))
{
	//static int currentElem;
	nodeCount++;
	int last;
	if (size&gt;currentMax)
	{
		currentMax=size;
		result=current;
	}
	if (size==0)
	{
		for (int i=0; i&lt;MaxNumber; i++)
		{
			choice.set(i);
		}
	}
	else
	{
		last=current.last();
		choice.intersect(A[last]);
		choice.intersect(B[last]);
	}

	currentBound=boundFunc(size, choice);
	
	if (boundFunc==greedyBound&amp;&amp;size==1&amp;&amp;last==8)	
	{
		cout&lt;&lt;&quot;the answer for question 1 is:&quot;&lt;&lt;currentBound&lt;&lt;endl;
	}

	if (currentBound&lt;=currentMax)
	{
		return;
	}
	else
	{
		int index=-1;
		while ((index=choice.next(index))!=-1)
		{
			current.set(index);
			doMaxClique(size+1, choice, boundFunc);
			current.reset(index);
		}
	}
}


void maxClique(int (*boundFunc)(int, const SimpleSet&amp;))
{
	SimpleSet choice;
	currentMax=0;
	nodeCount=0;
	doMaxClique(0, choice, boundFunc);
}





int main( )
{	
	initialize(&quot;data1.txt&quot;);
	display();

	cout&lt;&lt;&quot;using no bound:\n&quot;;
	maxClique(noBound);
	cout&lt;&lt;&quot;nodeCount=&quot;&lt;&lt;nodeCount&lt;&lt;&quot;\n&quot;;
	result.display();


	cout&lt;&lt;&quot;using size bound:\n&quot;;
	maxClique(sizeBound);
	cout&lt;&lt;&quot;nodeCount=&quot;&lt;&lt;nodeCount&lt;&lt;&quot;\n&quot;;
	result.display();


	cout&lt;&lt;&quot;using sample bound:\n&quot;;

	maxClique(samplingBound);
	cout&lt;&lt;&quot;nodeCount=&quot;&lt;&lt;nodeCount&lt;&lt;&quot;\n&quot;;
	result.display();

	cout&lt;&lt;&quot;using greedy bound:\n&quot;;

	maxClique(greedyBound);
	cout&lt;&lt;&quot;nodeCount=&quot;&lt;&lt;nodeCount&lt;&lt;&quot;\n&quot;;
	result.display();
  
   return 0;
}
	



</pre>
</div>
<pre>
　</pre>
<pre>
<b><font color="#0000FF" size="3"><span lang="en-ca">The input data file is named &quot;data1.txt&quot;:</span></font></b></pre>
<pre>
　</pre>
<pre>
0 1, 0 2, 0 7, 0 8, 0 9, 0 a, 0 e, 1 4, 1 7, 1 8,
1 9, 1 a, 1 b, 1 c, 1 e, 2 3, 2 5, 2 6, 2 7, 2 8,
2 a, 2 b, 3 4, 3 5, 3 6, 3 a, 3 f, 4 5, 4 6, 4 8,
4 9, 4 a, 4 e, 5 6, 5 8, 5 9, 5 b, 5 d, 5 e, 5 f,
6 f, 7 8, 7 a, 7 c, 7 e, 7 f, 8 b, 8 c, 8 e, 8 f,
9 d, 9 e, a c, a d, a e, b c, b e, b f, c d, e f</pre>
<pre>
　</pre>
<pre>
<span lang="en-ca"><font size="3" color="#0000FF"><b>another</b></font></span><b><font color="#0000FF" size="3"><span lang="en-ca"> input data file is named &quot;data2.txt&quot;: </span></font></b></pre>
<pre>
<b><font color="#0000FF" size="3"><span lang="en-ca">(I place it here because it takes quite some time to make it by hand.)</span></font></b></pre>
<pre>
　</pre>
<pre>
0 2, 0 3, 0 4, 0 5, 0 6, 0 7, 0 8, 0 9, 0 a, 0 c,
0 d, 0 e, 1 5, 1 6, 1 7, 1 8, 1 9, 1 a, 1 b, 1 c,
1 d, 1 e, 1 f, 2 5, 2 6, 2 7, 2 8, 2 a, 2 b, 2 e,
2 f, 3 4, 3 5, 3 6, 3 7, 3 8, 3 a, 3 c, 3 d, 3 e,
3 f, 4 6, 4 8, 4 9, 4 b, 4 c, 4 e, 4 f, 5 6, 5 7,
5 9, 5 a, 5 b, 5 c, 5 e, 5 f, 6 7, 6 8, 6 9, 6 a,
6 b, 6 d, 6 e, 6 f, 7 8, 7 9, 7 b, 7 c, 7 d, 7 e,
7 f, 8 9, 8 a, 8 b, 8 f, 9 b, 9 c, 9 d, 9 f, a c,
a d, a e, a f, b c, b d, b e, b f, c d, c e, d f</pre>
<pre>


<b><font color="#0000FF" size="3"><span lang="en-ca">A snapshot of running automated testing:</span></font></b></pre>
<pre>the name list is:
0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,
the adjacent list is:
(0,1),(0,2),(0,7),(0,8),(0,9),(0,a),(0,e),(1,0),(1,4),(1,7),
(1,8),(1,9),(1,a),(1,b),(1,c),(1,e),(2,0),(2,3),(2,5),(2,6),
(2,7),(2,8),(2,a),(2,b),(3,2),(3,4),(3,5),(3,6),(3,a),(3,f),
(4,1),(4,3),(4,5),(4,6),(4,8),(4,9),(4,a),(4,e),(5,2),(5,3),
(5,4),(5,6),(5,8),(5,9),(5,b),(5,d),(5,e),(5,f),(6,2),(6,3),
(6,4),(6,5),(6,f),(7,0),(7,1),(7,2),(7,8),(7,a),(7,c),(7,e),
(7,f),(8,0),(8,1),(8,2),(8,4),(8,5),(8,7),(8,b),(8,c),(8,e),
(8,f),(9,0),(9,1),(9,4),(9,5),(9,d),(9,e),(a,0),(a,1),(a,2),
(a,3),(a,4),(a,7),(a,c),(a,d),(a,e),(b,1),(b,2),(b,5),(b,8),
(b,c),(b,e),(b,f),(c,1),(c,7),(c,8),(c,a),(c,b),(c,d),(d,5),
(d,9),(d,a),(d,c),(e,0),(e,1),(e,4),(e,5),(e,7),(e,8),(e,9),
(e,a),(e,b),(e,f),(f,3),(f,5),(f,6),(f,7),(f,8),(f,b),(f,e),
using no bound:
nodeCount=184
[0 1 7 8 e ]
using size bound:
nodeCount=83
[0 1 7 8 e ]
using sample bound:
nodeCount=41
[0 1 7 8 e ]
using greedy bound:
the answer for question 1 is:4
nodeCount=33
[0 1 7 8 e ]
Press any key to continue
</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)" 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"> </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>