Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:46:13+00:00 2026-06-02T23:46:13+00:00

I have a really difficult problem to solve and Im just wondering what what

  • 0

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. node A gives 10 points to the object

  2. node B takes 15 from the object

  3. node C gives 20 points to the object

route=””

the starting node is A, and the ending node is C

given the graph structure as:-

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

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

nodes with no loops are c+20, so node c has a positive adjustment of 20 but has no loops

if the bot or object has 10 points in its resource then the best path would be :-

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

route=”a,b,c”

this is quite easy to implement, the next challenge is knowing how to backtrack to a good node, lets assume that at each node you can find out any of its neighbour’s nodes and their adjustment level. here is the next example:-

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

route=”a,a,b,c”

this was a very simple graph, but when you have lots of more nodes it becomes very difficult for the bot to know whether to loop at a good node or go from one good node to another, while keeping track of a possible route.

such a route would be a backtrack queue.

A harder example would result in lots of going back and forth

bot has 10 points

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

a > b > a > b > a > b > a > b > a > b > c having 5 pts left.

another way the bot could do it is:-

a > a > a > b > c

this is a more efficient way, but how the heck you can program this is partly my question.

does anyone know of a good algorithm to solve this, ive already looked into Bellman-fords and Dijkstra but these only give a simple path not a looping one.

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


referring to your analogy:-

I think I get what you mean, a bit of pseudo would be clearer, so far 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:46:15+00:00Added an answer on June 2, 2026 at 11:46 pm

    This is a straightforward dynamic programming problem.

    Suppose that for a given length of path, for each node, you want to know the best cost ending at that node, and where that route came from. (The data for that length can be stored in a hash, the route in a linked list.)

    Suppose we have this data for n steps. Then for the n+1st we start with a clean slate, and then take each answer for the n’th, move it one node forward, and if we land on a node we don’t have data for, or else that we’re better than the best found, then we update the data for that node with our improved score, and add the route (just this node linking back to the previous linked list).

    Once we have this for the number of steps you want, find the node with the best existing route, and then you have your score and your route as a linked list.

    ========

    Here is actual code implementing the algorithm:

    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
    

    And here is a sample of how to use it:

    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.

    (Note, there is one possible infinite loop left. Suppose that the starting node has a score of 0 and there is no way off of it. In that case we’ll loop forever. It would take work to add a check for this case.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I have been thinking about, but I can't really
I have a unique problem, which is proving difficult to solve using google. I
I have really basic question. How can I get form id by input element
I really have a problem, I have a VS 2010 solution and it suddenly
I have some really funky code. As you can see from the code below
Possibly a difficult problem to solve... Also I realize there may be more than
I have a feeling that this should really not be all that difficult, yet
Let's say we have an 'intrinsically parallel' problem to solve with our Erlang software.
I have really no idea why I'm asking this as this a really completely
Im new to iPhone development and I have really taken this to me. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.