Data Flow in the C# Application
Relevant source files
The following files were used as context for generating this wiki page:This document outlines the data flow and architecture of the C# network analysis and generation application. The project consists of two primary C# executables: a network analyzer with a GUI (Charts project) and a console application for generating networks using the Barabasi-Albert model (Networks project).
The network analyzer (Form1.cs) is a Windows Forms application that loads a network from a file, calculates various statistical properties, and visualizes them. The network generator (Barabasi.cs) creates a synthetic network based on preferential attachment and saves it to a file, which can then be analyzed by the former.
Application Entry and Core Structure
The main entry point for the GUI application is defined in Program.cs. It initializes and runs Form1, which contains all the logic for network analysis and visualization. The Barabasi.cs file contains a separate Program class with its own Main method, designed to be run as a console application for network generation.
Sources: Program.cs:17-19, Barabasi.cs:12-20
Core Data Structures
The application uses several custom classes to represent the network. The Edge class is a fundamental structure in both the analyzer and generator, representing a link between two nodes. The analyzer uses Node and NodeList classes to store node properties and manage the entire network graph.
This class diagram illustrates the main data-holding classes in both C# projects.
Sources: Form1.cs:48-167, Barabasi.cs:21-104
Node Properties
The Node class in Form1.cs stores calculated properties for each node in the network.
| Property | Type | Description |
|---|---|---|
node_ID | int | The unique identifier for the node. |
degree | int | The total number of connections the node has. |
neighbors | List<int> | A list of node_IDs for all connected nodes. |
clustering | double | The node's local clustering coefficient. |
neighbors_degree | double | The average degree of the node's neighbors. |
Sources: Form1.cs:61-73
Network Analysis Data Flow (Form1.cs)
The primary data flow in the Charts application begins with user interaction to load a file and ends with the display of calculated network metrics and charts. The process is orchestrated within the openToolStripMenuItem_Click event handler.
Sources: Form1.cs:176-323
The following diagram shows the sequence of operations for network analysis.
Sources: Form1.cs:176-323
1. File Loading and Edge List Creation
The process starts when the user selects a file via the openFileDialog1 component. The openToolStripMenuItem_Click event handler is triggered. It reads the selected file, which is expected to be a tab-separated list of node pairs, and constructs a List<Edge>.
// Sources: [Form1.cs:182-192](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs#L182-L192)
using (StreamReader file = new StreamReader(openFileDialog1.FileName))
{
string line;
while ((line = file.ReadLine()) != null)
{
string[] token = line.Split('\t'); //Remember files may use tab or comma
edgelist.Add(new Edge(token[0], token[1]));
textBox1.Text = "Building Edge List " + token[0];
Application.DoEvents();
}
}
2. Node List Processing and Property Calculation
A NodeList object is instantiated using the edgelist. The NodeList constructor performs several key data processing steps:
- Node Discovery: It iterates through the
edgelistto identify all unique nodes and createsNodeobjects for them. - Degree and Neighbor Calculation: It traverses the
edgelistagain for each node to calculate its degree and populate its list of neighbors. - Neighbor Degree Calculation: It calculates the average degree of each node's neighbors, a metric used for degree correlation analysis.
Sources: Form1.cs:85-132
3. Metric Calculation and Visualization
After the NodeList is fully constructed, the application calculates various network metrics:
- Basic Metrics: Total nodes, links, max/min degree, self-links, and duplicate links are computed and displayed in UI labels.
- Clustering Coefficient: If the
chkClusteringcheckbox is active, the average clustering coefficient for the network is calculated by theNodeList.Clusteringmethod. - Degree Correlation: The application builds a data series of average neighbor degree vs. node degree. It then uses
MathNet.Numerics.Fit.Powerto perform a power-law regression and find the correlation exponent. - Degree Distribution: A cumulative degree distribution is generated. A power-law regression is fitted to the tail of this distribution (controlled by
txtCutoff) to calculate the degree exponent (gamma).
Finally, the results are visualized. chart1 displays the degree correlation, and chart2 shows the cumulative degree distribution, both with their respective regression lines.
Sources: Form1.cs:197-322, Form1.Designer.cs:42-53, Form1.Designer.cs:202-213
Network Generation Data Flow (Barabasi.cs)
The Networks project is a console application that generates a network using the Barabasi-Albert model and saves it to a file.
The data flow for network generation is as follows:
This diagram shows the process of creating a network, modifying it, and writing it to a file.
Sources: Barabasi.cs:12-20
1. Network Initialization
The Main method sets parameters like the number of nodes and the degree for new nodes. It then instantiates an Edgelist object. The Edgelist constructor implements the Barabasi-Albert preferential attachment algorithm. It starts with a small, fully connected network and iteratively adds new nodes, connecting them to existing nodes with a probability proportional to their current degree.
// Sources: [Barabasi.cs:51-68](https://github.com/mvirag2000/Network-Science/blob/HEAD/Barabasi.cs#L51-L68)
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); //Treat edge list as single (one-based) node list
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); //Don't link to same node twice
edgelist.Add(new Edge(new_node, old_node));
}
}
2. Edge Association and Output
After generation, the Associate method can be called to rewire a specified percentage of edges, either to increase (sociable) or decrease (dissociative) the degree correlation. Finally, the Write method is called, which iterates through the final edgelist and writes each edge to a tab-separated text file.
Sources: Barabasi.cs:74-104
Conclusion
The C# applications in this repository provide a dual-functionality system for network science studies. The Barabasi.cs console application facilitates the creation of synthetic scale-free networks through a clear, procedural data flow. The Form1.cs Windows Forms application provides a user-friendly interface for analyzing these (or other) networks, implementing a data flow that transforms raw edge data into calculated metrics and graphical visualizations. The data flows from file input, through structured in-memory objects (NodeList), to statistical computation and finally to UI-bound charts and labels.