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




EDGE LIST CODE




Example :



  
import java.util.LinkedList;

class Edge {

    String startVertex;
    String endVertex;

    String value;

    public String getStartVertex() {
        return startVertex;
    }

    public void setStartVertex(String startVertex) {
        this.startVertex = startVertex;
    }

    public String getEndVertex() {
        return endVertex;
    }

    public void setEndVertex(String endVertex) {
        this.endVertex = endVertex;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

public class EdgeListGraph {

    void insertVertex(LinkedList<String> vertices, String vertex) {

        vertices.add(vertex);
    }

    void insertEdge(LinkedList<Edge> edgeList, String vertex1, String vertex2, String edgeVal) {

        Edge edge = new Edge();

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

        edgeList.add(edge);
    }


    void printEdgeList(LinkedList<Edge> edgeList) {

        for (Edge edge : edgeList) {

            System.out.println("The Edge between "+edge.getStartVertex()+" and "+edge.getEndVertex()+" is "+edge.getValue() );
        }
    }

    
    public static void main(String[] args) {

        EdgeListGraph edgeListGraph = new EdgeListGraph();

        LinkedList<String> vertices = new LinkedList<String>();

        LinkedList<Edge> edgeList = new LinkedList<Edge>();

        // Adding vertices one by one

        edgeListGraph.insertVertex(vertices, "a");
        edgeListGraph.insertVertex(vertices, "b");
        edgeListGraph.insertVertex(vertices, "c");
        edgeListGraph.insertVertex(vertices, "d");
        edgeListGraph.insertVertex(vertices, "e");

        //Adding edges with values.

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


        edgeListGraph.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 {

    String startVertex;
    String endVertex;

    String value;
}


We have 2 methods in the Code :


  1. void insertVertex(LinkedList<String> vertices, String vertex)
  2. void insertEdge(LinkedList<Edge> edgeList, String vertex1, String vertex2, String edgeVal)

The first method is quite easy to understand.


Explanation of 'void insertVertex(LinkedList<String> vertices, String vertex)' method :



void insertVertex(LinkedList<String> vertices, String vertex) {

    vertices.add(vertex);
}



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


In the 'main(...)' method, we have created a Linked List that would contain all the vertices :


LinkedList<String> vertices = new LinkedList<String>();

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


edgeListGraph.insertVertex(vertices, "a");

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


'LinkedList<String> vertices' 

and


'String vertex'.

It is quite self explanatory, 'LinkedList<String> vertices' contains the actual Linked List that contains the vertices and 'String vertex' contains the vertex 'a'.


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


vertices.add(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 'void insertEdge(LinkedList<Edge> edgeList, String vertex1, String vertex2, String edgeVal)' method :



void insertEdge(LinkedList<Edge> edgeList, String vertex1, String vertex2, String edgeVal) {

    Edge edge = new Edge();

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

    edgeList.add(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 a Linked List that would store all the edges in that Linked List.


LinkedList<Edge> edgeList = new LinkedList<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.


edgeListGraph.insertEdge(edgeList, "a", "b", "13A");

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


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


Edge edge = new 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.add(edge);

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



java_Collections