Question
The purpose of this lab is to help reinforce graph implementations in C++. Implement a function with three arguments: a graph, a starting vertex number,
The purpose of this lab is to help reinforce graph implementations in C++. Implement a function with three arguments: a graph, a starting vertex number, and an ending vertex number. The function determines whether there is a directed path from the starting vertex to the ending vertex.
Not sure why you need this updated. The question comes verbatum from the book. Inserted a screenshot.
_______________________
graph.h file
_______________________
#ifndef MAIN_SAVITCH_GRAPH_H #define MAIN_SAVITCH_GRAPH_H #include // Provides size_t #include // Provides set
namespace main_savitch_15 { template class graph { public: // MEMBER CONSTANTS static const std::size_t MAXIMUM = 20; // CONSTRUCTOR graph( ) { many_vertices = 0; } // MODIFICATION MEMBER FUNCTIONS void add_vertex(const Item& label); void add_edge(std::size_t source, std::size_t target); void remove_edge(std::size_t source, std::size_t target); Item& operator [ ] (std::size_t vertex); // CONSTANT MEMBER FUNCTIONS std::size_t size( ) const { return many_vertices; } bool is_edge(std::size_t source, std::size_t target) const; std::set neighbors(std::size_t vertex) const; Item operator[ ] (std::size_t vertex) const; private: bool edges[MAXIMUM][MAXIMUM]; Item labels[MAXIMUM]; std::size_t many_vertices; }; }
#include "graph.template" // Include the implementation. #endif
_______________________
graph.template file
_______________________
// FILE: graph.template (part of the namespace main_savitch_15) // TEMPLATE CLASS IMPLEMENTED: graph (See graph.h for documentation.) // This file is included in the header file and not compiled separately. // INVARIANT for the graph class: // 1. The number of vertices in the graph is stored in the member variable // many_vertices. // 1. These vertices are numbered from 0 to many_vertices-1. // 2. edges is the adjacency matrix for the graph (with true in edges[i][j] // to indicate an edge from vertex i to vertex j). // 3. For each i #include // Provides assert #include // Provides size_t #include // Provides set namespace main_savitch_15 { template class Item> const std::size_t graph::MAXIMUM; template class Item> void graph::add_edge(std::size_t source, std::size_t target) // Library facilities used: cassert, cstdlib { assert(source true; } template class Item> void graph::add_vertex(const Item& label) // Library facilities used: cassert, cstdlib { std::size_t new_vertex_number; std::size_t other_number; assert(size( ) for (other_number = 0; other_number false; edges[new_vertex_number][other_number] = false; } labels[new_vertex_number] = label; } template class Item> bool graph::is_edge(std::size_t source, std::size_t target) const // Library facilities used: cassert, cstdlib { assert(source return edges[source][target]; } template class Item> Item& graph::operator[ ] (std::size_t vertex) // Library facilities used: cassert, cstdlib { assert(vertex return labels[vertex]; // Returns a reference to the label } template class Item> Item graph::operator[ ] (std::size_t vertex) const // Library facilities used: cassert, cstdlib { assert(vertex return labels[vertex]; // Returns only a copy of the label } template class Item> std::set graph::neighbors(std::size_t vertex) const // Library facilities used: cassert, cstdlib, set { std::set answer; std::size_t i; assert(vertex for (i = 0; i if (edges[vertex][i]) answer.insert(i); } return answer; } template class Item> void graph::remove_edge(std::size_t source, std::size_t target) // Library facilities used: cassert, cstdlib { assert(source false; } }Implement a function with three arguments a graph, a starting vertex number, and an ending vertex number. The function deter- mines whether there is a directed path from the start- ing vertex to the ending vertex. Implement a function with three arguments a graph, a starting vertex number, and an ending vertex number. The function deter- mines whether there is a directed path from the start- ing vertex to the ending vertex
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