How to Add a New Network Model
Relevant source files
The following files were used as context for generating this wiki page:Introduction
This project is designed to accompany the Barabasi Network Science textbook and provides tools for network generation and analysis. The system architecture is decoupled, separating the network generation process from the analysis. This modularity allows for new network generation models to be added easily without altering the analysis tools.
Sources: README.md:2-5
Adding a new network model involves creating a program that generates a network and outputs it to a standardized "edgelist" file. This file can then be loaded into the provided C# analysis application (Network Analyzer) or the Python analysis script (Network.py) to compute various network properties like degree distribution, clustering coefficient, and degree correlation. The existing Barabasi-Albert preferential attachment model in Barabasi.cs serves as the primary template for creating new generators.
Sources: Barabasi.cs, Form1.cs, Network.py
Architecture Overview
The project follows a simple, file-based workflow. A generator program creates a network and writes it to a file. An analyzer program then reads this file to perform its calculations.
This diagram shows the decoupled workflow where a generator produces a file that is consumed by one or more analysis tools.
Sources: Barabasi.cs:113-116, Form1.cs:202-215, Network.py:10
Core Data Structures
The C# applications use several key classes to represent network data. A new model should generate data that can be represented by these structures.
| Class | File | Description |
|---|---|---|
Edge | Barabasi.cs, Form1.cs | Represents a link between two nodes, a and b. |
Edgelist | Barabasi.cs | A collection of Edge objects, representing the entire network structure. Contains the generation logic. |
Node | Form1.cs | Represents a node in the network, storing its ID, degree, neighbors, and calculated metrics. |
NodeList | Form1.cs | A collection of Node objects, built from an Edgelist for analysis purposes. |
| Sources: Barabasi.cs:25-32, 33-44, Form1.cs:57-65, 75-89 |
The following diagram illustrates the relationships between these classes.
Sources: Barabasi.cs:25-117, Form1.cs:57-187
Implementing a New Generation Model (C#)
The Barabasi.cs file provides a complete example of a network generator. To create a new model, you can replicate its structure.
1. Create the Generator Class
Create a new C# console application or class. The core logic will reside in a class analogous to Edgelist from Barabasi.cs. This class should manage a List<Edge>.
Sources: Barabasi.cs:33
2. Implement the Generation Algorithm
The Barabasi-Albert model is implemented in the Edgelist constructor. A new model would require a new constructor or method with its own generation logic.
// File: Barabasi.cs
public Edgelist(int nodes, int degree, bool multi)
{ //Creates edgelist using Barabasi-Albert preferential attachment
Random rnd = new Random();
int old_node, rnd_node_idx, count;
edgelist = new List<Edge> {new Edge(0, 1), new Edge(1, 2), new Edge(2, 3), new Edge(3, 0)};
for (int new_node = 4; new_node < nodes; new_node++)
{
count = edgelist.Count;
for (int i = 1; i <= degree; i++)
{
do
{
rnd_node_idx = rnd.Next(1, count * 2 + 1); //Preferential attachment
if (rnd_node_idx % 2 == 0)
old_node = edgelist[rnd_node_idx / 2 - 1].b;
else
old_node = edgelist[(int)rnd_node_idx / 2].a;
}
while (edgelist.Exists(x => (x.a == new_node && x.b == old_node)) && !multi);
edgelist.Add(new Edge(new_node, old_node));
}
}
}
Sources: Barabasi.cs:46-65
The following flowchart breaks down the existing Barabasi-Albert algorithm, which can be used as a template.
This flowchart visualizes the preferential attachment process in Barabasi.cs.
Sources: Barabasi.cs:49-64
3. Write to Edgelist File
Your generator must output the network to a text file in the specified format. The Edgelist class includes a Write method for this purpose.
File Format: The output must be a text file where each line represents one edge. The two node IDs for the edge should be separated by a tab character (\t).
// File: Barabasi.cs
public void Write(System.IO.StreamWriter file)
{
foreach (Edge edge in edgelist) file.WriteLine(edge.a + "\t" + edge.b);
}
Sources: Barabasi.cs:113-116
The Main method in Barabasi.cs demonstrates how to configure parameters, instantiate the Edgelist, and call the Write method.
// File: Barabasi.cs
static void Main(string[] args)
{
int nodes = 10000;
int degree = 3;
// ...
System.IO.StreamWriter file = new System.IO.StreamWriter("F:\\...\\dissociative.edgelist.txt");
Edgelist edgelist = new Edgelist(nodes, degree, true);
// ...
edgelist.Write(file);
file.Close();
}
Sources: Barabasi.cs:10-21
Analyzing the New Model
Once your new model generates an edgelist file, you can analyze it using the Network Analyzer C# application.
The application is a Windows Forms project, with Program.cs as the entry point that launches Form1.
Sources: Program.cs:17-19
The user interface, defined in Form1.Designer.cs, contains a menu strip with a "File" -> "Open" option. This action triggers the file loading and analysis process.
Sources: Form1.Designer.cs:152-161
The following sequence diagram shows the process of loading and analyzing a file.
This diagram illustrates the event flow when a user opens an edgelist file for analysis in the C# application.
Sources: Form1.cs:199-291, Form1.Designer.cs:156-161
Conclusion
The project's architecture effectively separates network generation from analysis, facilitating the extension of its capabilities. To add a new network model, a developer needs to create a standalone program that implements the desired generation algorithm and outputs the result to a tab-separated edgelist file. This file serves as a universal interface to the existing analysis tools, allowing any generated network to be examined for its statistical properties without modifying the analysis codebase. The Barabasi.cs implementation provides a clear and practical template for this process.