用于寻找最大化每个移动的变化总和的路径的快速 Python 算法

问题描述

假设我有一个无向图(可以是循环的或非循环的),其中每个节点都分配有一个整数状态。我想找到以下路径:

  1. 遍历每个节点,但只遍历一次
  2. 不需要遍历每一个边缘
  3. 最大化每个移动状态变化的总和

例如,我有一个循环图 -5-4-5-7-2-(前 5 个和最后 2 个相连)。如果我们从前 5 个开始,到最后 2 个结束,则每次移动的变化总和将为 -1 + 1 + 2 + (-5) = -3。该图可以用邻接矩阵描述如下:

import numpy as np
node_states = [5,4,5,7,2]
# Adjacency matrix
               #5 4 5 7 2
am = np.array([[0,1,1],# 5
               [1,0],# 4
               [0,# 5
               [0,# 7
               [1,0]])# 2

预期输出

max_delta_sum_path = [2,7]

其中路径总和最大3 + (-1) + 1 + 2 = 5

有谁知道有没有比较快的算法可以自动找到这条路径?

解决方法

  • 用两个有向、有成本的链接替换每个无向链接。例如,状态 5 和 7 节点之间的链接将被成本为 +2 和 -2 的两条链接替换。
  • 为每个链接的成本增加价值,使所有链接成本为正
  • 找出最昂贵链接的成本并从每个链接成本中减去
  • 将每个链接成本乘以 -1
  • 应用旅行商算法

因此,以您的示例为例:

0 -> 1 cost -1 converts to 6

0 -> 4 cost -3 converts to 8

1 -> 0 cost 1 converts to 4

1 -> 2 cost 1 converts to 4

2 -> 1 cost -1 converts to 6

2 -> 3 cost 2 converts to 3

3 -> 2 cost -2 converts to 7

3 -> 4 cost -5 converts to 10

4 -> 0 cost 3 converts to 2

4 -> 3 cost 5 converts to 0
,

我认为这就是您要找的:

import numpy as np
node_states = [5,4,5,7,2]
# Adjacency matrix
               #5 4 5 7 2
am = np.array([[0,1,1],# 5
               [1,0],# 4
               [0,# 5
               [0,# 7
               [1,0]])# 2

for i in range(len(node_states)):
    for j in range(len(node_states)):
        if am[i][j] == 1:
            am[i][j] = node_states[i] - node_states[j] # go through ever entry in every list,and if it is 1 replace it with the traversal cost
"""
am =    [[ 0  1  0  0  3]
         [-1  0 -1  0  0]
         [ 0  1  0 -2  0]
         [ 0  0  2  0  5]
         [-3  0  0 -5  0]]
"""

from itertools import permutations
def largest_sum(node_states,am):
    largest = None
    largest_journey = None
    traversal_list = list(permutations(range(len(node_states)),len(node_states))) # store all possible permutations of node_states indexes
    for trav in traversal_list: # go through each permuatation
        costs = [] # track the cost of each traversal
        for i in range(len(trav)):
            if i == 0: # there are one less traversals than nodes so we are ignoring the first node
                continue
            if am[trav[i]][trav[i-1]] == 0: # traversal cannot happen if the traversal has no adjacency
                continue
            costs.append(am[trav[i]][trav[i-1]]) # use the updated am matrix to get our costs,and store them here
        if len(costs) == len(node_states) - 1: # if one less traversal was made than we have nodes,we know all nodes were visited
            costs_sum = sum(costs) # sum the costs for our total of travel
            if largest is None or largest < costs_sum: # only keep this total if it was bigger than our old total
                largest = costs_sum # track the new total
                largest_trav = list(map(lambda x: node_states[x],trav)) # change our array of indexes (trav) into an array of node values
    return largest_trav # when the looping is done,return our total

out = largest_sum(node_states,am)
print(out)

输出:

[2,7]