A
A
Asya2018-01-29 17:25:19
Python
Asya, 2018-01-29 17:25:19

How to implement the calculation of the distance matrix in python by raising the adjacency matrix to a power?

I need to implement the calculation of the distance matrix by raising the adjacency matrix to a power. The input is a graph, the output is a matrix of distances (a list of lists of integers). Can you tell me how to get the distance matrix?
I can only find the adjacency matrix:

import networkx as nx
from networkx import DiGraph

digraph = DiGraph()
for node_val in range(1, 8):
    digraph.add_node(node_val)
    digraph.add_edge(1, 2)
    digraph.add_edge(1, 3)
    digraph.add_edge(1, 4)
    digraph.add_edge(2, 4)
    digraph.add_edge(2, 5)
    digraph.add_edge(3, 6)
    digraph.add_edge(4, 3)
    digraph.add_edge(4, 6)
    digraph.add_edge(4, 7)
    digraph.add_edge(5, 4)
    digraph.add_edge(5, 7)
    digraph.add_edge(7, 6)

adj_matrix_sparse = nx.adjacency_matrix(digraph)
adj_matrix_dense = adj_matrix_sparse.todense()
print(adj_matrix_dense)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question