【问题标题】:Maximum sum of k connected elements of a matrix矩阵的 k 个连通元素的最大和
【发布时间】:2015-09-28 13:16:34
【问题描述】:

给定一个具有正整数值和一个整数 K 的网格。 K 个连通元素的最大总和是多少?

这是一个 K 值为 6 的 5x5 矩阵示例。

有人可以帮我确定这个问题吗?我该如何开始解决它?
我知道的唯一方法是对该矩阵的每个单元格进行深度优先搜索。但我认为这不是最好的方法。

不允许重复单元格。

此处连接仅表示一个单元格与另一个单元格水平或垂直相邻

【问题讨论】:

  • 我不确定这个问题属于这里...
  • Hm... 可以建模为一个图论问题:为该矩阵找到一个大小为 K 的连接图的子图。
  • 听起来有点像prize-collecting Steiner problem
  • This paper 描述了您的问题的图论变体(他们称其为 prize-collecting Steiner 问题budget 变体)和列举可能的解决策略。
  • 您的问题更难,因为它允许任何连接的子集,而不仅仅是路径(如您的图片中所示)。并非所有连接的子集都是路径(考虑 T 形)。我首先尝试将 Project Euler 问题从三个方向路径概括为四个方向路径。

标签: algorithm graph


【解决方案1】:

我想你可以四处闲逛,边走边记。我使用镜像位集来表示记忆路径,以便从它们构建的任何方向都可以立即识别它们。这是 Python 中的一个版本(哈希长度包括从 1 到 6 的路径的计数):

from sets import Set

def f(a,k):
  stack = []
  hash = Set([])
  best = (0,0) # sum, path
  n = len(a)

  for y in range(n):
    for x in range(n):
      stack.append((1 << (n * y + x),y,x,a[y][x],1))

  while len(stack) > 0:
    (path,y,x,s,l) = stack.pop()

    if l == k and path not in hash:
      hash.add(path)
      if s > best[0]:
        best = (s,path)
    elif path not in hash:
      hash.add(path)
      if y < n - 1:
        stack.append((path | (1 << (n * (y + 1) + x)),y + 1,x,s + a[y + 1][x],l + 1))
      if y > 0:
        stack.append((path | (1 << (n * (y - 1) + x)),y - 1,x,s + a[y - 1][x],l + 1))
      if x < n - 1:
        stack.append((path | (1 << (n * y + x + 1)),y,x + 1,s + a[y][x + 1],l + 1))
      if x > 0:
        stack.append((path | (1 << (n * y + x - 1)),y,x - 1,s + a[y][x - 1],l + 1))

  print best
  print len(hash)

输出:

arr = [[31,12,7,1,14]
      ,[23,98,3,87,1]
      ,[5,31,8,2,99]
      ,[12,3,42,17,88]
      ,[120,2,7,5,7]]

f(arr,6) 

""" 
(377, 549312) sum, path
1042 hash length
549312 = 00000
         01110
         11000
         10000 
"""

更新:这个问题与Whats the fastest way to find biggest sum of M adjacent elements in a matrix 类似,我意识到需要在我的建议中进行修订,以包括从形状中间部分延伸的结构。这是我修改后的代码,使用集合来散列形状。在我看来,DFS 应该将堆栈大小保持在 O(m) 的顺序上(尽管搜索空间仍然很大)。

from sets import Set

def f(a,m):
  stack = []
  hash = Set([])
  best = (0,[]) # sum, shape
  n = len(a)

  for y in range(n):
    for x in range(n):
      stack.append((a[y][x],Set([(y,x)]),1))

  while len(stack) > 0:
    s,shape,l = stack.pop()

    key = str(sorted(list(shape)))

    if l == m and key not in hash:
      hash.add(key)
      if s > best[0]:
        best = (s,shape)
    elif key not in hash:
      hash.add(key)
      for (y,x) in shape:
        if y < n - 1 and (y + 1,x) not in shape:
          copy = Set(shape)
          copy.add((y + 1,x))
          stack.append((s + a[y + 1][x],copy,l + 1))
        if y > 0 and (y - 1,x) not in shape:
          copy = Set(shape)
          copy.add((y - 1,x))
          stack.append((s + a[y - 1][x],copy,l + 1))
        if x < n - 1 and (y,x + 1) not in shape:
          copy = Set(shape)
          copy.add((y,x + 1))
          stack.append((s + a[y][x + 1],copy,l + 1))
        if x > 0 and (y,x - 1) not in shape:
          copy = Set(shape)
          copy.add((y,x - 1))
          stack.append((s + a[y][x - 1],copy,l + 1))

  print best
  print len(hash)

输出:

arr = [[31,12,7,1,14]
      ,[23,98,3,87,1]
      ,[5,31,8,2,99]
      ,[12,3,42,17,88]
      ,[120,2,7,5,7]]

f(arr,6)

"""
(377, Set([(1, 2), (1, 3), (1, 1), (2, 3), (3, 4), (2, 4)]))
2394 hash length
"""

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多