Skip to content

Instantly share code, notes, and snippets.

@elcuervo
Created May 14, 2011 04:01
Show Gist options
  • Save elcuervo/971908 to your computer and use it in GitHub Desktop.
Save elcuervo/971908 to your computer and use it in GitHub Desktop.
Babarasi Model implementation
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
using namespace std;
int barabasi_albert(int number_of_nodes, float number_of_links, int core_size = 10, float connection_probability = 0.1) {
srand(time(NULL));
vector<int> degree_cumullative;
int degree_cumullative_size = 0;
int nodes[number_of_nodes];
for (int i = 1; i < core_size; i++) {
for (int j = i + 1; j < core_size; j++) {
if ((rand() * 1.) / (RAND_MAX * 1.) <= connection_probability) {
degree_cumullative.push_back(i);
degree_cumullative.push_back(j);
degree_cumullative_size += 2;
nodes[i].push(j);
cout << i << " " << j << endl;
}
}
}
for (int i = core_size; i < number_of_nodes; i++) {
double var = number_of_links - floor(number_of_links);
double plusone = (rand() * 1.) / (RAND_MAX * 1.);
double top_limit = (plusone < var) ? 1 : 0;
for(int j = 1; j <= number_of_links + top_limit; j++) {
int dest = degree_cumullative[rand() % degree_cumullative_size];
degree_cumullative.push_back(dest);
degree_cumullative.push_back(i);
cout << i << " " << dest << endl;
}
degree_cumullative_size += 2 * (int)floor(number_of_links);
}
}
int main(int argc, char **argv) {
int number_of_nodes = 100;
float number_of_links = 2.0;
int core_value = 10;
float connection_probability = 0.1;
if (argc >= 3) {
number_of_nodes = atoi(argv[1]);
number_of_links = atof(argv[2]);
if (argc >= 4) {
core_value = atoi(argv[3]);
if (core_value < 10) {
cout << "For better results must be > 10";
exit(EXIT_FAILURE);
}
if (argc == 5) {
connection_probability = atof(argv[4]);
}
}
}
barabasi_albert(number_of_nodes, number_of_links, core_value, connection_probability);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment