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




GO - EDGE LIST CODE




Example :



package main
import "fmt"

type Edge struct {

	startVertex string
	endVertex string
	
	value string
}

func insertVertex(vertices *[]string, vertex string) {

    *vertices = append(*vertices, vertex)
}

func insertEdge(edgeList *[]Edge, vertex1 string, vertex2 string, edgeVal string) {
 	
    var edge Edge
        
    edge.startVertex = vertex1
    edge.endVertex = vertex2
    edge.value = edgeVal

    *edgeList = append(*edgeList, edge)
}


func printEdgeList(edgeList *[]Edge) {

    for _, edge := range *edgeList {

        fmt.Println("The Edge between "+edge.startVertex+" and "+edge.endVertex+" is "+edge.value );
    }
}

	
func main() {

    var vertices[]string

    var edgeList[]Edge

    // Adding vertices one by one

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

    //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 an Array 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 Array 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 Array,

java_Collections

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


type Edge struct {

	startVertex string
	endVertex string

	value string
}

We have 2 methods in the Code :

  1. func insertVertex(vertices *[]string, vertex string)

  2. func insertEdge(edgeList *[]Edge, vertex1 string, vertex2 string, edgeVal string)

The first method is quite easy to understand.


Explanation of 'func insertVertex(vertices *[]string, vertex string)' method :


func insertVertex(vertices *[]string, vertex string) {

	*vertices = append(*vertices, vertex)
}

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


In the main(...) method, we have created an Array that would contain all the vertices :


var vertices[]string

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


insertVertex(&vertices, "a")

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


'vertices *[]string'

and


'vertex string'.

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


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


*vertices = append(*vertices, vertex)

That takes each vertex a and adds to the Array.


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

java_Collections

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


Explanation of 'func insertEdge(edgeList *[]Edge, vertex1 string, vertex2 string, edgeVal string)' method :


func insertEdge(edgeList *[]Edge, vertex1 string, vertex2 string, edgeVal string) {

	var edge Edge

	edge.startVertex = vertex1
	edge.endVertex = vertex2
	edge.value = edgeVal

	*edgeList = append(*edgeList, edge)
}

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, let us take a look at the main(...) method.


We have created an Array that would store all the edges in that Array.


var edgeList[]Edge

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 void insertEdge(...) method.


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

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


In the first line, we have created the Edge type variable named edge,


var edge Edge

Then, initialised the attributes of the Edge type variable with the actual values,


edge.startVertex = vertex1
edge.endVertex = vertex2
edge.value = edgeVal

And the Edge object looks somewhat like this,

java_Collections

And add the above edge object to the Array,


*edgeList = append(*edgeList, edge)

After adding all the edges to the Array, we get the below Array.

java_Collections