Class: Yargi::Random
- Inherits:
-
Object
- Object
- Yargi::Random
- Defined in:
- lib/yargi/random.rb
Overview
Random graph generator
Instance Attribute Summary collapse
-
#edge_builder ⇒ Proc
Proc to call on vertex generation.
-
#edge_count ⇒ Integer
Number of edges to generate.
-
#strip ⇒ Object
Returns the value of attribute strip.
-
#vertex_builder ⇒ Proc
Proc to call on vertex generation.
-
#vertex_count ⇒ Integer
Number of vertices to generate.
Instance Method Summary collapse
-
#execute ⇒ Object
Executes the random generation.
-
#initialize {|_self| ... } ⇒ Random
constructor
Creates an algorithm instance.
Constructor Details
#initialize {|_self| ... } ⇒ Random
Creates an algorithm instance
25 26 27 28 29 30 31 32 |
# File 'lib/yargi/random.rb', line 25 def initialize @vertex_count = 50 @vertex_builder = nil @edge_count = 150 @edge_builder = nil @strip = true yield(self) if block_given? end |
Instance Attribute Details
#edge_builder ⇒ Proc
Returns proc to call on vertex generation.
18 19 20 |
# File 'lib/yargi/random.rb', line 18 def edge_builder @edge_builder end |
#edge_count ⇒ Integer
Returns number of edges to generate.
15 16 17 |
# File 'lib/yargi/random.rb', line 15 def edge_count @edge_count end |
#strip ⇒ Object
Returns the value of attribute strip.
22 23 24 |
# File 'lib/yargi/random.rb', line 22 def strip @strip end |
#vertex_builder ⇒ Proc
Returns proc to call on vertex generation.
12 13 14 |
# File 'lib/yargi/random.rb', line 12 def vertex_builder @vertex_builder end |
#vertex_count ⇒ Integer
Returns number of vertices to generate.
9 10 11 |
# File 'lib/yargi/random.rb', line 9 def vertex_count @vertex_count end |
Instance Method Details
#execute ⇒ Object
Executes the random generation
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/yargi/random.rb', line 35 def execute graph = Digraph.new{|g| vertex_count.times do |i| vertex = g.add_vertex vertex_builder.call(vertex,i) if vertex_builder end edge_count.times do |i| source = g.ith_vertex(Kernel.rand(vertex_count)) target = g.ith_vertex(Kernel.rand(vertex_count)) edge = g.connect(source, target) edge_builder.call(edge,i) if edge_builder end } strip ? _strip(graph) : graph end |