这是一个C ++程序,其中我们为给定的边缘'e'生成无向随机图。该算法基本上在大型网络上实现,该算法的时间复杂度为O(log(n))。
Begin Function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list. Initialize i = 0 while(i < e) 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<iostream>
#include<stdlib.h>
#define N 10
using namespace std;
void GenerateRandomGraphs(int e) {
   int i, j, edge[e][2], count;
   i = 0;
   //生成两个随机数之间的连接,对于//小样本,将顶点数限制为10。-
   while(i < e) {
      edge[i][0] = rand()%N+1;
      edge[i][1] = rand()%N+1;
      i++;
   }
   //打印每个顶点的所有连接,而与//方向无关。
   cout<<"\nThe generated random graph is: ";
   for(i = 0; i < N; i++) {
      count = 0;
      cout<<"\n\t"<<i+1<<"-> { ";
         for(j = 0; j < e; j++) {
            if(edge[j][0] == i+1) {
               cout<<edge[j][1]<<" ";
               count++;
            }
            else if(edge[j][1] == i+1) {
               cout<<edge[j][0]<<" ";
               count++;
            }
            //打印零度顶点的“隔离顶点”。
            else if(j == e-1 && count == 0)
               cout<<"孤立的顶点!";
         }
      cout<<" }";
   }
}
int main() {
   int n, i ,e;
   cout<<"Enter the number of edges for the random graphs: ";
   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 }