English 中文(简体)
最好的算法是用负面的 no子和 lo子背书图表
原标题:what is the best algorithm to traverse a graph with negative nodes and looping nodes

I have a really difficult problem to solve and Im just wondering what what algorithm can be used to find the quickest route. The undirected graph consist of positive and negative adjustments, these adjustments effect a bot or thing which navigate the maze. The problem I have is mazes which contain loops that can be + or -. An example might help:-

  1. A. 标注10点

  2. B号:15

  3. C节对目标20点

路线=”

起始点为A,终点为C

given the graph structure as:-

a(+10)-----b(-15)-----c+20

 node() means the node loops to itself   - and + are the adjustments

没有休息室的节点是20周年,因此C节有20个积极调整,但没有休息室。

如果机器人或物体在其资源中有10点,那么最佳途径是:

a > b > c    the object would have 25 points when it arrives at c

线路=“a,b,c”

这一点非常容易执行,下一个挑战是了解如何回到好战线上来,让人们假设,在每一天,你都可以找到其邻国的任何des点和调整水平。 下面的例子就是:

if the bot started with only 5 points then the best path would be

a > a > b > c the bot would have 25 points when arriving at c

线路=“a,a,b,c”

这是一个非常简单的图表,但当你有太多的教训时,机器人非常难以知道是否在好的节点上 lo,还是从一个好的节点到另一个,同时保持一条可能的道路。

such a route would be a backtrack queue.

更困难的例子将造成大量回馈。

机器人有10点

a(+10)-----b(-5)-----c-30

a > b > a > b > a > b > a > b > a > a > b > c 拥有5个ts。

another way the bot could do it is:-

a > a > a > b > c

这是一种更有效的方法,但我对你如何规划这一问题表示怀疑。

任何人都知道解决这一问题的良好算法,他们已经研究过Belman-fords和Dijkstra,但只有一条简单的道路,而不是一条 lo路。

could it be recursive in some way or some form of heuristics?


提及您的类比:

我认为,我知道你所说的话,一个假想的镜子将更清楚,迄今为止的道路是这样。

q.add(v)
best=v
hash visited(v,true)

while(q is not empty)
    q.remove(v)
    for each u of v in G

        if u not visited before
            visited(u,true)
            best=u=>v.dist
        else
            best=v=>u.dist
最佳回答

This is a straightforward dynamic programming problem.

假设,就一定长的路程而言,就每一航道而言,你想要知道在这条路上结束的最佳成本,以及这条路从哪里走。 (该长度的数据可以储存在连接清单上的斜线上。)

为我们提供这一数据,以便采取零步骤。 然后,我们先用一个干净的圆板,然后对底部进行每一次回答,把它推向前进,如果我们没有数据,或者说我们比最佳数据更重,那么,我们就会更新有关这一节点的数据,并增加路线(将这一节点与先前的相关清单联系起来)。

一旦达到你所希望的步骤的数量,就找到了最好的现有路线,然后,你们的分量和路线就成了一个联系的清单。

================================================================================================================================================================================================================================================================

此处是实施算法的实际代码:

class Graph:
    def __init__(self, nodes=[]):
        self.nodes = {}
        for node in nodes:
            self.insert(node)

    def insert(self, node):
        self.nodes[ node.name ] = node

    def connect(self, name1, name2):
        node1 = self.nodes[ name1 ]
        node2 = self.nodes[ name2 ]
        node1.neighbors.add(node2)
        node2.neighbors.add(node1)

    def node(self, name):
        return self.nodes[ name ]

class GraphNode:
    def __init__(self, name, score, neighbors=[]):
        self.name = name
        self.score = score
        self.neighbors = set(neighbors)

    def __repr__(self):
        return self.name

def find_path (start_node, start_score, end_node):
    prev_solution = {start_node: [start_score + start_node.score, None]}
    room_to_grow = True
    while end_node not in prev_solution:
        if not room_to_grow:
            # No point looping endlessly...
            return None
        room_to_grow = False
        solution = {}
        for node, info in prev_solution.iteritems():
            score, prev_path = info
            for neighbor in node.neighbors:
                new_score = score + neighbor.score
                if neighbor not in prev_solution:
                    room_to_grow = True
                if 0 < new_score and (neighbor not in solution or solution[neighbor][0] < new_score):
                    solution[neighbor] = [new_score, [node, prev_path]]
        prev_solution = solution
    path = prev_solution[end_node][1]
    answer = [end_node]
    while path is not None:
        answer.append(path[0])
        path = path[1]
    answer.reverse()
    return answer

这里是如何使用这种方法的样本:

graph = Graph([GraphNode( A , 10), GraphNode( B , -5), GraphNode( C , -30)])
graph.connect( A ,  A )
graph.connect( A ,  B )
graph.connect( B ,  B )
graph.connect( B ,  B )
graph.connect( B ,  C )
graph.connect( C ,  C )

print find_path(graph.node( A ), 10, graph.node( C ))

Note that I explicitly connected each node to itself. Depending on your problem you might want to make that automatic.

(注,有一条可能的fin。) 假设起始点的分数为0分,而且没有办法。 在这种情况下,我们永远不休。 它将努力为这一案件增加检查。

问题回答

I m a little confused by your description, it seems like you are just looking for shortest path algorithms. In which case google is your friend.

比如,你已经做了一些调整,这些调整实际上应当为平常的图表的加价。 I.e. 你们想找到一条成本最低的道路,以便你们需要多加调整。

If your graph has loops that are beneficial to traverse (i.e. decrease cost or increase points through adjustments) then the best path is undefined because going through the loop one more time will improve your score.

这里有一些皮条码

steps = []
steps[0] = [None*graph.#nodes]
step = 1
while True:
     steps[step] = [None*graph.#nodes]
     for node in graph:
         for node2 in graph:
             steps[step][node2.index] = max(steps[step-1][node.index]+node2.cost, steps[step][node2.index])

     if steps[step][lastnode] >= 0:
         break;




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签