Overview

Your task is to implement a directed graph class, where each node/edge has a weight.

For example, the following weighted directed graph has five nodes { A, B, C, D, E } and seven edges { (A,B),(A,C), (B, E), (C, D), (D,A),(D,C),(D, E)}. Node C has a weight of 400, and its incident edges: (A, C) has the weight of 9; (D, C) has the weight of 7; (C, D) has the weight of 4. see image.

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 a tree rooted by the given vertex (the tree may not contain all vertices of the graph);
  • Pre-order, in-order, and post-order traversals of the tree;

Besides the above, the class should offer an operation to solve the following open question:

  • Consider each node as a twitter user, the node's weight as the number of followers of the user, and the weight of each directed edge as how many times a user liked another user. For example, in the above graph, A and C have 800 and 400 followers, respectively; A liked C nine times while C never liked A. Can you find a way to assess the importance or popularity of the users, so as to rank the users in descending order? Note that no single best answer may exist to this question.

You need to carefully consider:

  • Whether it is necessary to declare other classes apart from the directed graph class.

The Code

You are provided with a directed_graph.hpp file, which includes most (if not all) of the basic definitions you will need. You made add extra methods, classes, structs, etc., as long as they don't interfere with the existing definitions.

You have also been provided with a main.cpp for ad-hoc testing purposes. main.cpp file does not form part of the assignment, and will not be marked. You can do anything you like with it. When the "run" button is pressed, it will compile and run main.cpp. When the "mark" button is pressed, your code will be run against the tests. Note that the testing code can only mark your code when your code does not cause a program crash (e.g. a segfault). If you get any errors during compiling, make sure you fix that problem first!

#ifndef DIRECTED_GRAPH_H
#define DIRECTED_GRAPH_H

#include< iostream>
#include< string>
#include< vector>
// include more libraries here if you need to

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

/*
the vertex class
*/
template < typename T>
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 < typename T>
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< T>&); //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< vertex< T>> get_vertices(); //Returns a vector containing all the vertices.
vector< vertex< T>> 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< vertex< T>> 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< vertex< T>> 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< vertex< T>> 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< T> out_tree(const int&); //Returns a tree starting at the given vertex using the out-edges. This means every vertex in the tree is reachable from the root.

vector< vertex< T>> pre_order_traversal(const int&, directed_graph< T>&); // returns the vertices in the visiting order of a pre-order traversal of the tree starting at the given vertex.
vector< vertex< T>> in_order_traversal(const int&, directed_graph< T>&); // returns the vertices in the visiting order of an in-order traversal of the tree starting at the given vertex.
vector< vertex< T>> post_order_traversal(const int&, directed_graph< T>&); // returns the vertices in ther visitig order of a post-order traversal of the tree starting at the given vertex.

vector< vertex< T>> 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.

template < typename T>
directed_graph< T>::directed_graph() {}

template < typename T>
directed_graph< T>::~directed_graph() {}

template < typename T>
bool directed_graph< T>::contains(const int& u_id) const { return false; }

template < typename T>
bool directed_graph< T>::adjacent(const int& u_id, const int& v_id) const { return false; }

template < typename T>
void directed_graph< T>::add_vertex(const vertex< T>& u) {}

template < typename T>
void directed_graph< T>::add_edge(const int& u_id, const int& v_id, const T& weight) {}

template < typename T>
void directed_graph< T>::remove_vertex(const int& u_id) {}

template < typename T>
void directed_graph< T>::remove_edge(const int& u_id, const int& v_id) {}

template < typename T>
size_t directed_graph< T>::in_degree(const int& u_id) const { return 0; }

template < typename T>
size_t directed_graph< T>::out_degree(const int& u_id) const { return 0; }

template < typename T>
size_t directed_graph< T>::degree(const int& u_id) const { return 0; }

template < typename T>
size_t directed_graph< T>::num_vertices() const { return 0; }

template < typename T>
size_t directed_graph< T>::num_edges() const { return 0; }

template < typename T>
vector< vertex< T>> directed_graph< T>::get_vertices() { return vector< vertex< T>>(); }

template < typename T>
vector< vertex< T>> directed_graph< T>::get_neighbours(const int& u_id) { return vector< vertex< T>>(); }

template < typename T>
vector< vertex< T>> directed_graph< T>::get_second_order_neighbours(const int& u_id) { return vector< vertex< T>>(); }

template < typename T>
bool directed_graph< T>::reachable(const int& u_id, const int& v_id) const { return false; }

template < typename T>
bool directed_graph< T>::contain_cycles() const { return false; }

template < typename T>
vector< vertex< T>> directed_graph< T>::depth_first(const int& u_id) { return vector< vertex< T>>(); }

template < typename T>
vector< vertex< T>> directed_graph< T>::breadth_first(const int& u_id) { return vector< vertex< T>>(); }

template < typename T>
directed_graph< T> directed_graph< T>::out_tree(const int& u_id) { return directed_graph< T>(); }

template < typename T>
vector< vertex< T>> directed_graph< T>::pre_order_traversal(const int& u_id, directed_graph< T>& mst) { return vector< vertex< T>>(); }

template < typename T>
vector< vertex< T>> directed_graph< T>::in_order_traversal(const int& u_id, directed_graph< T>& mst) { return vector< vertex< T>>(); }

template < typename T>
vector< vertex< T>> directed_graph< T>::post_order_traversal(const int& u_id, directed_graph< T>& mst) { return vector< vertex< T>>(); }

template < typename T>
vector< vertex< T>> directed_graph< T>::significance_sorting() { return vector< vertex< T>>(); }

#endif
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.