本笔记本说明图中连接的组件的搜索。
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
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)
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)
从图中搜索环路
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
说明图的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)
说明图的聚类系数的集合计数和评估。
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
说明同构的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)
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