用c语言实现随机无向图的生成,C ++程序为给定数量的边生成随机无向图
這是一個C ++程序,其中我們為給定的邊緣'e'生成無向隨機圖。該算法基本上在大型網絡上實現,該算法的時間復雜度為O(log(n))。
算法Begin
Function?GenerateRandomGraphs(),?has?‘e’?as?the?number?edges?in?the?argument?list.
Initialize?i?=?0
while(i?
edge[i][0]?=?rand()%N+1
edge[i][1]?=?rand()%N+1
Increment?I;
For?i?=?0?to?N-1
Initialize?count?=?0
For?j?=?0?to?e-1
if(edge[j][0]?==?i+1)
Print?edge[j][1]
Increase?count
else?if(edge[j][1]?==?i+1)
Print?edge[j][0]
Increase?count
else?if(j?==?e-1?&&?count?==?0)
Print?Isolated?Vertex
End
示例#include
#include
#define?N?10
using?namespace?std;
void?GenerateRandomGraphs(int?e)?{
int?i,?j,?edge[e][2],?count;
i?=?0;
//生成兩個隨機數之間的連接,對于//小樣本,將頂點數限制為10。-
while(i?
edge[i][0]?=?rand()%N+1;
edge[i][1]?=?rand()%N+1;
i++;
}
//打印每個頂點的所有連接,而與//方向無關。
cout<
for(i?=?0;?i?
count?=?0;
cout<?{?";
for(j?=?0;?j?
if(edge[j][0]?==?i+1)?{
cout<
count++;
}
else?if(edge[j][1]?==?i+1)?{
cout<
count++;
}
//打印零度頂點的“隔離頂點”。
else?if(j?==?e-1?&&?count?==?0)
cout<
}
cout<
}
}
int?main()?{
int?n,?i?,e;
cout<
cin>>e;
GenerateRandomGraphs(e);
}
輸出結果Enter?the?number?of?edges?for?the?random?graphs:?10
The?generated?random?graph?is:
1->?{?10?7?}
2->?{?10?}
3->?{?7?8?7?}
4->?{?7?6?7?}
5->?{?孤立的頂點!?}
6->?{?8?4?}
7->?{?4?3?4?1?3?}
8->?{?6?3?}
9->?{?孤立的頂點!?}
10->?{?2?1?}
總結
以上是生活随笔為你收集整理的用c语言实现随机无向图的生成,C ++程序为给定数量的边生成随机无向图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言case可以判断多个条件吗,cas
- 下一篇: android 勿扰模式代码,andro