The task is to implement a directed_graph class, where each node/edge has a weight. The class should offer a reasonably effective suite of operations.C++ Programming

The task is to implement a directed_graph class, where each node/edge has a weight.

The class should offer a reasonably effective suite of operations. Some (but not all) of basic operations are:

  • Adding and removing nodes and edges (with weights);

  • Depth-first and breadth-first traversals;

  • Computing the minimum spanning tree (MST);

  • Pre-order, in-order, and post-order traversals of the MST

 

#ifndef DIRECTED_GRAPH_H

#define DIRECTED_GRAPH_H

 

#include

#include

#include

// include more libraries here if you need to

 

using namespace std; // the standard namespace are here just in case.

 

/*

                the vertex class

*/

template

class vertex {

 

public:

                int id;

                T weight;

 

                vertex(int x, T y) : id(x), weight(y) {}

 

                // add more functions here if you need to

};

 

/*

                the graph class

*/

template

class directed_graph {

 

private:

 

                //You will need to add some data members here

                //to actually represent the graph internally,

                //and keep track of whatever you need to.

 

public:

 

                directed_graph(); //A constructor for directed_graph. The graph should start empty.

                ~directed_graph(); //A destructor. Depending on how you do things, this may not be necessary.

 

                bool contains(const int&) const; //Returns true if the graph contains the given vertex_id, false otherwise.

                bool adjacent(const int&, const int&) const; //Returns true if the first vertex is adjacent to the second, false otherwise.

 

                void add_vertex(const vertex&); //Adds the passed in vertex to the graph (with no edges).

                void add_edge(const int&, const int&, const T&); //Adds a weighted edge from the first vertex to the second.

 

                void remove_vertex(const int&); //Removes the given vertex. Should also clear any incident edges.

                void remove_edge(const int&, const int&); //Removes the edge between the two vertices, if it exists.

 

                size_t in_degree(const int&) const; //Returns number of edges coming in to a vertex.

                size_t out_degree(const int&) const; //Returns the number of edges leaving a vertex.

                size_t degree(const int&) const; //Returns the degree of the vertex (both in edges and out edges).

 

                size_t num_vertices() const; //Returns the total number of vertices in the graph.

                size_t num_edges() const; //Returns the total number of edges in the graph.

 

                vector get_vertices(); //Returns a vector containing all the vertices.

                vector get_neighbours(const int&); //Returns a vector containing all the vertices reachable from the given vertex. The vertex is not considered a neighbour of itself.

                vector get_second_order_neighbours(const int&); // Returns a vector containing all the second_order_neighbours (i.e., neighbours of neighbours) of the given vertex.

                                                                                                                                                                                                                                                  // A vector cannot be considered a second_order_neighbour of itself.

                bool reachable(const int&, const int&) const; //Returns true if the second vertex is reachable from the first (can you follow a path of out-edges to get from the first to the second?). Returns false otherwise.

                bool contain_cycles() const; // Return true if the graph contains cycles (there is a path from any vertices directly/indirectly to itself), false otherwise.

 

                vector depth_first(const int&); //Returns the vertices of the graph in the order they are visited in by a depth-first traversal starting at the given vertex.

                vector breadth_first(const int&); //Returns the vertices of the graph in the order they are visisted in by a breadth-first traversal starting at the given vertex.

 

                directed_graph out_tree(const int&); //Returns a spanning tree of the graph starting at the given vertex using the out-edges. This means every vertex in the tree is reachable from the root.

 

                vector pre_order_traversal(const int&, directed_graph&); // returns the vertices in the visiting order of a pre-order traversal of the minimum spanning tree starting at the given vertex.

                vector in_order_traversal(const int&, directed_graph&); // returns the vertices in the visiting order of an in-order traversal of the minimum spanning tree starting at the given vertex.

                vector post_order_traversal(const int&, directed_graph&); // returns the vertices in ther visitig order of a post-order traversal of the minimum spanning tree starting at the given vertex.

 

                vector significance_sorting(); // Return a vector containing a sorted list of the vertices in descending order of their significance.

 

};

 

// Define all your methods down here (or move them up into the header, but be careful you don't double up). If you want to move this into another file, you can, but you should #include the file here.

// Although these are just the same names copied from above, you may find a few more clues in the full method headers.

// Note also that C++ is sensitive to the order you declare and define things in - you have to have it available before you use it.

Attachments:

Instructions Files

C++ Programming Experts

expert
Eric Bell
C++ Programming

88 Answers

expert
Dinh Hong P.
C++ Programming

20 Answers

View More Experts
Disclaimer

The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.

Get Free Quote!

252 Experts Online