1

我正在研究一个定向网络问题并尝试计算两点之间的所有有效路径。我需要一种方法来查看最长 30 个“行程”(由 [origin, destination] 对表示)的路径。然后,完整的路线由一系列这些对组成:

route = [[start, city2], [city2, city3], [city3, city4], [city4, city5], [city5, city6], [city6, city7], [city7, city8], [city8, stop]]

到目前为止,我最好的解决方案如下:

    def numRoutes(graph, start, stop, minStops, maxStops):
    routes = []

    route = [[start, stop]]
    if distance(graph, route) != "NO SUCH ROUTE" and len(route) >= minStops and len(route) <= maxStops: 
            routes.append(route)

    if maxStops >= 2:
        for city2 in routesFromCity(graph, start):
            route = [[start, city2],[city2, stop]]
            if distance(graph, route) != "NO SUCH ROUTE" and len(route) >= minStops and len(route) <= maxStops: 
                routes.append(route)
    if maxStops >= 3:       
        for city2 in routesFromCity(graph, start):
            for city3 in routesFromCity(graph, city2):
                route = [[start, city2], [city2, city3], [city3, stop]]
                if distance(graph, route) != "NO SUCH ROUTE" and len(route) >= minStops and len(route) <= maxStops:
                    routes.append(route)

    if maxStops >= 4:
        for city2 in routesFromCity(graph, start):
            for city3 in routesFromCity(graph, city2):
                for city4 in routesFromCity(graph, city3):
                    route = [[start, city2], [city2, city3], [city3, city4], [city4, stop]]
                    if distance(graph, route) != "NO SUCH ROUTE" and len(route) >= minStops and len(route) <= maxStops:
                        routes.append(route)
    if maxStops >= 5:
        for city2 in routesFromCity(graph, start):
            for city3 in routesFromCity(graph, city2):
                for city4 in routesFromCity(graph, city3):
                    for city5 in routesFromCity(graph, city4):
                        route = [[start, city2], [city2, city3], [city3, city4], [city4, city5], [city5, stop]]
                        if distance(graph, route) != "NO SUCH ROUTE" and len(route) >= minStops and len(route) <= maxStops:
                            routes.append(route)
return routes

其中 numRoutes 是我的网络图,其中数字表示距离:

[[0, 5, 0, 5, 7], [0, 0, 4, 0, 0], [0, 0, 0, 8, 2], [0, 0, 8, 0, 6], [0, 3, 0, 0, 0]]

起点城市、终点城市和路线长度参数。

distance 检查路线是否可行,并且 routesFromCity 将附加的节点返回给每个馈入城市的节点。

我感觉有一种更有效的方法来生成所有路线,尤其是当我迈向更多步骤时,但我似乎无法让其他任何工作。

4

1 回答 1

0

您可以使用递归函数。您的 maxStops 可以是一个参数,每次调用时都会将其减 1。当 minStops 为 0 时,您会产生结果,当 maxStops 为 0 时,您将停止递归。

这是一个代码示例:

def routesFromCity(x):
    for i in range(2, 10):
        yield x * i

def findRoutes(start, stop, minStops, maxStops):
    if start == stop:
        if minStops <= 0:
            yield []
    else:
        if maxStops > 0:
            for nextCity in routesFromCity(start):
                for route in findRoutes(nextCity, stop, minStops - 1, maxStops - 1):
                    yield [(start, nextCity)] + route

for route in findRoutes(1, 12, 2, 5):
    print route

输出:

[(1, 2), (2, 4), (4, 12)]
[(1, 2), (2, 6), (6, 12)]
[(1, 2), (2, 12)]
[(1, 3), (3, 6), (6, 12)]
[(1, 3), (3, 12)]
[(1, 4), (4, 12)]
[(1, 6), (6, 12)]
于 2010-03-24T20:23:00.497 回答