1

I am looking for the below code to be able to print out and return my final_path array from my #find_knight_path function. It runs just fine with the puts lines I have, but as soon as I try to puts the final_path array or print the return value of the function, I am getting infinite loop behavior in the console and I have no idea why.

I know the function is a little too big. That's my next item to tackle. Any insight is greatly appreciated!

Full code is here: Below code from this link is in lines 244 - 274.

  def find_knight_path(graph, start_pos, end_pos)
    queue = []
    final_path = []
    distance = 0
    predecessor = nil
    pos_start = graph.nodes[start_pos]
    pos_start.distance = 0
    pos_start.predecessor = nil
    queue.push(pos_start)
    final_path.push(pos_start)
    
    while !queue.empty?
      current_item = queue.shift
      # puts "line 97 - What is current_item.value? #{current_item.value}"
      distance += distance
      predecessor = current_item
      current_item.adjacent_nodes.each do |i|
        i.distance = distance
        i.predecessor = predecessor
        queue.push(i) 
      end
      puts "final_path value - #{current_item}"
      puts "final_path length - #{final_path.length}"
      final_path.push(current_item)
      # p "final_path #{final_path}"
      # p "queue #{queue}"
      return final_path.reverse if current_item.value == end_pos
      # puts "final_path length #{final_path.length}"
    end
    final_path.reverse
  end
Cali_Ranger
  • 59
  • 1
  • 6
  • Are you certain the graph is acyclic? – Cary Swoveland Oct 20 '22 at 06:53
  • I am not. I'm very turned around on this problem. It's clear I'm having a problem with adding the adjacencies. I'm just not sure how to fix it. – Cali_Ranger Oct 22 '22 at 01:38
  • Your graph does have cycles because the knight can do a tour and return to any starting point. That's why you have an infinite loop. You need to use a shortest path algorithm (such as Dijkstra's algorithm) with positive weights (say 1) on each arc. See [this SO question and the selected answer](https://stackoverflow.com/questions/43394847/dijkstras-algorithm-and-cycles). – Cary Swoveland Oct 22 '22 at 03:03

1 Answers1

1

You graph construction (or traversal, or both) logic seems to be corrupted. After you add adjacencies

final_graph = initial_object.add_adjacencies(initial_object)

check the 1st node:

pry(main)> k, v = final_graph.nodes.first;
[31] pry(main)> v
=> #<Node:0x000000013377a428
 @adjacent_nodes=
  [#<Node:0x00000001337799b0
    @adjacent_nodes=
     [#<Node:0x00000001337788d0
       @adjacent_nodes=
        [#<Node:0x0000000133773ab0
          @adjacent_nodes=
           [#<Node:0x00000001337734c0
             @adjacent_nodes=
              [#<Node:0x0000000133773ab0 ...>,
               #<Node:0x0000000133773880
                @adjacent_nodes=
                 [#<Node:0x00000001337734c0 ...>,
                  #<Node:0x0000000133773010
                   @adjacent_nodes=
                    [#<Node:0x0000000133773880 ...>,
                     #<Node:0x0000000133773c90
                      @adjacent_nodes=
                       [#<Node:0x0000000133773268
                         @adjacent_nodes=
                          [#<Node:0x0000000133773998
                            @adjacent_nodes=
                             [#<Node:0x00000001337735d8
                               @adjacent_nodes=
                                [#<Node:0x0000000133773998 ...>,
                                 #<Node:0x0000000133778100
                                  @adjacent_nodes=
                                   [#<Node:0x00000001337735d8 ...>,
                                    #<Node:0x0000000133773a10
                                     @adjacent_nodes=
                                      [#<Node:0x0000000133773650
                                        @adjacent_nodes=
                                         [#<Node:0x0000000133773a10 ...>,
                                          #<Node:0x0000000133773f60
                                           @adjacent_nodes=
                                            [#<Node:0x0000000133773650 ...>,
                                             #<Node:0x0000000133773560
                                              @adjacent_nodes=
                                               [#<Node:0x0000000133773b28
                                                 @adjacent_nodes=
                                                  [#<Node:0x0000000133773560 ...>,
                                                   #<Node:0x0000000133773ee8
                                                    @adjacent_nodes=
                                                     [#<Node:0x00000001337735d8 ...>,
                                                      #<Node:0x00000001337734c0 ...>,
                                                      #<Node:0x0000000133773b28 ...>,
                                                      #<Node:0x00000001337738f8
                                                       @adjacent_nodes=
                                                       ...

This beautiful ladder into a rabbit hole doesn't look like an adjacency list as it probably supposed to. And attempt to inspect it (v.inspect) goes into an infinite loop too.

There was a similar question some time ago, about the very same task. My suggestion stays the same - follow YAGNI principle, try to get a simple working solution first, don't add unnecessary abstractions from the very beginning.

Konstantin Strukov
  • 2,899
  • 1
  • 10
  • 14
  • There's something going on that makes absolutely zero sense to me. --I can print out the first node in my algorithm, `#find_knight_path`, on line 300 just fine. --I can print the empty list `final_path` just fine on line 304. --I can print out the first item in `final_path`, which is `pos_start`, just fine on line 306. --If I try to reference a second item in `final_path`, nothing returns, which I expect. --If I try to reference the entire `final_path` list, it gives me "infinite loop" behavior, which makes no sense. https://replit.com/@cvillere/KnightsTravils#scratch_paper.rb – Cali_Ranger Oct 24 '22 at 03:33