monyca98

code and decode with prufer

May 25th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4. #include<queue>
  5. #include<algorithm>
  6. #include<functional>
  7. using namespace std;
  8.  
  9. #define INF 999
  10. #define max_size 10000
  11.  
  12. vector<int> g[max_size];
  13. int n,m;
  14. vector<pair<int,int>> aux; //keeps the rank and parent so it can be accessed
  15. //easier than from the queue
  16. vector<int> prufer_code;
  17. priority_queue < pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
  18. vector<pair<int,int>> edges;
  19. vector<int> code;
  20. void read()
  21. {
  22.     ifstream in("code_file.txt");
  23.     if (in.is_open())
  24.     {
  25.         in >> n >> m;
  26.         int x, y, w;
  27.  
  28.         aux.assign(n+1,{0,-1});   //second is the parent
  29.         for (int i = 0; i<m; i++)
  30.         {
  31.             in >> x >> y;
  32.             aux[x].first++;
  33.             aux[x].second=y;
  34.             aux[y].first++;
  35.             aux[y].second=x;
  36.         }
  37.         for(int i=1;i<aux.size();i++)
  38.             q.push(aux[i]);
  39.     }
  40.     else
  41.         cout << "Unable to open the file\n";
  42.  
  43. }
  44.  
  45. void decrease_rank(int parent)
  46. {
  47.     deque<pair<int, int>> deq;
  48.     pair<int, int> pp;
  49.     while (!q.empty())
  50.     {
  51.         pp = q.top();
  52.         q.pop();
  53.         deq.push_back(pp);
  54.     }
  55.     for (deque<pair<int, int>>::iterator it = deq.begin(); it != deq.end(); ++it)
  56.         if ((*it).second == parent)
  57.         {
  58.             (*it).first--;
  59.             break;
  60.         }
  61.     for (deque<pair<int, int>>::iterator it = deq.begin(); it != deq.end(); ++it)
  62.         q.push(*it);
  63. }
  64. void prufer_code_()
  65. {
  66.     while (!q.empty())
  67.     {
  68.         pair<int, int> p = q.top();
  69.         decrease_rank(p.second);
  70.         q.pop();
  71.         prufer_code.push_back(p.second);
  72.     }
  73.     prufer_code.erase(prufer_code.end()-1);
  74.     prufer_code.erase(prufer_code.end()-1);
  75. }
  76. int find_minimum(vector<int> code)
  77. {
  78.     int minimum=1;
  79.     while(find(code.begin(),code.end(),minimum)!=code.end())
  80.         minimum++;
  81.     return minimum;
  82.  
  83.  
  84. }
  85. void prufer_decode_()
  86. {
  87.     int u,v;
  88.     int i=0;
  89.     while(i<code.size()+1)
  90.     {
  91.         u=code[0];
  92.         v=find_minimum(code);
  93.         cout<<"minimum:"<<v<<endl;
  94.         if(i!=code.size())
  95.         {
  96.             code.erase(code.begin());
  97.             code.push_back(v);
  98.             edges.push_back({u,v});
  99.         }
  100.         i++;
  101.     }
  102.     u=find_minimum(code);
  103.     code.push_back(u);
  104.     v=find_minimum(code);
  105.     edges.push_back({u,v});
  106.  
  107. }
  108. int main()
  109. {
  110.     read();
  111.     prufer_code_();
  112.     cout<<"prufer code:";
  113.     for(int i=0;i<prufer_code.size();i++)
  114.         cout<<prufer_code[i]<<" ";
  115.     cout<<endl;
  116.     code.push_back(1);
  117.     code.push_back(1);
  118.     code.push_back(5);
  119.  
  120.     prufer_decode_();
  121.     cout<<"the egdes:\n";
  122.     for(int i=0;i<edges.size();i++)
  123.         cout<<edges[i].first<<"-"<<edges[i].second<<endl;
  124.  
  125.  
  126.     return 0;
  127. }
Add Comment
Please, Sign In to add comment