Advertisement
monyca98

kruskal

May 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4. #include<queue>
  5. #include<functional>
  6. #include<algorithm>
  7. using namespace std;
  8. typedef pair<int, int> p;
  9. priority_queue<pair<int, p>, vector<pair<int, p>>, greater<pair<int, p>>> g;
  10. vector <p> T;
  11. vector <int> C1, C2;  //C1 will contain always the first node,and C2 the second
  12. vector<int> C;
  13. int n, m;
  14. void read()
  15. {
  16.     ifstream in("file.txt");
  17.     if (in.is_open())
  18.     {
  19.         in >> n >> m;
  20.         int x, y, w;
  21.         for (int i = 0; i<m; i++)
  22.         {
  23.             in >> x >> y >> w;
  24.             g.push({ w,{ x,y } });
  25.         }
  26.  
  27.     }
  28.     else
  29.         cout << "unable to open the file\n";
  30. }
  31. vector<int> find_(int val, vector<int> vect)
  32. {
  33.     //find all the positions
  34.     vector<int> all;
  35.     for (int i = 0; i < vect.size(); i++)
  36.         if (val == vect[i])
  37.             all.push_back(i);
  38.     return all;
  39. }
  40. int form_cicle(vector<int> C1, vector<int> C2)
  41. {
  42.     int i = 0, j = 0;
  43.     vector<int> one;
  44.     for (int i = 0; i < C1.size(); i++)
  45.         one.push_back(C1[i]);
  46.     one.push_back(C2[i]);
  47.     int start_ = one[one.size() - 2], end_ = one[one.size() - 1];
  48.     one.pop_back();
  49.     one.pop_back();
  50.     int finish = 0, pos;
  51.     vector<int> positions;
  52.     while (!finish)
  53.     {
  54.         positions = find_(end_, one);
  55.         if (positions.size() == 0)
  56.             return 0;
  57.         while (positions.size() != 0)
  58.         {
  59.             pos = positions[positions.size() - 1];
  60.             positions.pop_back();
  61.             one.erase(one.begin() + pos);
  62.             if (pos % 2 == 1)
  63.             {
  64.                 end_ = one[pos - 1];
  65.                 one.erase(one.begin() + pos - 1);
  66.             }
  67.             else
  68.             {
  69.                 end_ = one[pos];
  70.                 one.erase(one.begin() + pos);
  71.  
  72.             }
  73.             if (end_ == start_)
  74.                 return 1;
  75.         }
  76.     }
  77.  
  78. }
  79.  
  80.  
  81. void kruskal()
  82. {
  83.     while (T.size()<n - 1)
  84.     {
  85.         pair<int, p> eliminated = g.top();
  86.         g.pop();
  87.         pair<int, int> edge = eliminated.second;
  88.  
  89.         C1.push_back(edge.first);
  90.         C2.push_back(edge.second);
  91.         cout << edge.first << " " << edge.second << endl;
  92.         if (form_cicle(C1, C2) == 0)
  93.         {
  94.             T.push_back({ edge.second,edge.first });
  95.             C1.push_back(C2[0]);
  96.             C2.clear();
  97.         }
  98.         else
  99.         {
  100.             C1.pop_back();
  101.             C2.pop_back();
  102.         }
  103.     }
  104. }
  105. int main()
  106. {
  107.     read();
  108.     kruskal();
  109.     cout << "MST:\n";
  110.     for (int i = 0; i<T.size(); i++)
  111.         cout << T[i].first << " " << T[i].second << endl;
  112.     return 0;
  113. }
  114.  
  115.  
  116.  
  117. /*
  118. 7 11
  119. 1 2 7
  120. 1 7 5
  121. 2 3 8
  122. 2 4 7
  123. 2 7 9
  124. 3 4 5
  125. 4 5 9
  126. 4 6 8
  127. 4 7 15
  128. 5 6 11
  129. 6 7 6
  130.  
  131.  
  132. raspunsu should be 1 7,1 2, 4 2,4 3,4 5,7 6
  133.  
  134. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement