Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iomanip>
  5. #include<math.h>
  6. using namespace std;
  7.  
  8.  
  9.  
  10.  
  11. class Node {
  12.     //Cuva vjerovatnocu i pozicije od kojih je nastao ovaj cvor(ova vjerovatnoca)
  13. public:
  14.     float _probability;
  15.     vector<int> _positions;
  16. };
  17.  
  18. void sort(vector<Node> &array) {
  19.     for (int i = 0; i < array.size(); i++) {
  20.         int position = i;
  21.         for (int j = i + 1; j < array.size(); j++) {
  22.             if (array[j]._probability > array[position]._probability) position = j;
  23.         }
  24.         Node temp = array[i];
  25.         array[i] = array[position];
  26.         array[position] = temp;
  27.    
  28.     }
  29. }
  30. class Collection {
  31. public:
  32.     vector<char> _characters;
  33.     vector<int> _frequencies;
  34.     vector<string> _huffmancode;
  35.     vector<float> _probabilities;
  36.  
  37.     int _total; // duzina recenice/zbir frekvencija
  38.     float _L;
  39.     float _entropy;
  40.     float _efficiency;
  41.     float _entropyOfExtendedSource;
  42.     Collection(){}
  43. #pragma region konstruktor koji pravi kolekciju kada je ulaz niz frekvencija
  44.     Collection(int frequencies[],int size) {
  45.         _total = 0;
  46.         for (int i = 0; i < size; i++) {
  47.             _frequencies.push_back(frequencies[i]);
  48.             _total += frequencies[i];
  49.         }
  50.         for (int i = 0; i < size; i++) {
  51.             _probabilities.push_back(frequencies[i]/float(_total));
  52.         }
  53.        
  54.     }
  55. #pragma endregion
  56.  
  57. #pragma region konstruktor koji pravi kolekciju kada je ulaz recenica
  58.     Collection(string sentence) {
  59.         _total = sentence.size();
  60.         for (int i = 0; i < sentence.length(); i++) {
  61.             bool found = false; //bool varijabla koja nam govori da li slovo vec postoji u nizu
  62.             for (int j = 0; j < _characters.size(); j++) {
  63.                 if (sentence[i] == _characters[j]) {
  64.                     _frequencies[j]++;
  65.                     found = true;
  66.                 }
  67.             }
  68.             if (!found) {
  69.                 _characters.push_back(sentence[i]);
  70.                 _frequencies.push_back(1);
  71.             }
  72.         }
  73.         for (int i = 0; i < _frequencies.size(); i++) {
  74.             _probabilities.push_back(float(_frequencies[i])/float(_total));
  75.         }
  76.     }
  77. #pragma endregion
  78.  
  79. #pragma region konstruktor koji pravi kolekciju kada je ulaz niz vjerovatnoca
  80.     Collection(float probabilities[], int size) {
  81.         for (int i = 0; i < size; i++) {
  82.             _probabilities.push_back(probabilities[i]);
  83.         }
  84.     }
  85. #pragma endregion
  86.  
  87.     //friend ostream& operator<<(ostream& COUT, Collection & obj)
  88.     //{
  89.     //  for (int i = 0; i < obj._frequencies.size(); i++)
  90.     //  {
  91.     //      COUT << obj._characters[i] << ": " << obj._frequencies[i] << endl;
  92.     //  }
  93.     //  return COUT;
  94.     //}
  95.  
  96.     void sortCharacters() {
  97.         for (int i = 0; i < _frequencies.size(); i++) {
  98.             int position = i;
  99.             for (int j = i + 1; j < _frequencies.size(); j++) {
  100.                 if (_frequencies[j] > _frequencies[position]) position = j;
  101.             }
  102.             char temp = _characters[i];
  103.             _characters[i] = _characters[position];
  104.             _characters[position] = temp;
  105.  
  106.             int tempf = _frequencies[i];
  107.             _frequencies[i] = _frequencies[position];
  108.             _frequencies[position] = tempf;
  109.  
  110.             float tempp = _probabilities[i];
  111.             _probabilities[i] = _probabilities[position];
  112.             _probabilities[position] = tempp;
  113.         }
  114.     }
  115.     void sortFrequencies() {
  116.         for (int i = 0; i < _frequencies.size(); i++) {
  117.             int position = i;
  118.             for (int j = i + 1; j < _frequencies.size(); j++) {
  119.                 if (_frequencies[j] > _frequencies[position]) position = j;
  120.             }
  121.             int tempf = _frequencies[i];
  122.             _frequencies[i] = _frequencies[position];
  123.             _frequencies[position] = tempf;
  124.  
  125.             float tempp = _probabilities[i];
  126.             _probabilities[i] = _probabilities[position];
  127.             _probabilities[position] = tempp;
  128.         }
  129.     }
  130.     void sortProbabilities() {
  131.         for (int i = 0; i < _probabilities.size(); i++) {
  132.             int position = i;
  133.             for (int j = i + 1; j < _probabilities.size(); j++) {
  134.                 if (_probabilities[j] > _probabilities[position]) position = j;
  135.             }
  136.             float tempf = _probabilities[i];
  137.             _probabilities[i] = _probabilities[position];
  138.             _probabilities[position] = tempf;
  139.         }
  140.     }
  141.     void huffman() {
  142.         vector<Node>tempVector; //privremeni niz vjerovatnoca ili frekvencija koji prilikom racunanja huffmanovog koda se smanjiva usljed sabiranja najmanjih vjerovatnoca
  143.         int g = 0;
  144.         for (int i = 0; i < _probabilities.size(); i++) {
  145.             _huffmancode.push_back("");
  146.             Node n; //privremeni cvor
  147.             n._probability = _probabilities[i];
  148.             n._positions.push_back(i);
  149.             tempVector.push_back(n);
  150.         }
  151.         while (tempVector.size() > 1)
  152.         {
  153.             g++;
  154.             cout << g << endl;
  155.             //trazim najmanji i drugi najmanji
  156.             /*int smallest = 0;
  157.             int secondsmallest = 0;
  158.             for (int j = 0; j < tempVector.size(); j++) {
  159.                 if (tempVector[j]._probability <= tempVector[smallest]._probability)
  160.                     smallest = j;
  161.             }
  162.             for (int j = 0; j < tempVector.size(); j++) {
  163.                 if (tempVector[j]._probability <= tempVector[secondsmallest]._probability && j != smallest)
  164.                     secondsmallest = j;
  165.             }
  166.             cout << "najmanji:" << tempVector[smallest]._probability << endl;
  167.             cout << "drugi najmanji:" << tempVector[secondsmallest]._probability << endl;*/
  168.  
  169.  
  170.             sort(tempVector); //sortiramo vector da bi nasli dva najmanja
  171.             int smallest = tempVector.size() - 1;
  172.             int secondsmallest = tempVector.size() - 2;
  173.  
  174.             /*for (int i = 0; i < tempVector.size(); i++) {
  175.                 cout << tempVector[i]._probability << " ";
  176.                 for (int j = 0; j < tempVector[i]._positions.size(); j++) {
  177.                     cout << tempVector[i]._positions[j] << ",";
  178.                 }
  179.                 cout << endl;
  180.             }
  181.             cout << endl;
  182.             cout << endl;
  183.  
  184.  
  185.             cout << endl;
  186.             cout << "najmanji:" << tempVector[smallest]._probability << endl;
  187.             cout << "drugi najmanji:" << tempVector[secondsmallest]._probability << endl;*/
  188.            
  189.             vector<Node>temparray; //privremeni vector za tempVector
  190.  
  191.             //dodajemo 0 i 1 na pozicije gdje su dva najmanja elementa
  192.             for (int i = 0; i < tempVector[smallest]._positions.size(); i++) {
  193.                 string t = _huffmancode[tempVector[smallest]._positions[i]];
  194.                 _huffmancode[tempVector[smallest]._positions[i]] = "0";
  195.                 _huffmancode[tempVector[smallest]._positions[i]] += t;
  196.  
  197.             }
  198.             for (int i = 0; i < tempVector[secondsmallest]._positions.size(); i++) {
  199.                 string t = _huffmancode[tempVector[secondsmallest]._positions[i]];
  200.                 _huffmancode[tempVector[secondsmallest]._positions[i]] = "1";
  201.                 _huffmancode[tempVector[secondsmallest]._positions[i]] += t;
  202.             }
  203.  
  204.             Node tempnode; // privremeni node za sumu dvije najmanje frekvencije
  205.             tempnode._probability = tempVector[smallest]._probability + tempVector[secondsmallest]._probability;
  206.             tempnode._positions = tempVector[smallest]._positions;
  207.             tempnode._positions.insert(tempnode._positions.end(), tempVector[secondsmallest]._positions.begin(), tempVector[secondsmallest]._positions.end());
  208.             temparray.push_back(tempnode);
  209.  
  210.             //popunjavamo ostale clanove tempVectora u privremeni vector tempArray
  211.             for (int i = 0; i < tempVector.size(); i++) {
  212.                 if (i != smallest && i != secondsmallest) {
  213.                     temparray.push_back(tempVector[i]);
  214.                 }
  215.             }
  216.            
  217.  
  218.             tempVector = temparray;
  219.         }
  220.     }
  221.  
  222.     void AverageCodeLength(){
  223.         _L = 0;
  224.             for (int i = 0; i < _probabilities.size(); i++) {
  225.                 _L += _probabilities[i]* _huffmancode[i].length();
  226.             }
  227.     }
  228.     void OutputEverythingForCharacters() {
  229.         cout << "char|freq|huffman code|code length|probability|length*prob|" << endl;
  230.         cout << "----------------------------------------------------------+" << endl;
  231.         for (int i = 0; i < _huffmancode.size(); i++) {
  232.             cout << setw(4) << _characters[i] << "|" << setw(4) << _frequencies[i] << "| " << setw(11) << _huffmancode[i] << "| "
  233.                 << setw(10) << _huffmancode[i].length() << "| " << setw(10) << _probabilities[i] << "|" << setw(11) << _probabilities[i] * _huffmancode[i].length() << "|" << endl;
  234.         }
  235.  
  236.         cout << "----------------------------------------------------------+" << endl;
  237.         cout << "    |" << setw(4) << _total << "|            |           |" << setw(11) << "" << "|" << setw(11) << _L << "|" << endl;
  238.         cout << "----------------------------------------------------------+" << endl;
  239.     }
  240.     void OutputEverythingForFrequencies() {
  241.         cout << "|freq|huffman code|code length|probability|length*prob|" << endl;
  242.         cout << "----------------------------------------------------------+" << endl;
  243.         for (int i = 0; i < _huffmancode.size(); i++) {
  244.             cout << "|" << setw(4) << _frequencies[i] << "| " << setw(11) << _huffmancode[i] << "| "
  245.                 << setw(10) << _huffmancode[i].length() << "| " << setw(10) << _probabilities[i] << "|" << setw(11) << _probabilities[i]* _huffmancode[i].length() << "|" << endl;
  246.         }
  247.  
  248.         cout << "----------------------------------------------------------+" << endl;
  249.         cout << setw(4) << _total << "|            |           |" << setw(11) << "" << "|" << setw(11) << _L << "|" << endl;
  250.         cout << "----------------------------------------------------------+" << endl;
  251.     }
  252.     void OutputEverythingForProbabilities() {
  253.         cout << "huffman code|code length|probability|length*prob|" << endl;
  254.         cout << "----------------------------------------------------------+" << endl;
  255.         for (int i = 0; i < _huffmancode.size(); i++) {
  256.             cout << setw(11) << _huffmancode[i] << "| "
  257.                 << setw(10) << _huffmancode[i].length() << "| " << setw(10) << _probabilities[i] << "|" << setw(11) << _probabilities[i]* _huffmancode[i].length() << "|" << endl;
  258.         }
  259.  
  260.         cout << "----------------------------------------------------------+" << endl;
  261.         cout << "|            |           |" << setw(11) << "" << "|" << setw(11) << _L << "|" << endl;
  262.         cout << "----------------------------------------------------------+" << endl;
  263.     }
  264.    
  265.     void Entropy() {
  266.         float suma = 0;
  267.         for (int i = 0; i < _frequencies.size(); i++) {
  268.             suma += _frequencies[i] * log2(_frequencies[i]);
  269.         }
  270.         _entropy = log2(_total) - (1 / float(_total))*suma;
  271.     }
  272.     void Efficiency() {
  273.         _efficiency = _entropy / _L;
  274.     }
  275.     void EntropyOfExtendedSource() {
  276.         _entropyOfExtendedSource = _entropy*2;
  277.     }
  278.     void ExtendedCollectionMaker(Collection coll) {
  279.         _total = 0;
  280.        
  281.         for (int i = 0; i < coll._frequencies.size(); i++) {
  282.             for (int j = 0; j < coll._frequencies.size(); j++) {
  283.                 _frequencies.push_back(coll._frequencies[i] * coll._frequencies[j]);
  284.                 _total += coll._frequencies[i] * coll._frequencies[j];
  285.             }
  286.         }
  287.         for (int i = 0; i < _frequencies.size(); i++) {
  288.             _probabilities.push_back(_frequencies[i] / float(_total));
  289.         }
  290.         /////////
  291.         /*for (int j = 0; j < _frequencies.size(); j++) {
  292.             cout << _frequencies[j] << " " ;
  293.         }
  294.         cout << endl;*/
  295.         /////////
  296.     }
  297. };
  298.  
  299. int main() {
  300.     clock_t startTime = clock();
  301.  
  302.     /*Collection coll("La noche cae, brumosa ya y morada. Vagas claridades malvas y verdes perduran tras la torre de la iglesia. El camino sube, lleno de sombras, de campanillas, de fragancia de hierba, de canciones, de cansancio y de anhelo.");
  303.     coll.sortCharacters();
  304.     coll.huffman();
  305.     coll.AverageCodeLength();
  306.     coll.OutputEverythingForCharacters();*/
  307.    
  308.  
  309.  
  310.     /*int arr[] = { 27, 16, 4, 56, 22, 2, 78, 45, 36, 13, 12, 7 };*/  
  311.     int arr[] = { 83,69,67,82,69,84,79,32,68,69,32,85,78,79,32,83,69,67,82,69,84,79,32,83,69,71,85,82,79 };
  312.     Collection coll(arr,size(arr));
  313.     coll.sortFrequencies();
  314.     coll.huffman();
  315.     coll.AverageCodeLength();
  316.     coll.OutputEverythingForFrequencies();
  317.  
  318.    
  319.  
  320.     /*float arr[] = { 0.3, 0.2, 0.1, 0.1, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05 };
  321.     Collection coll(arr, size(arr));
  322.     coll.sortProbabilities();
  323.     coll.huffman();
  324.     coll.AverageCodeLength();
  325.     coll.OutputEverythingForProbabilities(); */
  326.  
  327.    
  328.     coll.Entropy();
  329.     coll.Efficiency();
  330.     coll.EntropyOfExtendedSource();
  331.  
  332.     cout <<"Entropy: "<< coll._entropy << endl;
  333.     cout <<"Efficiency: " << coll._efficiency << endl;
  334.     cout <<"Entropy F2: " << coll._entropyOfExtendedSource << endl;
  335.  
  336.     Collection coll2;
  337.     coll2.ExtendedCollectionMaker(coll);
  338.     coll2.huffman();
  339.     coll2.AverageCodeLength();
  340.     coll2.Entropy();
  341.     coll2.Efficiency();
  342.     cout << "Efficiency: " << coll2._efficiency << endl;
  343.  
  344.  
  345.     // some code here
  346.     // to compute its execution duration in runtime
  347.     cout << double(clock() - startTime) / (double)CLOCKS_PER_SEC << " seconds." << endl;
  348.     system("pause");
  349.     return 0;
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement