Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

16. Implement this MPEMP.

Answered: 1 week ago