Class: Grandprix::Graph::PredecessorCount

Inherits:
Object
  • Object
show all
Defined in:
lib/grandprix/graph.rb

Instance Method Summary collapse

Constructor Details

#initialize(edges) ⇒ PredecessorCount

Returns a new instance of PredecessorCount.



44
45
46
47
48
49
50
51
52
# File 'lib/grandprix/graph.rb', line 44

def initialize(edges)
  @counts = {}
  edges.each do |j, k|
    @counts[j] ||= 0 
    @counts[k] ||= 0 

    @counts[k] += 1
  end
end

Instance Method Details

#decrement_all(vertices) ⇒ Object



54
55
56
57
58
# File 'lib/grandprix/graph.rb', line 54

def decrement_all(vertices)
  vertices.each do |suc|
    @counts[suc] -= 1
  end
end

#sizeObject



72
73
74
# File 'lib/grandprix/graph.rb', line 72

def size
  @counts.size
end

#to_sObject



76
77
78
79
80
# File 'lib/grandprix/graph.rb', line 76

def to_s
  ks = @counts.keys.map{|k| k.to_s.rjust 2}.join(" ")
  vs = @counts.values.map{|v| v.to_s.rjust 2}.join(" ")
  "Predecessors:\n#{ks}\n#{vs}\n"
end

#zeroesObject



60
61
62
63
64
# File 'lib/grandprix/graph.rb', line 60

def zeroes
  selected = @counts.select {|vertex,count| count == 0 }
  hash_selected = Hash[selected]
  hash_selected.keys      
end

#zeroes_among(vertices) ⇒ Object



66
67
68
69
70
# File 'lib/grandprix/graph.rb', line 66

def zeroes_among(vertices)
  vertices.select do |v|
    @counts[v] == 0
  end
end