/* * Class Graph for Assignment 5a in CS 202, November 2002. I have * followed the style of putting "one-liner" function definitions into * the header file. (Note that a "one-liner" might be as much as * 3-4 simple lines with no loops * * Notice that dfs_reach and dfs_all are almost identical. I think a * more sophisticated solution would use inheritance, which we have * not covered in this class. Also, I used a map with value type bool * for visited there. A more normal solution would use the STL set * class for this, but we haven't covered that in class. */ #ifndef GRAPH_ #define GRAPH_ #include #include #include #include // For my solution with DFS using namespace std; typedef string VertexName; typedef float Weight; const Weight WEIGHT_NOT_FOUND = -1.0; // impossible weight missing edge class Graph { private: struct VertexAndWeight { VertexName to; // successor vertex Weight wt; // weight of edge to vertex to VertexAndWeight (const VertexName& x, Weight y) { to = x; wt = y; } // two-parameter constructor // It turns out we can need to define == bool operator==(const VertexAndWeight& rhs) const { return to == rhs.to && wt == rhs.wt;} }; // struct VertexAndWeight typedef map > vmap; typedef vmap::iterator vmapIterator; typedef pair > vpair; vmap vertexMap; public: class iterator; friend class iterator; class iterator { friend class Graph; private: vmapIterator theItr; public: VertexName operator*() const; VertexName operator++ (int); bool operator==(const iterator& rhs) const; bool operator!=(const iterator& rhs) const; }; // class iterator class dfs_reach_iterator; friend class dfs_reach_iterator; class dfs_reach_iterator { friend class Graph; private: stack dfsStack; // use a stack instead of recursion map visited; vmap graphMap; bool atEnd; dfs_reach_iterator (const VertexName& start, vmap g); // constructor public: // 0-argument constructor--makes empty one dfs_reach_iterator(): atEnd(false) {} // Returns the VertexName that this dfs_reach_iterator is positioned at VertexName operator*() const { return dfsStack.top(); } // operator* dfs_reach_iterator operator++ (int); bool operator==(const dfs_reach_iterator& rhs) const; bool operator!=(const dfs_reach_iterator& rhs) const; }; // class dfs_reach_iterator class dfs_all_iterator; friend class dfs_all_iterator; class dfs_all_iterator { friend class Graph; private: stack dfsStack; // use a stack instead of recursion map visited; vmap graphMap; bool atEnd; dfs_all_iterator (const VertexName& start, vmap g); // constructor public: // 0-argument constructor--makes empty one dfs_all_iterator(): atEnd(false) {} // Returns the VertexName that this dfs_all_iterator is positioned at VertexName operator*() const { return dfsStack.top(); } // operator* dfs_all_iterator operator++ (int); bool operator==(const dfs_all_iterator& rhs) const; bool operator!=(const dfs_all_iterator& rhs) const; }; // class dfs_all_iterator // Constructor for empty Graph Graph() { } // Returns the number of vertices in this Graph unsigned int vertexCount() const { return vertexMap.size(); } // Returns true has been returned if this Graph contains no // vertices. Otherwise, false is returned. bool empty() { return vertexMap.empty(); } // Returns an iterator positioned at the beginning of this Graph. iterator begin() { iterator temp; temp.theItr = vertexMap.begin(); return temp; } // begin // Returns an iterator positioned at the end of this Graph. iterator end() { iterator temp; temp.theItr = vertexMap.end(); return temp; } unsigned int edgeCount() const; Weight edgeWeight (const VertexName& v1, const VertexName& v2) const; bool containsEdge (const VertexName& v1, const VertexName& v2) const; bool containsVertex (const VertexName& v) const; list getNeighbors(const VertexName& v) const; bool insertVertex (const VertexName& v); void insertEdge (const VertexName& v1, const VertexName& v2, Weight w = 1.0); dfs_reach_iterator dfs_reach_begin (const VertexName& v); dfs_reach_iterator dfs_reach_end(); dfs_all_iterator dfs_all_begin (const VertexName& v); dfs_all_iterator dfs_all_end(); }; // class Graph #endif