Advertisement
Guest User

Untitled

a guest
Aug 12th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.89 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <random>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <set>
  7.  
  8. //#define DEBUG 0 //(1,2,3)
  9. #define DEBUG_ALLOC_SIZE
  10. //#define USE_VERTEX_DATA
  11.  
  12. using namespace std;
  13. //                             2                       1
  14. enum ERROR { SUCCESS, VERTEX_DOESNT_EXIST, VERTEX_FORM_DIFF_GRAPHS, EDGE_DOESNT_EXIST, EDGES_FROM_DIF_GRAPHS, POS_ERROR, TO_MUCH_TO_INIT };
  15.  
  16. template<
  17. #ifdef USE_VERTEX_DATA
  18.     typename VertexType,
  19. #endif
  20.     typename EdgeType>
  21. class Dynamic_Graph {
  22.     class _Edge;
  23. public:
  24.     /*
  25.     Struktura grafu
  26.  
  27.     tablica graph \/
  28.         1. _Edge->_Edge->_Edge->NULL<-End[1]
  29.         2. _Edge->_Edge->NULL<-End[2]
  30.         3. _Edge->_Edge->_Edge->_Edge->NULL<-End[3]
  31.  
  32.         unsigned int size_graph = 3
  33.  
  34.     _Edge:
  35.         unsigned int connected_to
  36.         _Edge* next;
  37.     */
  38.     class Edge;
  39.     class Edge_Iterator;
  40.  
  41.     class Vertex {
  42.     public:
  43. #ifdef USE_VERTEX_DATA
  44.         VertexType& get_props() {
  45.             return graph->data[id];
  46.         }
  47. #endif
  48.  
  49.         Edge_Iterator iter_begin() {
  50.             return Edge_Iterator(*this);
  51.         }
  52.         const unsigned int get_id() const {
  53.             return id;
  54.         }
  55.  
  56.         Dynamic_Graph<
  57. #ifdef USE_VERTEX_DATA
  58.             VertexType,
  59. #endif
  60.             EdgeType>&
  61.         get_graph()
  62.         {
  63.             return *graph;
  64.         }
  65.         bool isConnected(Vertex to) {
  66.             return graph->is_connected(id, to.get_id());
  67.         }
  68.         Vertex(
  69.             Dynamic_Graph<
  70. #ifdef USE_VERTEX_DATA
  71.             VertexType,
  72. #endif
  73.             EdgeType>& _graph, unsigned int i)
  74.         {
  75.             graph = &(_graph);
  76.             id = i;
  77.         }
  78.         Vertex() {
  79.             graph = NULL;
  80.             id = -1;
  81.         }
  82.     private:
  83.         Dynamic_Graph<
  84. #ifdef USE_VERTEX_DATA
  85.             VertexType,
  86. #endif
  87.             EdgeType>* graph;
  88.         unsigned int id;
  89.  
  90.         friend Dynamic_Graph<
  91. #ifdef USE_VERTEX_DATA
  92.             VertexType,
  93. #endif
  94.             EdgeType>;
  95.         friend Edge;
  96.         friend Edge_Iterator;
  97.     };
  98.    
  99.     class Edge {
  100.     public:
  101.         //Konstruktor
  102.         Edge(Dynamic_Graph<
  103. #ifdef USE_VERTEX_DATA
  104.             VertexType,
  105. #endif
  106.             EdgeType>& _graph, unsigned int _from, unsigned int _to)
  107.         {
  108.             graph = &(_graph);
  109.             from = _from;
  110.             _exist = graph->find_edge(from, _to, To);
  111.         }
  112.  
  113.         Edge(Vertex& _from, Vertex& _to) {
  114.             graph = (_from.graph);
  115.             from = _from.get_id();
  116.             _exist = graph->find_edge(from,_to.get_id(),To);
  117.         }
  118.  
  119.         bool exist() {
  120.             return _exist;
  121.         }
  122.         EdgeType& get_props() {
  123.             return To->data;
  124.         }
  125. #ifdef USE_VERTEX_DATA
  126.         VertexType& get_vertex_from_props() {
  127.             return graph->data[from];
  128.         }
  129.         VertexType& get_vertex_to_props() {
  130.             return graph->data[To->connection_to];
  131.         }
  132. #endif
  133.         Vertex get_vertex_to() {
  134.             return Vertex(*graph, To->connection_to);
  135.         }
  136.         Vertex get_vertex_from() {
  137.             return Vertex(*graph, from);
  138.         }
  139.         bool check_if_edge_exist() {
  140.             //TODO
  141.         }
  142.     private:
  143.         //Nie przypisać _to z innego źródła anizeli _from
  144.         Edge(
  145.             Dynamic_Graph<
  146. #ifdef USE_VERTEX_DATA
  147.             VertexType,
  148. #endif
  149.             EdgeType>& _graph, unsigned int _from, _Edge * _to) {
  150.             graph = &(_graph);
  151.             from = _from;
  152.             To = _to;
  153.             _exist = true;
  154.         }
  155.         Dynamic_Graph<
  156. #ifdef USE_VERTEX_DATA
  157.             VertexType,
  158. #endif
  159.             EdgeType>* graph;
  160.         unsigned int from; //Z kad
  161.         _Edge* To; //Do kad
  162.         bool _exist;
  163.  
  164.         friend Dynamic_Graph<
  165. #ifdef USE_VERTEX_DATA
  166.             VertexType,
  167. #endif
  168.             EdgeType>;
  169.         friend Vertex;
  170.         friend Edge_Iterator;
  171.     };
  172.  
  173.     class Edge_Iterator {
  174.     public:
  175.         Edge_Iterator(Vertex& Begin) {
  176.             graph = Begin.graph;
  177.             buff = &(graph->graph[Begin.get_id()]);
  178.             begin = Begin.get_id();
  179.         }
  180.         Edge_Iterator(Dynamic_Graph<
  181. #ifdef USE_VERTEX_DATA
  182.             VertexType,
  183. #endif
  184.             EdgeType>& _graph, unsigned int Begin) {
  185.             graph = &_graph;
  186.             buff = &(graph->graph[Begin]);
  187.             begin = Begin;
  188.         }
  189.         Edge_Iterator& operator ++(const int) {
  190.             buff = buff->next;
  191.             return *this;
  192.         }
  193.         const bool is_end() const {
  194.             if (buff == NULL || graph->end[begin] == NULL) {
  195.                 return false;
  196.             }
  197.             return true;
  198.         }
  199.         Edge operator*() const {
  200.             return Edge(*graph, begin, buff);
  201.         }
  202.         EdgeType get_edge_props() {
  203.             return buff->data;
  204.         }
  205.         Vertex get_connection_to() {
  206.             return Vertex(*graph, buff->connection_to);
  207.         }
  208.         unsigned int get_connection_to_id() {
  209.             return buff->connection_to;
  210.         }
  211.     private:
  212.         unsigned int begin;
  213.         _Edge* buff;
  214.         Dynamic_Graph<
  215. #ifdef USE_VERTEX_DATA
  216.             VertexType,
  217. #endif
  218.             EdgeType>* graph;
  219.  
  220.         friend Vertex;
  221.         friend Edge;
  222.     };
  223.  
  224. template<typename T>
  225.     class Vertex_data {
  226.     public:
  227.         Vertex_data(Dynamic_Graph<
  228. #ifdef USE_VERTEX_DATA
  229.             VertexType,
  230. #endif
  231.             EdgeType>& _graph)
  232.         {
  233.             graph = &_graph;
  234.             data = new T[_graph.get_vertex_count()];
  235.         }
  236.         T & operator ()(const unsigned int a) {
  237.             return data[a];
  238.         }
  239.         T& operator ()(Dynamic_Graph<
  240. #ifdef USE_VERTEX_DATA
  241.                 VertexType,
  242. #endif
  243.                 EdgeType>::Vertex a)
  244.         {
  245.             return data[a.id];
  246.         }
  247.         friend std::ostream& operator<< (std::ostream& s, const Dynamic_Graph<
  248. #ifdef USE_VERTEX_DATA
  249.             VertexType,
  250. #endif
  251.             EdgeType>::Vertex_data<T> & _graph)
  252.         {
  253.             for (int i = 0; i < _graph.get_size(); i++)
  254.             {
  255.                 s << i << "\t" << _graph.data[i] << endl;
  256.             }
  257.             return s;
  258.         }
  259.         const unsigned int get_size() const {
  260.             return graph->get_vertex_count();
  261.         }
  262.     private:
  263.         T * data;
  264.  
  265.         Dynamic_Graph<
  266. #ifdef USE_VERTEX_DATA
  267.             VertexType,
  268. #endif
  269.             EdgeType>* graph;
  270.     };
  271.  
  272.     Dynamic_Graph(unsigned int n) : vertex_count(n) {
  273.         graph = new _Edge[n];
  274. #ifdef USE_VERTEX_DATA
  275.         data = new VertexType[n];
  276. #endif
  277.         end = new _Edge * [n];
  278.  
  279.         for (unsigned int i = 0; i < n; i++)
  280.         {
  281.             end[i] = NULL;
  282.         }
  283.         cout << "alloc: " << sizeof(_Edge) * n + sizeof(_Edge*) * n + sizeof(this)
  284. #ifdef USE_VERTEX_DATA
  285.             + sizeof(VertexType) * n
  286. #endif
  287.             << " bytes" << endl;
  288.     }
  289. #ifdef USE_VERTEX_DATA
  290.     Dynamic_Graph(unsigned int n, VertexType* _data) : vertex_count(n) {
  291.         graph = new _Edge[n];
  292.         data = new VertexType[n];
  293.         end = new _Edge * [n];
  294.  
  295.         for (unsigned int i = 0; i < n; i++)
  296.         {
  297.             end[i] = NULL;
  298.             data[i] = _data[i];
  299.         }
  300.     }
  301. #endif
  302.     ~Dynamic_Graph() {
  303.         delete[] graph;
  304. #ifdef USE_VERTEX_DATA
  305.         delete[] data;
  306.         delete[] end;
  307. #endif
  308.     }
  309.     //Dodaje krawedz
  310.     void addEdge(unsigned int from, unsigned int to, EdgeType data) {
  311.         add_edge(data, from, to);
  312.     }
  313.     //Zwraca ilosc wierzchołków
  314.     const unsigned int get_vertex_count() {
  315.         return vertex_count;
  316.     }
  317.     //Zwraca wierzcholek
  318.     Vertex get_vertex(unsigned int id) {
  319.         return Vertex(*this, id);
  320.     }
  321.     //Zwraca wlasnosci wierzcholka
  322. #ifdef USE_VERTEX_DATA
  323.     VertexType & get_vertex_props(unsigned int id) {
  324.         return data[id];
  325.     }
  326. #endif
  327.     //Zwraca iterator po krawedziach
  328.     Edge_Iterator get_edge_iterator_from(Vertex from) {
  329.         return Edge_Iterator(from);
  330.     }
  331.     //Zwraca iterator po krawedziach
  332.     Edge_Iterator get_edge_iterator_from(unsigned int from) {
  333.         return Edge_Iterator(*this, from);
  334.     }
  335.     //Zwraca wlasnosci krawedzi
  336.     Edge get_edge(Vertex& from, Vertex& to) {
  337.         return get_edge(from.get_id(), to.get_id());
  338.     }
  339.     //Zwraca krawedz
  340.     Edge get_edge(unsigned int from, unsigned int to) {
  341.         return Edge(*this, from, to);
  342.     }
  343.     //Zwraca wlasnosci krawedzi
  344.     EdgeType & get_edge_props(Vertex& from, Vertex& to) {
  345.         get_edge_props(from.get_id(), to.get_id());
  346.     }
  347.     //Zwraca wlasnosci krawedzi
  348.     EdgeType & get_edge_props(unsigned int from, unsigned int to) {
  349.         _Edge* handeler;
  350.         if (find_edge(from, to, handeler)) {
  351.             return handeler->data;
  352.         }//Krawedz nie istnieje
  353.     }
  354. template<typename T>
  355.     Vertex_data<T> create_vertex_data() {
  356.         return Vertex_data<T>(*this);
  357.     }
  358.     //sprawdza czy istnije polaczenie miedzy from a to O(n)
  359.     bool is_connected(Vertex & from, Vertex & to) {
  360.         return is_connected(from.get_id(), to.get_id());
  361.     }
  362.     //sprawdza czy istnije polaczenie miedzy from a to O(n)
  363.     bool is_connected(unsigned int from, unsigned int to) {
  364.         _Edge* buff = &graph[from];
  365.         while (true) {
  366.             if (buff == NULL) {
  367.                 return false;
  368.             }
  369.             if (buff->connection_to == to) {
  370.                 return true;
  371.             }
  372.             buff = buff->next;
  373.         }
  374.     }
  375. private:
  376.     struct _Edge {
  377.     public:
  378.         unsigned int connection_to;
  379.         _Edge* next = NULL;
  380.         EdgeType data;
  381.  
  382.  
  383.         _Edge(EdgeType& _data) : data(_data) {}
  384.         _Edge(EdgeType& _data, unsigned int _connection_to) : data(_data), connection_to(_connection_to) {}
  385.         _Edge() : connection_to(-1) {}
  386.     };
  387.  
  388.     //Szuka krawedzi w tablicy, O(n)
  389.     bool find_edge(unsigned int from, unsigned int connected_to, _Edge *& source) {
  390.         _Edge* buff = &graph[from];
  391.         while (true) {
  392.             if (buff == NULL) {
  393.                 source = NULL;
  394.                 return false;
  395.             }
  396.             if (buff->connection_to == connected_to) {
  397.                 source = buff;
  398.                 return true;
  399.             }
  400.             buff = buff->next;
  401.         }
  402.     }
  403.     //sprawdza czy istnije polaczenie miedzy from a to
  404.     bool is_connected(_Edge from, unsigned int to) {
  405.         return from.connection_to == to;
  406.     }
  407.     //Dodaje krawedz do grafu O(1)
  408.     void add_edge(EdgeType& data, unsigned int from, unsigned int to) {
  409.         if (end[from] == NULL) {
  410.             graph[from].data = data;
  411.             end[from] = &(graph[from]);
  412.             graph[from].connection_to = to;
  413.         }
  414.         else {
  415.             end[from]->next = new _Edge(data, to);
  416.             end[from] = end[from]->next;
  417.         }
  418.     }
  419.     _Edge* graph;
  420.     _Edge** end;
  421. #ifdef USE_VERTEX_DATA
  422.     VertexType* data;
  423. #endif
  424.     const unsigned int vertex_count;
  425.  
  426.     friend Vertex;
  427.     friend Edge;
  428.     friend Edge_Iterator;
  429.  
  430. };
  431.  
  432. //Wysoka wydajnosc w przypadku gestego grafu (wysoki stosunek ilosci krawedzi do ilosci wierzcholkow), Złożoność pamięciowa: V+E*E
  433. template<typename VertexType, typename EdgeType>
  434. class Static_Graph {
  435.     class _Edge;
  436.     class _vertex;
  437. public:
  438.  
  439.     //########################################//
  440.     //             KLASY PUBLICZNE            //
  441.     //########################################//
  442.  
  443.     class Vertex;
  444.     class Edge;
  445.  
  446.     //Iterator po krawedziach
  447.     class Edge_Iterator {
  448.     public:
  449.         Edge_Iterator(Vertex& _Vertex) {
  450.             vertex = &_Vertex;
  451.             max = vertex->graph->vertexscount - 1;
  452.             iter = 0;
  453.  
  454.             while (true) {
  455.                 if (iter > max || vertex->graph->get(vertex->id, iter)->connected) {
  456.                     break;
  457.                 }
  458.                 iter++;
  459.             }
  460.  
  461.         }
  462.         Edge_Iterator& operator ++(const int) {
  463.             iter++;
  464.             while (true) {
  465.                 if (iter > max || vertex->graph->get(vertex->id, iter)->connected) {
  466.                     break;
  467.                 }
  468.                 iter++;
  469.             }
  470.             return *this;
  471.         }
  472.         bool operator<(const unsigned int& a) {
  473.             return iter < a;
  474.         }
  475.         Edge operator*() const {
  476.             return Edge(vertex->graph->pos(vertex->id, iter), vertex->get_Graph(), vertex->id, iter);
  477.         }
  478.     private:
  479.         unsigned int iter;
  480.         unsigned int max;
  481.         Vertex* vertex;
  482.  
  483.         friend Edge;
  484.         friend Vertex;
  485.     };
  486.  
  487.     //Publiczna klasa Wierzcholka
  488.     class Vertex {
  489.     public:
  490.         //Konstruktor - _graph - graf, vertex - numer wierzcholka
  491.         Vertex() {
  492.             graph = NULL;
  493.             id = -1;
  494.         }
  495.         Vertex(Static_Graph& _graph, unsigned int vertex) {
  496.  
  497. #if DEBUG > 1
  498.             if (_graph.vertexscount > vertex) {
  499.                 graph = &_graph;
  500.                 id = vertex;
  501.             }
  502. #if DEBUG > 2
  503.             else {
  504.                 throw VERTEX_DOESNT_EXIST;
  505.                 graph = NULL;
  506.                 id = -1;
  507.             }
  508. #endif
  509. #else
  510.             graph = &_graph;
  511.             id = vertex;
  512. #endif
  513.  
  514.         }
  515.         //Stworz iterator iterujacy po krawedziach wychodzacych z wierzcholka
  516.         Edge_Iterator iter_begin() {
  517.             return Edge_Iterator(*this);
  518.         }
  519.         inline unsigned int iter_end() const {
  520.             return graph->vertexscount;
  521.         }
  522.         VertexType& get_props() const {
  523.             return graph->vertexes[id].get_props();
  524.         }
  525.         bool isConnected(const Vertex& a) {
  526. #if DEBUG > 1
  527.             if (a.graph == this->graph) {
  528.                 return a.graph->pos(a.id, this->id)->connected;
  529.             }
  530.             else {
  531. #if DEBUG > 2
  532.                 throw VERTEX_FORM_DIFF_GRAPHS;
  533. #endif
  534.                 return false;
  535.             }
  536. #else
  537.             return a.graph->pos(a.id, this->id)->connected;
  538. #endif
  539.         }
  540.         bool areFromSameGraph(const Vertex& a) {
  541.             return (&a == this);
  542.         }
  543.         unsigned int get_id() const {
  544.             return id;
  545.         }
  546.         Static_Graph* get_Graph() {
  547. #if DEBUG > 1
  548.             if (graph == NULL) {
  549.                 throw VERTEX_DOESNT_EXIST;
  550.             }
  551. #endif
  552.             return graph;
  553.         }
  554.     private:
  555.  
  556.         friend Edge_Iterator;
  557.         friend Edge;
  558.         Static_Graph* graph;
  559.         unsigned int id;
  560.     };
  561.  
  562.     //Publiczna Klasa krawedzi
  563.     class Edge {
  564.     public:
  565.         //Stworz "handler"'a odwolujacego sie do danych krawedzi
  566.         Edge(Vertex& a, Vertex& b) {
  567. #ifdef DEBUG
  568.             if (a.graph == b.graph) {
  569.                 Graph = a.get_Graph();
  570.                 if (a.graph->pos(a.id, b.id)->connected) {
  571.                     edge = a.graph->pos(a.id, b.id);
  572.                     x = a.id;
  573.                     y = b.id;
  574.                     _exist = true;
  575.                 }
  576.                 else {
  577. #if DEBUG > 2
  578.                     throw EDGE_DOESNT_EXIST;
  579. #endif
  580.                     _exist = false;
  581.                 }
  582.             }
  583.             else {
  584. #if DEBUG > 1
  585.                 throw EDGES_FROM_DIF_GRAPHS;
  586. #endif
  587.                 _exist = false;
  588.             }
  589. #else
  590.             if (a.graph->pos(a.id, b.id)->connected) {
  591.                 edge = a.graph->pos(a.id, b.id);
  592.                 x = a.id;
  593.                 y = b.id;
  594.                 _exist = true;
  595.             }
  596.             else {
  597.                 x = -1;
  598.                 y = -1;
  599.                 _exist = false;
  600.             }
  601.  
  602. #endif
  603.         }
  604.  
  605.         Edge(unsigned int a, unsigned int b, Static_Graph* graph) {
  606. #ifdef DEBUG
  607.             Graph = graph;
  608.             if (graph->pos(a, b)->connected) {
  609.                 edge = graph->pos(a, b);
  610.                 x = a;
  611.                 y = b;
  612.                 _exist = true;
  613.             }
  614.             else {
  615. #if DEBUG > 2
  616.                 throw EDGE_DOESNT_EXIST;
  617. #endif
  618.                 _exist = false;
  619.             }
  620. #else
  621.             Graph = graph;
  622.             if (graph->pos(a, b)->connected) {
  623.                 edge = graph->pos(a, b);
  624.                 x = a;
  625.                 y = b;
  626.                 _exist = true;
  627.             }
  628.             else {
  629.                 _exist = false;
  630.             }
  631. #endif
  632.         }
  633.  
  634.         //Pobierz wlasnosci krawedzi
  635.         EdgeType& get_props() {
  636.             if (_exist) {
  637.                 return edge->get_props();
  638.             }
  639. #if DEBUG > 2
  640.             else {
  641.                 throw EDGE_DOESNT_EXIST;
  642.             }
  643. #endif
  644.         }
  645.         //Pobierz wierzcholek z ktorego wychodzi ta krawedz
  646.         Vertex get_neighbor_form() {
  647.             return Vertex(*(this->Graph), x);
  648.         }
  649.         //Pobierz wierzcholek do ktorego prowadzi ta krawedz
  650.         Vertex get_neighbor_to() {
  651.             return Vertex(*(this->Graph), y);
  652.         }
  653.  
  654.         //Pobierz wlasciwosci wierzcholka z ktorego wychodzi ta krawedz
  655.         VertexType get_neighbor_props_form() {
  656.             return Graph->vertexes[x].get_props(); //############################################ SPRAWDZIĆ CZY TU NIE POWINNO BYC Y
  657.         }
  658.         //Pobierz wlasciwosci wierzcholka do ktorego prowadzi ta krawedz
  659.         VertexType get_neighbor_props_to() {
  660.             return Graph->vertexes[y].get_props(); //############################################ SPRAWDZIĆ CZY TU NIE POWINNO BYC X
  661.         }
  662.  
  663.         //Czy krawedz istnieje?
  664.         const bool exist() const {
  665.             return _exist;
  666.         }
  667.     private:
  668.         Edge(Static_Graph::_Edge* _edge, Static_Graph* _Graph, unsigned int _x, unsigned int _y) {
  669. #ifdef DEBUG
  670.             if (_edge->connected) {
  671.                 Graph = _Graph;
  672.                 edge = _edge;
  673.                 x = _x;
  674.                 y = _y;
  675.                 _exist = true;
  676.             }
  677.             else {
  678. #if DEBUG > 2
  679.                 throw EDGE_DOESNT_EXIST;
  680. #endif
  681.                 _exist = false;
  682.             }
  683. #else
  684.             Graph = _Graph;
  685.             edge = _edge;
  686.             x = _x;
  687.             y = _y;
  688.             _exist = true;
  689. #endif
  690.         }
  691.  
  692.         bool _exist = false;
  693.         _Edge* edge = NULL;
  694.         Static_Graph* Graph;
  695.         unsigned int x, y;
  696.  
  697.         friend Edge_Iterator;
  698.         friend Vertex;
  699.     };
  700.  
  701.     //########################################//
  702.     //       KONSTRUKTORY/DESTRUKTORY         //
  703.     //########################################//
  704.  
  705.     //vertexes - ilosc wierzcholkow w grafie. Nie da sie jej zmienic.
  706.     Static_Graph(unsigned int vertexes_count) {
  707.         graph = new _Edge[vertexes_count * vertexes_count];
  708.         vertexes = new _vertex[vertexes_count];
  709.         vertexscount = vertexes_count;
  710.  
  711. #ifdef DEBUG_ALLOC_SIZE
  712.         cout << "alloced: " << vertexes_count * vertexes_count * sizeof(_Edge) + vertexes_count * sizeof(_vertex) + sizeof(Static_Graph) << " bytes" << endl;
  713. #endif
  714.     }
  715.     Static_Graph(unsigned int vertexes_count, const VertexType* data, unsigned int vertexes_to_init = vertexes_count) {
  716. #if DEBUG > 1
  717.         if (vertexes_to_init > vertexes_count) {
  718.             throw TO_MUCH_TO_INIT;
  719.         }
  720. #endif
  721.         graph = new _Edge[vertexes_count * vertexes_count];
  722.         vertexes = new _vertex[vertexes_count];
  723.         vertexscount = vertexes_count;
  724.  
  725.         for (unsigned long int i = 0; i < vertexes_to_init; i++)
  726.         {
  727.             vertexes[i] = _vertex(data[i]);
  728.         }
  729. #ifdef DEBUG_ALLOC_SIZE
  730.         cout << "alloced: " << vertexes_count * vertexes_count * sizeof(_Edge) + vertexes_count * sizeof(_vertex) + sizeof(Static_Graph) << " bytes" << endl;
  731. #endif
  732.     }
  733.     ~Static_Graph() {
  734.         delete[] graph;
  735.         delete[] vertexes;
  736.     }
  737.  
  738.  
  739.     //########################################//
  740.     //           FUNKCJE PUBLICZNE            //
  741.     //########################################//
  742.  
  743.  
  744.     //Tworzy polaczenie miedzy wierzcholkiem a i b
  745.     void addEdge(unsigned int vertex_a, unsigned int vertex_b, EdgeType edgeprops) {
  746. #if DEBUG > 2
  747.         if (vertex_a > vertexscount || vertex_b > vertexscount) {
  748.             throw VERTEX_DOESNT_EXIST;
  749.         }
  750. #endif
  751.         pos(vertex_a, vertex_b)->connected = true;
  752.         pos(vertex_a, vertex_b)->get_props() = edgeprops;
  753.     }
  754.     //pobiera ilosc wierzcholkow w grafie
  755.     const unsigned int get_vertex_count() const {
  756.         return vertexscount;
  757.     }
  758.     //Pobiera Wierzcholek z grafu
  759.     Vertex get_vertex(unsigned int vertex) {
  760.         return Vertex(*this, vertex);
  761.     }
  762.     //pobiera krawedz z grafu
  763.     Edge get_edge(Vertex from, Vertex to) {
  764.         return Edge(from, to);
  765.     }
  766.     Edge get_edge(unsigned int from, unsigned int to) {
  767.         return Edge(from, to, this);                                            //TUTAJ TODO
  768.     }
  769.     //Szybko pobiera wlasnosc krawedzi
  770.     EdgeType& get_edge_props(unsigned int from, unsigned int to) {
  771.         return pos(from, to).get_props();
  772.     }
  773.     VertexType& get_vertex_props(unsigned int id) {
  774.         return vertexes[id].get_props();
  775.     }
  776.     //drukuje macierz sasiedztwa
  777.     void printconnectionmatrix() {
  778.         for (int i = 0; i < vertexscount; i++)
  779.         {
  780.             for (int j = 0; j < vertexscount; j++)
  781.             {
  782.                 cout << get(i, j)->connected << " ";
  783.             }
  784.             cout << endl;
  785.         }
  786.     }
  787.  
  788.     Static_Graph<VertexType, EdgeType> copy() {
  789.         //TODO
  790.     }
  791. private: //###################################### PRIVATE STUFF ##########################################\\
  792.  
  793.     //Podklasa definiujaca wierzcholek (lowlevel)
  794.     class _vertex {
  795.     public:
  796.         _vertex() {}
  797.         _vertex(const VertexType& _props) : props(_props) {}
  798.         _vertex(VertexType& _props) : props(_props) {}
  799.         VertexType& get_props() {
  800.             return props;
  801.         }
  802.     private:
  803.         VertexType props;
  804.         friend Vertex;
  805.     };
  806.  
  807.     //Podklasa definiujaca krawedz (lowlevel)
  808.     class _Edge {
  809.     public:
  810.         bool connected = false;
  811.         _Edge() {
  812.  
  813.         }
  814.         EdgeType& get_props() {
  815.             return props;
  816.         }
  817.     private:
  818.         EdgeType props;
  819.         friend Edge;
  820.     };
  821.  
  822.  
  823.     //lista wierzcholkow. zawiera dane na ich temat
  824.     _vertex* vertexes;
  825.     //macierz sasiedztwa wierzcholkow
  826.     _Edge* graph;
  827.     //ilosc wierzcholkow w grafie
  828.     unsigned int vertexscount;
  829.  
  830.     //########################################//
  831.     //           FUNKCJE KLASY                //
  832.     //########################################//
  833.  
  834.     _Edge* pos(unsigned int x, unsigned int y) {
  835. #if DEBUG > 2
  836.         if (x > vertexscount || y > vertexscount) {
  837.             throw POS_ERROR;
  838.         }
  839. #endif
  840.         return (graph + y * vertexscount + x);
  841.     }
  842.     _Edge* get(unsigned int x, unsigned int y) {
  843. #if DEBUG > 2
  844.         if (x > vertexscount || y > vertexscount) {
  845.             throw POS_ERROR;
  846.         }
  847. #endif
  848.         return (graph + y * vertexscount + x);
  849.     }
  850.  
  851.     friend Edge_Iterator;
  852.     friend Vertex;
  853.     friend Edge;
  854. };
  855.  
  856. template<typename VertexType, typename EdgeType>
  857. void cout_graph(Static_Graph<VertexType, EdgeType>& G)
  858. {
  859.     cout << "cnt: " << "\t" << "props:" << endl;
  860.     for (int i = 0; i < G.get_vertex_count(); i++)
  861.     {
  862.         auto vertex = G.get_vertex(i);
  863.         for (auto j = vertex.iter_begin(); j < vertex.iter_end(); j++) {
  864.             cout << vertex.get_id() << "->" << (*j).get_neighbor_to().get_id() << " w: " << (*j).get_props().weight << endl;
  865.         }
  866.     }
  867. }
  868. template<typename VertexType, typename EdgeType>
  869. void randomize_graph(Static_Graph<VertexType, EdgeType>& G, float aproximate_fill = 0.5)
  870. {
  871.     unsigned int a, b;
  872.     const unsigned int graph_size = G.get_vertex_count();
  873.     const unsigned int iterations = (unsigned int)(graph_size * graph_size * aproximate_fill);
  874.  
  875.     for (unsigned int i = 0; i < iterations; i++)
  876.     {
  877.         a = rand() % graph_size;
  878.         b = rand() % graph_size;
  879.         G.addEdge(a, b, EdgeType(rand() % 1000));
  880.     }
  881. }
  882.  
  883. struct vertex_props {
  884.     vertex_props() {}
  885.     int distance = 0;
  886.     int last = -1;
  887. };
  888. struct edge_props {
  889.     edge_props() : weight(0), num(0) {}
  890.     edge_props(int w) : weight(w), num(0) {}
  891.     edge_props(int w, int n) : weight(w), num(n) {}
  892.     int weight;
  893.     //..
  894.     int num;
  895. };
  896.  
  897. //WERSJA DLA DYNAMIC GRAPH;
  898.  
  899. template<
  900. #ifdef USE_VERTEX_DATA
  901.     typename VertexType,
  902. #endif
  903.     typename EdgeType>
  904. void randomize_graph(Dynamic_Graph<
  905. #ifdef USE_VERTEX_DATA
  906.     VertexType,
  907. #endif
  908.     EdgeType>& G, int num_of_edges)
  909. {
  910.     unsigned int a, b;
  911.     const unsigned int graph_size = G.get_vertex_count();
  912.     const unsigned int iterations = num_of_edges;
  913.  
  914.     for (unsigned int i = 0; i < iterations; i++)
  915.     {
  916.         a = rand() % graph_size;
  917.         b = rand() % graph_size;
  918.         G.addEdge(a, b, EdgeType(rand() % 1000));
  919.     }
  920. }
  921.  
  922.  
  923. void DjikstraAlgoritm(
  924.     Dynamic_Graph<edge_props>& graph,
  925.     Dynamic_Graph<edge_props>::Vertex start,
  926.     Dynamic_Graph<edge_props>::Vertex_data<int> & distance,
  927.     Dynamic_Graph<edge_props>::Vertex_data<int>& last)
  928. {
  929.     //funkcja agregacyjna dla set
  930.     struct agg_function {
  931.         agg_function(Dynamic_Graph<edge_props>::Vertex_data<int>& _distance) {
  932.             distance = &_distance;
  933.         }
  934.         bool operator()(unsigned int a, unsigned int b) const {
  935.             return distance->operator()(a) < distance->operator()(b);
  936.         }
  937.         Dynamic_Graph<edge_props>::Vertex_data<int>* distance;
  938.     };
  939.  
  940.     //Ustaw dis na +inf
  941.     for (int i = 0; i < graph.get_vertex_count(); i++)
  942.     {
  943.         distance(i) = 2000000+i;
  944.     }
  945.     //Ustaw dis w wierzcholek pocz. na 0
  946.     distance(start) = 0;
  947.  
  948.     //doSprawdzenia <- wszystkie wierzcholki
  949.     auto doSprawdzenia = set<int, agg_function>(agg_function(distance));
  950.    
  951.     for (int i = 0; i < graph.get_vertex_count(); i++)
  952.     {
  953.         doSprawdzenia.insert(i);
  954.     }
  955.  
  956.     while (!doSprawdzenia.empty()) {
  957.         //znajdz wierzcholek o najmniejszej wartosci dis (ver)
  958.         auto ver = graph.get_vertex(*(doSprawdzenia.begin()));
  959.         doSprawdzenia.erase(ver.get_id());
  960.  
  961.         //Dla wszystkich krawedzi wychodzacych od ver i bedacych w doSprawdzenia, wykonaj cos tam
  962.         for (auto i = ver.iter_begin(); i.is_end(); i++)
  963.         {
  964.             if (doSprawdzenia.find(i.get_connection_to_id()) != doSprawdzenia.end()) {
  965.                 //cos tam
  966.  
  967.                 auto edge = i.get_edge_props().weight;
  968.                 if (distance(i.get_connection_to_id()) > distance(ver.get_id()) + edge) {
  969.                     doSprawdzenia.erase(i.get_connection_to_id());
  970.                     distance(i.get_connection_to_id()) = distance(ver.get_id()) + edge;
  971.                     doSprawdzenia.insert(i.get_connection_to_id());
  972.                     last(i.get_connection_to_id()) = ver.get_id();
  973.                 }
  974.             }
  975.         }
  976.     }
  977. }
  978.  
  979.  
  980. string get_string_error(ERROR a) {
  981.     switch (a)
  982.     {
  983.     case SUCCESS:
  984.         return "niby dziala";
  985.         break;
  986.     case VERTEX_DOESNT_EXIST:
  987.         return "Krawedz nie istnieje";
  988.         break;
  989.     case VERTEX_FORM_DIFF_GRAPHS:
  990.         return "Wierzchołki należą do różnych grafów";
  991.         break;
  992.     case EDGE_DOESNT_EXIST:
  993.         return "Krawedz nie istnieje";
  994.         break;
  995.     case EDGES_FROM_DIF_GRAPHS:
  996.         return "Krawdznie należą do różnych grafów";
  997.         break;
  998.     case POS_ERROR:
  999.         return "To nie powinno sie wydażyć";
  1000.         break;
  1001.     case TO_MUCH_TO_INIT:
  1002.         return "";
  1003.         break;
  1004.     default:
  1005.         break;
  1006.     }
  1007. }
  1008.  
  1009. #ifdef STATIC
  1010. const bool fnc(Static_Graph<vertex_props, edge_props>::Vertex& a, Static_Graph<vertex_props, edge_props>::Vertex& b) {
  1011.     return a.get_props().distance > b.get_props().distance;
  1012. }
  1013. void DjikstraAlgoritm(Static_Graph<vertex_props, edge_props>& graph, Static_Graph<vertex_props, edge_props>::Vertex start) {
  1014.     for (int i = 0; i < graph.get_vertex_count(); i++)
  1015.     {
  1016.         graph.get_vertex(i).get_props().distance = 2000000;
  1017.     }
  1018.     start.get_props().distance = 0;
  1019.  
  1020.  
  1021.     vector<Static_Graph<vertex_props, edge_props>::Vertex> doSprawdzenia;
  1022.     doSprawdzenia.resize(graph.get_vertex_count());
  1023.     for (int i = 0; i < graph.get_vertex_count(); i++)
  1024.     {
  1025.         doSprawdzenia[i] = graph.get_vertex(i);;
  1026.     }
  1027.  
  1028.     while (!doSprawdzenia.empty()) {
  1029.         int id = 0, iter = 0;
  1030.         auto ver = doSprawdzenia.front();
  1031.  
  1032.         for (auto a : doSprawdzenia) {
  1033.             if (ver.get_props().distance > a.get_props().distance) {
  1034.                 ver = a;
  1035.                 id = iter;
  1036.             }
  1037.             iter++;
  1038.         }
  1039.  
  1040.         doSprawdzenia[id] = doSprawdzenia.back();
  1041.         doSprawdzenia.pop_back();
  1042.  
  1043.         for (auto vertex : doSprawdzenia) {
  1044.             if (!vertex.isConnected(ver)) {
  1045.                 continue;
  1046.             }
  1047.  
  1048.             auto edge = graph.get_edge(ver, vertex).get_props().weight;
  1049.             if (vertex.get_props().distance > ver.get_props().distance + edge) {
  1050.                 vertex.get_props().distance = ver.get_props().distance + edge;
  1051.                 vertex.get_props().last = ver.get_id();
  1052.             }
  1053.         }
  1054.     }
  1055. }
  1056. int main()
  1057. {
  1058.  
  1059.     Static_Graph<vertex_props, edge_props> graph(5000);
  1060.  
  1061.     graph.addEdge(0, 7, edge_props(10));//a->h
  1062.     graph.addEdge(0, 4, edge_props(1));//a->e
  1063.  
  1064.     graph.addEdge(1, 2, edge_props(2));//b->c
  1065.  
  1066.     graph.addEdge(3, 0, edge_props(4));//d->a
  1067.     graph.addEdge(3, 7, edge_props(1));//d->h
  1068.  
  1069.     graph.addEdge(4, 5, edge_props(3));//e->f
  1070.  
  1071.     graph.addEdge(5, 6, edge_props(7));//f->g
  1072.     graph.addEdge(5, 8, edge_props(1));//f->i
  1073.     graph.addEdge(5, 1, edge_props(1));//f->b
  1074.     graph.addEdge(5, 2, edge_props(3));//f->c
  1075.  
  1076.     graph.addEdge(7, 8, edge_props(9));//h->i
  1077.  
  1078.     graph.addEdge(8, 9, edge_props(2));//i->j
  1079.  
  1080.     graph.addEdge(9, 6, edge_props(1));//j->g
  1081.  
  1082.     randomize_graph(graph, 0.01);
  1083.  
  1084.     //cout_graph(graph);
  1085.  
  1086.     //graph.printconnectionmatrix();
  1087.     auto vertex1 = graph.get_vertex(6);
  1088.     auto vertex2 = graph.get_vertex(9);
  1089.     auto edge = graph.get_edge(vertex2, vertex1);
  1090.  
  1091.     cout << "Weight: " << edge.get_props().weight << endl;
  1092.     cout << "from:" << edge.get_neighbor_form().get_id() << endl;
  1093.     cout << "to: " << edge.get_neighbor_to().get_id() << endl;
  1094.  
  1095.     cout << endl;
  1096.  
  1097.     cout << "All edges from " << vertex1.get_id() << endl;
  1098.     for (auto i = vertex1.iter_begin(); i < vertex1.iter_end(); i++)
  1099.     {
  1100.         cout << (*i).get_props().weight << endl;
  1101.     }
  1102.  
  1103.  
  1104.     try {
  1105.         auto thisdoesntexist = graph.get_vertex(graph.get_vertex_count());
  1106.     }
  1107.     catch (ERROR a) {
  1108.         cout << get_string_error(a) << endl;
  1109.     }
  1110.     DjikstraAlgoritm(graph, graph.get_vertex(3));
  1111.     cout << "Shortest path form 3 to: " << endl;
  1112.     for (int i = 0; i < graph.get_vertex_count(); i++)
  1113.     {
  1114.         if (graph.get_vertex(i).get_props().distance == 2000000) {
  1115.             continue;
  1116.         }
  1117.         cout << i << " " << graph.get_vertex(i).get_props().distance << "\t";
  1118.         if (i != 3) {
  1119.             int id = graph.get_vertex(i).get_props().last;
  1120.             while (id != 3) {
  1121.                 cout << id << " ";
  1122.                 id = graph.get_vertex(id).get_props().last;
  1123.             }
  1124.         }
  1125.         cout << endl;
  1126.     }
  1127.  
  1128.     getchar();
  1129. }
  1130. #else
  1131. int main()
  1132. {
  1133.  
  1134.     //Losowy graf z 40000 wierzcholkami i 300000 krawedziami
  1135.  
  1136.     Dynamic_Graph<edge_props> graph(10);
  1137.  
  1138.     graph.addEdge(0, 7, edge_props(10));//a->h
  1139.     graph.addEdge(0, 4, edge_props(1));//a->e
  1140.  
  1141.     graph.addEdge(1, 2, edge_props(2));//b->c
  1142.  
  1143.     graph.addEdge(3, 0, edge_props(4));//d->a
  1144.     graph.addEdge(3, 7, edge_props(1));//d->h
  1145.  
  1146.     graph.addEdge(4, 5, edge_props(3));//e->f
  1147.  
  1148.     graph.addEdge(5, 6, edge_props(7));//f->g
  1149.     graph.addEdge(5, 8, edge_props(1));//f->i
  1150.     graph.addEdge(5, 1, edge_props(1));//f->b
  1151.     graph.addEdge(5, 2, edge_props(3));//f->c
  1152.  
  1153.     graph.addEdge(7, 8, edge_props(9));//h->i
  1154.  
  1155.     graph.addEdge(8, 9, edge_props(2));//i->j
  1156.  
  1157.     graph.addEdge(9, 6, edge_props(1));//j->g
  1158.    
  1159.     //randomize_graph(graph, 300000);
  1160.    
  1161.     auto vertex = graph.get_vertex(0);
  1162.     auto vertex1 = graph.get_vertex(5);
  1163.     auto vertex2 = graph.get_vertex(4);
  1164.     auto edge = graph.get_edge(vertex2, vertex1);
  1165.  
  1166.     cout << "Weight: " << edge.get_props().weight << endl;
  1167.     cout << "from:" << edge.get_vertex_from().get_id() << endl;
  1168.     cout << "to: " << edge.get_vertex_to().get_id() << endl;
  1169.  
  1170.     cout << endl;
  1171.  
  1172.     cout << "All edges from " << vertex1.get_id() << endl;
  1173.     for (auto i = vertex1.iter_begin(); i.is_end(); i++)
  1174.     {
  1175.         cout << (*i).get_props().weight << endl;
  1176.     }
  1177.  
  1178.  
  1179. //#define PRINT_THIS_SHIT //(drukuje wszystkie wierzchokli)
  1180. #ifdef PRINT_THIS_SHIT
  1181.     for (int j = 0; j < graph.get_vetrex_count(); j++)
  1182.     {
  1183.         for (auto i = graph.get_vertex(j).iter_begin(); i.is_end(); i++) {
  1184.             cout << j << " " << i.get_connection_to_id() << " " << (*i).get_props().weight << endl;
  1185.         }
  1186.     }
  1187. #endif
  1188.     int target = 3;
  1189.     auto distance = graph.create_vertex_data<int>();
  1190.     auto last = graph.create_vertex_data<int>();
  1191.     DjikstraAlgoritm(graph, graph.get_vertex(target),distance,last);
  1192.     cout << endl;
  1193.     cout << distance << endl << endl;
  1194.  
  1195.     for (int i = 0; i < distance.get_size(); i++)
  1196.     {
  1197.         cout << distance(i) << "\t" << i << " ";
  1198.         if (i != target) {
  1199.             auto id = last(i);
  1200.             while (id != target) {
  1201.                 cout << id << " ";
  1202.                 id = last(id);
  1203.             }
  1204.         }
  1205.         cout << target << endl;
  1206.     }
  1207.     getchar();
  1208. }
  1209. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement