Distance

This notebook illustrates the computation of distances between nodes in graphs (in number of hops).

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import miserables, painters, movie_actor
from sknetwork.path import distance
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph
from sknetwork.utils import bipartite2undirected

Graphs

[4]:
graph = miserables(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[5]:
napoleon = 1
[6]:
dist = distance(adjacency, sources=napoleon)
[7]:
image = svg_graph(adjacency, position, names, scores = -dist, seeds=[napoleon], scale = 1.5)
[8]:
SVG(image)
[8]:
../../_images/tutorials_path_distance_10_0.svg

Digraphs

[9]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[10]:
cezanne = 11
[11]:
dist = distance(adjacency, sources=cezanne)
[12]:
dist_neg= {i: -d for i, d in enumerate(dist) if d < np.inf}
[13]:
image = svg_digraph(adjacency, position, names, scores=dist_neg , seeds=[cezanne])
[14]:
SVG(image)
[14]:
../../_images/tutorials_path_distance_17_0.svg

Bigraphs

[15]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[16]:
adjacency = bipartite2undirected(biadjacency)
[17]:
n_row, _ = biadjacency.shape
[18]:
seydoux = 9
[19]:
dist = distance(adjacency, sources=seydoux + n_row)
[20]:
image = svg_bigraph(biadjacency, names_row, names_col, scores_col=-dist[n_row:], seeds_col=seydoux)
[21]:
SVG(image)
[21]:
../../_images/tutorials_path_distance_25_0.svg