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 get_distances
from sknetwork.visualization import svg_graph, svg_bigraph
from sknetwork.utils import bipartite2undirected


Graphs

[4]:

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

[5]:

source = np.flatnonzero(names=='Cosette')
distances = get_distances(adjacency, source)

[6]:

image = svg_graph(adjacency, position, names, scores=-distances, seeds=[source], scale=1.5)
SVG(image)

[6]:


Directed graphs

[7]:

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

[8]:

# distances from Paul Cezanne
source = np.flatnonzero(names=='Paul Cezanne')
distances = get_distances(adjacency, source)

[9]:

# distances of unreachable nodes (for better visualization)
distances[distances < 0] = 5

[10]:

image = svg_graph(adjacency, position, names, scores=-distances, seeds=[source])
SVG(image)

[10]:


Bipartite graphs

[11]:

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

[12]:

source_row = np.flatnonzero(np.isin(names_row, ['Drive', 'The Grand Budapest Hotel']))
distances_row, distances_col = get_distances(biadjacency, source_row=source_row)

[13]:

image = svg_bigraph(biadjacency, names_row, names_col, scores_row=-distances_row, scores_col=-distances_col,
seeds_row=source_row)
SVG(image)

[13]: