Question: Using C++ I need to implement Dijkstra Algorithm to print a graph (Note: main() will be provided below ) Explanation of main() Operations: 1 -

Using C++

I need to implement Dijkstra Algorithm to print a graph (Note: main() will be provided below)

Explanation of main() Operations:

1 - insertVertex(vertex)

2 - insertEdge(from, to, weight)

3 - isEdge(from, to)

4 - getWeight(from, to)

5 - getAdjacent(vertex)

6 - printDijkstra(source)

7 - printGraph( )

Sample Input 1:

8 - Line 1 indicates the number of operations you will be performing or the number of lines that follow

1 5 - Each line's first integer indicates operation; 1 here means insertVertex() and this follows input to the operation - 5

1 6 - 1 is insertVertex(6)

2 5 6 50 - 2 is insertEdge. Therefore, operation is insertEdge(5,6,50)

3 6 5 - 3 is isEdge. Therefore, operation is isEdge(6,5)

4 5 6 - 4 is getWeight. Therefore, operation is getWeight(5,6)

5 5 - 5 is getAdjacent. Therefore, operation is getAdjacent(5)

6 5 - 6 is printDijkstra. Therefore, operation is printDijkstra(5)

7 - 7 is printGraph. Therefore, operation is printGraph()

Sample Output 1:

Functions insertVertex and insertEdge have no output.

0 - isEdge (6,5) returns false

50 - getWeight (5,6) returns 50

6 - getAdjacent(5) returns adjacent vertices in sorted order based on name.

V D P - printDijkstra(5) prints the graph in the format as explained below after this section

6 50 5-6 For this graph, there is only one vertex, 6 from 5, with distance 50 and path from source 5.

5 6 - printGraph() prints the graph in the (format: V EdgeList) in ascending order of vertices and their respective edge lists

6 This graph has 2 vertices 5, 6. So print vertex (1 per line) and in that line print the edges it is connected to in sorted order

Sample Input 2:

12 - Line 1 indicates the number of operations you will be performing 1 1 - 1 is insertVertex(1) 1 2 - 1 is insertVertex(2) 1 3 - 1 is insertVertex(3) 1 4 - 1 is insertVertex(4) 2 1 2 3 - 2 is insertEdge 2 2 3 7 - 2 is insertEdge 2 2 4 5 - 2 is insertEdge 2 3 4 15 - 2 is insertEdge 2 4 1 4 - 2 is insertEdge 7 - 7 is printGraph, prints the Vertex and the edges adjacent to it in ascending order 6 1 - 1 is printDijkstra(1) 5 2 - 1 is getAdjacent(2) Sample Output 2: (- Explanations do not need to be printed) 
1 2 - Explanation Vertex 1, has edge 2 connected to it 2 3 4 - Explanation Vertex 2, has edges 3 and 4 conncected to it 3 4 - Explanation Vertex 3, has edge 4 connected to it 4 1 - Explanation Vertex 4, has edge 1 connected to it V D P 2 3 1-2 3 10 1-2-3 4 8 1-2-4 3 4 class dijkstraGraphs{ private:  // dijkstraGraphs data structure could be inserted here public: void insertVertex(int vertex); //inserts new vertex in graph void insertEdge(int from, int to, int weight); //inserts new edge in graph bool isEdge(int from, int to); //returns true if there is an edge between the vertices from and to int getWeight(int from, int to); //returns the weight of the edge between the vertices from and to vector getAdjacent(int vertex);//return an array of integers represent vertices adjacent to vertex void printDijkstra(int source); //prints result of running Dijkstra algorithm with source vertex void printGraph(); //prints graph in a format sorted by ascending vertex and edge list }; *** Given Main Method *** 

int main(){ //DO NOT CHANGE THIS FUNCTION. int noOfLines, operation, vertex, to, fro, weight, source, j; vector arr; int arrSize; dijkstraGraphs g; cin >> noOfLines; for (int i=0;i>operation; switch(operation){ case 1: cin>>vertex; g.insertVertex(vertex); break; case 2: cin>>fro; cin>>to; cin>>weight; g.insertEdge(fro,to,weight); break; case 3: cin>>fro; cin>>to; cout<>fro; cin>>to; cout<>vertex; arr=g.getAdjacent(vertex); arrSize = arr.size(); j=0; while(j>source; g.printDijkstra(source); cout<<" "; break; case 7: g.printGraph(); cout<<" "; break; } } }

//THANK YOU!!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!