Network Data Structure
Relevant source files
The following files were used as context for generating this wiki page:
The Network-Science project utilizes several C# classes to represent and analyze network graphs. The fundamental data structures are centered around the concepts of nodes and edges, which are organized into lists for processing. An Edgelist is used for network generation and initial representation, while a NodeList is constructed from an Edgelist to perform detailed statistical analysis, such as calculating degree distribution, clustering coefficients, and degree correlation.
The C# implementation separates network generation (in Barabasi.cs) from network analysis (in Form1.cs), though they share a common Edge definition. A parallel Python script (Network.py) performs similar degree distribution analysis but uses a pandas DataFrame as its primary data structure, demonstrating an alternative approach to handling network data.
Core C# Data Structures
The C# applications use a set of nested classes to model the network. The primary classes are Edge, Edgelist, Node, and NodeList.
The following diagram illustrates the relationships between these core classes.
This diagram shows that an Edgelist is a collection of Edge objects, and a NodeList is a collection of Node objects. The NodeList is constructed using an Edgelist.
Sources: Barabasi.cs:25-41, Form1.cs:57-184
Edge Class
The Edge class is the most fundamental component, representing a link between two nodes in the network. It is defined in both Barabasi.cs for network generation and Form1.cs for network analysis.
| Property | Type | Description |
|---|---|---|
a | int | The identifier for the first node in the edge. |
b | int | The identifier for the second node in the edge. |
| Sources: Barabasi.cs:27-28, Form1.cs:59-60 |
The class provides two constructors: one for integer inputs and another for string inputs, which are parsed into integers.
// From Form1.cs
public class Edge
{
public int a, b;
public Edge(int AA, int BB)
{
a = AA;
b = BB;
}
public Edge(string AA, string BB)
{
a = int.Parse(AA);
b = int.Parse(BB);
}
}
Sources: Form1.cs:57-71
Edgelist Class
Defined in Barabasi.cs, the Edgelist class manages a collection of Edge objects and is primarily used for generating networks.
| Method/Constructor | Description |
|---|---|
Edgelist(List<Edge> l) | Constructor that creates an edgelist from a pre-existing list of edges. |
Edgelist(int nodes, int degree, bool multi) | Constructor that generates a network using the Barabási-Albert preferential attachment model. |
Associate(int p, bool positive) | Rewires a percentage (p) of edges to induce degree assortativity (positive) or disassortativity (negative). |
Write(System.IO.StreamWriter file) | Writes the entire edgelist to a specified file, with each line representing an edge. |
| Sources: Barabasi.cs:35-131 |
The network generation process using the Barabási-Albert model is encapsulated within the second constructor.
This flowchart shows the preferential attachment algorithm implemented in the Edgelist constructor. A new node connects to existing nodes with a probability proportional to their current degree.
Sources: Barabasi.cs:48-70
Node Class
The Node class, defined in Form1.cs, stores data for a single node, including its calculated properties. It is used exclusively for network analysis.
| 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. |
clustering | double | The local clustering coefficient of the node. |
neighbors_degree | double | The average degree of all its neighbors. |
| Sources: Form1.cs:73-86 |
NodeList Class
The NodeList class, also in Form1.cs, is a container for Node objects and serves as the primary engine for calculating network metrics. It is initialized from an Edgelist read from a file.
Key Properties:
selfies: Count of self-linking edges (e.g.,a == b).dupes: Count of duplicate edges.max_degree: The highest degree found in the network.min_degree: The lowest degree found in the network.
Sources: Form1.cs:91-94
Core Logic:
The constructor for NodeList performs a multi-step process to build the analytical view of the network.
This sequence shows the flow from a user opening a file to the NodeList being constructed and used to calculate network properties, which are then displayed on the UI.
Sources: Form1.cs:89-158, Form1.cs:211-233, Form1.Designer.cs:90-101
The Clustering method calculates the average clustering coefficient for the entire network by first computing the local clustering coefficient for each node.
// From Form1.cs
public double Clustering(List<Edge> e, Control msg)
{
double total_cluster = 0;
int nodes = nodelist.Count();
foreach (Node node in nodelist)
{
// ... logic to find links between neighbors ...
if (node.degree > 1) node.clustering = (double) 2 * cluster / (node.degree * (node.degree - 1));
total_cluster += node.clustering;
}
return total_cluster / nodes; //Average clustering
}
Sources: Form1.cs:160-184
Python Data Representation
The Python script Network.py offers a different implementation for network analysis. Instead of custom classes, it leverages the pandas library to represent the network.
- Edgelist Loading: The script reads a tab-separated edgelist file into a pandas
DataFrame.
Sources: Network.py:11edgelist = pd.read_csv('F:\\Math\\Network Science\\' + dataset, sep='\t', lineterminator='\n', names=['A', 'B']) - Degree Calculation: It calculates the in-degree and out-degree of each node using
groupby()andcount()operations on the DataFrame.
Sources: Network.py:15-18out = edgelist.groupby('A').count() degree = pd.DataFrame.from_dict(out) degree['In'] = edgelist.groupby('B').count() - Analysis: The resulting
degreeDataFrame is then used for statistical analysis, such as creating a log-binned degree distribution histogram and performing a linear regression to find the degree exponent (gamma).
This approach demonstrates a more data-centric method for network analysis, relying on a powerful, general-purpose library rather than custom-built data structures.
Sources: Network.py
Conclusion
The project employs two distinct but related models for representing network data. In C#, a structured, object-oriented approach with Edge, Edgelist, Node, and NodeList classes separates the concerns of network generation from analysis. Edgelist is tailored for building networks via preferential attachment, while NodeList is designed to process a static edgelist to compute a rich set of metrics. In parallel, the Python script showcases a more concise, data-driven approach using pandas DataFrames, which is well-suited for rapid statistical analysis and visualization. Both implementations effectively model the core node-edge structure of a network to fulfill the project's goal of studying network properties.
Sources: README.md, Barabasi.cs, Form1.cs, Network.py