Getting started

In scikit-network, graphs are represented by their adjacency matrix in the Compressed Sparse Row format of SciPy.

In this tutorial, we present a few methods to instantiate such inputs.

[1]:
from IPython.display import SVG

import numpy as np
from scipy import sparse

from sknetwork.utils import edgelist2adjacency, edgelist2biadjacency
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph

From a NumPy array

For small graphs, you can instantiate the adjacency matrix as a dense NumPy array and convert it into a sparse matrix in CSR format.

[2]:
adjacency = np.array([[0, 1, 1, 0], [1, 0, 1, 1], [1, 1, 0, 0], [0, 1, 0, 0]])
adjacency = sparse.csr_matrix(adjacency)

image = svg_graph(adjacency)
SVG(image)
[2]:
../_images/tutorials_getting_started_4_0.svg

From an edge list

Another natural way to build a graph is from a list of edges.

[3]:
edgelist = [(0, 1), (1, 2), (2, 3), (3, 0), (0, 2)]
adjacency = edgelist2adjacency(edgelist)

image = svg_digraph(adjacency)
SVG(image)
[3]:
../_images/tutorials_getting_started_6_0.svg

By default, the graph is treated as directed, but you can easily make it undirected.

[4]:
adjacency = edgelist2adjacency(edgelist, undirected=True)

image = svg_graph(adjacency)
SVG(image)
[4]:
../_images/tutorials_getting_started_8_0.svg

You might also want to add weights to your edges. Just use triplets instead of pairs!

[5]:
edgelist = [(0, 1, 1), (1, 2, 0.5), (2, 3, 1), (3, 0, 0.5), (0, 2, 2)]
adjacency = edgelist2adjacency(edgelist)

image = svg_digraph(adjacency)
SVG(image)
[5]:
../_images/tutorials_getting_started_10_0.svg

You can instantiate a bipartite graph as well.

[6]:
edgelist = [(0, 0), (1, 0), (1, 1), (2, 1)]
biadjacency = edgelist2biadjacency(edgelist)

image = svg_bigraph(biadjacency)
SVG(image)
[6]:
../_images/tutorials_getting_started_12_0.svg

From a NetworkX object

NetworkX has import and export functions from and towards the CSR format.

Other options

  • You have a TSV file containing a list of edges

  • You have a GraphML file

  • You want to test our toy graphs

  • You want to generate a graph from a model

  • You want to load a graph from one of our referenced repositories (see NetSets and KONECT)

Take a look at the tutorials of the data section !