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 more libraries here if you need to

#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

unordered_map

// 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

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

vector

vector

// 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

vector

directed_graph

vector

vector

vector

vector

};

// 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

}

template

directed_graph

template

bool directed_graph

if(vertice_list.find(u_id) != vertice_list.end()){ // if there’s a vertex in the vertice list

return true;

}

return false;

}

template

void directed_graph

if(!contains(u.id)){

vertice_list.insert({u.id, u.weight}); // step 1: add to all_vertices

adj_list[u.id]=std::unordered_map

}

//spent a week to make it working simply because forgot the num_verticle

}

template

void directed_graph

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

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

if(adj_list[u_id].find(v_id)==adj_list[u_id].end()){ //if no edge between them

adj_list[u_id].insert({v_id, uv_weight});

}

}

}

template

void directed_graph

if(adj_list[u_id].find(v_id) != adj_list[u_id].end()){ // if edge between them exists

adj_list[u_id].erase(v_id); //remove edge

}

}

template

bool directed_graph

if(contains(u_id) && contains(v_id)){ //both vertex exists

if(adj_list[u_id].find(v_id)!=adj_list[u_id].end()){ // a edge between them exist

return true;

}

}

return false;

}

template

size_t directed_graph

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

if(adj_list[i].find(u_id) != adj_list[i].end()){ // if i finds u_id

num = num+1; //increase 1

}

}

}

return num;

}

template

size_t directed_graph

// 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

if(adj_list[u_id].find(i) != adj_list[i].end()){ // if u_id finds i

num = num+1; //increase 1

}

}

}

return num;

}

template

size_t directed_graph

size_t num = 0;

num = in_degree(u_id) + out_degree(u_id);

return num; }

template

size_t directed_graph

return vertice_list.size();

}

template

size_t directed_graph

size_t count = 0;

for (auto& x: adj_list){ // x == pair

count += x.second.size(); // x.second == unordered_map

}

return count;

}

template

vector

vector

for(auto x: vertice_list){ // iterate vertex_weight to get all vertex_ids

v.push_back(vertex

}

return v;

}

template

vector

vector

if(contains(u_id)){ // first make sure the vertex is in the graph

for (auto x: adj_list[u_id]){ // adj_list[u_id] is an unordered_map

v.push_back(vertex

}

}

return v;

}

template

vector

// 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

return false; }

template

bool directed_graph

template

vector

template

vector

template

directed_graph

template

vector

template

vector

template

vector

template

vector

#endif

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

#include

#include

#include

#include

#include

#include

#include

#include

#include
#include

#include

#include