#ifndef _GRAPH_H #define _GRAPH_H #include #include "Digraph.h" // undirected graph // // restriction: at most one edge between any pair of vertices // we implement the graph G=(V,E) // using the directed graph D=(V,E') // where E' = { (i,j) : {i,j} in E and i <= j } // class Graph { private: Digraph D; void sort(int& i, int& j) const { if (i > j) { int t = i; i = j; j = t; } } public: ~Graph() {}; // destructor Graph() {}; // constructor Graph(const Graph & g) : D(g.D) {} // copy constructor int add_vertex() { // adds vertex, returns non-zero integer id for vertex return D.add_vertex(); } int add_vertex(int id) { // adds vertex using given id (if not already there) return D.add_vertex(id); } void add_edge(int from, int to) { // adds edge, vertices from and to must already exist sort(from, to); D.add_edge(from, to); } int edge_exists(int from, int to) const { // return whether exists edge from "from" to "to" sort(from, to); return D.edge_exists(from, to); } int vertex_exists(int v) const { // is there a vertex with id v return D.vertex_exists(v); } void remove_vertex(int v) { // remove any vertex with id v D.remove_vertex(v); } void remove_edge(int from, int to) { // remove edge between "from" and "to" sort(from, to); D.remove_edge(from, to); } Array vertices() const { // return array of vertex ids in graph return D.vertices(); } Array nbrs(int v) const { // return array of ids of neighbors Array in = D.in_nbrs(v); Array out = D.out_nbrs(v); for (int i = 0; i < out.size(); ++i) { in[in.size()] = out[i]; } return in; } void clear() { // make graph empty (no vertices, no edges) D.clear(); } Graph& operator=(const Graph& g) { // assignment operator D = g.D; } friend std::ostream& operator<<(std::ostream& out, const Graph & g); }; std::ostream& operator<<(std::ostream& out, const Graph & g) { out << g.D; } #endif