Advertisement
ifknot

Graph Class

Nov 4th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. #ifndef GRAPH_H_
  2. #define GRAPH_H_
  3.  
  4. #include <set>
  5. #include <map>
  6. #include <utility>
  7. #include <algorithm>
  8. #include <sstream>
  9. #include <string>
  10. #include <assert.h>
  11.  
  12. //#include "tbb/"
  13.  
  14. using namespace std;
  15.  
  16. namespace jst {
  17.  
  18.     namespace graph {
  19.  
  20.     template<typename vertex_type, typename weight_type>
  21.     class Graph {
  22.  
  23.     public:
  24.  
  25.         typedef pair<vertex_type, weight_type>  wedge;
  26.         typedef map<vertex_type, weight_type> edge_map;
  27.         typedef pair<edge_map, edge_map> in_out_set;
  28.         typedef map<vertex_type, in_out_set> graph;
  29.  
  30.         typedef set<vertex_type> vertex_set;
  31.         typedef pair<vertex_type, vertex_type> edge;
  32.         typedef set<edge> edge_set;
  33.  
  34.         typedef typename graph::const_iterator const_iterator;
  35.         typedef typename graph::iterator iterator;
  36.  
  37.         Graph(): graph_() {}
  38.  
  39.         ~Graph() {}
  40.  
  41.         void add(vertex_type u) {
  42.             graph_[u];
  43.         }
  44.  
  45.         const_iterator begin() {
  46.             return(graph_.begin());
  47.         }
  48.  
  49.         void connect(const vertex_type u, const vertex_type v, weight_type w) {
  50.             if(!contains(u))
  51.                 add(u);
  52.             if(!contains(v))
  53.                 add(v);
  54.             graph_[u].second.insert(wedge(v,w));
  55.             graph_[v].first.insert(wedge(u,w));
  56.         }
  57.  
  58.         void clear() {
  59.             graph_.clear();
  60.             assert(graph_.size() == 0);
  61.         }
  62.  
  63.         void clearEdges() {
  64.             for(iterator i = graph_.begin(); i != graph_.end(); ++i) {
  65.                 i->second.first.clear();
  66.                 i->second.second.clear();
  67.             }
  68.         }
  69.  
  70.         bool contains(const vertex_type u) const {
  71.             return (find(u) != graph_.end());
  72.         }
  73.  
  74.         void disconnect(const vertex_type u, const vertex_type v) {
  75.             assert(contains(u) && contains(v));
  76.             graph_[u].second.clear();
  77.             graph_[v].first.clear();
  78.         }
  79.  
  80.         void disconnect(const vertex_type u, const vertex_set V) {
  81.             for(typename vertex_set::const_iterator i = V.begin(); i != V.end(); ++i) {
  82.                 disconnect(u, *i);
  83.             }
  84.         }
  85.  
  86.         void disconnect(const vertex_set U, const vertex_type v) {
  87.             for(typename vertex_set::const_iterator i = U.begin(); i != U.end(); ++i) {
  88.                 disconnect(*i, v);
  89.             }
  90.         }
  91.  
  92.         void disconnectAll(const vertex_type u) {
  93.             disconnect(inNeighbourhood(u), u);
  94.             disconnect(u, outNeighbourhood(u));
  95.         }
  96.  
  97.         edge_set edgeSet() {
  98.             edge_set E;
  99.             for(const_iterator i = begin(); i != end(); ++i) {
  100.                 vertex_set V = outNeighbourhood(i->first);
  101.                 for (typename vertex_set::const_iterator j = V.begin(); j != V.end(); ++j) {
  102.                     E.insert(edge(i->first, *j));
  103.                 }
  104.             }
  105.             return(E);
  106.         }
  107.  
  108.         const_iterator end() {
  109.             return(graph_.end());
  110.         }
  111.  
  112.         const_iterator find(const vertex_type u) const {
  113.             return(graph_.find(u));
  114.         }
  115.  
  116.         int inDegree(const vertex_type u) {
  117.             assert(contains(u));
  118.             return(graph_[u].first.size());
  119.         }
  120.  
  121.         vertex_set inNeighbourhood(const vertex_type u) {
  122.             assert(contains(u));
  123.             vertex_set V;
  124.             for(typename edge_map::const_iterator i = graph_[u].first.begin(); i != graph_[u].first.end(); ++i) {
  125.                 V.insert(i->first);
  126.             }
  127.             return(V);
  128.  
  129.         }
  130.  
  131.         int outDegree(const vertex_type u) {
  132.             assert(contains(u));
  133.             return(graph_[u].second.size());
  134.         }
  135.  
  136.         vertex_set outNeighbourhood(const vertex_type u) {
  137.             assert(contains(u));
  138.             vertex_set V;
  139.             for(typename edge_map::const_iterator i = graph_[u].second.begin(); i != graph_[u].second.end(); ++i) {
  140.                 V.insert(i->first);
  141.             }
  142.             return(V);
  143.         }
  144.  
  145.         void remove (vertex_type u) {
  146.             disconnectAll(u);
  147.             graph_.erase(u);
  148.         }
  149.  
  150.         vertex_set topologicalVertexSet() {
  151.             vertex_set V;
  152.  
  153.             return(V);
  154.         }
  155.  
  156.         vertex_set vertexSet() {
  157.             vertex_set V;
  158.             for(const_iterator i = begin(); i != end(); ++i) {
  159.                 V.insert(i->first);
  160.             }
  161.             return(V);
  162.         }
  163.  
  164.         string toString(const vertex_set V) const {
  165.             stringstream ss;
  166.             ss << "{ ";
  167.             for (typename vertex_set::const_iterator i = V.begin(); i != V.end(); ++i) {
  168.                 ss << *i << " ";
  169.             }
  170.             ss << "}";
  171.             return(ss.str());
  172.         }
  173.  
  174.         string toString(const edge_set E) const {
  175.             stringstream ss;
  176.             ss << "{ ";
  177.             for (typename edge_set::const_iterator i = E.begin(); i != E.end(); ++i) {
  178.                 ss << "{" << i->first << "->" << i->second << "} ";
  179.             }
  180.             ss << "}";
  181.             return(ss.str());
  182.         }
  183.  
  184.  
  185.     private:
  186.  
  187.         graph graph_;
  188.  
  189.     };
  190.  
  191.     }
  192.  
  193. }
  194.  
  195. #endif /* GRAPH_H_ */
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement