关闭

ER随机图构造算法

标签: 网络科学ER随机图构造算法实现ER随机图度分布实现
2860人阅读 评论(2) 收藏 举报
分类:

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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:70094次
    • 积分:1423
    • 等级:
    • 排名:千里之外
    • 原创:74篇
    • 转载:0篇
    • 译文:0篇
    • 评论:47条
    最新评论