Class: RGraph::Graph

Inherits:
FFI::Struct
  • Object
show all
Includes:
IO::GraphML
Defined in:
lib/rgraph/graph.rb

Overview

Data structure for storing graphs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IO::GraphML

included, #write

Instance Attribute Details

#pointerObject (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.

Parameters:

  • graph (Graph)

    Graph instance to free.



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.

Parameters:

  • size (Integer)

    Number of vertices in the graph.

  • directed (Boolean) (defaults to: false)

    Indicates whether to create a directed graph.

Returns:

  • (Graph)

    The graph instance.



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.

Parameters:

  • type (Constants::ErdosRenyi)

    Type of the Erdős-Rényi graph.

  • size (Integer)

    Number of vertices in the graph.

  • p_or_m (Integer)

    This is the p parameter for G(n,p) graphs and the m parameter for G(n,m) graphs.

  • directed (Boolean) (defaults to: false)

    Indicates whether to create a directed graph.

  • loops (Boolean) (defaults to: false)

    Indicates whether to create loops.

Returns:

  • (Graph)

    The graph instance.



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.

Parameters:

  • size (Integer)

    Number of vertices in the graph.

  • directed (Boolean) (defaults to: false)

    Indicates whether to create a directed graph.

  • loops (Boolean) (defaults to: false)

    Indicates whether to include loops.

Returns:

  • (Graph)

    The graph instance.



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.

Parameters:

  • size (Integer)

    Number of vertices in the graph.

  • children (Ingere)

    The number of vertices in the graph

  • type (Constants::TreeMode)

    The tree orientation type.

Returns:

  • (Graph)

    The graph instance.



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.

Parameters:

  • size (Integer)

    Number of vertices in the graph.

  • directed (Boolean) (defaults to: false)

    Indicates whether to create a directed graph.

  • mutual (Boolean) (defaults to: false)

    Indicates whether to create mutual edges in directed graph. Ignored for undirected graphs.

  • circular (Boolean) (defaults to: false)

    Indicates whether to create a closed ring or an open path.

Returns:

  • (Graph)

    The graph instance.



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.

Parameters:

  • size (Integer)

    Number of vertices in the graph.

  • mode (Constants::StarMode)

    The type of the star graph to create.

  • center (Integer)

    The id of the center vertex.

Returns:

  • (Graph)

    The graph instance.



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.

Parameters:

  • dim (Integer)

    The dimension of the lattice.

  • size (Integer)

    The size of the lattice along each dimension.

  • nei (Integer)

    The size of the neighborhood for each vertex.

  • prob (Float)

    The rewriting probability.

  • loops (Boolean) (defaults to: false)

    Indicates whether to create loops.

  • multiple (Boolean) (defaults to: false)

    Indicates whether to allow multiple edges.

Returns:

  • (Graph)

    The graph instace.



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

.wheel(size, mode, center) ⇒ Graph

Creates a wheel graph.

Parameters:

  • size (Integer)

    Number of vertices in the graph.

  • mode (Constants::StarMode)

    Type of the star graph.

  • center (Integer)

    The id of the center vertex.

Returns:

  • (Graph)

    The graph instance.



178
179
180
# File 'lib/rgraph/graph.rb', line 178

def self.wheel(size, mode, center)
  new { |graph| Bindings.igraph_wheel(graph, size, mode, center) }
end

Instance Method Details

#==(other) ⇒ Boolean

Checks if two graphs are identical.

Parameters:

  • other (Graph)

    Graph to compare with.

Returns:

  • (Boolean)


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.

Parameters:

  • from (Integer)

    Id of the first vertex of the edge.

  • to (Integer)

    Id of the second vertex of the edge.



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.

Parameters:

  • number (Integer)

    Number of vertices to add.



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

#copyGraph

Creates a copy of a graph

Returns:

  • (Graph)

    The graph instance with copied attributes.



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.

Returns:

  • (Boolean)


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.

Parameters:

  • from (Integer)

    Start point of the edge.

  • to (Integer)

    End point of the edge.

  • directed (Boolean) (defaults to: false)

    Indicates whether to search for directed edges in a directed graph.

Returns:

  • (Boolean)


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_countInteger

Counts number of edges in a graph.

Returns:

  • (Integer)


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.

Parameters:

  • multiple (Boolean) (defaults to: true)

    Indicates whether to remove multiple edges.

  • loops (Boolen) (defaults to: true)

    Indicate whether to remove loops.



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_countInteger

Count number of vertices in a graph.

Returns:

  • (Integer)


332
333
334
# File 'lib/rgraph/graph.rb', line 332

def vertices_count
  Bindings.igraph_vcount(self)
end