[英]random undirected conected graph with even vertex degree
I need to create random conected undirected graph with even vertex degrees. 我需要创建具有偶数顶点度的随机选择无向图。 I have to do that to test the times of finding Euler cycles for different vertex number (N).
我必须这样做以测试找到不同顶点数(N)的Euler循环的时间。 The representation of the graph is adjacency list.
图的表示形式是邻接表。 I have such code:
我有这样的代码:
void make_graph()
{
int i,a,b,c;
int nasycenie;
nasycenie=(0.5)*((N*(N-1))/2);
if(nasycenie<N-1) nasycenie=N-1;
for(i=1;i<=nasycenie;++i)
{
if(i<N)
{
a=rand()%i;
b=i;
}
else
{
a=rand()%N;
b=rand()%(N-1);
b+=(b>=a);
}
L1[a].push_back(b);
L1[b].push_back(a);
}
}
It makes connected undirected graph but vertex degrees aren't even. 它使连通的无向图成为顶点,但顶点度不均匀。 How to improve that to have even vertex degrees?
如何提高顶点度?
This code will create the graph you specified , but in AdjacencyMatrix (only half a matrix actulay, since graph is undirect) presentation , it wouldn't be hard to turn it into an AdjacencyList. 这段代码将创建您指定的图形,但是在AdjacencyMatrix(仅半数矩阵运算,因为图形是非直接的)表示中,将其转换为AdjacencyList并不难。
bool[vNum][vNum] make_graph(int vNum)
{
bool[vNum][vNum] adjacency;
int edgeNum = rand()%C(vNum , 2);
for(int i=0 ; i<vNum; i++)
{
int j=0;
for(j ; j<i; j++)
{
adjacency[i][j] = rand()%2;
}
if(hasOddTrue(adjacency[i] , j))
{
int selectedIndex = rand()%j;
adjacency[i][selectedIndex ] = !adjacency[i][selectedIndex ];
}
}
return adjacency;
}
bool hasOddTrue(bool[] list , int length)
{
bool b = false;
for(int i=0 ; i<length ; i++) { b = xor(b, list[i]); }
return b;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.