![]() |
STAG Python
1.2.1
Spectral Toolkit of Algorithms for Graphs
|
The STAG library is an easy-to-use Python library providing several spectral algorithms for graphs. The library is based on the STAG C++ library.
Begin by installing STAG via pip, the python package manager.
The core graph object is stag.graph.Graph which can be created using a sparse adjacency matrix.
You can also create a variety of standard graphs using the stag.graph module. For example, to create a complete graph on \(5\) vertices, use the following code.
You can see the adjacency matrix of your constructed graphs with the followin code.
Notice that the stag.graph.Graph.adjacency method returns a sparse scipy
matrix and we make this dense with the todense()
method.
STAG supports the edgelist file format for reading graphs from disk and the stag.graphio module provides methods for reading and writing them.
Suppose you have a simple edgelist file like this one.
Then, we can read it as follows.
We can also save graphs as an edgelist.
Edgelist files can also include edge weights as a third parameter - notice that the generated star.edgelist
file specifies an edge weight of \(1\) for every edge.
See Graph File Formats for more information on the file formats supported by the STAG library.
The stag.cluster module provides methods for finding clusters in graphs using local clustering algorithms. The main method provided by this module is stag.cluster.local_cluster. This method takes three arguments:
Given these arguments, stag.cluster.local_cluster will return a list of vertices which form a cluster around the seed vertex, and whose total volume is close to the target volume.
For example, to find the clusters in a barbell graph: