Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShGraph.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
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 /* Framework for building directed graph classes and
00038  * algorithms on directed graph classes
00039  *
00040  * Built along the same lines as ShMesh.hpp, except
00041  * instead of a half-edge data structure this 
00042  * uses an ordered edge adjacency list.
00043  *
00044  */
00045 
00046 // This might be too weird, but
00047 // could make graph operations return other graphs
00048 // as an alternate operation mode
00049 //
00050 // e.g. shortest path would replace the edges with 
00051 // edges that hold an original edge for the first step in the
00052 // shortest path + a weight for total distance of the shortest
00053 // path...
00054 //
00055 // These new graphs would have predefined graphvizdump ops
00056 // so it would be easy to output the shortest-path
00057 // result through graphviz.
00058 // 
00059 
00060 
00061 // We do not impose any index scheme on vertices/edges 
00062 // (may want integer indexs on verts later to run faster)
00063 // so the user can choose an appropriate indexing scheme
00064 // or build a subclass that uses a specific indexing scheme
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 // TODO may want to not bother with memory management - use smart pointers for
00111 // verts/edges
00112 template<typename G>
00113 class ShGraph {
00114   public:
00115     // TODO maybe these typedefs should go somewhere else?
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     template<typename W>
00193     W::WeightType dijkstra(Vertex *start, Vertex *end, W &weigher); 
00194 
00195     template<typename W>
00196     W::WeightType dijkstra(Vertex *start, Vertex *end, W &weigher, EdgeList &path); 
00197     */
00198     // @}
00199 
00205     template<typename W>
00206     typename W::WeightType bellmanFord(Vertex *start, Vertex *end, W &weigher, EdgeList *path = 0); 
00207     // @}
00208 
00218     /* Describes a relationship between two vertices by the first step.
00219      * Mostly used in algorithms for backtracking one step at a time */
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     // implemented like floyd warshall as described in CLRS 25.2
00227     typedef VertexPairMap<bool> TransitiveClosureMap;
00228     void transitiveClosure(TransitiveClosureMap &tcm);
00229 
00230     // functions on DAGs
00231    
00232     // Returns the set of root vertices (those with no incoming edges) 
00233     void rootSet(VertexSet &roots);
00234     
00235     // Finds the height of each node 
00236     // where height is the longest path from any root
00237     typedef VertexMap<int> HeightMap;
00238     void vertexHeight(const VertexSet &roots, HeightMap &heights);
00239 
00240 
00241     // Least Common Ancestor of a DAG 
00242     //
00243     // TODO implement DAG checking code - just a dfs cycle checker
00244     //
00245     // A least common ancestor of two verts x, y 
00246     // is a common ancestor of x and y, and not the ancestor
00247     // of any other common ancestor. (paraphrase of definition from 
00248     // Bender '01).
00249     //
00250     // Bender '01 has the fastest known asymptotic behaviour, but
00251     // it's easier to use the slower transitive closure...so 
00252     // that's what we use here.
00253     typedef VertexPairMap<Vertex *> LCAMap;
00254     void leastCommonAncestor(LCAMap &ancestor);
00255 
00256 
00257     VertexSet verts;
00258     EdgeSet edges;
00259 
00260 };
00261 
00262 // TODO
00263 // * extract a subgraph from a graph based on a bool functor on edges/verts
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

Generated on Mon Jan 24 18:36:32 2005 for Sh by  doxygen 1.4.1