Class: Wakame::Graph
- Inherits:
-
Object
- Object
- Wakame::Graph
- Defined in:
- lib/wakame/graph.rb
Instance Attribute Summary collapse
-
#edges ⇒ Object
Returns the value of attribute edges.
Instance Method Summary collapse
- #add_edge(v, c) ⇒ Object
- #add_vertex(v) ⇒ Object
- #children(v) ⇒ Object
- #has_edge?(v, c) ⇒ Boolean
-
#initialize ⇒ Graph
constructor
A new instance of Graph.
- #inspect ⇒ Object
- #level_layout(root) ⇒ Object
- #parents(v) ⇒ Object
- #remove_edge(v, c) ⇒ Object
- #remove_vertex(v) ⇒ Object
Constructor Details
#initialize ⇒ Graph
Returns a new instance of Graph.
7 8 9 |
# File 'lib/wakame/graph.rb', line 7 def initialize @edges={} end |
Instance Attribute Details
#edges ⇒ Object
Returns the value of attribute edges.
5 6 7 |
# File 'lib/wakame/graph.rb', line 5 def edges @edges end |
Instance Method Details
#add_edge(v, c) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/wakame/graph.rb', line 11 def add_edge(v, c) return if v == c add_vertex(v) add_vertex(c) @edges[v][c]=1 end |
#add_vertex(v) ⇒ Object
26 27 28 |
# File 'lib/wakame/graph.rb', line 26 def add_vertex(v) @edges[v] ||= {} end |
#children(v) ⇒ Object
45 46 47 |
# File 'lib/wakame/graph.rb', line 45 def children(v) @edges[v].keys end |
#has_edge?(v, c) ⇒ Boolean
22 23 24 |
# File 'lib/wakame/graph.rb', line 22 def has_edge?(v, c) @edges.has_key?(v) && @edges[v].has_key?(c) end |
#inspect ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/wakame/graph.rb', line 49 def inspect str="#{self}:" str << @edges.keys.sort.collect { |v| "#{v}=>#{@edges[v].keys.inspect}" }.join(', ') str end |
#level_layout(root) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wakame/graph.rb', line 58 def level_layout(root) @vtx_ylevels = {root=>0} descend(root, 0) ycoord=Array.new(@vtx_ylevels.values.max) @vtx_ylevels.each { |v, lv| (ycoord[lv] ||= []) << v } ycoord end |
#parents(v) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/wakame/graph.rb', line 37 def parents(v) plist=[] @edges.each { |n, c| plist << n if c.has_key?(v) } plist end |
#remove_edge(v, c) ⇒ Object
18 19 20 |
# File 'lib/wakame/graph.rb', line 18 def remove_edge(v, c) @edges[v].delete(c) end |
#remove_vertex(v) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/wakame/graph.rb', line 30 def remove_vertex(v) @edges.keys.each { |n| n.delete(v) } @edges.delete(v) end |