Question: In this programming assignment, you are expected to implement a graph using adjacency list using the following header: On C++ class Graphs_P3 { private: //Graph

In this programming assignment, you are expected to implement a graph using adjacency list using the following header:

On C++

class Graphs_P3

{ private: //Graph as an adjacency list

public:

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 a vector of integers representing vertices adjacent to vertex

void printGraph(); //prints graph in a format sorted by ascending vertex and edge list

};

You are expected to write code for each of the functions as well as implement the graph. A sample int main() function that invokes your graph is provided and you can test it on two test cases. Make sure you do not change any code in the main() function.

Composition of main() Method (How we Test your code):

Input:

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

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)

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

Output:

Function 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.

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 1:

6 2 1 2 3 2 2 3 7 2 2 4 5 2 3 4 15 2 4 1 4 7

Sample Output 1:

1 2 2 3 4 3 4 4 1

Sample Input 2:

6 2 1 2 3 2 2 3 7 2 2 4 5 2 3 4 15 2 4 1 4 5 2

Sample Output 2:

3 4 

class Graphs_P3 { private: // Graph data structure here public: 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 representing vertices adjacent to vertex void printGraph(); //prints graph in a format sorted by ascending vertex and edge list };

int main() { //DO NOT CHANGE THIS FUNCTION. CHANGE YOUR IMPLEMENTATION CODE TO MAKE IT WORK int noOfLines, operation, vertex, to, fro, weight,source,j; vector arr; int arrSize; Graphs_P3 g; cin>>noOfLines; for(int i=0;i>operation; switch(operation) { 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

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!