Suppose I have a set of edges in the form (where each tupe (i,j) is a directed edge from node i to node j):
E=[(1, 6), (1, 7), (2, 3), (2, 6), (3, 2), (3, 8), (4, 5), (4, 7), (5, 4), (5, 9), (6, 1), (6, 7), (6, 2), (7, 1), (7, 6), (7, 4), (8, 9), (8, 3), (9, 8), (9, 5)]
and the distance matrix:
C=[2.5, 5.5, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 2.5, 5.0, 2.0, 5.59, 5.0, 2.0, 5.0, 2.0, 5.0, 2.0]
where each element in C
(say in the i-th position) corresponds to the distance between the 2 nodes of the corresponding edge in E
(at the i-th position).
Now, I will like to find the shortest path that begins at the origin (node 1), which passes through nodes 2 and 4, and then returns to the origin (a cycle). Is there a way to do this with the NetworkX package in Python? Or is there some other way (that is not computationally costly) to do this?
I looked up https://networkx.github.io/documentation/stable/reference/algorithms/shortest_paths.html for functions with relevance to the shortest path, but I am unable to find one that is suitable for my problem.. Some insight into this will be deeply appreciated!