Class: GraphLab::Vertex

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Vertex

Returns a new instance of Vertex.



350
351
352
353
354
355
356
357
358
359
# File 'lib/graphlab.rb', line 350

def initialize *args
     case args.size
	when 3
		init_valxy *args
	when 1
		init_val *args
	else
		error
	end
end

Instance Attribute Details

#adjListObject

attribute that represents the node in a graph canvas



346
347
348
# File 'lib/graphlab.rb', line 346

def adjList
  @adjList
end

#valueObject

value and coordinate of vertex



342
343
344
# File 'lib/graphlab.rb', line 342

def value
  @value
end

#visitedObject

Returns the value of attribute visited.



347
348
349
# File 'lib/graphlab.rb', line 347

def visited
  @visited
end

#xcoordObject

Returns the value of attribute xcoord.



343
344
345
# File 'lib/graphlab.rb', line 343

def xcoord
  @xcoord
end

#ycoordObject

Returns the value of attribute ycoord.



344
345
346
# File 'lib/graphlab.rb', line 344

def ycoord
  @ycoord
end

Instance Method Details

#addEdge(vertex) ⇒ Object

to add edge to this vertex



416
417
418
419
420
# File 'lib/graphlab.rb', line 416

def addEdge(vertex)
	raise "Value must be a Vertex!" if vertex.class != GraphLab::Vertex
	raise "cannot add edge to itself" if vertex == self
	adjList << vertex
end

#deleteEdge(vertex) ⇒ Object



422
423
424
425
# File 'lib/graphlab.rb', line 422

def deleteEdge(vertex)
	raise "Value must be a Vertex!" if vertex.class != GraphLab::Vertex
	adjList.delete(vertex)
end

#getAdjListObject



411
412
413
# File 'lib/graphlab.rb', line 411

def getAdjList()
	return self.adjList
end

#init_val(value) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/graphlab.rb', line 374

def init_val(value)
	raise "Enter other value. Vertex with this value has been created!" if @@createdValues.include?(value)
	if(!@@createdValues.include?(value))
		@@createdValues << value
		@value = value
		@xcoord = rand(450)+30
		@ycoord = rand(450)+30
		coord = (xcoord.to_s + ycoord.to_s)
		#to avoid overlapping vertex coordinates in canvas
		while (@@position.include?(coord))
			puts "regenerate to avoid collision"
			@xcoord = rand(450)+30
			@ycoord = rand(450)+30
			coord = (xcoord.to_s + ycoord.to_s)
		end
		#@@position << coord
		#need to add more y coord and x coord
		a = 50
	
		x = @xcoord - a
		y = @ycoord - a
		for i in 0..100
			x = x + i					
			for j in 0..100
				y = y + j
				coord = (x.to_s + y.to_s)
				@@position << coord
			end
		end
		
			
		@adjList = Array.new
		@visited = false
	end
	return @value
end

#init_valxy(value, x, y) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/graphlab.rb', line 361

def init_valxy(value, x , y)
	raise "Enter other value. Vertex with this value has been created!" if @@createdValues.include?(value)
	if(!@@createdValues.include?(value))
		@@createdValues << value
		@value = value
		@xcoord = x
		@ycoord = y
		@adjList = Array.new
		@visited = false
	end
	return @value
end

#inspectObject

return a string representation of the vertex



428
429
430
# File 'lib/graphlab.rb', line 428

def inspect
	return to_s
end

#isVisited?Boolean

Returns:

  • (Boolean)


441
442
443
# File 'lib/graphlab.rb', line 441

def isVisited?
	return @visited
end

#to_sObject

returns a string representation of the vertex



434
435
436
437
438
439
# File 'lib/graphlab.rb', line 434

def to_s	
	s = value.to_s+" ";
	#s+= "("+xcoord.to_s+",";
	#s+= ycoord.to_s+")";
	return s;
end