Advertisement
hugol

Untitled

Apr 12th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.90 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. const double eps = 1e-12; // stała przybliżenia zera
  8. double d = 0.85;
  9.  
  10. double getXY(map<int, map<int, double>> &macierz_sasiedztwa, int x, int y)
  11. {
  12.     if ((macierz_sasiedztwa.find(x) != macierz_sasiedztwa.end())
  13.         && (macierz_sasiedztwa[x].find(y) != macierz_sasiedztwa[x].end()) )
  14.     {
  15.         return macierz_sasiedztwa[x][y];
  16.     }
  17.     else
  18.         return 0;
  19.  
  20. }
  21.  
  22. void setXY(map<int, map<int, double>> &macierz_sasiedztwa, int x, int y, double val)
  23. {
  24.     if (fabs(val) >= eps)
  25.         macierz_sasiedztwa[x][y]= val;
  26. }
  27.  
  28. bool gauss0(map<int, map<int, double>> &AB, vector<double> &X)
  29. {
  30.     int i,j,k,n;
  31.     double m,s;
  32.     n=X.size(); // ilosc wierszy
  33.  
  34.     // usuwanie zer z przekatnej
  35.     //for(i = 0; i < n - 1; i++)
  36.     //{
  37.     //  if(abs(AB[i][i]) < eps) // 0
  38.     //  {
  39.     //      for(j = 0; j < n - 1; j++)
  40.     //      {
  41.     //          if (j==i)
  42.     //              continue;
  43.     //          if (AB[j][i] >= eps && AB[i][j] >= eps)
  44.     //          {
  45.     //              vector<double> tmp = AB[i];
  46.     //              AB[i] = AB[j];
  47.     //              AB[j] = tmp;
  48.     //              i = 0;
  49.     //          }
  50.     //      }
  51.     //  }
  52.     //}
  53.  
  54.  
  55.     // eliminacja
  56.     for(i = 0; i < n - 1; i++)
  57.     {
  58.         for(j = i + 1; j < n; j++)
  59.         {
  60.             // zero na przekatnej
  61.             //if(fabs(AB[i][i]) < eps) return false;
  62.             if(fabs(getXY(AB, i,i)) < eps) return false;
  63.  
  64.             //m = -AB[j][i] / AB[i][i];
  65.             m = -getXY(AB, j, i) / getXY(AB, i,i);
  66.  
  67.             for(k = i + 1; k <= n; k++)
  68.                 //AB[j][k] += m * AB[i][k];
  69.                 setXY(AB, j, k, getXY(AB, j, k) + (m*getXY(AB, i, k)));
  70.         }
  71.     }
  72.  
  73.     // wyliczanie niewiadomych
  74.     for(i = n - 1; i >= 0; i--)
  75.     {
  76.         //s = AB[i][n];
  77.         s = getXY(AB, i, n);
  78.  
  79.         for(j = n - 1; j >= i + 1; j--)
  80.             //s -= AB[i][j] * X[j];
  81.             s -= getXY(AB, i, j) * X[j];
  82.         // zero na przekatnej
  83.         //if(fabs(AB[i][i]) < eps) return false;
  84.         if(fabs( getXY(AB, i, i) ) < eps) return false;
  85.         X[i] = s / getXY(AB, i, i);
  86.     }
  87.     return true;
  88. }
  89.  
  90. bool gauss(vector <vector<double>> &AB, vector<double> &X)
  91. {
  92.     int i,j,k,n;
  93.     double m,s;
  94.     n=AB.size(); // ilosc wierszy
  95.  
  96.     // usuwanie zer z przekatnej
  97.     for(i = 0; i < n - 1; i++)
  98.     {
  99.         if(abs(AB[i][i]) < eps) // 0
  100.         {
  101.             for(j = 0; j < n - 1; j++)
  102.             {
  103.                 if (j==i)
  104.                     continue;
  105.                 if (AB[j][i] >= eps && AB[i][j] >= eps)
  106.                 {
  107.                     vector<double> tmp = AB[i];
  108.                     AB[i] = AB[j];
  109.                     AB[j] = tmp;
  110.                     i = 0;
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.  
  117.     // eliminacja
  118.     for(i = 0; i < n - 1; i++)
  119.     {
  120.         for(j = i + 1; j < n; j++)
  121.         {
  122.             // zero na przekatnej
  123.             if(fabs(AB[i][i]) < eps) return false;
  124.             m = -AB[j][i] / AB[i][i];
  125.             for(k = i + 1; k <= n; k++)
  126.                 AB[j][k] += m * AB[i][k];
  127.         }
  128.     }
  129.  
  130.     // wyliczanie niewiadomych
  131.     for(i = n - 1; i >= 0; i--)
  132.     {
  133.         s = AB[i][n];
  134.         for(j = n - 1; j >= i + 1; j--)
  135.             s -= AB[i][j] * X[j];
  136.         // zero na przekatnej
  137.         if(fabs(AB[i][i]) < eps) return false;
  138.         X[i] = s / AB[i][i];
  139.     }
  140.     return true;
  141. }
  142.  
  143.  
  144. bool wczytaj0(map<int, int> &mapa , vector <double> &ilosc, map<int, map<int, double>> &macierz_sasiedztwa)
  145. {
  146.     int currId = 0;
  147.     int src, dst;
  148.     int count = 0;
  149.  
  150.     while (!cin.eof())
  151.     {
  152.         cin >> src >> dst;
  153.         if (cin.eof())
  154.             break;
  155.  
  156.  
  157.         if (src > count)
  158.             count = src;
  159.         if (dst > count)
  160.             count = dst;
  161.         while(ilosc.size() <= count)
  162.             ilosc.push_back( 0 );
  163.         ilosc[src]++;
  164.  
  165.         setXY(macierz_sasiedztwa, dst, src, 1);
  166.     }
  167.  
  168.     return true;
  169. }
  170.  
  171.  
  172. bool wczytaj1()
  173. {
  174.     std::multimap<unsigned int, unsigned int> pairs;
  175.  
  176.     unsigned int left, right, max=0;
  177.     while (std::cin >> left >> right)
  178.     {
  179.         if (max < left)
  180.         {
  181.             max = left;
  182.         }
  183.         if (max < right)
  184.         {
  185.             max = right;
  186.         }
  187.         pairs.insert( pair<unsigned int,unsigned int> (left, right) );
  188.  
  189.     }
  190.  
  191.     //return pairs;
  192.     return true;
  193. }
  194.  
  195. bool wczytaj(vector <double> &ilosc, vector< vector<double> > &macierz_sasiedztwa)
  196. {
  197.     int currId = 0;
  198.  
  199.     int src, dst;
  200.     int count = 0;
  201.     while (!cin.eof())
  202.     {
  203.         int idSrc, idDst;
  204.  
  205.         cin >> src >> dst;
  206.         if (cin.eof() || src == -1)
  207.             break;
  208.  
  209.         /* powiekszanie wektorow*/
  210.  
  211.         if (src > count)
  212.             count = src;
  213.         if (dst > count)
  214.             count = dst;
  215.         while(ilosc.size() <= count)
  216.             ilosc.push_back( 0 );
  217.         while(macierz_sasiedztwa.size() <= count)
  218.         {
  219.             macierz_sasiedztwa.push_back(vector<double>());
  220.         }
  221.         for(int i=0; i<macierz_sasiedztwa.size(); i++)
  222.         {
  223.             while (macierz_sasiedztwa[i].size() <= count)
  224.             {
  225.                 macierz_sasiedztwa[i].push_back( 0 );
  226.             }
  227.         }
  228.  
  229.  
  230.         ilosc[src]++;
  231.         macierz_sasiedztwa[dst][src] = 1;
  232.  
  233.     }
  234.  
  235.     return true;
  236. }
  237.  
  238.  
  239.  
  240.  
  241. void wypisz_macierz(vector< vector<double> > &sasiedztwo)
  242. {
  243.     for(int i=0; i<sasiedztwo.size(); i++)
  244.     {
  245.         for(int j=0; j<sasiedztwo[i].size(); j++)
  246.         {
  247.             cout << sasiedztwo[i][j] << "\t";
  248.         }
  249.         cout << endl;
  250.     }
  251. }
  252.  
  253. void wypisz_macierz0(vector<double> &ilosc, map<int, map<int, double>> &sasiedztwo)
  254. {
  255.     for(int i=0; i<ilosc.size(); i++)
  256.     {
  257.         for(int j=0; j<ilosc.size()+1; j++)
  258.         {
  259.             //cout << getxsasiedztwo[i][j] << "\t";
  260.             cout << getXY(sasiedztwo, i, j) << "\t";
  261.         }
  262.         cout << endl;
  263.     }
  264. }
  265.  
  266. vector <vector<double>> do_wektora(vector<double> &ilosc, map<int, map<int, double>> &sasiedztwo)
  267. {
  268.     vector <vector<double>>  ret;
  269.     vector<double> wers;
  270.     for(int i=0; i<ilosc.size(); i++)
  271.     {
  272.         wers.clear();
  273.         ret.push_back(wers);
  274.         for(int j=0; j<ilosc.size()+1; j++)
  275.         {
  276.             ret[i].push_back(getXY(sasiedztwo, i, j) );
  277.         }
  278.         cout << endl;
  279.     }
  280.  
  281.     return ret;
  282. }
  283.  
  284. void pomnoz_razy_macierz_diagonalna0(vector<double> &ilosc, map<int, map<int, double>>  &sasiedztwo)
  285. {
  286.    
  287.     for(int i=0; i<ilosc.size(); i++)
  288.     {
  289.         // dodanie krawedzi do wszystkich
  290.         if (!(ilosc[i] > 0))
  291.         {
  292.             ilosc[i] = ilosc.size()-1;
  293.             for (int j=0; j<ilosc.size(); j++)
  294.             {
  295.                 if (i==j)
  296.                     continue;
  297.                 //sasiedztwo[j][i] = 1;
  298.                 setXY(sasiedztwo, j, i, 1);
  299.             }
  300.         }
  301.  
  302.         for(int j=0; j<ilosc.size(); j++)
  303.         {
  304.             //sasiedztwo[j][i] *= d/ilosc[i];
  305.             setXY(sasiedztwo, j, i, (getXY(sasiedztwo, j, i) * d)/ilosc[i]);
  306.         }
  307.     }
  308. }
  309.  
  310.  
  311. void pomnoz_razy_macierz_diagonalna(vector<double> &ilosc, vector< vector<double> > &sasiedztwo)
  312. {
  313.    
  314.     for(int i=0; i<ilosc.size(); i++)
  315.     {
  316.         // dodanie krawedzi do wszystkich
  317.         if (!(ilosc[i] > 0))
  318.         {
  319.             ilosc[i] = sasiedztwo[i].size()-1;
  320.             for (int j=0; j<sasiedztwo[i].size(); j++)
  321.             {
  322.                 if (i==j)
  323.                     continue;
  324.                 sasiedztwo[j][i] = 1;
  325.             }
  326.         }
  327.  
  328.         for(int j=0; j<sasiedztwo[i].size(); j++)
  329.         {
  330.             sasiedztwo[j][i] *= d/ilosc[i];
  331.         }
  332.     }
  333. }
  334.  
  335. void dodaj_kolumne(vector< vector<double> > &sasiedztwo)
  336. {
  337.     for(int i=0; i<sasiedztwo.size(); i++)
  338.     {
  339.         sasiedztwo[i].push_back( (1.0-d) / double(sasiedztwo.size()) );
  340.     }
  341. }
  342.  
  343. void dodaj_kolumne0(vector<double> &ilosc, map<int, map<int, double>> &sasiedztwo)
  344. {
  345.     for(int i=0; i<ilosc.size(); i++)
  346.     {
  347.         //sasiedztwo[i].push_back( (1.0-d) / double(sasiedztwo.size()) );
  348.         setXY(sasiedztwo, i, ilosc.size(),  (1.0-d) / double(ilosc.size()));
  349.     }
  350. }
  351.  
  352.  
  353. void I_odj_macierz(vector< vector<double> > &sasiedztwo)
  354. {
  355.     for(int i=0; i<sasiedztwo.size(); i++)
  356.     {
  357.         for(int j=0; j<sasiedztwo[i].size(); j++)
  358.         {
  359.             sasiedztwo[i][j] = 0-sasiedztwo[i][j];
  360.             sasiedztwo[i][j] += i==j;
  361.         }
  362.     }
  363. }
  364.  
  365. void I_odj_macierz0(vector<double> &ilosc, map<int, map<int, double>> &sasiedztwo)
  366. {
  367.     for(int i=0; i<ilosc.size(); i++)
  368.     {
  369.         for(int j=0; j<ilosc.size(); j++)
  370.         {
  371.             //sasiedztwo[i][j] = 0-sasiedztwo[i][j];
  372.             setXY(sasiedztwo, i, j, 0-getXY(sasiedztwo, i, j));
  373.  
  374.             //sasiedztwo[i][j] += i==j;
  375.             setXY(sasiedztwo, i, j, getXY(sasiedztwo, i, j) + i==j);
  376.  
  377.         }
  378.     }
  379. }
  380.  
  381. int main()
  382. {
  383.  
  384.    
  385.     vector<double> ilosc2;
  386.     vector< vector<double> > sasiedztwo;
  387.    
  388.  
  389.     vector<double> ilosc;
  390.     map<int, map<int, double>> macierz_sasiedztwa;
  391.     map<int, int> mapa;
  392.  
  393.     wczytaj(ilosc2, sasiedztwo);
  394.     wczytaj0(mapa, ilosc, macierz_sasiedztwa);
  395.    
  396.  
  397.     pomnoz_razy_macierz_diagonalna0(ilosc, macierz_sasiedztwa);
  398.     pomnoz_razy_macierz_diagonalna(ilosc2, sasiedztwo);
  399.  
  400.  
  401.  
  402.     I_odj_macierz0(ilosc, macierz_sasiedztwa);
  403.     I_odj_macierz(sasiedztwo);
  404.  
  405.     wypisz_macierz(sasiedztwo);
  406.     wypisz_macierz0(ilosc, macierz_sasiedztwa);
  407.  
  408.     dodaj_kolumne0(ilosc, macierz_sasiedztwa);
  409.  
  410.     //wypisz_macierz0(ilosc, macierz_sasiedztwa);
  411.  
  412.     vector<vector<double>> AB= do_wektora(ilosc, macierz_sasiedztwa);
  413.     //wypisz_macierz(AB);
  414.  
  415.     vector<double> X;
  416.     for(int i=0; i<macierz_sasiedztwa.size(); i++)
  417.         X.push_back( 0 );
  418.  
  419.     vector<double> Y;
  420.     for(int i=0; i<macierz_sasiedztwa.size(); i++)
  421.         Y.push_back( 0 );
  422.  
  423.  
  424.     bool done;
  425.     done = gauss0(macierz_sasiedztwa, X);
  426.  
  427.     done = gauss(AB, Y);
  428.  
  429.     int abc = 0;
  430.    
  431.  
  432.     //vector<double> wers;
  433.     //vector<vector<double>> AB;
  434.     //wers.push_back(0);
  435.     //wers.push_back(1);
  436.     //wers.push_back(2);
  437.  
  438.     //wers.push_back(1);
  439.     //AB.push_back(wers);
  440.     //wers.clear();
  441.  
  442.     //wers.push_back(1);
  443.     //wers.push_back(0);
  444.     //wers.push_back(3);
  445.  
  446.     //wers.push_back(1);
  447.     //AB.push_back(wers);
  448.     //wers.clear();
  449.  
  450.     //wers.push_back(1);
  451.     //wers.push_back(1);
  452.     //wers.push_back(1);
  453.  
  454.     //wers.push_back(1);
  455.     //AB.push_back(wers);
  456.     //wers.clear();
  457.  
  458.     //bool done;
  459.     //done = gauss(AB, X);
  460.  
  461.  
  462.  
  463.  
  464.  
  465.     return 0;
  466. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement