Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - EDGE LIST CODE




Example :



class Edge
    def getStartVertex()
        return @startVertex
    end    

    def setStartVertex( startVertex)
        @startVertex = startVertex
    end

    def getEndVertex()
        return @endVertex
    end    

    def setEndVertex( endVertex)
        @endVertex = endVertex
    end    

    def getValue()
        return @value
    end

    def setValue( value)
        @value = value
    end
end        

def insertVertex(vertices, vertex)
    vertices.push(vertex)
end

def insertEdge(edgeList, vertex1, vertex2, edgeVal)
	edge = Edge.new
	edge.setStartVertex(vertex1)
	edge.setEndVertex(vertex2)
	edge.setValue(edgeVal)
	edgeList.push(edge)
end	

def printEdgeList(edgeList)
	edgeList.each do |edge|
		puts "The Edge between #{edge.getStartVertex()} and #{edge.getEndVertex()}, is, #{edge.getValue()}"
	end
end		
		

vertices = []

# Adding vertices one by one

insertVertex(vertices, "a")
insertVertex(vertices, "b")
insertVertex(vertices, "c")
insertVertex(vertices, "d")
insertVertex(vertices, "e")

edgeList = []

# Adding edges with values.

insertEdge(edgeList, "a", "b", "13A")
insertEdge(edgeList, "b", "a", "45C")
insertEdge(edgeList, "a", "d", "20F")
insertEdge(edgeList, "d", "e", "23E")
insertEdge(edgeList, "e", "b", "12B")
insertEdge(edgeList, "e", "c", "30F")
insertEdge(edgeList, "c", "d", "42V")
insertEdge(edgeList, "a", "c", "18C")

printEdgeList(edgeList)


Output :



  The Edge between a and b is 13A
  The Edge between b and a is 45C
  The Edge between a and d is 20F
  The Edge between d and e is 23E
  The Edge between e and b is 12B
  The Edge between e and c is 30F
  The Edge between c and d is 42V
  The Edge between a and c is 18C

Code explanation for Edge List Data Structure


In the Edge List Data Structure, we are trying to create a Linked List of Edges.


Where each Edge will contain the start vertex, end vertex and value of the Edge.


Say for Example, the first element of the Edge Linked List i.e. Bus number 13A, travels from city/Vertex a to city/Vertex b.


So, we will be having two pointers in the first element of Edge Linked List,

java_Collections

And thus, we have constructed an Edge class that contains, start vertex, end vertex and value of the Edge.


class Edge
	def getStartVertex()
		return @startVertex
	end

	def setStartVertex( startVertex)
		@startVertex = startVertex
	end

	def getEndVertex()
		return @endVertex
	end

	def setEndVertex( endVertex)
		@endVertex = endVertex
	end

	def getValue()
		return @value
	end

	def setValue( value)
		@value = value
	end
end

We have 2 methods in the Code :

  1. def insertVertex(vertices, vertex)

  2. def insertEdge(edgeList, vertex1, vertex2, edgeVal)

The first method is quite easy to understand.


Explanation of def insertVertex(vertices, vertex) method :


def insertVertex(vertices, vertex)
	vertices.append(vertex)
end

Let us take the vertex a to understand the above method.


So, we have created a Array that would contain all the vertices :


vertices = []

Then we have taken the vertex a and passed it to the def insertVertex(...) method.


insertVertex(vertices, "a")

And if we see the void insertVertex(...) method, we would see it accepts two parameters,


'vertices'

and


'vertex'.

It is quite self explanatory, vertices contains the actual Array that contains the vertices and vertex contains the vertex a.


And we just have one statement in the def insertVertex(...) method.


vertices.push(vertex)

That takes each vertex a and adds to the Linked List.


After adding all the vertices to the LinkedList, we get the below LinkedList.

java_Collections

Then comes the second method, that adds the edges to the Linked List(with values). Justifying the name Edge List Data Structure.


Explanation of def insertEdge(edgeList, vertex1, vertex2, edgeVal) method :


def insertEdge(edgeList, vertex1, vertex2, edgeVal)
	edge = Edge.new
	edge.setStartVertex(vertex1)
	edge.setEndVertex(vertex2)
	edge.setValue(edgeVal)
	edgeList.push(edge)
end

We will take a small chunk to explain the above method.

java_Collections

Where the bus/edge 13A is traveling from city a to city b.


Now, we have created a Linked List that would store all the edges in that Linked List.


edgeList = []

Next, we will pass the edgeList, the start vertex(i.e. a), end vertex(i.e. b) and the value(i.e. 13A) to the def insertEdge(...) method.


insertEdge(edgeList, "a", "b", "13A")

Now, let us come to the contents of def insertEdge(...) method.


In the first line, we have created the Edge object,


edge = Edge()

Then, initialised the attributes of the Edge object with the actual values,


edge.setStartVertex(vertex1)
edge.setEndVertex(vertex2)
edge.setValue(edgeVal)

And the Edge object looks somewhat like this,

java_Collections

And add the above edge object to the Linked List,


edgeList.push(edge)

After adding all the edges to the Linked List, we get the below Linked List.

java_Collections