- #1
valesdn
- 42
- 1
Homework Statement
I need to write an Erdos-Renyi random graph, by using the adjacency matrix (or alternatively list) and calculate the fitness of the graph.
Definition: G(n, p) is a random graph with n vertices where each possible edge has probability p of existing.
Homework Equations
The number of edges in a G(n,p) graph is a random variable with a binomial expected value.
The Attempt at a Solution
I wrote the following C code:
Mod note: Edited by mentor to change code tag to code=c tag, and deleted extraneous spaces between lines.
C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define VOL 30
#define CONNECTIVITY 1
int main(){
int i , j , neighbour[VOL], connected[VOL];
int matrix[VOL][VOL];
for ( i = 0; i < VOL; i ++ ){
for ( j = 0; j < VOL; j ++ ){
matrix[i][j] = 0;
matrix[j][i] = 0;
}
}
srand((unsigned)time(NULL));
int num_edges = 0;
for(i = 0; i < VOL; i++){
connected[i] = 0;
for(j=VOL-1;j>=i;j--){
double prob=(rand()/(double)(RAND_MAX));
if(prob < CONNECTIVITY){
j = prob*VOL;
if(i != j ){
matrix[i][j] = 1;
matrix[j][i] = 1;
neighbour[i] = j;
neighbour[j] = i;
num_edges++;
printf("matrix[%d][%d]\n", i,j);
}
else{
j = prob*VOL;
matrix[i][j] = 1;
matrix[j][i] = 1;
neighbour[i] = j;
neighbour[j] = i;
connected[i] = 1;
}
}
}
return 0;
}
}
However there is something wrong. Any advice or help would be greatly appreciated. Thank you.
Last edited: