Question: In C++. In this assignment you are expected to develop code capable of finding the shortest path between two nodes in a given graph, if
In C++. In this assignment you are expected to develop code capable of finding the shortest path between two nodes in a given graph, if such a path exists, using the Floyd Warshall All Pairs Shortest Path Algorithm.
main.cpp - This is the main driver of your program. It is used to call the functions you write to satisfy the assignment requirements. You should not modify this file.
Graph.hpp - This file contains an incomplete C++ class for a graph data structure. This file is where you should write your code to implement your graphing data structure and algorithms.
Makefile - This file can be used to build your program for testing. You should not modify this file.
You will need to complete all empty functions listed in Graph.hpp. You should not modify any functions that have already been completed.
| #ifndef _GRAPH_HPP_ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define _GRAPH_HPP_ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using namespace std; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define MAX_NODES 150 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @brief Graph object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Graph{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uint32_t graph[MAX_NODES][MAX_NODES]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Graph(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ~Graph(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool add_path(uint32_t, uint32_t, uint32_t); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void floyd_warshall(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int shortest_path(uint32_t, uint32_t); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uint32_t get_path(uint32_t, uint32_t); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @brief Graph object default constructor, initialize | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| members to default values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Graph::Graph() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Code here */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @brief Graph object destructor, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Graph::~Graph() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Code here */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @brief Add new path to Graph, treating new path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| as weighted and directed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @param src - Source graph node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @param dst - Destination graph node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @param weight - Path weight | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @return True if success, False if not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool Graph::add_path(uint32_t src, uint32_t dst, uint32_t weight) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Code here */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @brief Calculate the shortest paths between all nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| in a given graph using the Floyd Warshall All | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pairs Shortest Path Algorithm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void Graph::floyd_warshall(void) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Code here */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @brief Return the shortest path between two given nodes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if such a patch exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @param src - Source graph node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @param dst - Destination graph node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @return - Shortest path distance between src and dst, -1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if there is no path between src and dst | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int Graph::shortest_path(uint32_t src, uint32_t dst) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Code here */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @brief Return path between two given nodes, used | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for debugging only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @param src - Source graph node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @param dst - Destination graph node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @return - Path value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uint32_t Graph::get_path(uint32_t src, uint32_t dst) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uint32_t path = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (MAX_NODES > src && MAX_NODES > dst) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path = graph[src][dst]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif /* _GRAPH_HPP_ */
|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
