1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <cstddef>
#include <iostream>
#include <list>
#include <utility>
#include <map>
////////////////////////////////////////////////////////////////////////////////
/// A generic adjacency-list graph where each vertex stores a VertexProperty and
/// each edge stores an EdgeProperty.
////////////////////////////////////////////////////////////////////////////////
template<typename VertexProperty, typename EdgeProperty>
class graph {
// The vertex and edge classes are forward-declared to allow their use in the
// public section below. Their definitions follow in the private section
// afterward.
class vertex;
class edge;
public:
// Required public types
/// Unique vertex identifier
typedef size_t vertex_descriptor;
/// Unique edge identifier represents pair of vertex descriptors
typedef std::pair<size_t, size_t> edge_descriptor;
///@todo Choose a container for the vertices. It should contain "vertex*" or
/// shared_ptr<vertex>.
/// example:
typedef std::map<vertex_descriptor,vertex*> MyVertexContainer;
///@todo Choose a container for the edges. It should contain "edge*" or
/// shared_ptr<edge>.
/// example:
typedef std::map<edge_descriptor,edge*> MyEdgeContainer;
///@todo Choose a container for the adjacency lists. It should contain
/// "edge*" or shared_ptr<edge>.
/// example:
typedef std::list<edge*> MyAdjEdgeContainer;
// Vertex iterators
typedef typename MyVertexContainer::iterator vertex_iterator;
typedef typename MyVertexContainer::const_iterator const_vertex_iterator;
// Edge iterators
typedef typename MyEdgeContainer::iterator edge_iterator;
typedef typename MyEdgeContainer::const_iterator const_edge_iterator;
// Adjacency list iterators
typedef typename MyAdjEdgeContainer::iterator adj_edge_iterator;
typedef typename MyAdjEdgeContainer::const_iterator const_adj_edge_iterator;
// Required graph operations
///@todo Define constructor/destructor
graph() {
};
~graph() = default;
graph(const graph&) = delete; ///< Copy is disabled.
graph& operator=(const graph&) = delete; ///< Copy is disabled.
///@todo Define vertex iterator operations
vertex_iterator vertices_begin() {return v_container.begin();}
const_vertex_iterator vertices_cbegin() const {return v_container.cbegin();}
vertex_iterator vertices_end() {return v_container.end();}
const_vertex_iterator vertices_cend() const {return v_container.cend();}
///@todo Define edge iterator operations
edge_iterator edges_begin(){return e_container.begin();}
const_edge_iterator edges_cbegin() const {return e_container.cbegin();}
edge_iterator edges_end(){return e_container.end();}
const_edge_iterator edges_cend() const {return e_container.cend();}
///@todo Define accessors
size_t num_vertices() const {return v_container.size();}
size_t num_edges() const {return e_container.size();}
vertex_iterator find_vertex(vertex_descriptor vd){
vertex_iterator vi=v_container.find(vd);
return vi;
}
const_vertex_iterator find_vertex(vertex_descriptor vd ) const{
const vertex_iterator vi=v_container.cfind(vd);
return vi;
}
edge_iterator find_edge(edge_descriptor ed){
edge_iterator ei=e_container.find(ed);
return ei;
}
const_edge_iterator find_edge(edge_descriptor ed) const{
const edge_iterator ei=e_container.cfind(ed);
return ei;
}
///@todo Define modifiers
vertex_descriptor insert_vertex(const VertexProperty&);
edge_descriptor insert_edge(vertex_descriptor, vertex_descriptor,
const EdgeProperty&);
void insert_edge_undirected(vertex_descriptor, vertex_descriptor,
const EdgeProperty&);
void erase_vertex(vertex_descriptor);
void erase_edge(edge_descriptor);
void clear();
// Friend declarations for input/output.
template<typename V, typename E>
friend std::istream& operator>>(std::istream&, graph<V, E>&);
template<typename V, typename E>
friend std::ostream& operator<<(std::ostream&, const graph<V, E>&);
private:
MyVertexContainer v_container;
MyEdgeContainer e_container;
// Required internal classes
////////////////////////////////////////////////////////////////////////////
/// Vertices represent the nodes in the graph.
///
/// @todo Specify the internal state of the vertex class and define all of
/// the functions declared in its interface.
////////////////////////////////////////////////////////////////////////////
class vertex {
public:
///@todo Define constructor
vertex(vertex_descriptor vd, const VertexProperty& v): vd(vd), v(v) {};
///@todo Define iterator operations
adj_edge_iterator begin(){return maec.begin();}
const_adj_edge_iterator cbegin() const{return maec.cbegin();}
adj_edge_iterator end(){return maec.end();}
const_adj_edge_iterator cend() const{return maec.cend();}
///@todo Define accessor operations
const vertex_descriptor descriptor() const
{
return desc;
}
VertexProperty& property()
{
return prop;
}
const VertexProperty& property() const
{
return prop;
}
private:
vertex_descriptor desc;
VertexProperty prop;
MyAdjEdgeContainer maec;
};
////////////////////////////////////////////////////////////////////////////
/// Edges represent the connections between nodes in the graph.
///
/// @todo Specify the internal state of the edge class and define all of the
/// functions declared in its interface.
////////////////////////////////////////////////////////////////////////////
class edge {
public:
///@todo Define constructor
edge(vertex_descriptor vd1, vertex_descriptor vd2, const EdgeProperty& ep): vd1(vd1),vd2(vd2),ep(ep) {};
///@todo Define accessor operations
const vertex_descriptor source() const;
const vertex_descriptor target() const;
const edge_descriptor descriptor() const;
EdgeProperty& property();
const EdgeProperty& property() const;
private:
///@todo Specify the internal state of an edge.
vertex_descriptor vd1;
vertex_descriptor vd2;
const EdgeProperty ep;
};
};
///@todo Define io operations for the graph.
template<typename V, typename E>
std::istream& operator>>(std::istream& is, graph<V, E>& g){
typedef typename graph <V,E>:: vertex vertex;
size_t nV,nE;
is>>nV>>nE;
for (size_t i=0; i<nV; i++){
V v;
is >> v;
g.v_container.push_back (new vertex (i,v));
}
// implement your edge import here //<---HELP WITH THIS
return is;
}
template<typename V, typename E>
std::ostream& operator<<(std::ostream& os, const graph<V, E>& g){
os << g.v_container.size() << std::setw(4) << g.e_container.size() << std::endl;
for(auto v : g.v_container)
os << v->property() << std::endl;
// implement your edge export here //<---HELP WITH THIS
return os;
}
#endif
|