简体   繁体   English

如何从散列中创建路径?

[英]How do I make a path out of a hash?

I am trying to locate the route of the shortest path of a map(connected nodes with weight/distance). 我试图找到地图最短路径的路线(连接节点的重量/距离)。

Let's assume I have a hash like this: 我们假设我有这样的哈希:

{"A"=>{"B"=>1, "E"=>1}, "B"=>{"A"=>1, "C"=>1, "F"=>1}, "C"=>{"B"=>1, "D"=>1, "G"=>1}, "D"=>{"C"=>1, "H"=>1}, "E"=>{"F"=>1, "A"=>1, "I"=>1}, "F"=>{"E"=>1, "G"=>1, "B"=>1, "J"=>1}, "G"=>{"F"=>1, "H"=>1, "C"=>1, "K"=>1}, "H"=>{"G"=>1, "D"=>1, "L"=>1}, "I"=>{"J"=>1, "E"=>1, "M"=>1}, "J"=>{"I"=>1, "K"=>1, "F"=>1, "N"=>1}, "K"=>{"J"=>1, "L"=>1, "G"=>1, "O"=>1}, "L"=>{"K"=>1, "H"=>1, "P"=>1}, "M"=>{"N"=>1, "I"=>1}, "N"=>{"M"=>1, "O"=>1, "J"=>1}, "O"=>{"N"=>1, "P"=>1, "K"=>1}, "P"=>{"O"=>1, "L"=>1}} 

Now I want to traverse and make a path from this hash. 现在我想遍历并从这个哈希中创建一个路径。 For example: 例如:

From Source A to Destination: L 从源A到目的地:L

Output should be: either A -> E -> I -> J -> K -> L or A -> B -> C -> D -> H -> L . 输出应为: A -> E -> I -> J -> K -> LA -> B -> C -> D -> H -> L

Here is the function I wrote: 这是我写的函数:

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(&:blank?).each{|key| self.find_path(key, dst, init << path) } )
  end
  return path
end

But, this prints only : ["A", ["B", "E"]] 但是,这只打印:[“A”,[“B”,“E”]]

Which is not the desired output, can anybody tell me how do I go make this work? 哪个不是理想的输出,谁能告诉我如何才能使这个工作? Thanks. 谢谢。

Update: 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: https://gist.github.com/suryart/6439102 以下是我在这个问题中试图实现的细节: https//gist.github.com/suryart/6439102

The original hash: 原始哈希:

h = {"A"=>{"B"=>1, "E"=>1}, "B"=>{"A"=>1, "C"=>1, "F"=>1}, "C"=>{"B"=>1, "D"=>1, "G"=>1}, "D"=>{"C"=>1, "H"=>1}, "E"=>{"F"=>1, "A"=>1, "I"=>1}, "F"=>{"E"=>1, "G"=>1, "B"=>1, "J"=>1}, "G"=>{"F"=>1, "H"=>1, "C"=>1, "K"=>1}, "H"=>{"G"=>1, "D"=>1, "L"=>1}, "I"=>{"J"=>1, "E"=>1, "M"=>1}, "J"=>{"I"=>1, "K"=>1, "F"=>1, "N"=>1}, "K"=>{"J"=>1, "L"=>1, "G"=>1, "O"=>1}, "L"=>{"K"=>1, "H"=>1, "P"=>1}, "M"=>{"N"=>1, "I"=>1}, "N"=>{"M"=>1, "O"=>1, "J"=>1}, "O"=>{"N"=>1, "P"=>1, "K"=>1}, "P"=>{"O"=>1, "L"=>1}} 

Converting the original hash since its format sucks: 转换原始哈希,因为它的格式很糟糕:

h.keys.each{|k| h[k] = h[k].keys}
h.default = []

The method: 方法:

def find_path h, src, dst
  paths = [[src]]
  loop do
    paths = paths.flat_map do |path|
      h[path.last].map do |nekst|
        a = [*path, nekst]
        a.last == dst ? (return a) : a
      end
    end
  end
end

Trying it: 试一试:

find_path(h, "A", "L")
# => ["A", "B", "C", "D", "H", "L"]

Notice that if there is no solution, then the loop may run forever. 请注意,如果没有解决方案,那么循环可能会永远运行。 You might want to limit that by adding a limit to the length. 您可能希望通过添加长度限制来限制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM