00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024 #ifndef SHGRAPH_HPP
00025 #define SHGRAPH_HPP
00026
00027 #include <list>
00028 #include <set>
00029 #include <map>
00030 #include <iostream>
00031
00032 namespace SH {
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
00060
00061
00062
00063 template<typename VertexType, typename EdgeType>
00064 struct ShGraphType {
00065 typedef VertexType Vertex;
00066 typedef EdgeType Edge;
00067
00068 };
00069
00070 template<typename G>
00071 struct ShGraphVertex {
00072 typedef typename G::Edge Edge;
00073 typedef std::list<Edge*> EdgeList;
00074
00076 ShGraphVertex();
00077
00079 ShGraphVertex(const ShGraphVertex<G> &other);
00080
00081 std::ostream& graphvizDump(std::ostream& out) const;
00082
00083 EdgeList edges;
00084 bool marked;
00085 };
00086
00087 template<typename G>
00088 struct ShGraphEdge {
00089 typedef typename G::Vertex Vertex;
00090
00093 ShGraphEdge();
00094
00096 ShGraphEdge(Vertex *start, Vertex *end);
00097
00099 ShGraphEdge(const ShGraphEdge<G> &other);
00100
00101 std::ostream& graphvizDump(std::ostream& out) const;
00102
00103 Vertex *start;
00104 Vertex *end;
00105 };
00106
00107
00108
00109 template<typename G>
00110 class ShGraph {
00111 public:
00112
00113 typedef typename G::Vertex Vertex;
00114 typedef typename G::Edge Edge;
00115
00116 typedef std::set<Vertex*> VertexSet;
00117 typedef std::set<Edge*> EdgeSet;
00118
00119 typedef std::list<Vertex*> VertexList;
00120 typedef std::list<Edge*> EdgeList;
00121
00122 typedef std::pair<Vertex*, Vertex*> VertexPair;
00123
00124 template<typename T>
00125 struct VertexMap: public std::map<Vertex*, T> {};
00126
00127 template<typename T>
00128 struct VertexPairMap: public std::map<VertexPair, T> {
00129 T& operator()(Vertex* u, Vertex *v) { return (*this)[VertexPair(u, v)]; }
00130 const T& operator()(Vertex* u, Vertex *v) const { return (*this)[VertexPair(u, v)]; }
00131 };
00132
00133 ShGraph();
00134
00136 ShGraph(const ShGraph<G> &other);
00137
00139 ~ShGraph();
00140
00142 void addVertex(Vertex *v);
00143
00146 void addEdge(Edge *e);
00147
00149 void removeVertex(Vertex *v);
00150
00152 void removeEdge(Edge *e);
00153
00155 void clear();
00156
00158 void clearMarked();
00159
00161 ShGraph<G>& operator=(const ShGraph<G> &other);
00162
00169 template<typename F>
00170 void dfs(Vertex *start, F &functor);
00171
00188
00189
00190
00191
00192
00193
00194
00195
00196
00202 template<typename W>
00203 typename W::WeightType bellmanFord(Vertex *start, Vertex *end, W &weigher, EdgeList *path = 0);
00204
00205
00215
00216
00217 typedef VertexPairMap<Edge*> FirstStepMap;
00218
00219 template<typename W>
00220 void floydWarshall(W &weigher, VertexPairMap<typename W::WeightType> &dist, FirstStepMap *path = 0);
00221
00222
00223
00224 typedef VertexPairMap<bool> TransitiveClosureMap;
00225 void transitiveClosure(TransitiveClosureMap &tcm);
00226
00227
00228
00229
00230 void rootSet(VertexSet &roots);
00231
00232
00233
00234 typedef VertexMap<int> HeightMap;
00235 void vertexHeight(const VertexSet &roots, HeightMap &heights);
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 typedef VertexPairMap<Vertex *> LCAMap;
00251 void leastCommonAncestor(LCAMap &ancestor);
00252
00253
00254 VertexSet verts;
00255 EdgeSet edges;
00256
00257 };
00258
00259
00260
00261
00280 template<typename G>
00281 struct ShGraphDefaultDumper {
00282 std::ostream& operator()(std::ostream& out, const typename G::Vertex *v);
00283 std::ostream& operator()(std::ostream& out, const typename G::Edge *e);
00284 };
00285
00286 template<typename G>
00287 std::ostream& graphvizDump(std::ostream &out, const ShGraph<G> &g);
00288
00289 template<typename G, typename D>
00290 std::ostream& graphvizDump(std::ostream &out, const ShGraph<G> &g, D &dumpFunctor);
00291
00292 }
00293
00294 #include "ShGraphImpl.hpp"
00295
00296 #endif