Scikit-network-04:网络拓扑 Topology

这篇文章介绍了使用Python库sknetwork进行网络拓扑分析,包括寻找图的联通量、最大联通分量、有向图的弱和强连接分量、二部图的最大联通分量以及环路检测。此外,还涉及了核分解、聚类系数计算以及图的同构测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网络拓扑 Topology

Connected components|联通量

本笔记本说明图中连接的组件的搜索。

import numpy as np
from IPython.display import SVG
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.topology import get_connected_components, get_largest_connected_component
from sknetwork.visualization import svg_graph, svg_bigraph
from sknetwork.utils.format import bipartite2undirected

构图

graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position

# subgraph

k = 15
adjacency = adjacency[:k][:, :k]
position = position[:k]

# connected compbnents
labels = get_connected_components(adjacency)

image = svg_graph(adjacency, position, labels=labels)
SVG(image)

在这里插入图片描述

寻找最大联通分量

# largest connected component
new_adjacency, index = get_largest_connected_component(adjacency, return_index=True)
len(index)
14

Directed graphs|有向图

graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position

# weak connected components 弱连接分量
labels = get_connected_components(adjacency)
image = svg_graph(adjacency, position=position, names=names, labels=labels)
SVG(image)

在这里插入图片描述

# strong connected components 获得强连接
labels = get_connected_components(adjacency, connection='strong')
image = svg_graph(adjacency, position, names, labels)
SVG(image)

在这里插入图片描述

# largest connected component 最大联通分量
new_adjacency, index = get_largest_connected_component(adjacency, connection='strong', return_index=True)
image = svg_graph(new_adjacency, position[index], names[index])
SVG(image)

在这里插入图片描述

Bipartite graphs|二部图

graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

# subgraph

k = 5
biadjacency = biadjacency[k:]
names_row = names_row[k:]

labels = get_connected_components(biadjacency, force_bipartite=True)
n_row, _ = biadjacency.shape

labels_row = labels[:n_row]
labels_col = labels[n_row:]

image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)

在这里插入图片描述

# largest connected component
new_biadjacency, index = get_largest_connected_component(biadjacency, force_bipartite=True, return_index=True)

n_row, n_col = new_biadjacency.shape
index_row = index[:n_row]
index_col = index[n_row:]

image = svg_bigraph(new_biadjacency, names_row[index_row], names_col[index_col])
SVG(image)

在这里插入图片描述


Cycles|环路

从图中搜索环路

from IPython.display import SVG
import numpy as np
from sknetwork.data import house, star, linear_digraph, cyclic_digraph
from sknetwork.topology import is_acyclic
from sknetwork.visualization import svg_graph

构图

# star
graph = star(5, metadata=True)
adjacency = graph.adjacency
position = graph.position

image = svg_graph(adjacency, scale=0.5)
SVG(image)

在这里插入图片描述

is_acyclic(adjacency)
# False
# house graph

graph = house(metadata=True)
adjacency = graph.adjacency
position = graph.position

image = svg_graph(adjacency, position, scale=0.5)
SVG(image)

在这里插入图片描述

is_acyclic(adjacency)
# False

有向图

# line

graph = linear_digraph(3, metadata=True)
adjacency = graph.adjacency
position = graph.position

image = svg_graph(adjacency, position, scale=0.5)
SVG(image)

在这里插入图片描述

is_acyclic(adjacency)
# True
# cycle

graph = cyclic_digraph(5, metadata=True)
adjacency = graph.adjacency
position = graph.position

image = svg_graph(adjacency, position, scale=0.5)
SVG(image)

在这里插入图片描述

is_acyclic(adjacency)
# False

Core decomposition|核分解

说明图的K核分解

from IPython.display import SVG
import numpy as np
from sknetwork.data import karate_club, painters
from sknetwork.topology import CoreDecomposition
from sknetwork.visualization import svg_graph
from sknetwork.utils import directed2undirected

构图

graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position

core = CoreDecomposition()
labels = core.fit_transform(adjacency)

image = svg_graph(adjacency, position, scores=labels)
SVG(image)

在这里插入图片描述

有向图

graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position

labels = core.fit_transform(directed2undirected(adjacency))

image = svg_graph(adjacency, position, names, scores=labels)
SVG(image)

在这里插入图片描述

Triangles and cliques|三角和团

说明图的聚类系数的集合计数和评估。

from IPython.display import SVG
import numpy as np

from sknetwork.data import karate_club
from sknetwork.topology import Triangles, Cliques
from sknetwork.visualization import svg_graph

构图

graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position

# graph
image = svg_graph(adjacency, position)
SVG(image)

在这里插入图片描述

三角形个数

triangles = Triangles()
triangles.fit_transform(adjacency)

聚类系数

# coefficient of clustering
np.round(triangles.clustering_coef_, 2)
# 0.26

# number of 4-cliques
cliques = Cliques(4)
cliques.fit_transform(adjacency)
# 11

Graph isomorphism| 同构图

说明同构的Weisfeiler-Lehman测试,该算法通过对相邻节点的节点标签排序后的集合来扩展节点标签,并将这些扩展后的标签压缩为新的短标签

在这里插入图片描述

from IPython.display import SVG
import numpy as np
from sknetwork.data import house
from sknetwork.topology import WeisfeilerLehman, are_isomorphic
from sknetwork.visualization import svg_graph

图标签

graph = house(metadata=True)
adjacency = graph.adjacency
position = graph.position

weiseiler_lehman = WeisfeilerLehman()
labels = weiseiler_lehman.fit_transform(adjacency)

image = svg_graph(adjacency, position, labels=labels)
SVG(image)

在这里插入图片描述

# first iteration
weisfeiler_lehman = WeisfeilerLehman(max_iter=1)
labels = weisfeiler_lehman.fit_transform(adjacency)

image = svg_graph(adjacency, position, labels=labels)
SVG(image)

在这里插入图片描述

Weisfeiler-Lehman测试

adjacency_1 = house()
n = adjacency_1.indptr.shape[0] - 1
reorder = list(range(n))
np.random.shuffle(reorder)
adjacency_2 = adjacency_1[reorder][:, reorder]

are_isomorphic(adjacency_1, adjacency_2)
# True

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uncle_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值