#ifndef _DIGRAPH_H #define _DIGRAPH_H #include #include "Hash.h" // directed graph // // restriction: from any vertex u to any vertex w there can be at most one edge // // for suggestions regarding implementation of the class, see notes // after the class definition class Digraph { public: ~Digraph(); // destructor Digraph(); // constructor Digraph(const Digraph & g); // copy constructor int add_vertex(); // adds a vertex to the graph, returns an integer id for vertex // should run in O(1) time int add_vertex(int id); // adds a vertex to the graph, using the given id, which should not be in use // should run in O(1) time void add_edge(int from, int to); // Adds a directed edge from vertex with id "from" to vertex with id "to". // Adds the vertices first, if necessary (i.e., if they don't exist). // should run in O(1) time int vertex_exists(int v) const; // return whether exists vertex with id v in the graph // should run in O(1) time int edge_exists(int from, int to) const; // return whether exists edge from vertex with "from" to one with id "to" // should run in O(1) time void remove_vertex(int v); // remove vertex with id v, if one exists, // after removing all edges into and out of the vertex // should run in O(#nbrs(v)) time void remove_edge(int from, int to); // remove edge from vertex "from" to "to" // should run in O(1) time Array vertices() const; // return array of ids of the vertices in the graph // should run in O(#vertices) time Array out_nbrs(int v) const; // return array of ids of the out-neighbors of the vertex with id v // should run in O(#out_nbrs) time Array in_nbrs(int v) const; // return array of ids of the in-neighbors of the vertex with id v // should run in O(#in_nbrs) time void clear(); // make graph empty (no vertices, no edges) Digraph& operator=(const Digraph& g); // assignment operator (deep copy) friend std::ostream& operator<<(std::ostream& out, const Digraph & g); // print function private: // your code here }; #endif // implementation suggestions // // Maintain a counter // int max_vertex_id; // with the maximum vertex id used so far. // Use it to make sure vertex id's handed out are unique. // // Keep a hash table of hash tables such as // // HashTable > ids_of_out_nbrs; // vertex ids of out neighbors // // So that the keys of ids_of_out_nbrs hash table are the ids of the vertices. // For any vertex id v, ids_of_out_nbrs[v] // should be a hash table whose keys are the ids of the out-neighbors of v. // // Likewise, maintain a hash table // // HashTable > ids_of_in_nbrs; // vertex ids of in neighbors // // for the in-neigbors. //