Class: RGraph::Graph
- Inherits:
-
FFI::Struct
- Object
- FFI::Struct
- RGraph::Graph
- Includes:
- IO::GraphML
- Defined in:
- lib/rgraph/graph.rb
Overview
Data structure for storing graphs.
Instance Attribute Summary collapse
-
#pointer ⇒ Object
readonly
Returns the value of attribute pointer.
Class Method Summary collapse
-
.destroy(graph) ⇒ Object
Frees memory allocated for the graph.
-
.empty(size, directed: false) ⇒ Graph
Creates an empty graph.
-
.erdos_renyi_game(type, size, p_or_m, directed: false, loops: false) ⇒ Graph
Generates a random (Erdős-Rényi) graph.
-
.full(size, directed: false, loops: false) ⇒ Graph
Creates a full graph.
-
.kary_tree(size, children, type) ⇒ Graph
Creates a k-ary tree in wchich almost all vertices have k children.
-
.ring(size, directed: false, mutual: false, circular: false) ⇒ Graph
Creates a ring graph.
-
.star(size, mode, center) ⇒ Graph
Creates a star graph.
-
.watts_strogatz_game(dim, size, nei, prob, loops: false, multiple: false) ⇒ Graph
Generates Watts-Strogatz small-world model.
-
.wheel(size, mode, center) ⇒ Graph
Creates a wheel graph.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Checks if two graphs are identical.
-
#add_edge(from, to) ⇒ Object
Adds an edge to a graph.
-
#add_vertices(number) ⇒ Object
Adds a vertices to a graph.
-
#copy ⇒ Graph
Creates a copy of a graph.
-
#directed? ⇒ Boolean
Indicates where the graph is directed.
-
#edge?(from, to, directed: false) ⇒ Boolean
Checks if the edge exists in the graph.
-
#edges_count ⇒ Integer
Counts number of edges in a graph.
-
#simplify(multiple: true, loops: true) ⇒ Object
Removes loops and/or multiple edges from the graph.
-
#vertices_count ⇒ Integer
Count number of vertices in a graph.
Methods included from IO::GraphML
Instance Attribute Details
#pointer ⇒ Object (readonly)
Returns the value of attribute pointer.
11 12 13 |
# File 'lib/rgraph/graph.rb', line 11 def pointer @pointer end |
Class Method Details
.destroy(graph) ⇒ Object
Frees memory allocated for the graph.
258 259 260 |
# File 'lib/rgraph/graph.rb', line 258 def self.destroy(graph) Bindings.igraph_destroy(graph) end |
.empty(size, directed: false) ⇒ Graph
Creates an empty graph.
43 44 45 |
# File 'lib/rgraph/graph.rb', line 43 def self.empty(size, directed: false) new { |graph| Bindings.igraph_empty(graph, size, directed) } end |
.erdos_renyi_game(type, size, p_or_m, directed: false, loops: false) ⇒ Graph
Generates a random (Erdős-Rényi) graph.
204 205 206 207 208 209 210 |
# File 'lib/rgraph/graph.rb', line 204 def self.erdos_renyi_game(type, size, p_or_m, directed: false, loops: false) unless [Constants::ErdosRenyi::GNP, Constants::ErdosRenyi::GNM].include?(type) raise(ArgumentError, "#{type.inspect} is an invalid Erdos Renyi type.") end new { |graph| Bindings.igraph_erdos_renyi_game(graph, type, size, p_or_m, directed, loops) } end |
.full(size, directed: false, loops: false) ⇒ Graph
Creates a full graph.
62 63 64 65 66 67 68 |
# File 'lib/rgraph/graph.rb', line 62 def self.full(size, directed: false, loops: false) if size < 2 raise(ArgumentError, "#{size.inspect} is an invalid number of vertices.") end new { |graph| Bindings.igraph_full(graph, size, directed, loops) } end |
.kary_tree(size, children, type) ⇒ Graph
Creates a k-ary tree in wchich almost all vertices have k children.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rgraph/graph.rb', line 113 def self.kary_tree(size, children, type) if size < 0 raise(ArgumentError, "#{size.inspect} is an invalid number of vertices. Can't be negative.") end if children <= 0 raise(ArgumentError, "#{children} is an invalid number of children. Must be positive.") end unless [Constants::TreeMode::OUT, Constants::TreeMode::IN, Constants::TreeMode::UNDIRECTED].include?(type) raise(ArgumentError, "#{type.inspect} is an invalid tree orientation type.") end new { |graph| Bindings.igraph_kary_tree(graph, size, children, type) } end |
.ring(size, directed: false, mutual: false, circular: false) ⇒ Graph
Creates a ring graph.
89 90 91 92 93 94 95 |
# File 'lib/rgraph/graph.rb', line 89 def self.ring(size, directed: false, mutual: false, circular: false) if size < 0 raise(ArgumentError, "#{size.inspect} is an invalid number of vertices. Can't be negative.") end new { |graph| Bindings.igraph_ring(graph, size, directed, mutual, circular) } end |
.star(size, mode, center) ⇒ Graph
Creates a star graph.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rgraph/graph.rb', line 144 def self.star(size, mode, center) if size < 0 raise(ArgumentError, "#{size.inspect} is an invalid number of vertices. Can't be negative.") end if center < 0 || center > size - 1 raise(ArgumentError, "#{center.inspect} is an invalid center vertex.") end unless [Constants::StarMode::IN, Constants::StarMode::OUT, Constants::StarMode::MUTUAL, Constants::StarMode::UNDIRECTED].include?(mode) raise(ArgumentError, "#{mode.inspect} is an invalid star mode.") end new { |graph| Bindings.igraph_star(graph, size, mode, center) } end |
.watts_strogatz_game(dim, size, nei, prob, loops: false, multiple: false) ⇒ Graph
Generates Watts-Strogatz small-world model.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/rgraph/graph.rb', line 236 def self.watts_strogatz_game(dim, size, nei, prob, loops: false, multiple: false) if dim < 1 raise(ArgumentError, "#{dim.inspect} is an invalid value. Should be at least one.") end if size < 1 raise(ArgumentError, "#{size.inspect} is an invalid value. Should be at least one.") end if prob < 0 || prob > 1 raise(ArgumentError, "#{prob.inspect} is an invalid value. Should be between 0 and 1.") end new { |graph| Bindings.igraph_watts_strogatz_game(graph, dim, size, nei, prob, loops, multiple) } end |
Instance Method Details
#==(other) ⇒ Boolean
Checks if two graphs are identical.
270 271 272 273 274 |
# File 'lib/rgraph/graph.rb', line 270 def ==(other) result_pointer = FFI::MemoryPointer.new(:bool) Bindings.igraph_is_same_graph(self, other, result_pointer) result_pointer.read_uchar != 0 end |
#add_edge(from, to) ⇒ Object
Adds an edge to a graph.
295 296 297 |
# File 'lib/rgraph/graph.rb', line 295 def add_edge(from, to) Bindings.igraph_add_edge(self, from, to) end |
#add_vertices(number) ⇒ Object
Adds a vertices to a graph.
351 352 353 354 355 356 357 |
# File 'lib/rgraph/graph.rb', line 351 def add_vertices(number) if number < 0 raise(ArgumentError, "#{number.inspect} is an invalid value. Must be positive.") end Bindings.igraph_add_vertices(self, number, nil) end |
#copy ⇒ Graph
Creates a copy of a graph
282 283 284 |
# File 'lib/rgraph/graph.rb', line 282 def copy self.class.new { |new_graph| Bindings.igraph_copy(new_graph, self) } end |
#directed? ⇒ Boolean
Indicates where the graph is directed.
341 342 343 |
# File 'lib/rgraph/graph.rb', line 341 def directed? Bindings.igraph_is_directed(self) end |
#edge?(from, to, directed: false) ⇒ Boolean
Checks if the edge exists in the graph.
321 322 323 324 325 |
# File 'lib/rgraph/graph.rb', line 321 def edge?(from, to, directed: false) eid = FFI::MemoryPointer.new(:int) Bindings.igraph_get_eid(self, eid, from, to, directed, false) eid.read_int >= 0 end |
#edges_count ⇒ Integer
Counts number of edges in a graph.
304 305 306 |
# File 'lib/rgraph/graph.rb', line 304 def edges_count Bindings.igraph_ecount(self) end |
#simplify(multiple: true, loops: true) ⇒ Object
Removes loops and/or multiple edges from the graph.
368 369 370 |
# File 'lib/rgraph/graph.rb', line 368 def simplify(multiple: true, loops: true) Bindings.igraph_simplify(self, multiple, loops, nil) end |
#vertices_count ⇒ Integer
Count number of vertices in a graph.
332 333 334 |
# File 'lib/rgraph/graph.rb', line 332 def vertices_count Bindings.igraph_vcount(self) end |