Question: Write a C++ function to determine if two vertices in a graph are adjacent. Your function takes two pointers to vertices in the graph. Return

Write a C++ function to determine if two vertices in a graph are adjacent. Your function takes two pointers to vertices in the graph. Return 1 if the vertices are adjacent and 0 if they are not.

bool Graph::checkIfAdjacent(vertex *first, vertex *second);

struct adjVertex{

vertex *v;

int weight;

};

struct vertex{

std::string name;

std::vector adj;

};

Here is the definition of the class

class Graph

{

public:

Graph();

~Graph();

void addEdge(std::string v1, std::string v2, int weight);

void addVertex(std::string name);

void displayEdges();

bool checkIfAdjacent(vertex* first, vertex *second);

vertex* search(std::string name);

protected:

private:

//vector edges;

std::vector vertices;

};

Here is the main function to build the graph

int main() {

Graph g;

g.addVertex("A");

g.addVertex("B");

g.addVertex("C");

g.addVertex("D");

//edge written to be undirected

g.addEdge("A", "B", 2);

g.addEdge("B", "A", 2);

g.addEdge("B", "C", 3);

g.addEdge("B", "D", 4);

g.addEdge("C", "B", 3);

g.addEdge("D", "B", 4);

{{TEST.testcode}} //this will contain the test case values

cout<

return 0;

}

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!