What is the best algorithm for moving a graph with negative nodes and loop nodes

I have a really difficult problem and I'm just wondering which algorithm can be used to find the fastest route. An undirected graph consists of positive and negative adjustments; these adjustments affect a bot or an object that moves through a maze. The problem is I have mazes that contain loops that can be + or -. An example may help: -

  • node A gives 10 points to an object

  • node B takes 15 from an object

  • node C gives 20 points to an object

path = ""

start node is A and end node is C

given the structure of the graph as: -

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

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

nodes without loops c + 20, so node c has a positive setting of 20, but has no loops

10 , : -

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

= ", , "

, - , node, , node . : -

5 ,

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

= ", , , "

, , , node node , .

.

,

10

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

a > b > a > b > a > b > a > b > a > b > c 5 .

, : -

a > a > a > b > c

, , .

- , - , , .

- - ?


: -

, , , , route()

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
+3
3

.

, node , node, . ( , .)

, n . n + 1 , n'th, node , node, , , , node ( node, ).

, node , .

========

, :

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'))

, node . .

( : . , node 0, . . .)

+2

, , . Google .

, , , + ve . , + ve.

, (.. ), - undefined, .

0

Here is some psuedocode

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;
0
source

All Articles