Module: Doodl::GraphGenerator
- Included in:
- Graph
- Defined in:
- lib/graph_generator.rb
Instance Method Summary collapse
- #gen_binary_tree(depth) ⇒ Object
- #gen_connected_graph(number_of_nodes) ⇒ Object
- #gen_linear_graph(size) ⇒ Object
- #gen_mesh_graph(num_rows, num_columns = nil) ⇒ Object
- #gen_random_graph(number_of_nodes, number_of_links = nil) ⇒ Object
- #gen_ring_graph(size = 3) ⇒ Object
Instance Method Details
#gen_binary_tree(depth) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/graph_generator.rb', line 40 def gen_binary_tree(depth) @nodes = [] ancestors = [self.add_node] depth.times do |index| children = [] ancestors.each do |node| a = self.add_node b = self.add_node children << a << b self.add_edge(node, a, false) self.add_edge(node, b, false) end ancestors = children end self.changed self.notify_observers(self) end |
#gen_connected_graph(number_of_nodes) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/graph_generator.rb', line 31 def gen_connected_graph(number_of_nodes) @nodes = [] number_of_nodes.times { self.add_node } combinations(@nodes).each { |tupel| add_edge(tupel[0], tupel[1]) } self.changed self.notify_observers(self) end |
#gen_linear_graph(size) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/graph_generator.rb', line 68 def gen_linear_graph(size) @nodes = [] link_linear(size) self.changed self.notify_observers(self) end |
#gen_mesh_graph(num_rows, num_columns = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/graph_generator.rb', line 75 def gen_mesh_graph(num_rows, num_columns = nil) num_columns = num_rows unless num_columns @nodes = [] rows = [] num_rows.times { rows << link_linear(num_columns) } index = 0 while index < rows.size - 1 link_rows(rows[index], rows[index+1]) index += 1 end self.changed self.notify_observers(self) end |
#gen_random_graph(number_of_nodes, number_of_links = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/graph_generator.rb', line 9 def gen_random_graph(number_of_nodes, number_of_links = nil) if number_of_links raise ArgumentError if directed? and number_of_links > (number_of_nodes*(number_of_nodes-1)) raise ArgumentError if (not directed? and number_of_links > (number_of_nodes*(number_of_nodes-1)/2)) else if directed? number_of_links = rand(number_of_nodes*(number_of_nodes-1)+1) else number_of_links = rand(number_of_nodes*(number_of_nodes-1)/2+1) end end @nodes = [] number_of_nodes.times { self.add_node } combinations = combinations(@nodes) number_of_links.times do tupel = combinations.delete_at(rand(combinations.size)) self.add_edge(tupel[0], tupel[1]) end self.changed self.notify_observers(self) end |
#gen_ring_graph(size = 3) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/graph_generator.rb', line 58 def gen_ring_graph(size=3) @nodes = [] link_linear(size) if (@nodes.first != @nodes.last and not get_edge(@nodes.last, @nodes.first)) self.add_edge(@nodes.last, @nodes.first, false) end self.changed self.notify_observers(self) end |