Advertisement
Xom9ik

Dijkstra's Algorithm

May 15th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.05 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7.  
  8. bool put(bool put_b, int pk, int* put_temp_metka, int** VES, int V)
  9. {
  10.     if ((put_b == true && put_temp_metka[pk] != 1))
  11.     {
  12.         int* temp_x = new int[V];
  13.         int k = -1;
  14.         for (int j = 0; j < V - 1; j++)
  15.         {
  16.             if (VES[j][put_temp_metka[pk] - 1] > 0 && VES[j][put_temp_metka[pk] - 1] < 100)
  17.             {
  18.                 k++;
  19.                 temp_x[k] = j;
  20.             }
  21.         }
  22.         int temp_k = k;
  23.         if (temp_k>0)
  24.         {
  25.             while (temp_k > 0)
  26.             {
  27.                 bool flag = false;
  28.                 for (int i = 0; i <= pk; i++)
  29.                 {
  30.                     for (int j = 0; j <= pk; j++)
  31.                     {
  32.                         if (put_temp_metka[i] != put_temp_metka[j])
  33.                             flag = true;
  34.                         else
  35.                         {
  36.                             flag = false;
  37.                             break;
  38.                         }
  39.                     }
  40.                 }
  41.                 if (flag == true)
  42.                 {
  43.                     pk++;
  44.                     put_temp_metka[pk] = temp_x[temp_k] + 1;
  45.                     put_b = put(put, pk, put_temp_metka, VES, V);
  46.                     if (put_temp_metka[pk] == put_temp_metka[pk - 1])
  47.                         return false;
  48.                     if (put == false)
  49.                         put_b = true;
  50.                     else if (put_b == true)
  51.                     {
  52.                         return true;
  53.                     }
  54.                     temp_k--;
  55.                 }
  56.                 else
  57.                 {
  58.                     temp_k--;
  59.                 }
  60.                
  61.             }
  62.         }
  63.         if (temp_k == 0)
  64.         {
  65.             pk++;
  66.             put_temp_metka[pk] = temp_x[0] + 1;
  67.         }
  68.         if (put_temp_metka[pk] != put_temp_metka[pk - 1] && k != -1)
  69.         {
  70.             put_b = true;
  71.         }
  72.         else
  73.         {
  74.             put_b = false;
  75.         }
  76.         return put(put_b, pk, put_temp_metka, VES, V);
  77.     }
  78.     else
  79.         return put_b;
  80.     /////////////////////////////////
  81. }
  82.  
  83. int main()
  84. {
  85.     while (1)
  86.     {
  87.         setlocale(0, "Russian");
  88.         SetConsoleCP(1251);
  89.         SetConsoleOutputCP(1251);
  90.         int V;
  91.         cout << "Введите размерность графа: ";
  92.         while (!(cin >> V) || (cin.peek() != '\n' || !(V >= 2))) //проверка на корректность ввода
  93.         {
  94.             cin.clear();
  95.             while (cin.get() != '\n');
  96.             cout << "Введено недопустимое значение " << V << " Повторите попытку." << endl;
  97.         }
  98.         int **VES = new int*[V]; //массив временных меток
  99.         for (int i = 0; i < V; i++)
  100.             VES[i] = new int[V];
  101.         int VV = V*V;
  102.         int start = 0;;
  103.         cout << "Заполните матрицу весов графа " << endl;      // Матрица весов графа
  104.         cout << setw(4);
  105.         for (int i = 0; i < V; i++)
  106.             cout << "|x" << i + 1;
  107.         cout << endl;
  108.         for (int i = 0; i < V; i++)
  109.         {
  110.             cout << "X" << i + 1 << '|';
  111.             for (int j = 0; j < V; j++)
  112.                 while (!(cin >> VES[i][j]) || !(VES[i][j] >= 0)) //проверка на корректность ввода
  113.                 {
  114.                     cin.clear();
  115.                     cout << "Введено недопустимое значение " << VES[i][j] << " Повторите ввод матрицы заново." << endl;
  116.                 }
  117.         }
  118.         //проверка пути
  119.         bool put_b = true;
  120.         int* put_mas = new int[V];
  121.         int kk = 0;
  122.         int put_count_iteration_2 = -1;
  123.         int* put_temp_metka = new int[V*V];
  124.         put_temp_metka[0] = -100;
  125.         int pk = 1;
  126.         put_temp_metka[pk] = V;
  127.         put_b = put(put_b, pk, put_temp_metka, VES, V);
  128.         if (put_b == true)
  129.         {
  130.             int t = 0;
  131.             int p = 0;
  132.             int *mas_use = new int[VV];
  133.             int count_iteration_1 = -1;
  134.             int temp_metka = start;
  135.             int **total_min = new int*[VV]; //минимум в каждой итерации
  136.             for (int i = 0; i < VV; i++)
  137.                 total_min[i] = new int[VV];
  138.             for (int i = 0; i < VV; i++)
  139.                 total_min[i][p] = INT_MAX;
  140.             int **massiv_temp_metok = new int*[VV]; //массив временных меток
  141.             for (int i = 0; i < VV; i++)
  142.                 massiv_temp_metok[i] = new int[VV];
  143.             int *mass_x = new int[V];
  144.             int r = 0;
  145.             for (int i = 1; i < V; i++)
  146.                 if (i != 0)
  147.                 {
  148.                     mass_x[i - 1] = i;
  149.                     r++;
  150.                 }
  151.             cout << "Шаг 1. Т.к. d(s)=" << temp_metka << "*, то полагаем, d(x1)=0*, X=x1, d(x2)=...=d(x" << V << ")= inf" << endl;
  152.             //////////////////////////
  153.             bool put = false;
  154.             if (VES[0][V] != 0)
  155.                 put = true;
  156.             int **massiv_metok_for_output = new int*[V];
  157.             for (int i = 0; i < V; i++)
  158.                 massiv_metok_for_output[i] = new int[VV];
  159.             /////////////////////////
  160.             while (temp_metka != V)
  161.             {
  162.                 count_iteration_1++;
  163.                 cout << endl;
  164.                 int temp;
  165.                 int q = 0;
  166.                 int g = 0;
  167.                 int **min = new int*[1];
  168.                 for (int i = 0; i < V; i++)
  169.                     min[i] = new int[V];
  170.                 int m_i = 0, m_j = 0;
  171.                 int *temp_x = new int[V];
  172.                 for (int i = 0; i < V; i++)
  173.                     temp_x[i] = INT_MAX;
  174.                 int *temp_value = new int[V];
  175.                 int *temp_value_original = new int[V];
  176.                 int k = 0;
  177.                 cout << count_iteration_1 + 1 << " итерация" << endl;
  178.                 /////////////////////////////////////////////////////////
  179.                 cout << "Шаг 2. Множество вершин, непосредственно следующих за X=х" << temp_metka + 1 << " с временными метками" << endl;
  180.                 int *mass_x_temp = new int[r];
  181.                 int y = 0;
  182.                 for (int j = 0; j < V; j++)
  183.                 {
  184.                     massiv_temp_metok[count_iteration_1][j] = VES[temp_metka][j];
  185.                     massiv_metok_for_output[count_iteration_1][j] = VES[temp_metka][j];
  186.                     if (VES[temp_metka][j] != 0)
  187.                     {
  188.                         bool flag = true;
  189.                         for (int i = 0; i < t; i++)
  190.                         {
  191.                             if (mas_use[i] == j)
  192.                             {
  193.                                 flag = false;
  194.                                 break;
  195.                             }
  196.                             else
  197.                             {
  198.                                 flag = true;
  199.                             }
  200.                         }
  201.                         if (flag == true)
  202.                         {
  203.                             temp_x[k] = j;
  204.                             k++;
  205.                         }
  206.                         temp_value[k] = VES[temp_metka][j];
  207.                         temp_value_original[j] = VES[count_iteration_1][j];
  208.                     }
  209.                 }
  210.                 if (count_iteration_1 >= 1)
  211.                 {
  212.                     int y = 0;
  213.                     bool flag2 = false;
  214.                     for (int i = 0; i < r; i++)
  215.                     {
  216.                         if (mass_x[i] != temp_metka)
  217.                         {
  218.                             mass_x_temp[y] = mass_x[i];
  219.                             y++;
  220.                         }
  221.                     }
  222.                     mass_x = &mass_x_temp[0];
  223.                     r--;
  224.                 }
  225.                 cout << "S = {";
  226.                 for (int i = 0; i < k; i++)
  227.                 {
  228.                     if (i < k - 1)
  229.                         cout << "x" << temp_x[i] + 1 << ", ";
  230.                     else
  231.                         cout << "x" << temp_x[i] + 1;
  232.                 }
  233.                 cout << "}.Пересчитаем временные метки этих вершин: " << endl;
  234.                 if (count_iteration_1 == 0)
  235.                 {
  236.                     for (int i = 0; i < k; i++)
  237.                     {
  238.                         int one = INT_MAX;
  239.                         int two = temp_metka + temp_value[i + 1];
  240.                         if (one < two)
  241.                         {
  242.                             min[q][g] = one;
  243.                             for (int i = 0; i < V; i++)
  244.                                 if (massiv_temp_metok[count_iteration_1][i] == min[q][g])
  245.                                 {
  246.                                     min[q][g + 1] = i;
  247.                                 }
  248.                         }
  249.                         else
  250.                         {
  251.                             min[q][g] = two;
  252.                             for (int i = 0; i < V; i++)
  253.                                 if (massiv_temp_metok[count_iteration_1][i] == min[q][g])
  254.                                 {
  255.                                     min[q][g + 1] = i;
  256.                                 }
  257.                         }
  258.                         cout << "d(x" << temp_x[i] + 1 << ") = min {" << "inf" << "," << 0 << "*+" << temp_value[i + 1] << "}= " << min[q][g] << endl;
  259.                         q++;
  260.                     }
  261.                 }
  262.                 else if (count_iteration_1 >= 1)
  263.                 {
  264.                     for (int i = 0; i < k; i++)
  265.                     {
  266.                         int one = massiv_temp_metok[count_iteration_1 - 1][temp_x[i]];
  267.                         if (massiv_temp_metok[count_iteration_1 - 1][temp_x[i]] == 0)
  268.                             one = INT_MAX;
  269.                         int two = total_min[count_iteration_1 - 1][p] + temp_value[i + 1];
  270.                         if (one > two)
  271.                         {
  272.                             min[q][g] = two;
  273.                         }
  274.                         else if (one < two)
  275.                         {
  276.                             min[q][g] = one;
  277.                         }
  278.                         else if (one == two)
  279.                             min[q][g] = one;
  280.                         cout << "d(x" << temp_x[i] + 1 << ") = min {";
  281.                         if (massiv_temp_metok[count_iteration_1 - 1][temp_x[i]] == INT_MAX)
  282.                             cout << "inf";
  283.                         else
  284.                             cout << massiv_temp_metok[count_iteration_1 - 1][temp_x[i]];
  285.                         cout << "," << total_min[count_iteration_1 - 1][p] << "*+" << temp_value[i + 1];
  286.                         cout << "}=" << min[q][g] << endl;
  287.                         q++;
  288.                     }
  289.                 }
  290.                 /////////////////////////////////////////////////////////
  291.                 cout << "Шаг 3. min{";
  292.                 for (int i = 0; i < r; i++)
  293.                 {
  294.                     if (i < r - 1)
  295.                         cout << "x" << mass_x[i] + 1 << ", ";
  296.                     else
  297.                         cout << "x" << mass_x[i] + 1;
  298.                 }
  299.                 cout << "} = min{";
  300.                 if (count_iteration_1 >= 1)
  301.                     for (int j = 0; j < V; j++)
  302.                     {
  303.                         massiv_temp_metok[count_iteration_1][j] = massiv_temp_metok[count_iteration_1 - 1][j];
  304.                         //if(massiv_temp_metok[count_iteration_1 - 1][j] == INT_MAX)
  305.                         massiv_metok_for_output[count_iteration_1][j] = massiv_metok_for_output[count_iteration_1 - 1][j];
  306.                         //massiv_metok_for_output[count_iteration_1][j] = 999;
  307.                     }
  308.                 for (int j = 0; j < V; j++)
  309.                     if (massiv_temp_metok[count_iteration_1][j] == 0)
  310.                         massiv_temp_metok[count_iteration_1][j] = INT_MAX;
  311.                 int temp_count = 0;
  312.                 for (int i = 0; i < V; i++)
  313.                 {
  314.                     for (int a = 0; a < k; a++)
  315.                     {
  316.                         if (temp_x[a] == i)
  317.                         {
  318.                             temp_count++;
  319.                             massiv_temp_metok[count_iteration_1][i] = min[temp_count - 1][g];
  320.                             massiv_metok_for_output[count_iteration_1][i] = min[temp_count - 1][g];
  321.                         }
  322.                     }
  323.                 }
  324.                 for (int i = 0; i < r; i++)
  325.                 {
  326.                     if (i < r - 1)
  327.                     {
  328.                         if (massiv_temp_metok[count_iteration_1][mass_x[i]] == INT_MAX)
  329.                             cout << "inf" << ", ";
  330.                         else
  331.                             cout << massiv_temp_metok[count_iteration_1][mass_x[i]] << ", ";
  332.                     }
  333.                     else
  334.                     {
  335.                         if (massiv_temp_metok[count_iteration_1][mass_x[i]] == INT_MAX)
  336.                             cout << "inf" << ", ";
  337.                         else
  338.                             cout << massiv_temp_metok[count_iteration_1][mass_x[i]];
  339.                     }
  340.                 }
  341.                 int temp_int;
  342.                 for (int i = 0; i < V; i++)
  343.                 {
  344.                     if (total_min[count_iteration_1][p] > massiv_temp_metok[count_iteration_1][i])
  345.                     {
  346.                         total_min[count_iteration_1][p] = massiv_temp_metok[count_iteration_1][i];
  347.                         total_min[count_iteration_1][p + 1] = i;
  348.                         temp = i + 1;
  349.                         temp_int = i;
  350.                     }
  351.                 }
  352.                 massiv_temp_metok[count_iteration_1][temp_int] = INT_MAX;
  353.                 cout << "}=" << total_min[count_iteration_1][p] << "*=x";
  354.                 mas_use[t] = temp - 1;
  355.                 t++;
  356.                 cout << temp << ", тогда X=x" << temp << endl;
  357.                 /////////////////////////////////////////////////////////
  358.                 if (temp != V)
  359.                 {
  360.                     cout << "Шаг 4. т.к.х" << temp << "!=х" << V << ", то возвращаемся ко второму шагу." << endl;
  361.                     temp_metka = temp - 1;
  362.                 }
  363.                 else
  364.                 {
  365.                     cout << "Шаг 4. т.к.х" << temp << "=х" << V << " - КОНЕЦ АЛГОРИТМА" << endl;
  366.                     temp_metka = V;
  367.                 }
  368.             }
  369.             cout << "Минимальный путь = " << total_min[count_iteration_1][p] << endl << endl;
  370.             ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  371.             cout << "ЭТАП 2" << endl;
  372.             int count_iteration_2 = -1;
  373.             total_min[count_iteration_1 + 1][p + 1] = 0;
  374.             total_min[count_iteration_1 + 1][p] = 0;
  375.             int *total_min_step2 = new int[V];
  376.             for (int i = 0; i < V; i++)
  377.                 total_min_step2[i] = INT_MAX;
  378.             int h = 0;
  379.             int *temp_result = new int[V*V];
  380.             int *metka = new int[V];
  381.             while (temp_metka != 1)
  382.             {
  383.                 count_iteration_2++;
  384.                 cout << count_iteration_2 + 1 << " итерация" << endl;
  385.                 int *temp_x = new int[V];
  386.                 int k = 0;
  387.                 for (int j = 0; j < V; j++)
  388.                 {
  389.                     if (VES[j][temp_metka - 1] != 0)
  390.                     {
  391.                         {
  392.                             temp_x[k] = j;
  393.                             k++;
  394.                         }
  395.                     }
  396.                 }
  397.                 cout << "Шаг 5. S={";
  398.                 int c;
  399.                 for (int i = 0; i < k; i++)
  400.                 {
  401.                     c = temp_x[i];
  402.                     if (i < k - 1)
  403.                         cout << "x" << temp_x[i] + 1 << ", ";
  404.                     else
  405.                         cout << "x" << temp_x[i] + 1;
  406.                 }
  407.                 cout << "}" << endl;
  408.                 int index;
  409.                 int a = 0;
  410.                 int m = 0;
  411.                 /////////
  412.                 for (int i = 0; i < k; i++)
  413.                 {
  414.                     for (int j = 0; j < V; j++)
  415.                         if (total_min[j][p + 1] == temp_x[i])
  416.                         {
  417.                             //cout << "Делаю" << total_min[j][p] << "+" << VES[temp_x[i]][temp_metka - 1] << endl;
  418.                             //cout << "Прошло сравнение " << total_min[j][p + 1] << " == " << temp_x[i] << "\n";
  419.                             if (total_min[j][p] != INT_MAX)
  420.                             {
  421.                                 temp_result[m] = total_min[j][p] + VES[temp_x[i]][temp_metka - 1];
  422.                                 if (total_min_step2[a] >= temp_result[m])
  423.                                 {
  424.                                     total_min_step2[a] = temp_result[m];
  425.                                     index = temp_x[i];
  426.                                 }
  427.                                 m++;
  428.                             }
  429.                         }
  430.                 }
  431.                 m = 0;
  432.                 for (int i = 0; i < k; i++)
  433.                 {
  434.                     if (total_min_step2[a] != temp_result[m])
  435.                         cout << "d(X)=" << total_min_step2[a] << "!=d(x";
  436.                     else
  437.                         cout << "d(X)=" << total_min_step2[a] << "=d(x";
  438.                     cout << temp_x[i] + 1 << ")+w(x";
  439.                     cout << temp_x[i] + 1 << ", x" << temp_metka << ")=";
  440.                     for (int j = 0; j < V; j++)
  441.                     {
  442.                         if (total_min[j][p + 1] == temp_x[i])
  443.                         {
  444.                             if (total_min[j][p] != INT_MAX)
  445.                                 cout << total_min[j][p] << "+" << VES[temp_x[i]][temp_metka - 1] << "=" << temp_result[m] << endl;
  446.                         }
  447.                     }
  448.                     m++;
  449.                 }
  450.                 a++;
  451.                 cout << "Включаем дугу(";
  452.                 for (int i = 0; i < k; i++)
  453.                 {
  454.                     if (temp_x[i] == index)
  455.                     {
  456.                         cout << "x" << temp_x[i] + 1 << ",x" << temp_metka;
  457.                         temp_metka = temp_x[i] + 1;
  458.                     }
  459.                 }
  460.                 cout << ") в кратчайший путь Х = х" << temp_metka << "." << endl << endl;
  461.                 metka[h] = temp_metka;
  462.                 h++;
  463.             }
  464.             ////////////
  465.             if (temp_metka == 1)
  466.             {
  467.                 cout << "Шаг 6. Х=s=х" << temp_metka << ", завершение второго этапа." << endl << endl;
  468.                 cout << "Кратчайший путь от вершины х" << V << " построен.";
  469.                 cout << "Его длина(вес) равна " << total_min[count_iteration_1][p] << "." << endl;
  470.                 cout << "Cам путь образует следующая последовательность дуг: µ=";
  471.                 for (int i = h - 1; i >= 0; --i)
  472.                     cout << "x" << metka[i] << "->";
  473.                 cout << "x" << V << endl;
  474.             }
  475.             cout << "\t\tМассив временных меток" << endl;
  476.             for (int i = 0; i < V; i++)
  477.             {
  478.                 if (i == 0)
  479.                     cout << 0 << "\t";
  480.                 else
  481.                     cout << "inf\t";
  482.             }
  483.             cout << endl;
  484.             int metk;
  485.             int cc = 0;
  486.             int *col = new int[V];
  487.             int c_col = 0;
  488.             for (int i = 0; i <= count_iteration_1; i++)
  489.             {
  490.                 bool met = false;
  491.                 bool stroka = false;
  492.                 for (int j = 0; j < V; j++)
  493.                 {
  494.                     if (massiv_metok_for_output[i][j] == total_min[cc][0] && col[c_col - 1] != j)
  495.                     {
  496.                         if (stroka == false)
  497.                         {
  498.                             cc++;
  499.                             met = true;
  500.                         }
  501.                     }
  502.                     if (met == true && stroka == false)
  503.                     {
  504.                         stroka = true;
  505.                         if (j == 0)
  506.                             cout << "0*\t";
  507.                         else
  508.                         {
  509.                             if (massiv_metok_for_output[i][j] == 0)
  510.                                 cout << "inf\t";
  511.                             else
  512.                             {
  513.                                 if (col[c_col - 1] == j)
  514.                                 {
  515.                                     cout << massiv_metok_for_output[i][j] << "\t";
  516.                                 }
  517.                                 else
  518.                                     cout << massiv_metok_for_output[i][j] << "*\t";
  519.                                 col[c_col] = j;
  520.                                 c_col++;
  521.                             }
  522.                         }
  523.                     }
  524.                     else
  525.                     {
  526.                         if (j == 0)
  527.                             cout << "0\t";
  528.                         else
  529.                         {
  530.                             if (massiv_metok_for_output[i][j] == 0)
  531.                                 cout << "inf\t";
  532.                             else
  533.                                 cout << massiv_metok_for_output[i][j] << "\t";
  534.                         }
  535.                     }
  536.                 }
  537.                 cout << endl;
  538.             }
  539.         }
  540.         ///////////////////////
  541.         system("pause");
  542.     }
  543.    
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement