Class: Yargi::Random

Inherits:
Object
  • Object
show all
Defined in:
lib/yargi/random.rb

Overview

Random graph generator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Random

Creates an algorithm instance

Yields:

  • (_self)

Yield Parameters:

  • _self (Yargi::Random)

    the object that the method was called on



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_builderProc

Returns proc to call on vertex generation.

Returns:

  • (Proc)

    proc to call on vertex generation



18
19
20
# File 'lib/yargi/random.rb', line 18

def edge_builder
  @edge_builder
end

#edge_countInteger

Returns number of edges to generate.

Returns:

  • (Integer)

    number of edges to generate



15
16
17
# File 'lib/yargi/random.rb', line 15

def edge_count
  @edge_count
end

#stripObject

Returns the value of attribute strip.



22
23
24
# File 'lib/yargi/random.rb', line 22

def strip
  @strip
end

#vertex_builderProc

Returns proc to call on vertex generation.

Returns:

  • (Proc)

    proc to call on vertex generation



12
13
14
# File 'lib/yargi/random.rb', line 12

def vertex_builder
  @vertex_builder
end

#vertex_countInteger

Returns number of vertices to generate.

Returns:

  • (Integer)

    number of vertices to generate



9
10
11
# File 'lib/yargi/random.rb', line 9

def vertex_count
  @vertex_count
end

Instance Method Details

#executeObject

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