sf.apps.sample.waw_matrix

waw_matrix(A, w)[source]

Rescale adjacency matrix to account for node weights.

Given a graph with adjacency matrix \(A\) and a vector \(w\) of weighted nodes, this function rescales the adjacency matrix according to [8]:

\[A \rightarrow WAW,\]

with \(W=w_{i}\delta_{ij}\) the diagonal matrix formed by the weighted nodes \(w_{i}\). The rescaled adjacency matrix can be passed to sample(), resulting in a distribution that increases the probability of observing a node proportionally to its weight.

Example usage:

>>> g = nx.erdos_renyi_graph(5, 0.7)
>>> a = nx.to_numpy_array(g)
>>> a
array([[0., 1., 1., 1., 1.],
       [1., 0., 1., 1., 1.],
       [1., 1., 0., 1., 0.],
       [1., 1., 1., 0., 0.],
       [1., 1., 0., 0., 0.]])
>>> w = [10, 1, 0, 1, 1]
>>> a = waw_matrix(a, w)
>>> a
array([[ 0., 10.,  0., 10., 10.],
       [10.,  0.,  0.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [10.,  1.,  0.,  0.,  0.],
       [10.,  1.,  0.,  0.,  0.]])
>>> sample(a, 3, 4)
[[1, 0, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 1], [1, 0, 0, 1, 0]]
Parameters
  • A (array) – adjacency matrix to rescale

  • w (array or list) – vector of real node weights

Returns

matrix rescaled according to the WAW encoding

Return type

array