00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020 #ifndef SHGRAPH_HPP
00021 #define SHGRAPH_HPP
00022
00023 #include <list>
00024 #include <set>
00025 #include <map>
00026 #include <iostream>
00027
00028 namespace SH {
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 template<typename VertexType, typename EdgeType>
00060 struct ShGraphType {
00061 typedef VertexType Vertex;
00062 typedef EdgeType Edge;
00063
00064 };
00065
00066 template<typename G>
00067 struct ShGraphVertex {
00068 typedef typename G::Edge Edge;
00069 typedef std::list<Edge*> EdgeList;
00070
00072 ShGraphVertex();
00073
00075 ShGraphVertex(const ShGraphVertex<G> &other);
00076
00077 std::ostream& graphvizDump(std::ostream& out) const;
00078
00079 EdgeList edges;
00080 bool marked;
00081 };
00082
00083 template<typename G>
00084 struct ShGraphEdge {
00085 typedef typename G::Vertex Vertex;
00086
00089 ShGraphEdge();
00090
00092 ShGraphEdge(Vertex *start, Vertex *end);
00093
00095 ShGraphEdge(const ShGraphEdge<G> &other);
00096
00097 std::ostream& graphvizDump(std::ostream& out) const;
00098
00099 Vertex *start;
00100 Vertex *end;
00101 };
00102
00103
00104
00105 template<typename G>
00106 class ShGraph {
00107 public:
00108
00109 typedef typename G::Vertex Vertex;
00110 typedef typename G::Edge Edge;
00111
00112 typedef std::set<Vertex*> VertexSet;
00113 typedef std::set<Edge*> EdgeSet;
00114
00115 typedef std::list<Vertex*> VertexList;
00116 typedef std::list<Edge*> EdgeList;
00117
00118 typedef std::pair<Vertex*, Vertex*> VertexPair;
00119
00120 template<typename T>
00121 struct VertexMap: public std::map<Vertex*, T> {};
00122
00123 template<typename T>
00124 struct VertexPairMap: public std::map<VertexPair, T> {
00125 T& operator()(Vertex* u, Vertex *v) { return (*this)[VertexPair(u, v)]; }
00126 const T& operator()(Vertex* u, Vertex *v) const { return (*this)[VertexPair(u, v)]; }
00127 };
00128
00129 ShGraph();
00130
00132 ShGraph(const ShGraph<G> &other);
00133
00135 ~ShGraph();
00136
00138 void addVertex(Vertex *v);
00139
00142 void addEdge(Edge *e);
00143
00145 void removeVertex(Vertex *v);
00146
00148 void removeEdge(Edge *e);
00149
00151 void clear();
00152
00154 void clearMarked();
00155
00157 ShGraph<G>& operator=(const ShGraph<G> &other);
00158
00165 template<typename F>
00166 void dfs(Vertex *start, F &functor);
00167
00184
00185
00186
00187
00188
00189
00190
00191
00192
00198 template<typename W>
00199 typename W::WeightType bellmanFord(Vertex *start, Vertex *end, W &weigher, EdgeList *path = 0);
00200
00201
00211
00212
00213 typedef VertexPairMap<Edge*> FirstStepMap;
00214
00215 template<typename W>
00216 void floydWarshall(W &weigher, VertexPairMap<typename W::WeightType> &dist, FirstStepMap *path = 0);
00217
00218
00219
00220 typedef VertexPairMap<bool> TransitiveClosureMap;
00221 void transitiveClosure(TransitiveClosureMap &tcm);
00222
00223
00224
00225
00226 void rootSet(VertexSet &roots);
00227
00228
00229
00230 typedef VertexMap<int> HeightMap;
00231 void vertexHeight(const VertexSet &roots, HeightMap &heights);
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 typedef VertexPairMap<Vertex *> LCAMap;
00247 void leastCommonAncestor(LCAMap &ancestor);
00248
00249
00250 VertexSet verts;
00251 EdgeSet edges;
00252
00253 };
00254
00255
00256
00257
00276 template<typename G>
00277 struct ShGraphDefaultDumper {
00278 std::ostream& operator()(std::ostream& out, const typename G::Vertex *v);
00279 std::ostream& operator()(std::ostream& out, const typename G::Edge *e);
00280 };
00281
00282 template<typename G>
00283 std::ostream& graphvizDump(std::ostream &out, const ShGraph<G> &g);
00284
00285 template<typename G, typename D>
00286 std::ostream& graphvizDump(std::ostream &out, const ShGraph<G> &g, D &dumpFunctor);
00287
00288 }
00289
00290 #include "ShGraphImpl.hpp"
00291
00292 #endif