Class: GoNodes::Graph
- Inherits:
-
Object
- Object
- GoNodes::Graph
- Defined in:
- lib/gonodes/graph.rb
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #==(other_graph) ⇒ Object
- #completeness ⇒ Object
-
#initialize(params = {}) ⇒ Graph
constructor
A new instance of Graph.
- #randomize_edges(completeness) ⇒ Object
- #randomize_weights(random_weights) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Graph
Returns a new instance of Graph.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gonodes/graph.rb', line 9 def initialize(params={}) @nodes = NodeList.new @nodes.populate_with_count(params[:node_count]) if params[:node_count] @nodes.populate_with_names(params[:node_names]) if params[:node_names] @edges = EdgeList.new(@nodes) completeness = params[:completeness] ? params[:completeness] : 1.0 if params[:random_edges] && completeness != 1.0 randomize_edges(completeness) else @edges.populate_with_completeness(completeness) end randomize_weights(params[:random_weights]) if params[:random_weights] end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
7 8 9 |
# File 'lib/gonodes/graph.rb', line 7 def edges @edges end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
7 8 9 |
# File 'lib/gonodes/graph.rb', line 7 def nodes @nodes end |
Instance Method Details
#==(other_graph) ⇒ Object
31 32 33 |
# File 'lib/gonodes/graph.rb', line 31 def ==(other_graph) @nodes == other_graph.nodes && @edges == other_graph.edges end |
#completeness ⇒ Object
27 28 29 |
# File 'lib/gonodes/graph.rb', line 27 def completeness @edges.completeness end |
#randomize_edges(completeness) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/gonodes/graph.rb', line 35 def randomize_edges(completeness) edge_count = @edges.normalized_edge_count(completeness) edge_nodes = @edges.possible_edges.sample(edge_count) edge_nodes.each do |nodes| @edges.add_edge(start_node: nodes[0], end_node: nodes[1]) end end |
#randomize_weights(random_weights) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/gonodes/graph.rb', line 43 def randomize_weights(random_weights) median = random_weights[:median] range = random_weights[:range] @edges.each do |edge| edge.weight = median + rand(range) - (range / 2) end end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/gonodes/graph.rb', line 51 def to_s "Nodes:\n#{@nodes.to_s}\n\nEdges:\n#{@edges.to_s}" end |