Can someone please help me fix my code on this assignment that's due in the morning?
Please read the question carefully. Using C++, construct a graph class, graph.template, in which the edges are stored in adjacency sets. You need to use graph.h and test_graph.cpp to implement graph.template. The author provides an implementation of a graph using an adjacency matrix. I have started the template file but I cannot get it to compile. I have posted my template file of what I have so far below. I will upvote if the problem is done correctly.
"Another way to implement a graph is to just store the edges. This is done by using a set for each vertex. Each set will set the "ending" vertex of each edge starting at the specific vertex. For example, to store the edge , v2 will be stored in the set associated with v1. "
============================================================================
graph.template
//File: graph.template
#include
#include
#include
#include
#include
#include "graph.h"
using namespace std;
using namespace main_savitch_15;
//Postcondition: The graph has been initialized with no vertices
and no edges.
template
graph:: graph( ) {
}
//Postcondition: The size of the graph has been increased by
adding one new
//vertex. This new vertex has the specified label and no
edges.
template
void graph:: add_vertex(const Item& label) {
std::size_t new_vertex;
std::size_t number;
vector< pair > v;
for (number = 0; number < size(); ++number) {
pair edges(number, new_vertex);
v.push_back(edges);
edges(new_vertex, number);
v.push_back(edges);
}
labels[new_vertex] = label;
}
//Precondition: source & target are vertices in the
graph
//Postcondition: The graph has all the edges that it originally
had, and it
//also has another edge from the specified source to the specified
target.template
template
void graph:: add_edge(size_t source, size_t target) {
assert(source < size() && target < size());
vector< pair > v;
for(int i = 0; i < size(); i++) {
pair edges(source, target);
v.push_back(edges);
}
}
//Precondition: source & target are vertices in the
graph
//Postcondition: The graph has all the edges that it originally had
except
//for the edge from the specified source to the specified
target.
template
void graph:: remove_edge(size_t source, size_t target) {
assert(source < size() && target < size());
vector< pair > v;
for(int i = 0; i < size(); i++) {
pair edges(source, target);
v.pop_back(edges);
}
}
//Precondition: vertex is in the graph
//Postcondition: The return value is a reference to the label of
the
//specified vertex.
template
Item& graph:: operator [ ] (size_t vertex) {
assert(vertex < size( ));
return labels[vertex];
}
//Postcondition: The return value is the number of vertices in
the graph.
template
size_t graph:: size( ) const {
return edges.size();
}
//Precondition: source & target are vertices in the
graph
//Postcondition: The return value is true if the graph has an edge
from
//source to target. Otherwise the return value is false.
template
bool graph:: is_edge(size_t source, size_t target) const {
assert(source < size() && target < size());
if(source == target) {
return true;
} else {
return false;
}
}
//Precondition: vertex is in the graph
//Postcondition: The return value is a set that contains all the
vertex
//numbers of vertices that are the target of an edge whose source
is at
//the specified vertex.
template
set graph:: neighbors(size_t vertex) const {
std::set size_t result;
std::size_t i;
assert(vertex < size( ));
for (i = 0; i < size( ); ++i) {
if (edges.push_back(make_pair(vertex, i)))
result.insert(i);
}
return result;
}
//Precondition: vertex is in the graph
//Postcondition: The return value is a reference to the label of
the
//specified vertex.
template
Item graph:: operator[ ] (std::size_t vertex) const {
assert(vertex < size());
return labels[vertex];
}
=================================================================
graph.h
// FILE: graph.h (part of the namespace main_savitch_15)
// TEMPLATE CLASS PROVIDED: graph (a class for labeled graphs)
// The vertices of an n-vertex graph are numbered from zero to n-1. Each vertex
// has a label of type Item. It may be any of the C++ built-in types (int,
// char, etc.), or any class with a default constructor and an assignment
// operator. The graph may not have multiple edges.
//
// MEMBER CONSTANTS for the graph template class:
// static const size_t MAXIMUM = ______
// graph::MAXIMUM is the maximum number of vertices that a graph can have.
//
// CONSTRUCTOR for the graph template class:
// graph( )
// Postcondition: The graph has been initialized with no vertices and no edges.
//
// MODIFICATION MEMBER FUNCTIONS for the graph template class:
// void add_vertex(const Item& label)
// Postcondition: The size of the graph has been increased by adding one new
// vertex. This new vertex has the specified label and no edges.
//
// void add_edge(size_t source, size_t target)
// Precondition: source & target are vertices in the graph
// Postcondition: The graph has all the edges that it originally had, and it
// also has another edge from the specified source to the specified target.
// (If this edge was already present, then the graph is unchanged.)
//
// void remove_edge(size_t soure, size_t target)
// Precondition: source & target are vertices in the graph
// Postcondition: The graph has all the edges that it originally had except
// for the edge from the specified source to the specified target. (If this
// edge was not originally present, then the graph is unchanged.)
//
// Item& operator [ ] (size_t vertex)
// Precondition: vertex is in the graph
// Postcondition: The return value is a reference to the label of the
// specified vertex.
//
// CONSTANT MEMBER FUNCTIONS for the graph template class:
// size_t size( ) const
// Postcondition: The return value is the number of vertices in the graph.
//
// bool is_edge(size_t source, size_t target) const
// Precondition: source & target are vertices in the graph
// Postcondition: The return value is true if the graph has an edge from
// source to target. Otherwise the return value is false.
//
// set neighbors(size_t vertex) const
// Precondition: vertex is in the graph
// Postcondition: The return value is a set that contains all the vertex
// numbers of vertices that are the target of an edge whose source is at
// the specified vertex.
//
// Item operator [ ] (size_t vertex) const
// Precondition: vertex is in the graph
// Postcondition: The return value is a reference to the label of the
// specified vertex.
// NOTE: This function differs from the other operator [ ] because its
// return value is simply a copy of the Item (rather than a reference of
// type Item&). Since this function returns only a copy of the Item, it is
// a const member function.
//
#ifndef MAIN_SAVITCH_GRAPH_H
#define MAIN_SAVITCH_GRAPH_H
#include // Provides size_t
#include // Provides set
#include
namespace main_savitch_15
{
template
class graph
{
public:
// CONSTRUCTOR
graph( );
// 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;
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:
std::vector > edges;
std::vector labels;
};
}
#include "graph.template" // Include the implementation.
#endif
==================================================================
test_graph.cpp
#include "graph.h"
#include
#include
using namespace main_savitch_15;
using namespace std;
template
ostream& operator << (ostream& output, const set& s);
int main ()
{
graph g;
g.add_vertex ("A");
g.add_vertex ("B");
g.add_vertex ("C");
g.add_vertex ("D");
g.add_vertex ("E");
g.add_edge (0, 1);
g.add_edge (0, 3);
g.add_edge (2, 0);
g.add_edge (3, 2);
g.add_edge (4, 3);
for (size_t i = 0; i < g.size(); ++i)
cout << g[i] << ": " << g.neighbors (i) << endl;
return 0;
}
template
ostream& operator << (ostream& output, const set& s)
{
typename set::iterator itr = s.begin();
while (itr != s.end())
{
output << *itr << " ";
++itr;
}
return output;
}// graph.h
// FILE: graph.h (part of the namespace main_savitch_15)
// TEMPLATE CLASS PROVIDED: graph (a class for labeled graphs)
// The vertices of an n-vertex graph are numbered from zero to n-1. Each vertex
// has a label of type Item. It may be any of the C++ built-in types (int,
// char, etc.), or any class with a default constructor and an assignment
// operator. The graph may not have multiple edges.
//
// MEMBER CONSTANTS for the graph template class:
// static const size_t MAXIMUM = ______
// graph::MAXIMUM is the maximum number of vertices that a graph can have.
//
// CONSTRUCTOR for the graph template class:
// graph( )
// Postcondition: The graph has been initialized with no vertices and no edges.
//
// MODIFICATION MEMBER FUNCTIONS for the graph template class:
// void add_vertex(const Item& label)
// Postcondition: The size of the graph has been increased by adding one new
// vertex. This new vertex has the specified label and no edges.
//
// void add_edge(size_t source, size_t target)
// Precondition: source & target are vertices in the graph
// Postcondition: The graph has all the edges that it originally had, and it
// also has another edge from the specified source to the specified target.
// (If this edge was already present, then the graph is unchanged.)
//
// void remove_edge(size_t soure, size_t target)
// Precondition: source & target are vertices in the graph
// Postcondition: The graph has all the edges that it originally had except
// for the edge from the specified source to the specified target. (If this
// edge was not originally present, then the graph is unchanged.)
//
// Item& operator [ ] (size_t vertex)
// Precondition: vertex is in the graph
// Postcondition: The return value is a reference to the label of the
// specified vertex.
//
// CONSTANT MEMBER FUNCTIONS for the graph template class:
// size_t size( ) const
// Postcondition: The return value is the number of vertices in the graph.
//
// bool is_edge(size_t source, size_t target) const
// Precondition: source & target are vertices in the graph
// Postcondition: The return value is true if the graph has an edge from
// source to target. Otherwise the return value is false.
//
// set neighbors(size_t vertex) const
// Precondition: vertex is in the graph
// Postcondition: The return value is a set that contains all the vertex
// numbers of vertices that are the target of an edge whose source is at
// the specified vertex.
//
// Item operator [ ] (size_t vertex) const
// Precondition: vertex is in the graph
// Postcondition: The return value is a reference to the label of the
// specified vertex.
// NOTE: This function differs from the other operator [ ] because its
// return value is simply a copy of the Item (rather than a reference of
// type Item&). Since this function returns only a copy of the Item, it is
// a const member function.
//
#ifndef MAIN_SAVITCH_GRAPH_H
#define MAIN_SAVITCH_GRAPH_H
#include<iostream> // Provides size_t
#include<set> // Provides set
#include<vector>
#include <utility>
namespace main_savitch_15
{
template <class Item>
class graph
{
public:
// CONSTRUCTOR
graph( );
// 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;
bool is_edge(std::size_t source, std::size_t target) const;
std::set<size_t> neighbors(std::size_t vertex) const;
Item operator[ ] (std::size_t vertex) const;
private:
std::vector< std::pair<size_t,size_t> > edges;
std::vector<Item>labels;
};
}
#include "graph.template.h" // Include the implementation.
#endif
// graph.template.h
#ifndef GRAPH_TEMPLATE_H
#define GRAPH_TEMPLATE_H
#include <iostream>
#include <cassert>
#include "graph.h"
using namespace std;
using namespace main_savitch_15;
//Postcondition: The graph has been initialized with no vertices and no edges.
template<class Item>
graph<Item>:: graph( )
{}
//Postcondition: The size of the graph has been increased by adding one new
//vertex. This new vertex has the specified label and no edges.
template <class Item>
void graph<Item>:: add_vertex(const Item& label) {
std::size_t new_vertex;
std::size_t number;
//vector< pair<size_t,size_t> > v;
labels.push_back(label);
/*for (number = 0; number < size(); ++number) {
edges.push_back(make_pair(number,new_vertex));
edges.push_back(make_pair(new_vertex,number));
}
labels[new_vertex] = label;
*/
}
//Precondition: source & target are vertices in the graph
//Postcondition: The graph has all the edges that it originally had, and it
//also has another edge from the specified source to the specified target.template
template <class Item>
void graph<Item>:: add_edge(size_t source, size_t target) {
assert(source < size() && target < size());
//vector< pair > v;
edges.push_back(make_pair(source,target));
}
//Precondition: source & target are vertices in the graph
//Postcondition: The graph has all the edges that it originally had except
//for the edge from the specified source to the specified target.
template <class Item>
void graph<Item>:: remove_edge(size_t source, size_t target) {
assert(source < size() && target < size());
//vector< pair > v;
/*
for(int i = 0; i < size(); i++) {
pair edges(source, target);
v.pop_back(edges);
}*/
if(is_edge(source,target))
{
for(int i=0;i<edges.size();i++)
{
if(edges[i].first == source && edges[i].second == target)
{
edges.erase(edges.begin()+i);
break;
}
}
}
}
//Precondition: vertex is in the graph
//Postcondition: The return value is a reference to the label of the
//specified vertex.
template <class Item>
Item& graph<Item>:: operator [ ] (size_t vertex) {
assert(vertex < size( ));
return labels[vertex];
}
//Postcondition: The return value is the number of vertices in the graph.
template<class Item>
size_t graph<Item>:: size( ) const {
return labels.size();
}
//Precondition: source & target are vertices in the graph
//Postcondition: The return value is true if the graph has an edge from
//source to target. Otherwise the return value is false.
template<class Item>
bool graph<Item>:: is_edge(size_t source, size_t target) const {
assert(source < size() && target < size());
/*
if(source == target) {
return true;
} else {
return false;
}
*/
for(int i=0;i<edges.size();i++)
{
if(edges[i].first == source && edges[i].second == target)
return true;
}
return false;
}
//Precondition: vertex is in the graph
//Postcondition: The return value is a set that contains all the vertex
//numbers of vertices that are the target of an edge whose source is at
//the specified vertex.
template<class Item>
set<size_t> graph<Item>:: neighbors(size_t vertex) const {
std::set<size_t> result;
std::size_t i;
assert(vertex < size( ));
/*
for (i = 0; i < size( ); ++i) {
if (edges.push_back(make_pair(vertex, i)))
result.insert(i);
}*/
for(int i=0;i<edges.size();i++)
if(edges[i].first == vertex)
result.insert(edges[i].second);
return result;
}
//Precondition: vertex is in the graph
//Postcondition: The return value is a reference to the label of the
//specified vertex.
template <class Item>
Item graph<Item>:: operator[ ] (std::size_t vertex) const {
assert(vertex < size());
return labels[vertex];
}
#endif
//end of graph.template.h
//test_graph.cpp
#include "graph.h"
#include <iostream>
#include <set>
using namespace main_savitch_15;
using namespace std;
template<class Item>
ostream& operator << (ostream& output, const set<Item>& s);
int main ()
{
graph<string> g;
g.add_vertex ("A");
g.add_vertex ("B");
g.add_vertex ("C");
g.add_vertex ("D");
g.add_vertex ("E");
g.add_edge (0, 1);
g.add_edge (0, 3);
g.add_edge (2, 0);
g.add_edge (3, 2);
g.add_edge (4, 3);
for (size_t i = 0; i < g.size(); ++i)
cout << g[i] << ": " << g.neighbors (i) << endl;
return 0;
}
template <class Item>
ostream& operator << (ostream& output, const set<Item>& s)
{
typename set<Item>::iterator itr = s.begin();
while (itr != s.end())
{
output << *itr << " ";
++itr;
}
return output;
}
//end of test_graph.cpp
Output:

Can someone please help me fix my code on this assignment that's due in the morning?...
The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...
I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...
Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...
Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every member function must include a description in the header file including precondition and postcondition. Rule 3: every member variable must include a description of what the variable holds. Rule 4: Classes should be capitalized. Bag, not bag. Person, not person. (I know this is different than how the book does it) Rule 5: member variables of classes should be preceded by “m_”. If it’s...
Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...
The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...
Was converting the main into a template but I am recieving errors, what should I be doing? #include<iostream> #include <cstdlib> #include "set.h" using namespace std; int main() { set<Item> list; list.insert(4); list.insert(7); list.insert(9); list.insert(3); cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl; list.erase(4); cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl; } #ifndef SET_H #define SET_H #include<cstdlib> namespace std { template<class Item> class set_item { public: typedef Item value_type; set_item( ) { next...
PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
(C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...