Models

This notebook shows how to load some graphs based on simple models.

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import erdos_renyi, block_model, linear_graph, cyclic_graph, linear_digraph, cyclic_digraph, grid, albert_barabasi, watts_strogatz
from sknetwork.visualization import svg_graph, svg_digraph

Graphs

Erdos-Renyi model

[4]:
adjacency = erdos_renyi(20, 0.2)
[5]:
image = svg_graph(adjacency)
[6]:
SVG(image)
[6]:
../../_images/tutorials_data_models_9_0.svg

Stochastic block model

[7]:
graph = block_model([20,25,30], p_in=[0.5,0.4,0.3], p_out=0.02, metadata=True)
adjacency = graph.adjacency
labels = graph.labels
[8]:
image = svg_graph(adjacency, labels=labels)
[9]:
SVG(image)
[9]:
../../_images/tutorials_data_models_13_0.svg

Linear graph

[10]:
graph = linear_graph(8, metadata=True)
adjacency = graph.adjacency
position = graph.position
[11]:
image = svg_graph(adjacency, position)
[12]:
SVG(image)
[12]:
../../_images/tutorials_data_models_17_0.svg
[13]:
# adjacency matrix only
adjacency = linear_graph(8)

Cyclic graph

[14]:
graph = cyclic_graph(8, metadata=True)
adjacency = graph.adjacency
position = graph.position
[15]:
image = svg_graph(adjacency, position, width=200, height=200)
[16]:
SVG(image)
[16]:
../../_images/tutorials_data_models_22_0.svg

Grid

[17]:
graph = grid(6, 4, metadata=True)
adjacency = graph.adjacency
position = graph.position
[18]:
image = svg_graph(adjacency, position)
[19]:
SVG(image)
[19]:
../../_images/tutorials_data_models_26_0.svg

Albert-Barabasi model

[20]:
adjacency = albert_barabasi(n=100, degree=3)
[21]:
image = svg_graph(adjacency, labels={i:0 for i in range(3)}, display_node_weight=True, node_order=np.flip(np.arange(100)))
[22]:
SVG(image)
[22]:
../../_images/tutorials_data_models_30_0.svg

Watts-Strogatz model

[23]:
adjacency = watts_strogatz(n=100, degree=6, prob=0.2)
[24]:
image = svg_graph(adjacency, display_node_weight=True, node_size_max=10)
[25]:
SVG(image)
[25]:
../../_images/tutorials_data_models_34_0.svg

Digraphs

Linear graph

[26]:
graph = linear_digraph(8, metadata=True)
adjacency = graph.adjacency
position = graph.position
[27]:
image = svg_digraph(adjacency, position)
[28]:
SVG(image)
[28]:
../../_images/tutorials_data_models_39_0.svg

Cyclic graph

[29]:
graph = cyclic_digraph(8, metadata=True)
adjacency = graph.adjacency
position = graph.position
[30]:
image = svg_digraph(adjacency, position, width=200, height=200)
[31]:
SVG(image)
[31]:
../../_images/tutorials_data_models_43_0.svg