Scikit-network-05:路径 Path

Path

Distance|距离

明图中节点之间距离的计算(hop的数量)

from IPython.display import SVG
import numpy as np
from sknetwork.data import miserables, painters, movie_actor
from sknetwork.path import get_distances
from sknetwork.visualization import svg_graph, svg_bigraph
from sknetwork.utils import bipartite2undirected

# 构图
graph = miserables(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position

napoleon = 1  # 求其它点到napoleon的距离,index设置为1
distances = get_distances(adjacency, sources=napoleon)
distances

image = svg_graph(adjacency, position, names, scores=-distances, seeds=[napoleon], scale=2)
SVG(image)
array([ 1.,  0.,  9.,  9.,  2.,  2.,  2.,  2.,  3.,  2.,  7.,  6.,  7.,
        7.,  7.,  7., 10., 12., 12., 12., 13., 13., 13.,  9.,  8.,  8.,
        9.,  8.,  9.,  8., 10.,  9.,  7.,  8.,  9.,  9.,  8.,  8.,  8.,
        9.,  9.,  8.,  9.,  9.,  7., 11., 10.,  9.,  7.,  8.,  9.,  8.,
        9.,  9.,  9.,  9.,  9.,  8.,  8.,  9.,  8.,  9.,  9.,  9.,  7.,
        9.,  8., 11.,  7.,  7.,  7.,  7.,  7.,  9.,  9.,  8.,  8.])

在这里插入图片描述

有向图
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position

cezanne = 11
distances = get_distances(adjacency, sources=cezanne)

dis_net = {i: -d for i, d in enumerate(distances) if d < np.inf}
dis_net

image = svg_graph(adjacency, position, names, scores=dis_net, seeds=[cezanne])
SVG(image)
{0: -1.0,
 1: -1.0,
 2: -3.0,
 3: -1.0,
 4: -2.0,
 5: -3.0,
 7: -2.0,
 8: -2.0,
 9: -3.0,
 10: -1.0,
 11: -0.0,
 12: -1.0}

在这里插入图片描述

二部图
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

adjacency = bipartite2undirected(biadjacency)
n_row, _ = biadjacency.shape

seydoux = 9
distances = get_distances(adjacency, sources=seydoux+n_row)
distances

image = svg_bigraph(biadjacency, names_row, names_col, scores_col=-distances[n_row:], seeds_col=seydoux)
SVG(image)

在这里插入图片描述

Shortest paths| 最短距离

说明图中最短路径的搜索。

from IPython.display import SVG
import numpy as np
from sknetwork.data import miserables, painters, movie_actor
from sknetwork.path import get_shortest_path
from sknetwork.visualization import svg_graph, svg_bigraph
from sknetwork.utils import bipartite2undirected
graph = miserables(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position

# shortest path
napoleon = 1
jondrette = 46  # 找这两个节点的最短距离

path = get_shortest_path(adjacency, sources=napoleon, targets=jondrette)
path

# visualization
edge_lables = [(path[k], path[k+1], 0) for k in range(len(path)-1)]
edge_lables

image = svg_graph(adjacency, position, names, edge_labels=edge_lables, edge_width=3, display_edge_weight=False, scale=1.5)
SVG(image)
[1, 0, 11, 48, 47, 46]
[(1, 0, 0), (0, 11, 0), (11, 48, 0), (48, 47, 0), (47, 46, 0)]

在这里插入图片描述

有向图
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position

# short path
klimt = 6
vinci = 9
path = get_shortest_path(adjacency, sources=klimt, targets=vinci)
path

edge_labels = [(path[k], path[k+1], 0) for k in range(len(path)-1)]
edge_labels

image = svg_graph(adjacency, position, names, edge_labels=edge_labels, edge_width=2)
SVG(image)
[6, 13, 8, 4, 9]

[(6, 13, 0), (13, 8, 0), (8, 4, 0), (4, 9, 0)]

在这里插入图片描述

二部图
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

adjacency = bipartite2undirected(biadjacency)
n_row = biadjacency.shape[0]

# shortest path
seydoux = 9
lewitt = 2
path = get_shortest_path(adjacency, sources=seydoux + n_row, targets=lewitt + n_row)
path

# visualization
edge_labels = []
labels_row = {}
labels_col = {}

for k in range(len(path)-1):
    i = path[k]
    j = path[k+1]
    if i > j:
        i, j = j, i
    j -= n_row
    labels_row[i] = 0
    labels_col[j] = 0
    edge_labels.append((i, j, 0))
edge_labels

image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col,
                    edge_labels=edge_labels, edge_color='gray', edge_width=3)
SVG(image)
[24, 12, 16, 0, 17]
[(12, 9, 0), (12, 1, 0), (0, 1, 0), (0, 2, 0)]

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

uncle_ll

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

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

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

打赏作者

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

抵扣说明:

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

余额充值