|
| 1 | +#ifndef CH4_DIGRAPH_H |
| 2 | +#define CH4_DIGRAPH_H |
| 3 | + |
| 4 | +#include <forward_list> |
| 5 | +#include <vector> |
| 6 | +#include <stdexcept> |
| 7 | +#include <fstream> |
| 8 | +#include <string> |
| 9 | +#include <stack> |
| 10 | +#include <ostream> |
| 11 | +#include <sstream> |
| 12 | + |
| 13 | +using std::forward_list; |
| 14 | +using std::vector; |
| 15 | +using std::runtime_error; |
| 16 | +using std::string; |
| 17 | +using std::fstream; |
| 18 | +using std::to_string; |
| 19 | +using std::stack; |
| 20 | +using std::distance; |
| 21 | +using std::ostream; |
| 22 | +using std::stringstream; |
| 23 | + |
| 24 | +/** |
| 25 | + * The {@code Digraph} class represents a directed graph of vertices |
| 26 | + * named 0 through <em>V</em> - 1. |
| 27 | + * It supports the following two primary operations: add an edge to the digraph, |
| 28 | + * iterate over all of the vertices adjacent from a given vertex. |
| 29 | + * Parallel edges and self-loops are permitted. |
| 30 | + * <p> |
| 31 | + * This implementation uses an adjacency-lists representation, which |
| 32 | + * is a vertex-indexed array of {@link Bag} objects. |
| 33 | + * All operations take constant time (in the worst case) except |
| 34 | + * iterating over the vertices adjacent from a given vertex, which takes |
| 35 | + * time proportional to the number of such vertices. |
| 36 | + * <p> |
| 37 | + * For additional documentation, |
| 38 | + * see <a href="https://algs4.cs.princeton.edu/42digraph">Section 4.2</a> of |
| 39 | + * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. |
| 40 | + * |
| 41 | + * @author Robert Sedgewick |
| 42 | + * @author Kevin Wayne |
| 43 | + */ |
| 44 | +class Digraph { |
| 45 | +public: |
| 46 | + /** |
| 47 | + * Initializes an empty digraph with <em>V</em> vertices. |
| 48 | + * |
| 49 | + * @param V the number of vertices |
| 50 | + * @throws IllegalArgumentException if {@code V < 0} |
| 51 | + */ |
| 52 | + Digraph(int V) : V(V), E(0), indegree(V), adj(V) { |
| 53 | + if (V < 0) throw runtime_error("Number of vertices in a Digraph must be nonnegative"); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Initializes a digraph from the specified input stream. |
| 58 | + * The format is the number of vertices <em>V</em>, |
| 59 | + * followed by the number of edges <em>E</em>, |
| 60 | + * followed by <em>E</em> pairs of vertices, with each entry separated by whitespace. |
| 61 | + * |
| 62 | + * @param in the input stream |
| 63 | + * @throws IllegalArgumentException if the endpoints of any edge are not in prescribed range |
| 64 | + * @throws IllegalArgumentException if the number of vertices or edges is negative |
| 65 | + * @throws IllegalArgumentException if the input stream is in the wrong format |
| 66 | + */ |
| 67 | + Digraph(string in): E(0) { |
| 68 | + fstream file(in); |
| 69 | + file >> V; |
| 70 | + if (V < 0) throw runtime_error("number of vertices in a Digraph must be nonnegative"); |
| 71 | + indegree.resize(V); |
| 72 | + adj.resize(V); |
| 73 | + int tmpE; |
| 74 | + file >> tmpE; |
| 75 | + if (tmpE < 0) throw runtime_error("number of edges in a Digraph must be nonnegative"); |
| 76 | + int v, w; |
| 77 | + for (int i = 0; i < tmpE; i++) { |
| 78 | + file >> v >> w; |
| 79 | + addEdge(v, w); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Initializes a new digraph that is a deep copy of the specified digraph. |
| 85 | + * |
| 86 | + * @param G the digraph to copy |
| 87 | + */ |
| 88 | + Digraph(const Digraph &G) : Digraph(G.getV()) { |
| 89 | + E = G.getE(); |
| 90 | + for (int v = 0; v < V; v++) |
| 91 | + indegree[v] = G.getindegree(v); |
| 92 | + for (int v = 0; v < G.getV(); v++) { |
| 93 | + // reverse so that adjacency list is in same order as original |
| 94 | + stack<int> reverse; |
| 95 | + for (int w : G.adj[v]) { |
| 96 | + reverse.push(w); |
| 97 | + } |
| 98 | + while (!reverse.empty()) { |
| 99 | + auto w = reverse.top(); |
| 100 | + reverse.pop(); |
| 101 | + adj[v].push_front(w); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the number of vertices in this digraph. |
| 108 | + * |
| 109 | + * @return the number of vertices in this digraph |
| 110 | + */ |
| 111 | + int getV() const { |
| 112 | + return V; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns the number of edges in this digraph. |
| 117 | + * |
| 118 | + * @return the number of edges in this digraph |
| 119 | + */ |
| 120 | + int getE() const { |
| 121 | + return E; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Adds the directed edge v→w to this digraph. |
| 126 | + * |
| 127 | + * @param v the tail vertex |
| 128 | + * @param w the head vertex |
| 129 | + * @throws IllegalArgumentException unless both {@code 0 <= v < V} and {@code 0 <= w < V} |
| 130 | + */ |
| 131 | + void addEdge(int v, int w) { |
| 132 | + validateVertex(v); |
| 133 | + validateVertex(w); |
| 134 | + adj[v].push_front(w); |
| 135 | + indegree[w]++; |
| 136 | + E++; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Returns the vertices adjacent from vertex {@code v} in this digraph. |
| 141 | + * |
| 142 | + * @param v the vertex |
| 143 | + * @return the vertices adjacent from vertex {@code v} in this digraph, as an iterable |
| 144 | + * @throws IllegalArgumentException unless {@code 0 <= v < V} |
| 145 | + */ |
| 146 | + forward_list<int> getadj(int v) const { |
| 147 | + validateVertex(v); |
| 148 | + return adj[v]; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Returns the number of directed edges incident from vertex {@code v}. |
| 153 | + * This is known as the <em>outdegree</em> of vertex {@code v}. |
| 154 | + * |
| 155 | + * @param v the vertex |
| 156 | + * @return the outdegree of vertex {@code v} |
| 157 | + * @throws IllegalArgumentException unless {@code 0 <= v < V} |
| 158 | + */ |
| 159 | + int outdegree(int v) const { |
| 160 | + validateVertex(v); |
| 161 | + return distance(adj[v].begin(), adj[v].end()); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Returns the number of directed edges incident to vertex {@code v}. |
| 166 | + * This is known as the <em>indegree</em> of vertex {@code v}. |
| 167 | + * |
| 168 | + * @param v the vertex |
| 169 | + * @return the indegree of vertex {@code v} |
| 170 | + * @throws IllegalArgumentException unless {@code 0 <= v < V} |
| 171 | + */ |
| 172 | + int getindegree(int v) const { |
| 173 | + validateVertex(v); |
| 174 | + return indegree[v]; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Returns the reverse of the digraph. |
| 179 | + * |
| 180 | + * @return the reverse of the digraph |
| 181 | + */ |
| 182 | + Digraph *reverse() { |
| 183 | + Digraph *reverse = new Digraph(V); |
| 184 | + for (int v = 0; v < V; v++) { |
| 185 | + for (int w : getadj(v)) { |
| 186 | + reverse->addEdge(w, v); |
| 187 | + } |
| 188 | + } |
| 189 | + return reverse; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Returns a string representation of the graph. |
| 194 | + * |
| 195 | + * @return the number of vertices <em>V</em>, followed by the number of edges <em>E</em>, |
| 196 | + * followed by the <em>V</em> adjacency lists |
| 197 | + */ |
| 198 | + friend ostream &operator<<(ostream &stream, const Digraph &dg) { |
| 199 | + stringstream ss; |
| 200 | + ss << dg.V << " vertices, " << dg.getE() << " edges" << std::endl; |
| 201 | + for (int v = 0; v < dg.V; ++v) { |
| 202 | + ss << v << ": "; |
| 203 | + for (auto w: dg.getadj(v)) |
| 204 | + ss << w << " "; |
| 205 | + ss << std::endl; |
| 206 | + } |
| 207 | + stream << ss.str(); |
| 208 | + return stream; |
| 209 | + } |
| 210 | + |
| 211 | +private: |
| 212 | + // throw an IllegalArgumentException unless {@code 0 <= v < V} |
| 213 | + void validateVertex(int v) const { |
| 214 | + if (v < 0 || v >= V) |
| 215 | + throw runtime_error("vertex " + to_string(v) + " is not between 0 and " + to_string(V - 1)); |
| 216 | + } |
| 217 | + |
| 218 | +private: |
| 219 | + int V; // number of vertices in this digraph |
| 220 | + int E; // number of edges in this digraph |
| 221 | + vector<forward_list<int>> adj; // adj[v] = adjacency list for vertex v |
| 222 | + vector<int> indegree; // indegree[v] = indegree of vertex v |
| 223 | +}; |
| 224 | + |
| 225 | +#endif //CH4_DIGRAPH_H |
0 commit comments