<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>bit matrix</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp; 
 
</b></span><b>&nbsp;&nbsp;&nbsp; Bit Matrix</b></font></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 just for fun and I confess I never expect to make anything from this futile search of polynomial algorithm</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>of clique problem. It is just for fun and help me to realize the difficulty of NP-complete problem.</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"><b>What is clique? In a graph G(V,E), what is the biggest 
  size of clique which is a sub graph G with each vertex in it incidented with 
  all other vertices in clique. i.e. clique = {G(V,E) ,&nbsp; k}</b></span><p>
  <span lang="en-ca"><b>Sub = {C(V', E')} with </b></span></p>
  <p><span lang="en-ca"><b>1) C(V', E') and v in V' =&gt; v in V and e in E'&nbsp; 
  =&gt; e in E</b></span></p>
  <p><span lang="en-ca"><b>2) v1,v2 in V' =&gt; (v1,v2) in E'</b></span></p>
  <p><span lang="en-ca"><b>And for all c in Sub such that |c|&lt;=k</b></span></p>
  <p><span lang="en-ca"><b>In English, it says that for a graph G, to find out 
  if exists a completed sub-graph of G with size of k.</b></span></p>
  <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 ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>This is a rather straight 
forward, simple, trivial copy of bitset template. However, it makes a good tool 
for </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>future study of relations, 
graphs, etc.</b></font></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>กก</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><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><font size="3"><b><span lang="en-ca">1. bitmatrix.h</span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>2. main.cpp</b></font></span></pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: bitmatrix.h</b></font></span></pre>
</div>
<pre>#include &lt;bitset&gt;
#include &lt;string.h&gt;

typedef unsigned long ulong;
using namespace std;


template&lt;int size&gt;
class BitMatrix
{
private:
	bitset&lt;size&gt; matrix[size];
	bool degreeArray[size];
	ulong values[size];
	void update(int row);
	bool degree(int row, int deg);
	bool first(int deg);
	bool second(int deg);

public:
	BitMatrix();
	void set(int row, int col, bool val=true);
	void assign(int row, ulong seq);
	BitMatrix&lt;size&gt;* power(int row);
	void showPower(int times);
	void showPower(int row, int times);
	void randomSet();
	bool clique(int deg);
	void swap(int row1, int row2);
	void arrange();

	void display();
};


template&lt;int size&gt;
void BitMatrix&lt;size&gt;::arrange()
{
	int front, back;
	front=back=0;
	while (true)
	{
		//find first false one
		while (degreeArray[back])
		{
			back++;
			if (back&gt;=size)
			{
				return;
			}
		}

		//find first true one
		front=back;
		while (!degreeArray[front])
		{
			front++;
			if (front&gt;=size)
			{
				return;
			}
		}
		swap(front, back);
		printf(&quot;swap %d and %d \n&quot;, front, back);
		degreeArray[front]=false;
		degreeArray[back]=true;
	}
}

template&lt;int size&gt;
void BitMatrix&lt;size&gt;::swap(int row1, int row2)
{
	bool hold;
	for (int i=0; i&lt;size; i++)
	{
		//exchange row
		hold=matrix[row1][i];
		matrix[row1][i]=matrix[row2][i];
		matrix[row2][i]=hold;
		//exchange col
		hold=matrix[i][row1];
		matrix[i][row1]=matrix[i][row2];
		matrix[i][row2]=hold;
	}
}


template&lt;int size&gt;
bool BitMatrix&lt;size&gt;::first(int deg)
{
	int total=0;
	for (int i=0; i&lt;size; i++)
	{
		if (matrix[i].count()&gt;=deg)
		{
			total++;
			degreeArray[i]=true;
		}
		else
		{
			degreeArray[i]=false;
		}
	}
	return total&gt;=deg;
}

template&lt;int size&gt;
bool BitMatrix&lt;size&gt;::second(int deg)
{
	int total=0;
	int sub;
	for (int i=0; i&lt;size; i++)
	{
		//if it is the possible candidate
		if (degreeArray[i])
		{
			sub=0;
			//begin testify the candidate
			for (int j=0; j&lt;size; j++)
			{
				if (matrix[i][j])
				{
					if (matrix[j].count()&gt;=deg)
					{
						sub++;
					}
				}
			}
			//count the number of qualified candidates
			if (sub&gt;=deg)
			{
				total++;
				//printf(&quot;row[%d] is ok\n&quot;, i);
			}
			else
			{
				degreeArray[i]=false;
			}
		}
	}
	return total&gt;=deg;
}


template&lt;int size&gt;
bool BitMatrix&lt;size&gt;::clique(int deg)
{
	if (first(deg))
	{
		if (second(deg))
		{
			return true;
		}
	}
	return false;
}

template&lt;int size&gt;
bool BitMatrix&lt;size&gt;::degree(int row, int deg)
{
	int result=0;
	if (matrix[row].count()&lt;deg)
	{
		return false;
	}
	for (int i=0; i&lt;size; i++)
	{
		if (matrix[row][i])
		{
			if (matrix[i].count()&gt;=deg)
			{
				result++;
			}
		}
	}
	return result&gt;=deg-1;
}

		


template&lt;int size&gt;
void BitMatrix&lt;size&gt;::showPower(int row, int times)
{
	BitMatrix&lt;size&gt;* result=this;
	for (int i=0; i&lt;times; i++)
	{
		result=result-&gt;power(row);
		printf(&quot;show power of row #%d of times %d\n&quot;, row, i);
		result-&gt;display();

	}
}



template&lt;int size&gt;
void BitMatrix&lt;size&gt;::showPower(int times)
{
	BitMatrix&lt;size&gt;* result;
	if (times==0)
	{
		return;
	}
	for (int i=0; i&lt;size; i++)
	{
		result=power(i);
		
		printf(&quot;show power of times:%d, row of %d\n\n&quot;, times, i);
		result-&gt;display();
		result-&gt;showPower(times-1);
	}
}

template&lt;int size&gt;
void BitMatrix&lt;size&gt;::update(int row)
{
	values[row]=matrix[row].to_ulong();
}

//the dynamic memory is not handled yet
template&lt;int size&gt;
BitMatrix&lt;size&gt;* BitMatrix&lt;size&gt;::power(int row)
{
	BitMatrix&lt;size&gt;* result=new BitMatrix&lt;size&gt;;
	
	for (int i=0; i&lt;size; i++)
	{
		result-&gt;matrix[i].reset();
		result-&gt;matrix[i]|=matrix[row];
		result-&gt;matrix[i]&amp;=matrix[i];
		result-&gt;update(i);
	}
	return result;
}



template&lt;int size&gt;
void BitMatrix&lt;size&gt;::randomSet()
{
	bool result;
	for (int r=0; r&lt;size; r++)
	{
		for (int c=r; c&lt;size; c++)
		{
			if (r==c)
			{
				result=true;				
			}
			else
			{
				result=rand()%2==0;
			}
			matrix[r].set(size-c-1, result);
			matrix[c].set(size-r-1, result);
		}
		
	}
	for (int i=0; i&lt;size; i++)
	{
		update(i);
	}
}


template&lt;int size&gt;
void BitMatrix&lt;size&gt;::assign(int row, ulong seq)
{
	bitset&lt;size&gt; B(seq);
	matrix[row].reset();
	matrix[row].operator|=(B);
	values[row]=seq;
}



template&lt;int size&gt;
BitMatrix&lt;size&gt;::BitMatrix&lt;size&gt;()
{
	for (int i=0; i&lt;size; i++)
	{
		matrix[i].reset();
	}
}

template&lt;int size&gt;
void BitMatrix&lt;size&gt;::set(int row, int col, bool val)
{
	if (row&gt;=size||col&gt;=size||row&lt;0||col&lt;0)
	{
		printf(&quot;out of bound\n&quot;);
	}
	else
	{
		matrix[row].set(col, val);
		update(row);
	}
}



template&lt;int size&gt;
void BitMatrix&lt;size&gt;::display()
{
	for (int i=0; i&lt;size; i++)
	{
		printf(&quot;%s     (%X)\n&quot;, matrix[i].to_string().data(), values[i]);
	}
}
</pre>
<pre>	</pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: main.cpp</b></font></span></pre>
<pre>#include &quot;bitmatrix.h&quot;
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

const int MatrixSize=8;

unsigned long pos=0x00000003;

int main()
{
	srand(time(0));
	BitMatrix&lt;MatrixSize&gt; M;
	bitset&lt;MatrixSize&gt; B;
	//M.display();
	/*
	for (int r=0; r&lt;MatrixSize; r++)
	{
		for (int c=0; c&lt;MatrixSize; c++)
		{
			M.set(r, c, rand()%2==1);
		}
	}
	*/
	M.randomSet();
	M.display();
	//M.assign(0, pos);
	//printf(&quot;now it is \n&quot;);
	//M.display();
	//printf(&quot;%s\n&quot;, B.to_string().data());

	//B&lt;&lt;=pos;
	//printf(&quot;%s\n&quot;, B.to_string().data());
	//printf(&quot;begin to power\n&quot;);
	/*
	for (int i=0; i&lt;MatrixSize; i++)
	{
		printf(&quot;power of row of %d\n&quot;, i);
		M.power(i)-&gt;display();
	}
	*/
	//M.showPower(2);
	
	for (int i=1; i&lt;MatrixSize; i++)
	{
		if (M.clique(i))
		{
			printf(&quot;matrix has clique of size of %d\n&quot;, i);
			M.arrange();
			M.display();
		}
	}
	




	return 0;
}
	</pre>
<pre>กก</pre>
<pre></pre>
<pre></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>How to run?</b></font></span></pre>

<pre>11101001 (E9)
11100000 (E0)
11110010 (F2)
00110110 (36)
10001111 (8F)
00011110 (1E)
00111110 (3E)
10001001 (89)
matrix has clique of size of 1
11101001 (E9)
11100000 (E0)
11110010 (F2)
00110110 (36)
10001111 (8F)
00011110 (1E)
00111110 (3E)
10001001 (89)
matrix has clique of size of 2
11101001 (E9)
11100000 (E0)
11110010 (F2)
00110110 (36)
10001111 (8F)
00011110 (1E)
00111110 (3E)
10001001 (89)
matrix has clique of size of 3
11101001 (E9)
11100000 (E0)
11110010 (F2)
00110110 (36)
10001111 (8F)
00011110 (1E)
00111110 (3E)
10001001 (89)
Press any key to continue</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; <a href="PocketRuler.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>