Class: GraphLab::Graph
- Inherits:
-
Object
- Object
- GraphLab::Graph
- Defined in:
- lib/graphlab.rb
Instance Attribute Summary collapse
-
#vertices ⇒ Object
readonly
to keep array of vertices.
Instance Method Summary collapse
-
#addEdge(v1, v2) ⇒ Object
to draw edges between 2 vertices in graph randomly from v1 to v2.
-
#addEdgeForGraph(v1, v2) ⇒ Object
to add edge from v1 to v2 - directed graph.
-
#addEdges(x) ⇒ Object
to draw edges between 2 vertices in graph randomly.
-
#addVertex(vertex) ⇒ Object
to add new vertex in the graph.
-
#count_v ⇒ Object
to count number of vertices in the graph.
- #deleteEdge(v1, v2) ⇒ Object
-
#deleteVertex(vertex) ⇒ Object
to delete the vertex from the graph.
- #drawNot(u, o, x) ⇒ Object
- #drawOrNot(u, adjList, x) ⇒ Object
- #include?(v1) ⇒ Boolean
-
#initialize ⇒ Graph
constructor
class constructor.
-
#inspect ⇒ Object
return a string representation of the array.
-
#isEmpty? ⇒ Boolean
checks whether the graph is empty.
-
#to_s ⇒ Object
returns a string representation of the array.
Constructor Details
#initialize ⇒ Graph
class constructor
225 226 227 |
# File 'lib/graphlab.rb', line 225 def initialize @vertices = Array.new end |
Instance Attribute Details
#vertices ⇒ Object (readonly)
to keep array of vertices
222 223 224 |
# File 'lib/graphlab.rb', line 222 def vertices @vertices end |
Instance Method Details
#addEdge(v1, v2) ⇒ Object
to draw edges between 2 vertices in graph randomly from v1 to v2
248 249 250 251 252 253 254 255 256 |
# File 'lib/graphlab.rb', line 248 def addEdge(v1,v2) raise "Value must be a Vertex!" if v1.class != GraphLab::Vertex raise "Value must be a Vertex!" if v2.class != GraphLab::Vertex raise "Vertex already added in the graph!" if v1.adjList.include?(v2) if(v1.class == GraphLab::Vertex && v2.class == GraphLab::Vertex && !v1.adjList.include?(v2)) v1.addEdge(v2) end view_graph(self) end |
#addEdgeForGraph(v1, v2) ⇒ Object
to add edge from v1 to v2 - directed graph
289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/graphlab.rb', line 289 def addEdgeForGraph(v1,v2) raise "Value must be a Vertex!" if v1.class != GraphLab::Vertex raise "Value must be a Vertex!" if v2.class != GraphLab::Vertex raise "Vertex must be in the graph" if !include?(v1) raise "Vertex must be in the graph" if !include?(v2) raise "Vertex "+v2+" already adjacent vertex of "+v1+" in the graph!" if v1.adjList.include?(v2) raise "cannot add edge to itself" if v1 == v2 if(v1.class == GraphLab::Vertex && v2.class == GraphLab::Vertex && include?(v1) && include?(v2)) v1.addEdge(v2) end end |
#addEdges(x) ⇒ Object
to draw edges between 2 vertices in graph randomly
269 270 271 272 273 274 275 |
# File 'lib/graphlab.rb', line 269 def addEdges(x) raise "Value must be smaller than 1!" if x > 1 raise "Value must be larger than 0!" if x <= 0 x = x*100 vertices.each { |u| drawOrNot(u,vertices,x) } view_graph(self) end |
#addVertex(vertex) ⇒ Object
to add new vertex in the graph
237 238 239 240 241 242 243 244 245 |
# File 'lib/graphlab.rb', line 237 def addVertex(vertex) raise "Value must be a Vertex!" if vertex.class != GraphLab::Vertex raise "Vertex already added in the graph!" if vertices.include?(vertex) if(vertex.class == GraphLab::Vertex && !vertices.include?(vertex)) vertices << vertex end view_graph(self) return @vertices end |
#count_v ⇒ Object
to count number of vertices in the graph
302 303 304 |
# File 'lib/graphlab.rb', line 302 def count_v() return vertices.length end |
#deleteEdge(v1, v2) ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/graphlab.rb', line 258 def deleteEdge(v1, v2) raise "Value must be a Vertex!" if v1.class != GraphLab::Vertex raise "Value must be a Vertex!" if v2.class != GraphLab::Vertex raise "Vertex "+v1.value.to_s+" does not have "+v2.value.to_s+" edge" if !v1.adjList.include?(v2) if(v1.class == GraphLab::Vertex && v2.class == GraphLab::Vertex && v1.adjList.include?(v2)) v1.deleteEdge(v2) end view_graph(self) end |
#deleteVertex(vertex) ⇒ Object
to delete the vertex from the graph
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/graphlab.rb', line 307 def deleteVertex(vertex) if(vertex.class == GraphLab::Vertex) for i in 0..graph.vertices.length-1 vi = graph.vertices[i] if(vi.adjList.include?(vertex)) vi.adjList.delete(vertex) end end vertex.adjList.each { |adj| adj.adjList.delete(vertex)} vertex.adjList.clear vertices.delete(vertex) end view_graph(self) return @vertices end |
#drawNot(u, o, x) ⇒ Object
281 282 283 284 285 286 |
# File 'lib/graphlab.rb', line 281 def drawNot(u,o,x) a = rand(101) #from 100, what is the generated random no if(a<=x && u!=o) addEdgeForGraph(u,o) end end |
#drawOrNot(u, adjList, x) ⇒ Object
277 278 279 |
# File 'lib/graphlab.rb', line 277 def drawOrNot(u,adjList,x) adjList.each{ |o| drawNot(u,o,x) } end |
#include?(v1) ⇒ Boolean
324 325 326 |
# File 'lib/graphlab.rb', line 324 def include?(v1) return vertices.include?(v1) end |
#inspect ⇒ Object
return a string representation of the array
329 330 331 |
# File 'lib/graphlab.rb', line 329 def inspect return to_s end |
#isEmpty? ⇒ Boolean
checks whether the graph is empty
231 232 233 |
# File 'lib/graphlab.rb', line 231 def isEmpty? return vertices == nil end |
#to_s ⇒ Object
returns a string representation of the array
334 335 336 |
# File 'lib/graphlab.rb', line 334 def to_s return @vertices.inspect end |