00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027 #ifndef SHGRAPH_HPP
00028 #define SHGRAPH_HPP
00029
00030 #include <list>
00031 #include <set>
00032 #include <map>
00033 #include <iostream>
00034
00035 namespace SH {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 template<typename VertexType, typename EdgeType>
00067 struct ShGraphType {
00068 typedef VertexType Vertex;
00069 typedef EdgeType Edge;
00070
00071 };
00072
00073 template<typename G>
00074 struct ShGraphVertex {
00075 typedef typename G::Edge Edge;
00076 typedef std::list<Edge*> EdgeList;
00077
00079 ShGraphVertex();
00080
00082 ShGraphVertex(const ShGraphVertex<G> &other);
00083
00084 std::ostream& graphvizDump(std::ostream& out) const;
00085
00086 EdgeList edges;
00087 bool marked;
00088 };
00089
00090 template<typename G>
00091 struct ShGraphEdge {
00092 typedef typename G::Vertex Vertex;
00093
00096 ShGraphEdge();
00097
00099 ShGraphEdge(Vertex *start, Vertex *end);
00100
00102 ShGraphEdge(const ShGraphEdge<G> &other);
00103
00104 std::ostream& graphvizDump(std::ostream& out) const;
00105
00106 Vertex *start;
00107 Vertex *end;
00108 };
00109
00110
00111
00112 template<typename G>
00113 class ShGraph {
00114 public:
00115
00116 typedef typename G::Vertex Vertex;
00117 typedef typename G::Edge Edge;
00118
00119 typedef std::set<Vertex*> VertexSet;
00120 typedef std::set<Edge*> EdgeSet;
00121
00122 typedef std::list<Vertex*> VertexList;
00123 typedef std::list<Edge*> EdgeList;
00124
00125 typedef std::pair<Vertex*, Vertex*> VertexPair;
00126
00127 template<typename T>
00128 struct VertexMap: public std::map<Vertex*, T> {};
00129
00130 template<typename T>
00131 struct VertexPairMap: public std::map<VertexPair, T> {
00132 T& operator()(Vertex* u, Vertex *v) { return (*this)[VertexPair(u, v)]; }
00133 const T& operator()(Vertex* u, Vertex *v) const { return (*this)[VertexPair(u, v)]; }
00134 };
00135
00136 ShGraph();
00137
00139 ShGraph(const ShGraph<G> &other);
00140
00142 ~ShGraph();
00143
00145 void addVertex(Vertex *v);
00146
00149 void addEdge(Edge *e);
00150
00152 void removeVertex(Vertex *v);
00153
00155 void removeEdge(Edge *e);
00156
00158 void clear();
00159
00161 void clearMarked();
00162
00164 ShGraph<G>& operator=(const ShGraph<G> &other);
00165
00172 template<typename F>
00173 void dfs(Vertex *start, F &functor);
00174
00191
00192
00193
00194
00195
00196
00197
00198
00199
00205 template<typename W>
00206 typename W::WeightType bellmanFord(Vertex *start, Vertex *end, W &weigher, EdgeList *path = 0);
00207
00208
00218
00219
00220 typedef VertexPairMap<Edge*> FirstStepMap;
00221
00222 template<typename W>
00223 void floydWarshall(W &weigher, VertexPairMap<typename W::WeightType> &dist, FirstStepMap *path = 0);
00224
00225
00226
00227 typedef VertexPairMap<bool> TransitiveClosureMap;
00228 void transitiveClosure(TransitiveClosureMap &tcm);
00229
00230
00231
00232
00233 void rootSet(VertexSet &roots);
00234
00235
00236
00237 typedef VertexMap<int> HeightMap;
00238 void vertexHeight(const VertexSet &roots, HeightMap &heights);
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 typedef VertexPairMap<Vertex *> LCAMap;
00254 void leastCommonAncestor(LCAMap &ancestor);
00255
00256
00257 VertexSet verts;
00258 EdgeSet edges;
00259
00260 };
00261
00262
00263
00264
00283 template<typename G>
00284 struct ShGraphDefaultDumper {
00285 std::ostream& operator()(std::ostream& out, const typename G::Vertex *v);
00286 std::ostream& operator()(std::ostream& out, const typename G::Edge *e);
00287 };
00288
00289 template<typename G>
00290 std::ostream& graphvizDump(std::ostream &out, const ShGraph<G> &g);
00291
00292 template<typename G, typename D>
00293 std::ostream& graphvizDump(std::ostream &out, const ShGraph<G> &g, D &dumpFunctor);
00294
00295 }
00296
00297 #include "ShGraphImpl.hpp"
00298
00299 #endif