Top Level Namespace
Defined Under Namespace
Modules: Glymour Classes: WeightNet
Instance Method Summary collapse
- #cartprod(*args) ⇒ Object
-
#complete_graph(n) ⇒ Object
Generates the complete graph on n vertices if n is an integer, otherwise the complete graph on the vertices in the enumerable given.
-
#make_directed(vertices, directed_edges) ⇒ Object
Takes a list of vertices and a hash of source => [targets] pairs and generates a directed graph.
-
#make_implicit(vertices, edges) ⇒ Object
Takes a list of vertices and a hash of source => [targets] pairs and generates an implicit (undirected) graph.
- #remove_edge(orig, e) ⇒ Object
Instance Method Details
#cartprod(*args) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/glymour.rb', line 55 def cartprod(*args) result = [[]] while [] != args t, result = result, [] b, *args = args t.each do |a| b.each do |n| result << a + [n] end end end result end |
#complete_graph(n) ⇒ Object
Generates the complete graph on n vertices if n is an integer, otherwise the complete graph on the vertices in the enumerable given
10 11 12 13 14 15 16 17 18 |
# File 'lib/glymour.rb', line 10 def complete_graph(n) set = (Integer === n) ? 1..n : n RGL::ImplicitGraph.new do |g| g.vertex_iterator { |b| set.each(&b) } g.adjacent_iterator do |x, b| set.each { |y| b.call(y) unless x == y } end end end |
#make_directed(vertices, directed_edges) ⇒ Object
Takes a list of vertices and a hash of source => [targets] pairs and generates a directed graph
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/glymour.rb', line 32 def make_directed(vertices, directed_edges) g = RGL::DirectedAdjacencyGraph.new vertices.each { |v| g.add_vertex(v) } directed_edges.each do |source, targets| targets.each { |target| g.add_edge(source, target) } end g end |
#make_implicit(vertices, edges) ⇒ Object
Takes a list of vertices and a hash of source => [targets] pairs and generates an implicit (undirected) graph
45 46 47 48 49 50 51 52 53 |
# File 'lib/glymour.rb', line 45 def make_implicit(vertices, edges) RGL::ImplicitGraph.new do |g| edges.default = [] g.vertex_iterator { |b| vertices.each(&b) } g.adjacent_iterator do |x, b| vertices.each {|y| b.call(y) if edges[x].include? y} end end end |
#remove_edge(orig, e) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/glymour.rb', line 20 def remove_edge(orig, e) new_graph = RGL::ImplicitGraph.new do |g| g.vertex_iterator { |b| orig.vertices.each(&b) } g.adjacent_iterator do |x, b| new_adj = orig.adjacent_vertices(x).reject { |v| e.source == v or e.target == v } new_adj.each { |y| b.call(y) } end end new_graph end |