Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. # include <iostream>
  2. # include <climits>
  3. # include <cstdlib>
  4. # include <ctime>
  5. # include <cmath>
  6. #include <fstream>
  7. #include <vector>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. class UnionFind {
  13. private:
  14. int* set;
  15. public:
  16. UnionFind(int n){ //kazdy wierzcholek jest osobnym drzewem
  17. set = new int[n];
  18. for(int i=0;i<n;i++) set[i] = i;
  19. }
  20. ~UnionFind();
  21. int FindSet(int e){ //usyawiamy nazwe, ktora jest zawsze najmniejszy element
  22. if (set[e] != e) set[e] = FindSet(set[e]);
  23. return set[e];
  24. }
  25. void Union(int x, int y){//laczymy x oraz y
  26. int x_root = FindSet(x);
  27. int y_root = FindSet(y);
  28. if (x_root<y_root) set[y_root] = x_root;//ustawiamy nazwe po polaczeniu
  29. else set[x_root] = y_root;
  30. }
  31. };
  32.  
  33. class Edge { //tworzymy klase krawedz
  34. public:
  35. int start;
  36. int end;
  37. float value;
  38. Edge(int s, int e, float w){//polaczone wierzcholki to s i e, w to waga
  39. start = s;
  40. end = e;
  41. value = w;
  42. }
  43. inline bool operator<(const Edge &s) const {return value < s.value;} //definiujemy operatory <,>,==,by móc porównywac elementy edge
  44.  
  45. inline bool operator>(const Edge &s) const {return value > s.value;}
  46.  
  47. inline bool operator==(const Edge &s) const {return value == s.value;}
  48. };
  49. void print_edges(vector<Edge> list){
  50. int size = list.size(); //zwraca liczbe elementow znajdujacych sie aktualnie w kontenerze(zwraca rozmiar listy)
  51. for (int i=0;i<size;i++)
  52. cout<<"["<<list[i].start<<","<<list[i].end<<","<<list[i].value<<"]"<<endl; //daje początek, koniec i wartosc krawedzi
  53. }
  54. vector<Edge> generuj_graf(int ilosc, int pro, float max){ //ilosc //graf losowy //pro-to nasze prawdopodobienstwo
  55. vector<Edge> l;
  56. l.clear(); //pusty kontener
  57. for(int i=0;i<ilosc;i++)
  58. for(int j=i+1;j<ilosc;j++){
  59. int a=rand();
  60. if (a<pro){
  61. float val = rand()/max;
  62. l.push_back(Edge(i,j,val)); //dodajemy nowy element na koniec kontenera
  63. }
  64. }
  65. sort(l.begin(),l.end()); //sortujemy od początku do końca
  66. return l; //zwracamy krawedz
  67. }
  68.  
  69. int main(){
  70. cout<<"RAND_MAX:"<<RAND_MAX<<endl; //maksymalna wartosc jaka moze byc zwrocona przez rand
  71. srand(time(0)); //za kazdym razem zarodek liczb pseudolosowy jest inny
  72. clock_t t1 = clock();
  73. vector<Edge> l;
  74. float max = float(RAND_MAX);
  75. for(int k=1;k<=9;k++){
  76. cout<<"k:"<<k<<endl;
  77. int counter = 0;
  78. int nbr_of_nodes = 1<<k; //liczba wierzcholkow
  79. int pro = (RAND_MAX/(1<<k))*k;
  80. float global_values = 0;
  81. for (int i=0;i<1000;i++){
  82. l.clear();
  83. UnionFind* graph = new UnionFind(nbr_of_nodes);
  84. l = generuj_graf(nbr_of_nodes, pro, max);
  85. int unions = 0, size = l.size();
  86. float values = 0;
  87. for(int x=0;x<size;x++){
  88. Edge current_edge = l[x];
  89. int first_set = graph->FindSet(current_edge.start);
  90. int second_set = graph->FindSet(current_edge.end);
  91. if (first_set != second_set){
  92. graph->Union(first_set, second_set);
  93. unions++;
  94. values += current_edge.value; //suma wag
  95. }
  96. if (unions==nbr_of_nodes-1){
  97. counter++;
  98. global_values += values;
  99. break;
  100. }
  101. }
  102. }
  103. float per=(counter/float(1000))*100; //procent spójnych grafow
  104. cout<<"Ilosc spojnych grafow: "<<per<<"%"<<endl;
  105. cout<<"Srednia waga drzewa dla spojnych grafow: "<<global_values/counter<<endl;
  106. }
  107. clock_t t2 = clock();
  108. float diff ((float)t2-(float)t1);
  109. cout<<diff / CLOCKS_PER_SEC<<"sekund"<<endl;
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement