Class: Wakame::Graph

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

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



6
7
8
# File 'lib/wakame/graph.rb', line 6

def initialize
  @edges={}
end

Instance Method Details

#add_edge(v, c) ⇒ Object



10
11
12
13
14
15
# File 'lib/wakame/graph.rb', line 10

def add_edge(v, c)
  return if v == c
  add_vertex(v)
  add_vertex(c)
  @edges[v][c]=1
end

#add_vertex(v) ⇒ Object



25
26
27
# File 'lib/wakame/graph.rb', line 25

def add_vertex(v)
  @edges[v] ||= {}
end

#children(v) ⇒ Object



44
45
46
# File 'lib/wakame/graph.rb', line 44

def children(v)
  @edges[v].keys
end

#has_edge?(v, c) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/wakame/graph.rb', line 21

def has_edge?(v, c)
  @edges.has_key?(v) && @edges[v].has_key?(c)
end

#inspectObject



48
49
50
51
52
53
54
# File 'lib/wakame/graph.rb', line 48

def inspect
  str="#{self}:"
  str << @edges.keys.sort.collect { |v|
    "#{v}=>#{@edges[v].keys.inspect}"
  }.join(', ')
  str
end

#level_layout(root) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wakame/graph.rb', line 57

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



36
37
38
39
40
41
42
# File 'lib/wakame/graph.rb', line 36

def parents(v)
  plist=[]
  @edges.each { |n, c|
    plist << n if c.has_key?(v)
  }
  plist
end

#remove_edge(v, c) ⇒ Object



17
18
19
# File 'lib/wakame/graph.rb', line 17

def remove_edge(v, c)
  @edges[v].delete(c)
end

#remove_vertex(v) ⇒ Object



29
30
31
32
33
34
# File 'lib/wakame/graph.rb', line 29

def remove_vertex(v)
  @edges.keys.each { |n|
    n.delete(v)
  }
  @edges.delete(v)
end