The Barabasi.cs Class
Relevant source files
The following files were used as context for generating this wiki page:Barabasi.cs is a C# console application within the Network-Science project that programmatically generates a network using the Barabasi-Albert model, which employs a preferential attachment mechanism. The primary purpose of this module is to create a synthetic network and save it as an edgelist file. This generated file can then be analyzed by other tools within the repository, such as the C# Network Analyzer (Form1.cs) or the Python analysis script (Network.py), to study properties of scale-free networks.
The program's functionality is encapsulated within the Networks.Program class and its nested helper classes, Edge and Edgelist. It not only generates the network but also includes a feature to rewire edges to introduce positive (assortative) or negative (dissociative) degree correlations.
Sources: Barabasi.cs, README.md:4
Core Components
The logic for network generation is contained within the Networks namespace in Barabasi.cs. It consists of a main Program class and two nested classes, Edge and Edgelist, which define the network structure and generation logic.
Sources: Barabasi.cs:5-102
The class diagram below illustrates the structure of the components within Barabasi.cs.
Sources: Barabasi.cs:5-102
Edge Class
The Edge class is a simple data structure representing a link between two nodes in the network.
| Field | Type | Description |
|---|---|---|
a | int | The ID of the first node in the edge. |
b | int | The ID of the second node in the edge. |
Sources: Barabasi.cs:25-32
Edgelist Class
The Edgelist class is the core component that manages the list of edges and contains the algorithms for network generation and modification.
Key Methods:
Edgelist(int nodes, int degree, bool multi): The constructor that generates a network using the Barabasi-Albert model.Associate(int p, bool positive): A method to rewire a percentage of edges to create assortative or disassortative networks.Write(System.IO.StreamWriter file): Writes the final edgelist to a specified file stream.
Sources: Barabasi.cs:33-102
Network Generation Process
The network generation is initiated and controlled by the Main method in the Program class. It sets the parameters for the network, instantiates the Edgelist class to trigger the generation, optionally rewires the network, and writes the result to a file.
The following flowchart shows the high-level execution flow of the Main method.
Sources: Barabasi.cs:9-17
Barabasi-Albert Algorithm
The network generation occurs within the Edgelist constructor. It follows the Barabasi-Albert model for preferential attachment. The process starts with a small, fully connected seed network and iteratively adds new nodes, connecting them to existing nodes with a probability proportional to their current degree.
The algorithm is implemented as follows:
- An initial network of 4 nodes connected in a square is created.
- The algorithm iterates from the 5th node up to the total number of
nodesspecified. - For each
new_node, it creates a number of new links equal to thedegreeparameter. - To select an existing node (
old_node) to connect to, it randomly picks from a flattened list of all node endpoints in the currentedgelist. This inherently gives nodes with a higher degree (i.e., those that appear more often in theedgelist) a higher chance of being selected. - A new
Edge(new_node, old_node)is added to the list. A check prevents a new node from linking to the sameold_nodemore than once if themultiflag is false.
This flowchart details the preferential attachment logic in the constructor.
Sources: Barabasi.cs:51-70
Edge Rewiring (Associate method)
The Associate method modifies the network topology post-generation to introduce a specific degree correlation. It can make the network assortative (high-degree nodes connect to other high-degree nodes) or disassortative (high-degree nodes connect to low-degree nodes).
Parameters:
p: An integer representing the percentage of links to be rewired.positive: A boolean that determines the type of correlation.truefor assortative (sociable),falsefor disassortative.
The rewiring process is as follows:
- The method iterates for a number of times equal to
ppercent of the total edge count. - In each iteration, two edges,
edge1(a, b)andedge2(c, d), are chosen randomly. - The four node IDs (
a, b, c, d) are collected and sorted. - Assortative (
positive = true): The original edges are removed and replaced with new edges connecting the two smallest-ID nodes and the two largest-ID nodes:(values[0], values[1])and(values[2], values[3]). This promotes linking between nodes of similar "age" and likely similar degree. - Disassortative (
positive = false): The edges are rewired to connect the smallest-ID node with the largest, and the two middle ones:(values[0], values[3])and(values[1], values[2]). - A check is performed in both cases to prevent the creation of self-links.
Sources: Barabasi.cs:72-96
Output and Integration
The final step of the Barabasi.cs program is to write the generated network to an output file using the Write method. This method iterates through the final edgelist and writes each edge as a tab-separated pair of node IDs on a new line.
// Source: Barabasi.cs:98-101
public void Write(System.IO.StreamWriter file)
{
foreach (Edge edge in edgelist) file.WriteLine(edge.a + "\t" + edge.b);
}
This output format is compatible with the other analysis tools in the repository:
- C# Network Analyzer (
Form1.cs): TheopenToolStripMenuItem_Clickevent handler reads a file where lines are split by tabs to create anedgelist. This allows it to analyze networks generated byBarabasi.cs. - Python Analyzer (
Network.py): This script usespandas.read_csvwithsep='\t'to load an edgelist, demonstrating its ability to process the same file format.
Sources: Barabasi.cs:98-101, Form1.cs:202-205, Network.py:12
Related Analysis Tools
The Network-Science repository contains tools to analyze the networks generated by Barabasi.cs.
C# Network Analyzer
The Charts project, with its main window Form1, provides a GUI to load an edgelist file and compute various network metrics. It can calculate:
- Number of nodes and links
- Average, min, and max degree
- Average clustering coefficient
- Degree correlation exponent
It then visualizes the degree correlation and the cumulative degree distribution on two separate charts, fitting a power-law regression to the data. The application is launched via Program.cs in the Charts namespace.
Sources: Form1.cs, Form1.Designer.cs, Program.cs:16-18
Python Analyzer (Network.py)
A parallel analysis tool is provided as a Python script, Network.py. This script performs a similar function:
- Reads a tab-separated edgelist.
- Calculates node degrees.
- Bins the degrees logarithmically.
- Plots the degree distribution on a log-log scale.
- Performs a linear regression on the log-transformed data to calculate the degree exponent (gamma).
This script provides an alternative, script-based way to verify the scale-free properties of the generated networks.
Sources: Network.py, README.md:3
Conclusion
Barabasi.cs is a foundational module in the Network-Science project, serving as a generator for synthetic scale-free networks using the Barabasi-Albert model. Its ability to create networks with preferential attachment and then fine-tune their degree correlation makes it a versatile tool for the experiments described in the accompanying textbook. The simple, portable edgelist format it produces ensures seamless integration with the C# and Python analysis tools included in the repository, allowing for a comprehensive workflow from network generation to property analysis.
Sources: Barabasi.cs, README.md