In my pursuit after a fast graph library for python, I stumbled upon retworkx, and I'm trying to achieve the same (desired) result I've achieved using networkx.
In my networkx code, I instantiate a digraph object with an array of weighted edges, activate it's built-in shortest_path (dijkstra-based), and receive that path. I do so by using the following code:
graph = nx.DiGraph()
in_out_weight_triplets = np.concatenate((in_node_indices, out_node_indices,
np.abs(weights_matrix)), axis=1)
graph.add_weighted_edges_from(in_out_weight_triplets)
shortest_path = nx.algorithms.shortest_path(graph, source=n_nodes, target=n_nodes + 1,
weight='weight')
when trying to reproduce the same shortest path using retworkx:
graph = rx.PyDiGraph(multigraph=False)
in_out_weight_triplets = np.concatenate((in_node_indices.astype(int),
out_node_indices.astype(int),
np.abs(weights_matrix)), axis=1)
unique_nodes = np.unique([in_node_indices.astype(int), out_node_indices.astype(int)])
graph.add_nodes_from(unique_nodes)
graph.extend_from_weighted_edge_list(list(map(tuple, in_out_weight_triplets)))
shortest_path = rx.digraph_dijkstra_shortest_paths(graph, source=n_nodes,
target=n_nodes + 1)
but for using a triplet with float weights I get the error:
"C:\Users\tomer.d\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-23-752a42ce79d7>", line 1, in <module> graph.extend_from_weighted_edge_list(list(map(tuple, in_out_weight_triplets))) TypeError: argument 'edge_list': 'numpy.float64' object cannot be interpreted as an integer ```
and when i try the workaround of multiplying the weights by factor of 10^4 and casting them to ints:
np.concatenate((in_node_indices.astype(int), out_node_indices.astype(int),
(np.abs(weights_matrix) * 10000).astype(int), axis=1)
so that I supposedly won't lose the weight subtleties - no errors are being raised, but the output of the shortest path is different than the one I get when using networkx.
I'm aware of the fact that the weights aren't necessarily the issue here, but they are currently my main suspect.
any other advice would be thankfully accepted.