Feature: Degree Correlation Calculation
Relevant source files
The following files were used as context for generating this wiki page:The Degree Correlation Calculation is a core feature of the C# Network Analyzer application. Its purpose is to analyze the assortativity of a network by measuring the relationship between the degrees of connected nodes. The system loads a network from an edgelist file, calculates the average degree of the neighbors for each node, and then plots this correlation. A power-law regression is fitted to the data to compute a degree correlation exponent, which indicates whether the network is assortative (hubs connect to hubs) or disassortative (hubs connect to low-degree nodes).
This functionality is part of a larger suite of tools for network analysis, as described in the project's README.md. The analysis is performed within the main application form (Form1) and visualized in a dedicated chart. The project also includes tools to generate networks with specific correlation properties, which can then be analyzed by this feature.
Sources: Form1.cs, README.md, Barabasi.cs
System Architecture
The feature is implemented within a single C# Windows Forms application. The user interacts with the UI to load a file, which triggers a series of calculations in the code-behind. The results are then displayed in UI labels and a chart control.
Core Components
| Component | Description | Source File |
|---|---|---|
Form1 | The main application window. Contains the UI controls and the event handling logic that drives the analysis process. | Form1.cs, Program.cs:17 |
NodeList | A class that encapsulates the entire network structure and orchestrates the primary calculations, including degree and neighbor degree averages. | Form1.cs:79-181 |
Node | A class representing a single node in the network, storing its ID, degree, neighbors, and the calculated average degree of its neighbors. | Form1.cs:63-77 |
Edge | A simple class representing a connection between two nodes. | Form1.cs:49-59 |
chart1 | A Chart control used to visualize the degree correlation as a scatter plot with a regression line. | Form1.Designer.cs:61-73 |
lblCorrelation | A Label control used to display the calculated degree correlation exponent. | Form1.Designer.cs:178-184 |
Data Flow
The following diagram illustrates the high-level data flow, from user action to the final visualization.
This flowchart shows the sequence starting from the user opening a file, through data parsing, network construction, core calculations within the NodeList class, and finally rendering the results on the UI.
Sources: Form1.cs:192-269
Network Data Structures
The network is represented by a set of custom classes that model nodes, edges, and the list of nodes.
Class Diagram
This diagram shows the relationships between the Edge, Node, and NodeList classes.
Sources: Form1.cs:49-181
Node Class Properties
The Node class is central to the calculation, storing per-node metrics.
| Property | Type | Description |
|---|---|---|
node_ID | int | The unique identifier for the node. |
degree | int | The total number of connections (degree) of the node. |
neighbors | List<int> | A list of the node_IDs of its neighboring nodes. |
neighbors_degree | double | The average degree of all nodes in the neighbors list. This is the key value for the correlation calculation. |
Sources: Form1.cs:63-77
Degree Correlation Calculation Logic
The calculation is a multi-step process handled primarily by the NodeList constructor and the openToolStripMenuItem_Click event handler.
Sequence of Operations
The following sequence diagram details the interactions between components during the calculation.
Sources: Form1.cs:192-269
Step 1: Network Construction (NodeList Constructor)
When a NodeList object is created, it immediately processes the input edgelist.
-
Build Node List: It iterates through all edges to create a unique list of
Nodeobjects, one for each unique node ID found in the edgelist.
Sources: Form1.cs:95-98 -
Calculate Degrees and Neighbors: It traverses the
nodelistagain. For each node, it scans the entireedgelistto find its neighbors and count its degree.
Sources: Form1.cs:103-119 -
Calculate Average Neighbor Degree: In a final loop over the
nodelist, it calculates theneighbors_degreeproperty for each node. This is done by summing thedegreeof each of its neighbors and then dividing by its owndegree.// File: Form1.cs, Lines: 121-129 foreach (Node node in nodelist) { msg.Text = "Calculating degree correlation: " + node.node_ID.ToString(); Application.DoEvents(); foreach (int neighbor in node.neighbors) { node.neighbors_degree += nodelist.Find(x => neighbor == x.node_ID).degree; } node.neighbors_degree = node.neighbors_degree / node.degree; //Average of neighbors' degrees }
Step 2: Data Aggregation for Plotting
After the NodeList is fully initialized, the openToolStripMenuItem_Click handler aggregates the data for plotting. It iterates through all possible degrees (deg from 1 to max_degree). For each degree deg, it finds all nodes with that degree and calculates the average of their neighbors_degree values. This creates the data points (k, k_nn(k)) for the correlation plot.
// File: Form1.cs, Lines: 228-242
Series neighbors = new Series();
for (int deg = 1; deg <= max; deg++)
{
double count = 0;
double accum = 0;
// ...
foreach (Node node in nodelist)
{
if (node.degree == deg)
{
accum += node.neighbors_degree;
count++;
}
}
if (count > 0) neighbors.Points.Add(new DataPoint(deg, accum/count));
}
Step 3: Power-Law Fit and Visualization
The aggregated data points are used to perform a power-law fit using the MathNet.Numerics.Fit.Power method. This function models the data as y = c * x^exp and returns the coefficient c and the exponent exp. The exponent is the "Degree correlation exponent" displayed in the UI.
Finally, the aggregated data points are plotted on chart1 as a scatter plot, and the calculated power-law regression is overlaid as a red line. Both axes of the chart are set to a logarithmic scale to linearize the power-law relationship.
Sources: Form1.cs:243-269, Form1.Designer.cs:61-73
Related Functionality
Network Generation
The repository also contains a tool to generate synthetic networks with specific correlation properties. The Barabasi.cs file includes a method Associate(int p, bool positive) that can rewire a Barabasi-Albert network to induce assortative (positive = true) or disassortative (positive = false) mixing. This allows for the creation of test data for the analysis feature.
Sources: Barabasi.cs:78-102
Python Analysis Script
A Python script, Network.py, is included for performing similar network analysis, focusing on degree distribution. While it does not compute degree correlation in the same manner, it demonstrates the project's broader goal of providing tools for network science analysis, as stated in the README.
Sources: Network.py, README.md
Summary
The Degree Correlation Calculation feature is a well-defined module within the Network-Science project that provides a robust mechanism for analyzing network assortativity. It follows a clear pipeline: loading an edgelist, constructing an in-memory graph representation, calculating per-node degree metrics, and aggregating these metrics to compute and visualize the degree correlation function. The use of a power-law fit provides a single quantitative metric—the correlation exponent—to characterize the network's mixing patterns.