Class: GraphList

Inherits:
Object
  • Object
show all
Defined in:
lib/graphify/graph_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ GraphList

Returns a new instance of GraphList.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/graphify/graph_list.rb', line 3

def initialize(args)
  @type = args[:type]
  @weight_limit = args[:weight_limit]
  @edge_lists = {}
  if args.has_key?(:num_vertices)
    (0..args[:num_vertices] - 1).each do |num|
      @edge_lists[num] = {}
    end
    initialize_edges(args[:num_vertices], args[:num_edges])
  else
    @edge_lists = args[:edge_lists]
  end
end

Instance Method Details

#edgesObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/graphify/graph_list.rb', line 38

def edges
  result = []
  @edge_lists.keys.each do |k|
    @edge_lists[k].keys.each do |s_k|
      next if @type == 'undirected' && s_k > k
      result << [k, s_k]
    end
  end
  result
end

#edges_with_weightsObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/graphify/graph_list.rb', line 49

def edges_with_weights
  result = []
  @edge_lists.keys.each do |k|
    @edge_lists[k].keys.each do |s_k|
      next if @type == 'undirected' && s_k > k
      result << [k, s_k, @edge_lists[k][s_k]]
    end
  end
  result
end

#get_edge_weight(to, from) ⇒ Object



26
27
28
# File 'lib/graphify/graph_list.rb', line 26

def get_edge_weight(to, from)
  @edge_lists[to][from]
end

#get_in_edges(vertex) ⇒ Object



22
23
24
# File 'lib/graphify/graph_list.rb', line 22

def get_in_edges(vertex)
  @edge_lists.reduce([]) { |memo, (key, value)| value.has_key?(vertex) ? memo << key : memo }
end

#get_out_edges(vertex) ⇒ Object



18
19
20
# File 'lib/graphify/graph_list.rb', line 18

def get_out_edges(vertex)
  @edge_lists[vertex].keys
end

#verticesObject



30
31
32
33
34
35
36
# File 'lib/graphify/graph_list.rb', line 30

def vertices
  result = []
  (0..@edge_lists.keys.size - 1).each do |vertex|
    result << vertex
  end
  result
end