ER随机图构造算法

ER随机图构造算法思路:

(1)初始化:给定N个节点以及连边概率p~[0,1]

(2)随机连边:
    1.选择一对没有边相连的不同的节点。
    2.生成一个随机数 r~(0,1)。
    3.如果r < p,那么在这对节点之间添加一条边,否则就不添加。
    4.重复1,2,3,直到所有的节点对都被选择。



代码简单注释

initial():做一些初始化工作:由用户指定的网络大小生成相应的adjacentMatrix等

generateRandomNetwork():随机网络的生成算法

writeRandomNetworkToFile():将生成的随机ER网络写入到文件中(作图或者是其他的处理用)

calculateDegreeDistribution():计算随机网络的度分布

write2File_degreedistribution(double*): 将计算的网络的度分布写入文件中(可视化用)

代码如下


#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int NETWORK_SIZE;
double PROBABILITY_OF_EAGE;

int** adjacentMatrix;

void initial();
void generateRandomNetwork();
void writeRandomNetworkToFile();
void calculateDegreeDistribution();
void write2File_degreedistribut(double*);

int main(int argc, char** argv)
{
	if( 3 != argc )
	{
		printf("This algorithm requires 2 user-specified parameter\n");
		printf("\t1.the size of network\n");
		printf("\t2.the probability of eage\n");
		printf("\texample \"a.exe 100 0.06\"\n");
		exit(0);
	}
	NETWORK_SIZE = atoi(argv[1]);
	PROBABILITY_OF_EAGE = atof(argv[2]);
	srand((unsigned)time(NULL));
	initial();
	generateRandomNetwork();
	writeRandomNetworkToFile();
	calculateDegreeDistribution();
	return 0;
}
void initial()
{
	if( !(adjacentMatrix = (int**)malloc(sizeof(int*) * (NETWORK_SIZE + 1))) )
	{
		printf("adjacentMatrix** malloc error");
		exit(0);
	}
	int i;
	for( i = 1; i <= NETWORK_SIZE; i++ )
	{
		if( !(adjacentMatrix[i] = (int*)malloc(sizeof(int) * (NETWORK_SIZE + 1))) )
		{
			printf("adjacentMatrix[%d]* malloc error", i);
			exit(0);
		}
	}
}
void generateRandomNetwork(){
	int i, j;
	for( i = 1; i <= NETWORK_SIZE; i++ )
		for( j = i; j <= NETWORK_SIZE; j++ )
			adjacentMatrix[i][j] = adjacentMatrix[j][i] = 0;
	int k;
	int count = 0;
	double probability = 0.0;
	for( i = 1; i <= NETWORK_SIZE; i++ )
	{
		for( j = i + 1; j <= NETWORK_SIZE; j++ )
		{
			probability = (rand() % NETWORK_SIZE) / (double)NETWORK_SIZE;		
			if( probability < PROBABILITY_OF_EAGE )
			{
				count++;
				adjacentMatrix[i][j] = adjacentMatrix[j][i] = 1;
			}
		}
	}
	printf("count : %d\n", count);
}
void writeRandomNetworkToFile()
{
	FILE* fout;
	if( NULL == (fout = fopen("randomNetwork.data", "w")))
	{
		printf("open file(randomNetwork.data) error!\n");
		exit(0);
	}
	int i,j;
	for( i = 1; i <= NETWORK_SIZE; i++ )
	{
		for( j = 1; j <= NETWORK_SIZE; j++ )
			fprintf(fout, "%d ", adjacentMatrix[i][j]);
		fprintf(fout, "\n");
	}
	fclose(fout);
}

void calculateDegreeDistribution(){
	int* degree;
	double* statistic;
	int i, j;
	double averageDegree = 0.0;
	if( !(degree = (int*)malloc(sizeof(int) * (NETWORK_SIZE + 1))) )
	{
		printf("degree* malloc error\n");
		exit(0);
	}
	for( i = 1; i <= NETWORK_SIZE; i++ )degree[i] = 0;
	if( !(statistic = (double*)malloc(sizeof(double) * NETWORK_SIZE)) )
	{
		printf("statistic* malloc error");
		exit(0);
	}
	for( i = 1; i < NETWORK_SIZE; i++ )statistic[i] = 0.0;
	for( i = 1; i <= NETWORK_SIZE; i++ )
		for( j = 1; j <= NETWORK_SIZE; j++ )
			degree[i] = degree[i] + adjacentMatrix[i][j];
	for( i = 1; i <= NETWORK_SIZE; i++ )
		averageDegree += degree[i];
	printf("<k> = %f\n", averageDegree/(double)NETWORK_SIZE);
	for( i = 1; i <= NETWORK_SIZE; i++ )
		statistic[degree[i]]++;
	double indentify = 0.0;
	for( i = 0; i < NETWORK_SIZE; i++ )
	{
		statistic[i] = statistic[i]/(double)NETWORK_SIZE;
		indentify += statistic[i];
		//printf("%f ", statistic[i]);
	}
	printf("\nthe algorithm is correct if this output is 1. output = %f\n", indentify);
	write2File_degreedistribut(statistic);
}
void write2File_degreedistribut(double* statistic)
{
	FILE* fwrite;
    	if( NULL == (fwrite = fopen("ER_degreedistribute.data", "w")))
    	{
        	printf("open file error!\n");
        	exit(0);
    	}
    	int i;
    	for( i = 0; i < NETWORK_SIZE; i++ )
        	fprintf(fwrite, "%d %f\n",i, statistic[i]);
    	fclose(fwrite);	
}

附使用igraph对在特定的参数下生成的随机网络的可视化图:网络大小为200,连边概率为0.03


评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值