Question: Write a c++ function to check if a path exists between two vertices. The function takes two parameters: source vertex and destination vertex. It should
Write a c++ function to check if a path exists between two vertices. The function takes two parameters: source vertex and destination vertex. It should return true if there is a path between the two vertices. Otherwise, it should return false.
bool Graph::ifPathExists(string vertex1, string vertex2);
struct adjVertex{ vertex *v; int weight; };
struct vertex{ std::string name; bool visited; std::vector adj; };
Here is the definition of the Graph class: class Graph { public: Graph(); ~Graph(); void addEdge(std::string v1, std::string v2, int weight); void addVertex(std::string name); bool ifPathExists(string name1, string name2); protected: private: std::vector vertices; };
Here is the main function that is used to build the tree: int main() { Graph g; g.addVertex("A"); g.addVertex("B"); g.addVertex("C"); g.addVertex("D"); g.addVertex("E"); g.addVertex("F"); g.addVertex("G"); g.addVertex("H"); g.addVertex("I"); g.addVertex("J"); g.addVertex("K"); //edge written to be undirected g.addEdge("A", "B", 2); g.addEdge("B", "C", 2); g.addEdge("C", "E", 2); g.addEdge("C", "D", 2); g.addEdge("F", "G", 2); g.addEdge("F", "H", 2); g.addEdge("G", "I", 2); return 0; } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
