![]() |
STAG C++
2.0.0
Spectral Toolkit of Algorithms for Graphs
|
The STAG library supports two simple file formats for storing graphs on disk: EdgeList and AdjacencyList.
In an EdgeList file, each line in the file corresponds to one edge in the graph. A line consists of two node IDs which should be integers and optionally an edge weight which can be a real number. The IDs and weight should be separated with spaces.
For example, here is a simple EdgeList file.
This file defines a graph on three nodes (0
, 1
, and 2
) with three edges: (0, 1)
, (1, 2)
, and (0, 2)
. Here is another example, which specifies edge weights.
In an AdjacencyList file, each line in the file corresponds to one node in the graph. A line consists of the node ID, followed by a list of adjacent nodes. The node IDs at the beginning of each line must be sorted in increasing order. For example, here is a simple AdjacencyList file.
In this example, the node with ID 1
has edges to nodes 0
, 2
, and 3
. All edges are taken to have weight 1. The following example shows how to specify the weight of each edge.
Here, the node with ID 1
has an edge of weight 0.5
to node 0
and an edge of weight 1
to node 2
.
The graphio.h module provides several methods for reading, writing and converting between EdgeList and AdjacencyList files. For example, the following code will read an AdjacencyList file and create a stag::Graph object.
The following example will generate a random graph and save it as an edgelist.
The stag::edgelist_to_adjacencylist and stag::adjacencylist_to_edgelist methods allow for conversion between the two file formats.
When working with huge graphs, the stag::AdjacencyListLocalGraph object provides a way to query a graph locally without loading the entire graph into memory. For example, the following code will query the neighbours of the node with ID 100
.
One of the most useful applications of the stag::AdjacencyListLocalGraph object is for local clustering. The following example finds a cluster containing node 100
.
Finally, the STAG tools provide command line tools for converting between graph file formats.