Question: PYTHON CODE Part B - All Paths Write a function all paths(M, u, v) which takes as input an adjacency matrix of a graph, M,
PYTHON CODE
Part B - All Paths
Write a function all paths(M, u, v) which takes as input an adjacency matrix of a graph, M, and two vertices u, v and returns a list of all paths from u to v in M. A path is a list of vertices. Note that a path cannot use the same vertex twice, but two different paths can use some of the same vertices. The returned list of paths must be in lexicographical order. You must use backtracking to solve this problem (i.e. do not use brute force).
Do not use print function to display, need to use return.
Calling (all_paths([[0,1], [1,0]], 0, 0)) will return [[0]]
Calling (all_paths([[0,1], [1,0]], 0, 1)) will return [[0,1]]
Calling (all_paths([[0,1,0,1,0], [1,0,0,1,0], [0,0,0,0,1], [1,1,0,0,1], [0,0,1,1,0]], 0, 2)) will return [[0, 1, 3, 4, 2], [0, 3, 4, 2]]
Calling (all_paths([[0,1,0,1,0], [0,0,0,1,0], [1,0,0,1,1], [0,1,0,0,1], [0,0,1,0,0]], 4, 1)) will return [[4, 2, 0, 1], [4, 2, 0, 3, 1], [4, 2, 3, 1]]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
