Advertisement
Guest User

Untitled

a guest
Oct 28th, 2013
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #ifndef GRAPH_H
  2. #define GRAPH_H
  3.  
  4. #include <iostream>
  5. #include <map>
  6. #include <vector>
  7. #include <set>
  8. #include <memory>
  9.  
  10.  
  11. template<class Vertice>
  12. class Graph
  13. // My stl-based implementation of an undirected weighted graph.
  14. // It operates only on vertice numbers and stores associated objects
  15. // in vector<shared_ptr>. Each vertice caches its neighboring indeces as set<int>.
  16. // Edges are tracked as map< pair<int,int>, int> - two vertice numbers to weight.
  17. // All functions that take two vertice indeces are symmetric:
  18. // calls some_function( index1, index2 ) and some_function( index2, index1) are equivalent.
  19. {
  20. public:
  21.     typedef std::pair<int, int> EdgeKey;
  22.     typedef std::shared_ptr<Vertice> SpVertice;
  23.  
  24.     Graph(int capacity) :
  25.     // constructor reservers space for selected capacity
  26.         verticeNeighbors(capacity, std::set<int>())
  27.     {
  28.         vertices.reserve(capacity);
  29.     }
  30.  
  31.     int V() const
  32.     //returns the number of vertices in the graph
  33.     {
  34.         return vertices.size();
  35.     }
  36.  
  37.     int E() const
  38.     //returns the number of edges in the graph
  39.     {
  40.         return edges.size();
  41.     }
  42.  
  43.     Vertice* vertice(int index) const
  44.     // pass out vertice associated with index
  45.     {
  46.         auto it = vertices.at(index);
  47.         return it.get();
  48.     }
  49.  
  50.     void addVertice(SpVertice sp)
  51.     {
  52.         vertices.push_back(sp);
  53.     }
  54.  
  55.     int distance(int x, int y) const
  56.     // returns non-negative value if there is an edge from node x to node y or -1 otherwise
  57.     {
  58.         vertices.at(x);
  59.         vertices.at(y);
  60.         // the code above should throw out_of_bounds if indeces are wrong
  61.         auto iElement = vertices.find(makeEdgeKey(x, y));
  62.         if (iElement == vertices.end())
  63.             return -1;
  64.         else
  65.             return iElement->second;
  66.     }
  67.  
  68.     void connect(int x, int y, int weight = 0)
  69.     //adds to G the edge from x to y, or sets new weight to existing edge
  70.     {
  71.         vertices.at(x);
  72.         vertices.at(y);
  73.         // the code above should throw out_of_bounds if indeces are wrong
  74.         edges.insert(std::make_pair(makeEdgeKey(x, y), weight));
  75.  
  76.         verticeNeighbors[x].insert(y);
  77.         verticeNeighbors[y].insert(x);
  78.     }
  79.  
  80.     void disconnect(int x, int y)
  81.     //removes the edge from x to y, if it is there
  82.     {
  83.         vertices.at(x);
  84.         vertices.at(y);
  85.         // the code above should throw out_of_bounds if indeces are wrong
  86.         edges.erase(makeEdgeKey(x, y));
  87.         verticeNeighbors[x].erase(y);
  88.         verticeNeighbors[y].erase(x);
  89.     }
  90.  
  91.  
  92.     std::set<int> neighbors(int index) const
  93.     //lists all nodes y such that there is an edge from x to y.
  94.     {
  95.         return verticeNeighbors.at(index);;
  96.     }
  97.  
  98.  
  99. private:
  100.     std::map<EdgeKey, int> edges;
  101.     std::vector<SpVertice> vertices;
  102.     std::vector<std::set<int>> verticeNeighbors;
  103.  
  104.     EdgeKey makeEdgeKey(int x, int y)
  105.     // construct a two-value key where values are ordered
  106.     {
  107.         if(x > y)
  108.             return std::make_pair(y, x);
  109.         else
  110.             return std::make_pair(x, y);
  111.     }
  112. };
  113.  
  114. template <class Vertice>
  115. std::ostream& operator<< (std::ostream& stream, const Graph<Vertice>& G)
  116. {
  117.     int v = G.V();
  118.     int e = G.E();
  119.  
  120.     std::cout << "Graph of " << v << " vertices and " << e << " edges:" << std::endl;
  121.  
  122.     for(int i = 0; i < v; i++)
  123.     {
  124.         std::cout << "V(" << *G.vertice(i) << ") : ";
  125.         auto neighbors = G.neighbors(i);
  126.  
  127.         for(auto p : neighbors)
  128.         {
  129.             std::cout << p << " ";
  130.         }
  131.         std::cout << std::endl;
  132.     }
  133.  
  134.     return stream;
  135. }
  136.  
  137.  
  138. #endif // GRAPH_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement