Barabasi Albert Graph (for Scale Free Models) in C/C++?

The Barabási-Albert model is one of the most important models for generating scale-free networks. It combines two fundamental concepts: growth and preferential attachment. Growth means that the number of nodes in the network increases over time, while preferential attachment means that highly connected nodes are more likely to receive new connections.

This model simulates real-world networks where popular nodes (like well-known websites or influential people) attract more connections than less popular ones, following the "rich get richer" principle.

Syntax

// Basic structure for BA model implementation
typedef struct {
    int **adjacencyMatrix;
    int *degrees;
    int nodes;
} Graph;

Graph* createBAGraph(int initialNodes, int finalNodes, int edgesPerNode);

Algorithm Overview

The Barabási-Albert algorithm works as follows −

  1. Start with a small connected graph of m? nodes
  2. Add new nodes one by one
  3. Each new node connects to m existing nodes (m ? m?)
  4. Connection probability is proportional to existing node degrees

Example: Simple BA Model Implementation

Here's a basic implementation that demonstrates the preferential attachment mechanism −

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

typedef struct {
    int **adj;
    int *degree;
    int nodes;
} Graph;

Graph* initGraph(int nodes) {
    Graph *g = malloc(sizeof(Graph));
    g->nodes = nodes;
    g->adj = malloc(nodes * sizeof(int*));
    g->degree = calloc(nodes, sizeof(int));
    
    for(int i = 0; i < nodes; i++) {
        g->adj[i] = calloc(nodes, sizeof(int));
    }
    return g;
}

void addEdge(Graph *g, int u, int v) {
    if(!g->adj[u][v]) {
        g->adj[u][v] = 1;
        g->adj[v][u] = 1;
        g->degree[u]++;
        g->degree[v]++;
    }
}

int selectNodeByDegree(Graph *g, int currentNodes) {
    int totalDegree = 0;
    for(int i = 0; i < currentNodes; i++) {
        totalDegree += g->degree[i];
    }
    
    if(totalDegree == 0) return rand() % currentNodes;
    
    int target = rand() % totalDegree;
    int sum = 0;
    
    for(int i = 0; i < currentNodes; i++) {
        sum += g->degree[i];
        if(sum > target) return i;
    }
    return currentNodes - 1;
}

void printGraph(Graph *g, int nodes) {
    printf("Adjacency Matrix (%dx%d):\n", nodes, nodes);
    for(int i = 0; i < nodes; i++) {
        for(int j = 0; j < nodes; j++) {
            printf("%d ", g->adj[i][j]);
        }
        printf("\n");
    }
    
    printf("\nNode Degrees: ");
    for(int i = 0; i < nodes; i++) {
        printf("Node %d: %d  ", i, g->degree[i]);
    }
    printf("\n");
}

int main() {
    srand(time(NULL));
    int finalNodes = 6;
    int initialNodes = 3;
    int edgesPerNode = 2;
    
    Graph *g = initGraph(finalNodes);
    
    // Create initial complete graph
    for(int i = 0; i < initialNodes; i++) {
        for(int j = i + 1; j < initialNodes; j++) {
            addEdge(g, i, j);
        }
    }
    
    printf("Initial graph with %d nodes:\n", initialNodes);
    printGraph(g, initialNodes);
    
    // Add new nodes with preferential attachment
    for(int newNode = initialNodes; newNode < finalNodes; newNode++) {
        printf("\nAdding node %d:\n", newNode);
        
        for(int e = 0; e < edgesPerNode; e++) {
            int target = selectNodeByDegree(g, newNode);
            addEdge(g, newNode, target);
            printf("Connected node %d to node %d\n", newNode, target);
        }
    }
    
    printf("\nFinal BA Graph:\n");
    printGraph(g, finalNodes);
    
    free(g->degree);
    for(int i = 0; i < finalNodes; i++) {
        free(g->adj[i]);
    }
    free(g->adj);
    free(g);
    
    return 0;
}
Initial graph with 3 nodes:
Adjacency Matrix (3x3):
0 1 1 
1 0 1 
1 1 0 

Node Degrees: Node 0: 2  Node 1: 2  Node 2: 2  

Adding node 3:
Connected node 3 to node 1
Connected node 3 to node 2

Adding node 4:
Connected node 4 to node 1
Connected node 4 to node 3

Adding node 5:
Connected node 5 to node 1
Connected node 5 to node 3

Final BA Graph:
Adjacency Matrix (6x6):
0 1 1 0 0 0 
1 0 1 1 1 1 
1 1 0 1 0 0 
0 1 1 0 1 1 
0 1 0 1 0 0 
0 1 0 1 0 0 

Node Degrees: Node 0: 2  Node 1: 5  Node 2: 3  Node 3: 4  Node 4: 2  Node 5: 2

Key Properties

  • Scale-free: Degree distribution follows power law P(k) ? k^(-?)
  • Small world: Average path length grows logarithmically with network size
  • Rich get richer: High-degree nodes acquire connections faster
  • Growth parameter m: Controls network density and clustering

Applications

Domain Example Characteristic
Social Networks Facebook, Twitter Popular users gain followers faster
Web Graph World Wide Web Popular sites get more links
Citation Networks Scientific Papers Highly cited papers get cited more
Protein Networks Biological Systems Hub proteins have many interactions

Conclusion

The Barabási-Albert model successfully captures the scale-free property observed in many real-world networks through its simple growth and preferential attachment mechanism. This model provides fundamental insights into how complex networks evolve and maintain their characteristic degree distributions.

Updated on: 2026-03-15T12:42:16+05:30

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements