Class: Graph

Inherits:
Object
  • Object
show all
Includes:
Search
Defined in:
lib/graph.rb

Direct Known Subclasses

Square

Defined Under Namespace

Classes: Square

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Search

#breadth_first_search

Constructor Details

#initializeGraph

Returns a new instance of Graph.



10
11
12
# File 'lib/graph.rb', line 10

def initialize
  @edges = {}
end

Instance Attribute Details

#directedObject

Returns the value of attribute directed.



8
9
10
# File 'lib/graph.rb', line 8

def directed
  @directed
end

#edgesObject

Returns the value of attribute edges.



8
9
10
# File 'lib/graph.rb', line 8

def edges
  @edges
end

#maxdistObject

Returns the value of attribute maxdist.



8
9
10
# File 'lib/graph.rb', line 8

def maxdist
  @maxdist
end

Instance Method Details

#add_edge(from:, to:, cost: 1) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/graph.rb', line 14

def add_edge(from:, to:, cost: 1)
  edges[from.id] ||= []
  edges[from.id] << Edge.new.tap { |e| e.from = from, e.to = to, e.cost = cost }
  if !directed
    edges[to.id] ||= []
    edges[to.id] << Edge.new.tap { |e| e.from = to, e.to = from, e.cost = cost }
  end
end