Project 5 is due either at 12:00 midnight of the last day of class. There are no resubmissions or late submissions for Project 5; however, projects which achieve a grade less than 80 will be examined to determine if they deserve a grade great than what was actually received (to a maximum of 80).
You may use all standard libraries available on ecelinux. This includes the Standard Template Library (STL), cstring, etc.
In this sub-project, you will implement one class:
Note that there are no templates used in this project.
This class stores a number of prioritized vertices and edges which connect those vertices. Initially the class has no edges and the priority (a double) of each vertex is equal to its numeric identifier (0 through n − 1). A topological sort is performed in the following manner: at any step of the topological sort where there are more than one vertices with in-degree zero, that vertex with highest priority (smallest numeric value) is chosen next.
The design of the class is up to you: you may use any data structure you see fit.
The constructor takes an argument n which defines a graph with vertices numbered from 0 through n − 1. The vertex i is given an initial priority of i. The default value of n is 10.
The destructor frees up any memory allocated by the constructor.
The copy constructor makes a complete copy of the directed acyclic graph passed in the argument.
The default assignment operator is used.
This class has six accessors:
cout << ">>>>>>>>>"; graph.topological_sort(); cout << "<<<<<<<<<";may be
>>>>>>>>>1-3-2-4-5-7-6-9-0-8<<<<<<<<<Note that the >>>>>>>>> and <<<<<<<<< are a result of the two cout statements, both before and after.
This class has four mutators:
The class has no friends.
Consider testing your code with something like:
#include <iostream> #include "Directed_acyclic_graph.h" using namespace std; int main() { Directed_acyclic_graph graph(4); graph.insert_edge( 0, 2 ); graph.insert_edge( 2, 1 ); graph.insert_edge( 1, 0 ); // should return false graph.insert_edge( 0, 3 ); graph.connected( 0, 1 ); // should return true graph.connected( 1, 0 ); // should return false graph.adjacent( 0, 1 ); // should return false graph.adjacent( 0, 2 ); // should return true graph.adjacent( 1, 0 ); // should return false graph.adjacent( 2, 0 ); // should return false graph.topological_sort(); // prints 0-2-1-3 cout << endl; graph.set_priority( 3, 0.5 ); graph.topological_sort(); // prints 0-3-2-1 cout << endl; return 0; }
Note that if there are no edges in the graph, the topological sort will be a printout of the vertices in order of their priority.
Recall that the order in which member variables are assigned in the copy constructor is the same order in which the member variables are declared in the class definition. Thus, the following would fail:
class Faulty { private: int *array; int towers; public: Fault( int ); Fault( Fault const & ); }; // 'array' is assigned before 'towers' even though it looks like // 'towers' is assigned the value of the argument first. Fault::Fault( int n ): towers( n ), array( new int[towers] ) { // empty } // Similarly, 'array' is assigned before 'towers' even though it looks like // 'towers' is assigned the value of 'faulty.towers' first. Fault::Fault( int n ): Fault::Fault( Fault const &faulty ): towers( faulty.towers ), array( new int[towers] ) { for ( int i = 0; i < towers; ++i ) { array[i] = faulty.array[i]; } }