#include <iostream> #include <list> using namespace std; class Edge { private: string startVertex; string endVertex; string value; public: string getStartVertex() { return startVertex; } void setStartVertex(string startVertex) { this->startVertex = startVertex; } string getEndVertex() { return endVertex; } void setEndVertex(string endVertex) { this->endVertex = endVertex; } string getValue() { return value; } void setValue(string value) { this->value = value; } }; class EdgeListGraph { public: void insertVertex(list<string> *vertices, string vertex) { vertices->push_back(vertex); } void insertEdge(list<Edge> *edgeList, string vertex1, string vertex2, string edgeVal) { Edge edge; edge.setStartVertex(vertex1); edge.setEndVertex(vertex2); edge.setValue(edgeVal); edgeList->push_back(edge); } void printEdgeList(list<Edge> edgeList) { for (Edge edge : edgeList) { cout << "The Edge between " << edge.getStartVertex() << " and " << edge.getEndVertex() << " is " << edge.getValue() << endl; } } }; int main() { EdgeListGraph edgeListGraph; list<string> vertices; list<Edge> edgeList; // 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); }
In the Edge List Data Structure, we are trying to create a 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 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 List,
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 :
The first method is quite easy to understand.
void insertVertex(list<string> *vertices, string vertex) { vertices->push_back(vertex); }
Let us take the vertex a to understand the above method.
In the main(...) method, we have created a List that would contain all the vertices :
list<string> vertices;
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,
'list<string> *vertices'
and
'string vertex'.
It is quite self explanatory, list<string> *vertices contains the actual List that contains the vertices and string vertex contains the vertex a.
And we just have one statement in the void insertVertex(...) method.
vertices.push_back(vertex);
That takes each vertex a and adds to the List.
After adding all the vertices to the LinkedList, we get the below LinkedList.
Then comes the second method, that adds the edges to the List(with values). Justifying the name Edge List Data Structure.
void insertEdge(list<Edge> *edgeList, string vertex1, string vertex2, string edgeVal) { Edge edge; edge.setStartVertex(vertex1); edge.setEndVertex(vertex2); edge.setValue(edgeVal); edgeList->push_back(edge); }
We will take a small chunk to explain the above method.
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 List that would store all the edges in that List.
list<Edge> 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 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;
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,
And add the above edge object to the List,
edgeList.push_back(edge);
After adding all the edges to the List, we get the below List.