Class: MiniGraph::Core::Search::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mini_graph/core/search.rb

Overview


Base Search Implementation


Direct Known Subclasses

BFS, DFS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, vertex_index) ⇒ Base

Returns a new instance of Base.



14
15
16
17
# File 'lib/mini_graph/core/search.rb', line 14

def initialize(graph, vertex_index)
  @graph        = graph
  @vertex_index = vertex_index
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



12
13
14
# File 'lib/mini_graph/core/search.rb', line 12

def graph
  @graph
end

#vertex_indexObject (readonly)

Returns the value of attribute vertex_index.



12
13
14
# File 'lib/mini_graph/core/search.rb', line 12

def vertex_index
  @vertex_index
end

Instance Method Details

#eachObject



19
20
21
22
23
24
25
# File 'lib/mini_graph/core/search.rb', line 19

def each
  return enum_for(:each) unless block_given?

  visit(vertex_index) do |vi|
    yield graph[vi]
  end
end

#visit(index, visited = Array.new(graph.size, false), &block) ⇒ Object

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/mini_graph/core/search.rb', line 27

def visit(index, visited=Array.new(graph.size, false), &block)
  raise NotImplementedError, "#visit must be implemented"
end