完全图所有子图算法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
fout<<endl;
fout<<setw(3)<<" ";
for(int i = 0; i < graph->vernum;i++)
{
fout<<setw(3)<<graph->vertices[i];
}
fout<<endl;
for(int row = 0; row < graph->vernum; row++)
edges[row] = new int[i];
subgraph->edges = edges;
//get all the combination of i vertices
int m=calc_cv(vernum,i);
int** vcombs = new int*[m];
for(int l = 0; l < m; l++)
{
fout<<setw(3)<<graph->vertices[row];
for(int col = 0; col <graph->vernum; col++)
{
fout<<setw(3)<<graph->edges[row][col];
}
fout<<endl;
}
fout.close();
}
void combination(int n, int k,int** combs)
edges[row][col] = 0;
}
for(int index = 0; index < edge_k; index++)
{
//get the vertices of the selected edges
int row, col;
getIndex(ecombs[k][index],row,col);
p++;
array[cur]++;
continue;
}else{
if(cur == 0) break;
array[--cur]++;
for(int i = 1; i < k-cur; i++)
array[cur+i] = array[cur]+i;
if(array[cur]-cur < n-k+1)
{
int p=0;
int* array = new int[k];
for(int i = 0; i < k; i++)
{
array[i] = i + 1;
}
int cur = k-1;
do{
if(array[cur]-cur <= n-k+1)
{
for(int i = 0; i < k; i++)
combs[p][i] = array[i];
void printGraph(struct Graph*);//print the subgraphs to a txt file
int calc_cv(int, int);//calculate the value of C(n, k)
struct Graph
{
int vernum;
int *vertices;
for(int i = 1; i <= vernum; i++)
{
Graph* subgraph = new Graph;
subgraph->vernum = i;
//initialize the edges
int** edges = new int*[i];
for(int row = 0; row < i; row++)
int **edges;
};
int main(int argc, char *argv[])
{
int vernum;
cout<<"请输入完全图的顶点数";
cin>>vernum;
//set values to origional graph
struct Graph* origGraph = new Graph;
}
int factorial(int n)
{
if(n == 0) return 1;
int s = 1;
for(int i = 2; i <= n; i++) s=s*i;
return s;
}
void getIndex(int n, int &i, int &j)
{
int k=0,l=0,sum=0;
origGraph->edges = edges;
//generate subgraphs
genSubgraphs(origGraph);
//free memory
for(int i = 0; i < vernum; i++)
delete(origGraph->edges[i]);
delete(origGraph->edges);
ecombs[l] = new int[edge_k];
combination(edgenum,edge_k,ecombs);
for(int k = 0; k < combnum; k++)
{
//set the edges
for(int row = 0; row < i; row++)
{
for(int col = 0; col < i; col++)
void combination(int, int,int**);//get the C(n, k) combinations and store in int**
int factorial(int n);//get the factorial value of n
void getIndex(int, int&, int&);//used when getting combinations of edges
for(int edge_k = 0; edge_k <= edgenum; edge_k++)
{
//set the edges
for(int row = 0; row < i; row++)
{
for(int col = 0; col < i; col++)
edges[row][col] = 0;
}
edges[row][col] = 1;
edges[col][row] = 1;
}
printGraph(subgraph);
count++;
delete(ecombs[k]);
}
delete(ecombs);
}
delete(vcombs[j]);
}
//free memory
delete(vcombs);
if(edge_k == 0)
{
printGraph(subgraph);
count++;
continue;
}
//select edges
int combnum = calc_cv(edgenum,edge_k);
int** ecombs = new int*[combnum];
for(int l = 0; l < combnum; l++)
cur = k-1;
}
}while(1);
delete array;
}
int calc_cv(int n, int k)
{
if(k > n/2) return calc_cv(n,n-k);
int s = 1;
for(int i = n; i > n-k; i--)
s *=i;
return s/factorial(k);
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <fstream>
using namespace std;
void genSubgraphs(struct Graph*);//generate subgraphs
delete(origGraph->vertices);
delete(origGraph);
system("PAUSE");
return EXIT_SUCCESS;
}
void genSubgraphs(struct Graph* graph)
{
int vernum = graph->vernum;
int count=0;//记录非空子图的个数
delete(subgraph);
}
cout<<count<<endl;
}
void printGraph(struct Graph* graph)
{
char* filename = "graph_result.txt";
fstream fout;
fout.open(filename,ios::app|ios::out);
origGraph->vernum = vernum;
int *vertices = new int[vernum];
for(int i = 1; i <= vernum; i++)
{
vertices[i-1] = i;
}
int **edges = new int*[vernum];
for(int i = 0; i < vernum; i++)
int temp = 0;
while(sum < n)
{
sum += k;
k++;
if((sum + k) >= n)
{
l = n - sum - 1;
break;
}Biblioteka Baidu
}
i = k; j = l;
}
The number of non-empty subgraph is 40068.
{
edges[i] = new int[vernum];
}
for(int i = 0; i < vernum; i++)
for(int j = 0; j < vernum; j++)
{
if(i == j) edges[i][j] = 0;
else edges[i][j] = 1;
}
origGraph->vertices = vertices;
vcombs[l] = new int[i];
combination(vernum,i,vcombs);
for(int j = 0; j < m; j++)
{
subgraph->vertices = vcombs[j];
//get all the conbinations of edges
int edgenum = i*(i-1)/2;
相关文档
最新文档