python中任何给定数字的路径复杂性(最快路径)

时间:2016-01-05 17:14:16

标签: python algorithm

今天我参加了数学竞赛,有一个类似的问题:

  

您有一个给定的号码n,现在您必须要计算最短的路线到哪个号码,但是有规则。

     
      
  1. 您从号码1
  2. 开始   
  3. 当您到达n
  4. 时结束   
  5. 您可以通过将之前的号码加倍,或者添加前两个号码来到达n
  6.         

    示例:n = 25

         

    最慢路线1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25   (您只需继续添加1

         

    最快路线1,2,4,8,16,24,25,复杂度= 6

         

    示例:n = 8   最快路线1,2,4,8,复杂度= 3

         

    示例:n = 15   最快的路线1,2,3,6,9,15,复杂度= 5

如何制作能够计算给定数字nn <= 32)的复杂程度的程序?

我已经知道,对于任何给定数量n(n <= 32),复杂度低于1.45 x 2log(n)。 所以现在我只需要计算复杂度低于1.45 x 2log(n)的所有路线,然后比较它们,看看哪一条是最快的路线&#39;。 但我不知道如何将所有路由和所有这些路由放在python中,因为当给定的数字n发生变化时路由的数量会发生变化。

这就是我现在所拥有的:

number = raw_input('Enter your number here : ')
startnumber = 1
complexity = 0

while startnumber <= number

4 个答案:

答案 0 :(得分:1)

你的问题有一个动态的编程解决方案,因为你要么添加任意两个数字,要么将数字乘以2我们可以尝试所有这些情况并选择最小值,如果25的复杂度为5且路由包含9则我们知道9的解决方案是4,我们可以使用9的解决方案来生成25的解决方案。我们还需要跟踪m的每个可能的最小解决方案,以便能够使用它来解决n m&lt; Ñ

def solve(m):
    p = [set([frozenset([])]) for i in xrange(m+1)] #contains all paths to reach n
    a = [9999 for _ in xrange(m+1)]
    #contains all complexities initialized with a big number
    a[1] = 0
    p[1] = set([frozenset([1])])
    for i in xrange(1,m+1):
        for pos in p[i]:
            for j in pos: #try adding any two numbers and 2*any number
                for k in pos:
                    if (j+k <= m):
                        if a[j+k] > a[i]+1:
                            a[j+k] = a[i] + 1
                            p[j+k] = set([frozenset(list(pos) + [j+k])])
                        elif a[j+k] == a[i]+1:
                            p[j+k].add(frozenset(list(pos) + [j+k]))
    return a[m],sorted(list(p[m].pop()))

n = int(raw_input())
print solve(n)

这可以解决最多n = 100

对于较大的数字,您可以通过添加几行来从内循环中删除一些冗余计算,从而获得30%或更高的加速。为此,在每次迭代时创建并修剪pos2变量:

def solve(m):
    p = [set([frozenset([])]) for i in xrange(m+1)] #contains all paths to reach n
    a = [9999 for _ in xrange(m+1)]
    #contains all complexities initialized with a big number
    a[1] = 0
    p[1] = set([frozenset([1])])
    for i in xrange(1,m+1):
        for pos in p[i]:
            pos2 = set(pos)
            for j in pos: #try adding any two numbers and 2*any number
                for k in pos2:
                    if (j+k <= m):
                        if a[j+k] > a[i]+1:
                            a[j+k] = a[i] + 1
                            p[j+k] = set([frozenset(list(pos) + [j+k])])
                        elif a[j+k] == a[i]+1:
                            p[j+k].add(frozenset(list(pos) + [j+k]))
                pos2.remove(j)
    return a[m],sorted(list(p[m].pop()))

答案 1 :(得分:1)

我接受挑战:)

算法相对较快。它在我的计算机上计算50ms内前32个数字的复杂性,我不使用多线程。 (或前100个数字为370毫秒。)

它是递归分支和剪切算法。 _shortest函数有3个参数:优化位于max_len参数中。例如。如果函数找到长度为9的解,则它会停止考虑长度大于&gt;的任何路径。 9.找到的第一条路径总是非常好的,它直接来自数字的二进制表示。例如。二进制:111001 =&gt; [1,10,100,1000,10000,100000,110000,111000,111001]。这并不总是最快的路径,但如果您只搜索速度最快的路径,则可以切断远离搜索树的大部分。

#!/usr/bin/env python

# Find the shortest addition chain...
# @param acc List of integers, the "accumulator". A strictly monotonous
#        addition chain with at least two elements.
# @param target An integer > 2. The number that should be reached.
# @param max_len An integer > 2. The maximum length of the addition chain
# @return A addition chain starting with acc and ending with target, with
#         at most max_len elements. Or None if such an addition chain
#         does not exist. The solution is optimal! There is no addition
#         chain with these properties which can be shorter.
def _shortest(acc, target, max_len):
    length = len(acc)
    if length > max_len:
        return None
    last = acc[-1]
    if last == target:
        return acc;
    if last > target:
        return None
    if length == max_len:
        return None
    last_half = (last / 2)
    solution = None
    potential_solution = None
    good_len = max_len

    # Quick check: can we make this work?
    # (this improves the performance considerably for target > 70)
    max_value = last
    for _ in xrange(length, max_len):
        max_value *= 2
        if max_value >= target:
            break
    if max_value < target:
        return None

    for i in xrange(length-1, -1, -1):
        a = acc[i]
        if a < last_half:
            break

        for j in xrange(i, -1, -1):
            b = acc[j]
            s = a+b
            if s <= last:
                break

            # modifying acc in-place has much better performance than copying
            # the list and doing
            #   new_acc = list(acc)
            #   potential_solution = _shortest(new_acc, target, good_len)

            acc.append(s)
            potential_solution = _shortest(acc, target, good_len)
            if potential_solution is not None:
                new_len = len(potential_solution)
                solution = list(potential_solution)
                good_len = new_len-1

            # since we didn't copy the list, we have to truncate it to its
            # original length now.
            del acc[length:]

    return solution

# Finds the shortest addition chain reaching to n.
# E.g. 9 => [1,2,3,6,9]
def shortest(n):
    if n > 3:
        # common case first
        return _shortest([1,2], n, n)
    if n < 1:
        raise ValueError("n must be >= 1")
    return list(xrange(1,n+1))

for i in xrange(1,33):
    s = shortest(i)
    c = len(s) - 1
    print ("complexity of %2d is %d: e.g. %s" % (i,c,s))

输出:

complexity of  1 is 0: e.g. [1]
complexity of  2 is 1: e.g. [1, 2]
complexity of  3 is 2: e.g. [1, 2, 3]
complexity of  4 is 2: e.g. [1, 2, 4]
complexity of  5 is 3: e.g. [1, 2, 4, 5]
complexity of  6 is 3: e.g. [1, 2, 4, 6]
complexity of  7 is 4: e.g. [1, 2, 4, 6, 7]
complexity of  8 is 3: e.g. [1, 2, 4, 8]
complexity of  9 is 4: e.g. [1, 2, 4, 8, 9]
complexity of 10 is 4: e.g. [1, 2, 4, 8, 10]
complexity of 11 is 5: e.g. [1, 2, 4, 8, 10, 11]
complexity of 12 is 4: e.g. [1, 2, 4, 8, 12]
complexity of 13 is 5: e.g. [1, 2, 4, 8, 12, 13]
complexity of 14 is 5: e.g. [1, 2, 4, 8, 12, 14]
complexity of 15 is 5: e.g. [1, 2, 4, 5, 10, 15]
complexity of 16 is 4: e.g. [1, 2, 4, 8, 16]
complexity of 17 is 5: e.g. [1, 2, 4, 8, 16, 17]
complexity of 18 is 5: e.g. [1, 2, 4, 8, 16, 18]
complexity of 19 is 6: e.g. [1, 2, 4, 8, 16, 18, 19]
complexity of 20 is 5: e.g. [1, 2, 4, 8, 16, 20]
complexity of 21 is 6: e.g. [1, 2, 4, 8, 16, 20, 21]
complexity of 22 is 6: e.g. [1, 2, 4, 8, 16, 20, 22]
complexity of 23 is 6: e.g. [1, 2, 4, 5, 9, 18, 23]
complexity of 24 is 5: e.g. [1, 2, 4, 8, 16, 24]
complexity of 25 is 6: e.g. [1, 2, 4, 8, 16, 24, 25]
complexity of 26 is 6: e.g. [1, 2, 4, 8, 16, 24, 26]
complexity of 27 is 6: e.g. [1, 2, 4, 8, 9, 18, 27]
complexity of 28 is 6: e.g. [1, 2, 4, 8, 16, 24, 28]
complexity of 29 is 7: e.g. [1, 2, 4, 8, 16, 24, 28, 29]
complexity of 30 is 6: e.g. [1, 2, 4, 8, 10, 20, 30]
complexity of 31 is 7: e.g. [1, 2, 4, 8, 10, 20, 30, 31]
complexity of 32 is 5: e.g. [1, 2, 4, 8, 16, 32]

答案 2 :(得分:0)

强行执行此操作

def solve(m, path):
    if path[-1] == m:
        return path
    if path[-1] > m:
        return False
    best_path = [i for i in range(m)]

    test_path = solve (m, path + [path[-1]*2]) 
    if test_path and len(test_path) < len(best_path):
        best_path = test_path
    for k1 in path[:-1] :
        for k2 in path[:-1] :
            test_path = solve (m, path + [path[-1] + k1 + k2]) 
            if test_path and len(test_path) < len(best_path):
                #retain best
                best_path = test_path
    return best_path 

print (solve(19, [1,2])) #[1, 2, 4, 8, 16, 19]
print (solve(25, [1,2])) #[1, 2, 4, 8, 16, 25]

运行速度相当慢,我很确定存在一个更智能的解决方案,但这看起来在语义上是正确的

答案 3 :(得分:0)

一种蛮力技术,只需搜索所有可能的路径,直到达到目标。它速度极快,因为它不会评估较低复杂度的数字,但可以保证找到最佳路径。

# Use a node class that contains the number as well as a pointer to its parent
class Node:
    def __init__(self, number, parent):
        self.number = number
        self.parent = parent

    # get a list of all the numbers to reach this node
    def getPath(self):
        path = [self.number]
        parent = self.parent
        while parent != None:
            path.append(parent.number)
            parent = parent.parent
        return path

def solve(start, target):    
    currentList = []                                    # List of visited nodes in the previous round
    nextList = [Node(start, None)]                      # List of visited nodes in the next round (start with at least one number)
    seen = set([start])                                 # store all number that have already been seen in the previous round

    while nextList:                                     # continue until the final number has reached, on each iteration the complexity grows
        currentList = nextList                          # swap the lists around
        nextList = []

        for n in currentList:
            path = n.getPath()                          # fetch all the number that are needed to reach this point
            if n.number == target:                      # check of we have reach our target
                return path[::-1]                       # the path is in reverse at this point, so reverse it back
            for a in path:                              # for any combination of the last number and a previous number (including the last, which is the same as multiplying it by 2)
                newnumber = a + path[0]
                if newnumber <= target and newnumber not in seen:   # only evaluate the new number if is is not yet seen already on a lower complexity 
                    nextList.append(Node(newnumber, n))

        for n in nextList:                              # update the seen list
            seen.add(n.number)
    return []                                           # if the destination could not be reached

print "path to 25  = ", solve(1, 25)
print "path to 8   = ", solve(1, 8)
print "path to 15  = ", solve(1, 15)
print "path to 500 = ", solve(1, 500)

将输出以下内容:

path to 25  = [1, 2, 4, 8, 16, 24, 25]
path to 8   = [1, 2, 4, 8]
path to 15  = [1, 2, 4, 5, 10, 15]
path to 500 = [1, 2, 4, 8, 16, 32, 64, 96, 100, 200, 400, 500]

我已经测试了这个方法来解决高达500的变量,它能够在0.36秒内解决它。