'python: what is a best way to find paths with x length in 2d array?

Assume that I have an array like this [[0, 1, 0, 0], [1, 0, 2, 5], [0, 2, 0, 0], [0, 5, 0, 0]]

each index is a node index so [0, 1, 0, 0] means node0 has a edge to node1 with length 1

[1, 0, 2, 5] means node1 has edges to node0, node2 and node3 with following lengths 1, 2 and 5

(Every edge is bidirected <->)

And with this 2d array, I want to find out a number of paths with exact x length for all nodes.

but a visited edge will not be used again.

if x is 3, node 0-1-2 so should return 2 (node0 to node1 = 1, node1 to node 2 = 2, sum of length is 3 and used 2 paths)

(if the length of between node1 and node3 is 2, it won't be included as already used node0-node1 path so still must return 2)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source