# Assignment 2 – Algorithms on Directed Graphs (35%) Overview You task is to code

Assignment 2 – Algorithms on Directed Graphs (35%)
Overview
You task is to code a small collection of graph algorithms, based on your directed_graph class for assignment 1 (all vertices and edges have positive weights):
• Shortest paths.
• Strongly connected components.
• Topological sort.
Besides the above, the class should offer an operation to solve the following open question:
• Consider each vertex as a city, vertex weight as the city’s population, and the weight of each directed edge as the cost of delivering any amount of goods from one city to another. Given a city that plans to deliver goods to all other cities, can you find a way to deliver the goods to every other city with the minimum average-delivery-cost per person? Your function should return this minimum cost-per-person.

For example, in the above graph, A and C have the population of 800 and 400, respectively, and it takes the cost of 900 to deliver any amout of goods from A to C while there is no way to deliver the good directly from C to A.
Suppose A -> B, A -> C, A -> C -> D, A -> B -> E are the paths you use to deliver goods from A to other cities, your function should return ( cost(A->B) + cost (A->C) + cost(C->D) + cost(B->E) ) / (the total population of B,C,D,E) = (600+900+4000+3000)/(300+400+710+221) = 5. Your tasks is to figure out the minimal return possible by determining the optimal paths for the delivery. Note that, you can deliver any amout of goods at a fixed cost between two adjacent cities; therefore, you only need to countcost(A-C) and cost(A->B) once in this example.
The Code
You are provided with the following:
• directed_graph_algorithms.cpp. The four methods present in the skeleton are the entry point for the tests. As with the first assignment, you should be able to complete the assignment within the directed_graph_algorithms.cpp file. You main task is to implement the four functions, which are not part of the class.
• directed_graph.hpp. Please copy your submission for assignment 1 to this file. The fully working implementation of a directed graph class in this file will be reused by directed_graph_algorithms.cpp.
• main.cpp. This is where you can do any testing you like.
The “run” button will compile and execute main.cpp, the “mark” button will run directed_graph_algorithms.cpp against the tests.
You may modify any of these files as you see fit, e.g., adding extra functions and extra classes, as long as the tests still execute.

1. directed_graph.hpp //This is not a something to implement, just use for the structure

#ifndef DIRECTED_GRAPH_H
#define DIRECTED_GRAPH_H

#include
#include
#include
#include
#include
#include
#include
#include #include
#include
#include
// include more libraries here if you need to

#include
#include
#include
#include
#include
#include
#include
#include #include
#include
#include

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

/*
the vertex class
*/
template
class vertex {

public:
int id;
T weight;

vertex(int v_id, T v_weight) : id(v_id), weight(v_weight) { // constructor
}
// add more functions here if you need to
};

/*
the graph class
*/

template
class directed_graph {

private:

unordered_map vertice_list;

// adj_list stores all edges in the graph, as well as the edges’ weights.
// each element is a pair(vertex, the neighbours of this vertex)
// each neighbour is also a pair (neighbour_vertex, weight for edge from vertex to neighbour_vertex)

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( int , int ) ; //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&) ; //Returns number of edges coming in to a vertex.
size_t out_degree(const int&) ; //Returns the number of edges leaving a vertex.
size_t degree(const int&) ; //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 tree 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 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 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 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.

template
directed_graph::directed_graph() {
}

template
directed_graph::~directed_graph() {}

template
bool directed_graph::contains(const int& u_id) const {
if(vertice_list.find(u_id) != vertice_list.end()){ // if there’s a vertex in the vertice list

return true;
}
return false;
}

template
if(!contains(u.id)){
vertice_list.insert({u.id, u.weight}); // step 1: add to all_vertices
}
//spent a week to make it working simply because forgot the num_verticle
}

template
void directed_graph::remove_vertex(const int& u_id) {
vertice_list.erase(u_id); // step 1: remove the vertex from all_vertices

adj_list.erase(u_id); // step 2: remove all edges starting from this vertex

for (auto& x: adj_list){ // Step 3: iterate adj_list to remove all edges ending at this vertex
x.second.erase(u_id);}

}

template
void directed_graph::add_edge(const int& u_id, const int& v_id, const T& uv_weight) {
if(contains(u_id) && contains(v_id)){ // add the edge only if both vertices are in the graph and the edge is not in the graph
}
}
}

template
void directed_graph::remove_edge(const int& u_id, const int& v_id) {
}
}

template // return true if u_id is adjacent to v_id
bool directed_graph::adjacent( int u_id, int v_id) {
if(contains(u_id) && contains(v_id)){ //both vertex exists
return true;

}
}
return false;
}

template // return number of incoming edges into vertex, i finds u_id
size_t directed_graph::in_degree(const int& u_id) {
size_t num=0;
//if vertex exist
if(contains(u_id)){
for(int i=1; vertice_list.size() >= i; i++) { // increase i to the size of adj_list
num = num+1; //increase 1
}
}
}
return num;
}

template
size_t directed_graph::out_degree(const int& u_id) { // return number of outgoing edges into vertex
// u_id finds i
size_t num=0;
//if vertex exist
if(contains(u_id)){
for(int i=1; vertice_list.size() >= i; i++) { // increase i to the size of adj_list
num = num+1; //increase 1
}
}
}
return num;
}

template
size_t directed_graph::degree(const int& u_id) {
size_t num = 0;
num = in_degree(u_id) + out_degree(u_id);
return num; }

template
size_t directed_graph::num_vertices() const {
return vertice_list.size();
}

template
size_t directed_graph::num_edges() const {
size_t count = 0;
for (auto& x: adj_list){ // x == pair>
count += x.second.size(); // x.second == unordered_map
}
return count;
}

template
vector> directed_graph::get_vertices() {
vector> v;
for(auto x: vertice_list){ // iterate vertex_weight to get all vertex_ids
v.push_back(vertex(x.first, x.second)); // and then build a vertex class for each vertex_id
}
return v;

}

template
vector> directed_graph::get_neighbours(const int& u_id) {
vector> v;
if(contains(u_id)){ // first make sure the vertex is in the graph
v.push_back(vertex(x.first, vertice_list[x.first])); // insert vector of vector(neighbour) into v
}
}
return v;
}

template
vector> directed_graph::get_second_order_neighbours(const int& u_id) {
// 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.
// First step, mark u as visited and
// Second step, find the 1stN of u, without marking them as visited
// Last step, find 1st N of 1stN
// first neighbor

return vector>();

}

template
bool directed_graph::reachable(const int& u_id, const int& v_id) const {

return false; }

template
bool directed_graph::contain_cycles() const { return false; }

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

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

template
directed_graph directed_graph::out_tree(const int& u_id) { return directed_graph(); }

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

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

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

template
vector> directed_graph::significance_sorting() { return vector>(); }

#endif

2. directed_graph_algorithms.cpp class //this is a assignment
#include
#include
#include
#include
#include
#include
#include
#include
#include #include
#include
#include

#include
#include
#include
#include
#include #include
#include
#include

#include “directed_graph.hpp”

using namespace std;

/*
* Computes the shortest distance from u to v in graph g.
* The shortest path corresponds to a sequence of vertices starting from u and ends at v,
* which has the smallest total weight of edges among all possible paths from u to v.
*/
template
vector> shortest_path(directed_graph g, int u_id, int v_id) {
vector> shortest_path;

return shortest_path;

}

return shortest_path;

}

/*
* Computes the strongly connected components of the graph.
* A strongly connected component is a subset of the vertices
* such that for every pair u, v of vertices in the subset,
* v is reachable from u and u is reachable from v.
*/

template //Tarjan’s algorithm
vector>> strongly_connected_components(directed_graph g) {
vector>> scc;

return scc;

}

/*
* Computes a topological ordering of the vertices.
* For every vertex u in the order, and any of its
* neighbours v, v appears later in the order than u.
* You will be given a DAG as the argument.
*/
template
vector> topological_sort(directed_graph g) {

return vector>();

}

/*
* Computes the lowest cost-per-person for delivery over the graph.
* u is the source vertex, which send deliveries to all other vertices.
* vertices denote cities; vertex weights denote cities’ population;
* edge weights denote the fixed delivery cost between cities, which is irrelevant to
* the amount of goods being delivered.
*/
template
T low_cost_delivery(directed_graph g, int u_id) {

return 0;

}