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
};
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
std::vector
};
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started