明图中节点之间距离的计算(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)
说明图中最短路径的搜索。
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)]