title: How do I make a path out of a hash?
body: <p>I am trying to locate the route of the shortest path of a map(connected nodes with weight/distance).</p>
  
  <p>Let's assume I have a hash like this:</p>
  
  <pre><code>{"A"=&gt;{"B"=&gt;1, "E"=&gt;1}, "B"=&gt;{"A"=&gt;1, "C"=&gt;1, "F"=&gt;1}, "C"=&gt;{"B"=&gt;1, "D"=&gt;1, "G"=&gt;1}, "D"=&gt;{"C"=&gt;1, "H"=&gt;1}, "E"=&gt;{"F"=&gt;1, "A"=&gt;1, "I"=&gt;1}, "F"=&gt;{"E"=&gt;1, "G"=&gt;1, "B"=&gt;1, "J"=&gt;1}, "G"=&gt;{"F"=&gt;1, "H"=&gt;1, "C"=&gt;1, "K"=&gt;1}, "H"=&gt;{"G"=&gt;1, "D"=&gt;1, "L"=&gt;1}, "I"=&gt;{"J"=&gt;1, "E"=&gt;1, "M"=&gt;1}, "J"=&gt;{"I"=&gt;1, "K"=&gt;1, "F"=&gt;1, "N"=&gt;1}, "K"=&gt;{"J"=&gt;1, "L"=&gt;1, "G"=&gt;1, "O"=&gt;1}, "L"=&gt;{"K"=&gt;1, "H"=&gt;1, "P"=&gt;1}, "M"=&gt;{"N"=&gt;1, "I"=&gt;1}, "N"=&gt;{"M"=&gt;1, "O"=&gt;1, "J"=&gt;1}, "O"=&gt;{"N"=&gt;1, "P"=&gt;1, "K"=&gt;1}, "P"=&gt;{"O"=&gt;1, "L"=&gt;1}} 
  </code></pre>
  
  <p>Now I want to traverse and make a path from this hash. For example:</p>
  
  <p>From Source A to Destination: L</p>
  
  <p>Output should be: either <code>A -&gt; E -&gt; I -&gt; J -&gt; K -&gt; L</code> or <code>A -&gt; B -&gt; C -&gt; D -&gt; H -&gt; L</code>.</p>
  
  <p>Here is the function I wrote:</p>
  
  <pre><code>def find_path(src, dst, init = [])
    path = [src]
    neighbors = self.neighbors(src)
    puts "src: #{src}"
    puts "dst: #{dst}"
    # puts "node: #{node}"
    puts "init: #{init}"
    puts "path: #{path}"
    puts "----\n"
  
    if neighbors.include?(dst)
      path.push(dst)
    else
      path.push(@nodes[src].keys.map{|k| k unless init.flatten.include? k }.reject(&amp;:blank?).each{|key| self.find_path(key, dst, init &lt;&lt; path) } )
    end
    return path
  end
  </code></pre>
  
  <p>But, this prints only :  ["A", ["B", "E"]]</p>
  
  <p>Which is not the desired output, can anybody tell me how do I go make this work? Thanks.</p>
  
  <p><strong>Update:</strong> This was used for locating the route of the shortest path of a map(connected nodes with weight/distance). Here are the details on what I was trying to achieve in this question: <a href="https://gist.github.com/suryart/6439102" rel="nofollow">https://gist.github.com/suryart/6439102</a></p>