Say I have a undirected graph (can be cyclic or acyclic), where each node is asigned with an integer state. I want to find the path that:
- goes through every node but only once
- doesn't need to go through every edge
- maximize the sum of the state changes of each move
As an example, I have a cyclic graph -5-4-5-7-2- (first 5 and last 2 are connected). If we start from the first 5 and end at the last 2, the sum of the changes of each move will be -1 + 1 + 2 + (-5) = -3
. The graph can be described by an adjacency matrix as follows:
import numpy as np
node_states = [5, 4, 5, 7, 2]
# Adjacency matrix
#5 4 5 7 2
am = np.array([[0,1,0,0,1], # 5
[1,0,1,0,0], # 4
[0,1,0,1,0], # 5
[0,0,1,0,1], # 7
[1,0,0,1,0]])# 2
The expected output is
max_delta_sum_path = [2, 5, 4, 5, 7]
where the path has the largest sum 3 + (-1) + 1 + 2 = 5
Anyone knows if there is any relatively fast algorithm that can automatically find this path?