Module: Elasticsearch::Model::Extensions::ShortestPath::ClassMethods

Included in:
Elasticsearch::Model::Extensions::ShortestPath
Defined in:
lib/elasticsearch/model/extensions/shortest_path.rb

Instance Method Summary collapse

Instance Method Details

#breadth_first_search(node, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/elasticsearch/model/extensions/shortest_path.rb', line 63

def breadth_first_search(node, &block)
  original_paths = node.each.map { |e| [e] }
  paths = original_paths

  depth = 0

  loop {
    a = paths.select { |p|
      if block.call(p.last)
        p
      end
    }

    return a if a.size != 0
    raise RuntimeError, 'Maximum depth exceeded while calculating the shortest path' if depth >= Elasticsearch::Model::Extensions::ShortestPath::MAX_DEPTH

    paths = paths.flat_map { |p|
      p.last.destination.each.map { |e|
        p + [e]
      }
    }

    depth += 1
  }
end

#depth_first_search(node, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/elasticsearch/model/extensions/shortest_path.rb', line 89

def depth_first_search(node, &block)
  node.each.select do |edge|
    if block.call(edge)
      [[edge]]
    else
      depth_first_search(edge.destination, &block).map do |path|
        [edge] + path
      end
    end
  end
end