模型

此笔记本展示了如何基于简单模型加载一些图。

[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 visualize_graph

Erdos-Renyi 模型

[4]:
adjacency = erdos_renyi(20, 0.2)
[5]:
image = visualize_graph(adjacency)
SVG(image)
[5]:
../../_images/tutorials_data_models_7_0.svg

随机块模型

[6]:
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
[7]:
image = visualize_graph(adjacency, labels=labels)
SVG(image)
[7]:
../../_images/tutorials_data_models_10_0.svg

线性图

[8]:
graph = linear_graph(8, metadata=True)
adjacency = graph.adjacency
position = graph.position
[9]:
image = visualize_graph(adjacency, position)
SVG(image)
[9]:
../../_images/tutorials_data_models_13_0.svg
[10]:
# adjacency matrix only
adjacency = linear_graph(8)
[11]:
# directed
graph = linear_digraph(8, metadata=True)
adjacency = graph.adjacency
position = graph.position
[12]:
image = visualize_graph(adjacency, position)
SVG(image)
[12]:
../../_images/tutorials_data_models_16_0.svg

循环图

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

网格

[16]:
graph = grid(6, 4, metadata=True)
adjacency = graph.adjacency
position = graph.position
[17]:
image = visualize_graph(adjacency, position)
[18]:
SVG(image)
[18]:
../../_images/tutorials_data_models_24_0.svg

Albert-Barabasi 模型

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

Watts-Strogatz 模型

[21]:
adjacency = watts_strogatz(n=100, degree=6, prob=0.2)
[22]:
image = visualize_graph(adjacency, display_node_weight=True, node_size_max=10)
SVG(image)
[22]:
../../_images/tutorials_data_models_30_0.svg