Class: GraphMatrix
- Inherits:
-
Object
- Object
- GraphMatrix
- Defined in:
- lib/graphify/graph_matrix.rb
Instance Attribute Summary collapse
- #edges ⇒ Object readonly
Instance Method Summary collapse
- #edges_with_weights ⇒ Object
- #get_edge_weight(from, to) ⇒ Object
- #get_in_edges(vertex) ⇒ Object
- #get_out_edges(vertex) ⇒ Object
-
#initialize(args) ⇒ GraphMatrix
constructor
A new instance of GraphMatrix.
- #vertices ⇒ Object
Constructor Details
#initialize(args) ⇒ GraphMatrix
Returns a new instance of GraphMatrix.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/graphify/graph_matrix.rb', line 3 def initialize(args) @type = args[:type] @weight_limit = args[:weight_limit] if args.has_key?(:num_vertices) @edges = Array.new(args[:num_vertices]) { Array.new(args[:num_vertices]) } initialize_helper(args[:num_vertices], args[:num_edges]) else @edges = args[:edge_matrix] end end |
Instance Attribute Details
#edges ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/graphify/graph_matrix.rb', line 44 def edges result = [] (0..(@edges.length - 1)).each do |index| (0..(@edges.length - 1)).each do |s_index| next if @type == 'undirected' && s_index < index result << [index, s_index] if @edges[index][s_index] != nil end end result end |
Instance Method Details
#edges_with_weights ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/graphify/graph_matrix.rb', line 55 def edges_with_weights result = [] (0..(@edges.length - 1)).each do |index| (0..(@edges.length - 1)).each do |s_index| next if @type == 'undirected' && s_index < index result << [index, s_index, @edges[index][s_index]] if @edges[index][s_index] != nil end end result end |
#get_edge_weight(from, to) ⇒ Object
32 33 34 |
# File 'lib/graphify/graph_matrix.rb', line 32 def get_edge_weight(from, to) @edges[from][to] end |
#get_in_edges(vertex) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/graphify/graph_matrix.rb', line 23 def get_in_edges(vertex) return if (vertex < 0 || vertex >= @edges.length) vertices = [] (0..(@edges.length - 1)).each do |index| vertices << index if @edges[index][vertex] != nil end vertices end |
#get_out_edges(vertex) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/graphify/graph_matrix.rb', line 14 def get_out_edges(vertex) return if (vertex < 0 || vertex >= @edges.length) vertices = [] (0..(@edges.length - 1)).each do |index| vertices << index if @edges[vertex][index] != nil end vertices end |
#vertices ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/graphify/graph_matrix.rb', line 36 def vertices vertices = [] (0..(@edges.length - 1)).each do |index| vertices << index end vertices end |