药物输送经济学:长度最大路径k?

时间:2012-11-30 13:59:11

标签: algorithm graph

我是一名毒贩,附有送货路线图,每张都值得一定数量。但我不能覆盖整个区域,我只需要打破我的一点点草皮。但是,我可以选择哪一块草皮可以解决。

此图表的顶点是街角,所有街道长度相同和1路。有些街道比其他街道值得多。我想找到行走的最大节拍,这样我必须经过的街角数量最少。所有路径均从42街地铁站和罗斯福岛电车的终点站开始。

我已经快速浏览了一下网,但似乎并不像我记得在任何地方看过的那样。

使用动态编程在DAG 上轻松计算最大权重/流量/成本路径

但是如果我们还需要找到一个尽可能短的最大路径怎么办? 好吧,我们可以迭代所有长度为k的最大路径,以获得接近某个球场值的k值。但是,如何获得长度为k的最大路径?

有什么想法可以找到指向这个的指针吗?

编辑1

好的..我有一种感觉Floyd-Warshall可以在特定长度做最大路径。这可能足够好,我可以在所有可能的长度上运行它。但是......任何更好的想法?

编辑2

修改为仅提及DAG。

编辑3

Floyd-Warshall可以执行特定长度的最大路径,k:意义,将DAG最大路径与使用Floyd Warshall相结合,以获得任何顶点和最终停靠点之间的最短路径(沿边数)。因此,在最大路径算法中的任何顶点处,当当前边数(当前最大路径数)+(Floyd-Warshall确定的)最小边数与该顶点之间的边数之和时,杀死该顶点处的搜索分支最后一站将超过我们的特定长度,k。

编辑4

我想我有一个答案。

要查找此图的最大长度k,我们假设对于图中的任何节点,我们知道该节点与终点之间的最小边数。

所以我们只运行这样的最大路径算法,确保我们检查给定分支可能的最小边数是否低于我们的排除边界

在此代码中,图形是由其起始顶点索引的边的字典,每个边是元组(destination_edge,weight)。此算法最大化权重,但确保其返回的路径不超过参数k:

def maxpathoflength(k,start_vertex,graph_out_edges,memory,frame_size):
    # first of all we leverage dynamic programming to save any results we have checked before
    if start_vertex in memory:
        return memory[start_vertex]
    # otherwise we calculate it
    # first we set the current maximum gold at this vertex
    maxgold = 0
    # corresponding to the following maximum path
    maxpath = [start_vertex]
    #then we check to see if we have broken our k-length path constraint
    if k < 0:
        # we failed, yet somehow we should never make it to here anyway
        return { 'gold' : maxgold, 'path' : maxpath }
        # if on the other hand k were equal to zero and this was the end point
        # we would have succeeded
    # we save this size since we can use it to calculate the lower bound 
    # on the number of edges required to get to the finish from this vertex 
    graph_size = len(graph_out_edges)
    # we get the edges adjacent to start_vertex
    # they are all out_edges, the graph has the DAG property

    # the graph also has this property : there are no out edges from the last vertex :
    if start_vertex > graph_size - 1:
        return { 'gold' : maxgold, 'path' : maxpath }
    out_edges = graph_out_edges[start_vertex]
    print 'Out edges: ' + str(out_edges)
    for edge in out_edges:
        # each edge is stored as a 2-tuple (to_vertex,edge_weight)
        arrival_vertex = edge[0]
        edge_weight = edge[1]
        # the following property holds for this graph, hence we can know the lower bound on edges of any path from this vertex
        min_edges_from_arrival_vertex_to_end = ceil((1.0*graph_size - int(arrival_vertex))/frame_size)
        # and so we can determine if we want should search it or not
        if min_edges_from_arrival_vertex_to_end > k-1:
            print 'Skipping this edge'
            continue
        # or if this branch is ok
        # we search down this branch and take 1 off the edges
        # remaining
        # we search it and find the result of searching it is...
        result = maxpathoflength(k-1,arrival_vertex,graph_out_edges,memory,frame_size)  
        gold = edge_weight + result.get('gold')
        path = result.get('path') 
        # if that result is greater than our current best
        if gold > maxgold:
            # we make that result our new personal best
            maxgold = gold
            maxpath = [start_vertex] + path

    # our return value is what we found the maximum to be from this vertex
    returnvalue = { 'gold' : maxgold, 'path' : maxpath }
    # we save this so we never ever have to do this again
    memory[start_vertex] = returnvalue
    # and send it back to up to the calling function 
    return returnvalue

0 个答案:

没有答案