Advertisement
baadgeorge

Untitled

Mar 26th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. const int inf = INT32_MAX; // бесконечно большая величина
  7. const int null = INT32_MIN;
  8.  
  9. const int n = 15; // кол-во вершин графа
  10.  
  11. struct _edge
  12. {
  13.     int cost;
  14.     int parent1;
  15.     int parent2;
  16. };
  17.  
  18. int leader[n] = { 0 };
  19.  
  20. void make_set(int v)
  21. {
  22.     leader[v] = v;
  23. }
  24.  
  25. int find_set(int v)
  26. {
  27.     if (v == leader[v])
  28.         return v;
  29.     return leader[v] = find_set(leader[v]);
  30. }
  31.  
  32. int union_sets(int a, int b)
  33. {
  34.     a = find_set(a);
  35.     b = find_set(b);
  36.     if (a != b)
  37.     {
  38.         leader[b] = a;
  39.         return 1;
  40.     }
  41.     return 0;
  42. }
  43.  
  44. void Floyd_Warshall(int matrix[][n], int(*shortest)[n][n][n + 1], int(*pred)[n][n][n + 1])
  45. {
  46.     for (int u = 0; u < n; u++)
  47.         for (int v = 0; v < n; v++)
  48.         {
  49.             (*shortest)[u][v][0] = matrix[u][v];
  50.             if ((matrix[u][v] == inf) || (matrix[u][v] == 0)) (*pred)[u][v][0] = null;
  51.             else (*pred)[u][v][0] = u + 1;
  52.         }
  53.  
  54.     for (int x = 1; x < n + 1; x++)
  55.     {
  56.         for (int i = 0; i < n; i++)
  57.             for (int j = 0; j < n; j++)
  58.                 (*shortest)[i][j][x] = (*shortest)[i][j][x - 1];
  59.  
  60.         for (int u = 0; u < n; u++)
  61.             for (int v = 0; v < n; v++)
  62.             {
  63.                 if (((*shortest)[u][v][x] > (((*shortest)[u][x - 1][x - 1] + (*shortest)[x - 1][v][x - 1]))) && ((((*shortest)[u][x - 1][x - 1]) != inf) && (((*shortest)[x - 1][v][x - 1]) != inf)))
  64.                 {
  65.  
  66.                     (*shortest)[u][v][x] = (*shortest)[u][x - 1][x - 1] + (*shortest)[x - 1][v][x - 1];
  67.                     (*pred)[u][v][x] = (*pred)[x - 1][v][x - 1];
  68.                 }
  69.                 else
  70.                 {
  71.                     (*shortest)[u][v][x] = (*shortest)[u][v][x - 1];
  72.                     (*pred)[u][v][x] = (*pred)[u][v][x - 1];
  73.                 }
  74.             }
  75.     }
  76. }
  77.  
  78. void outputFW(int shortest[n][n][n + 1], int pred[n][n][n + 1])
  79. {
  80.     for (int x = 0; x < n + 1; x++)
  81.     {
  82.         cout << "\n****************************************************\n";
  83.         cout << "\nx = " << x << "\n";
  84.  
  85.         cout << "\nSHORTEST: \n\n";
  86.         for (int u = 0; u < n; u++)
  87.         {
  88.             for (int v = 0; v < n; v++)
  89.             {
  90.                 if (shortest[u][v][x] == inf) cout << setw(8) << "inf" << " ";
  91.                 else cout << setw(8) << shortest[u][v][x] << " ";
  92.             }
  93.             cout << "\n";
  94.         }
  95.  
  96.         cout << "\nPRED: \n\n";
  97.         for (int u = 0; u < n; u++)
  98.         {
  99.             for (int v = 0; v < n; v++)
  100.             {
  101.                 if (pred[u][v][x] == null) cout << setw(8) << "NULL" << " ";
  102.                 else cout << setw(8) << pred[u][v][x] << " ";
  103.             }
  104.             cout << "\n";
  105.         }
  106.     }
  107.     cout << "\n****************************************************\n\n";
  108. }
  109.  
  110. void outputMatrix(int matrix[][n])
  111. {
  112.     for (int u = 0; u < n; u++)
  113.     {
  114.         for (int v = 0; v < n; v++)
  115.         {
  116.             if (matrix[u][v] == inf) cout << setw(8) << "inf" << " ";
  117.             else cout << setw(8) << matrix[u][v] << " ";
  118.         }
  119.         cout << "\n";
  120.     }
  121. }
  122.  
  123. void makeGraphUnoriented(int(*matrix)[n][n], int* RealEdgeCount)
  124. {
  125.     for (int u = 0; u < n; u++)
  126.     {
  127.         for (int v = u + 1; v < n; v++)
  128.         {
  129.             if (((*matrix)[u][v] == inf) && ((*matrix)[v][u] != inf))
  130.             {
  131.                 (*matrix)[u][v] = (*matrix)[v][u];
  132.                 (*RealEdgeCount)++;
  133.             }
  134.                
  135.             if (((*matrix)[v][u] == inf) && ((*matrix)[u][v] != inf))
  136.             {
  137.                 (*matrix)[v][u] = (*matrix)[u][v];
  138.                 (*RealEdgeCount)++;
  139.             }
  140.                
  141.         }
  142.     }
  143. }
  144.  
  145. void ShellSort(_edge **edge, int RealEdgeCount) // СОРТИРОВКА ШЕЛЛА
  146. {
  147.     int h = 0;
  148.  
  149.     while (h < RealEdgeCount / 3)
  150.     {
  151.         h = 3 * h + 1;
  152.     }
  153.  
  154.     for (h; h > 0; h = (h - 1) / 3)
  155.     {
  156.         for (int i = h; i < RealEdgeCount; i++)
  157.         {
  158.             int j = i;
  159.             _edge tmp = (*edge)[i];
  160.  
  161.             for (j = i; j >= h && (*edge)[j - h].cost > tmp.cost; j -= h)
  162.             {
  163.                 (*edge)[j] = (*edge)[j - h];
  164.  
  165.             }
  166.             (*edge)[j] = tmp;
  167.         }
  168.     }
  169.  
  170.     return;
  171. }
  172.  
  173. void graphKruskalAlgorithm(int matrix [n][n], int RealEdgeCount)
  174. {
  175.     int newMatrix[n][n];
  176.  
  177.     for (int u = 0; u < n; u++)
  178.         for (int v = 0; v < n; v++)
  179.             if (u == v) newMatrix[u][v] = 0;
  180.             else newMatrix[u][v] = inf;
  181.  
  182.     //outputMatrix(newMatrix);
  183.  
  184.     int i = 0;
  185.  
  186.     cout << "\n\nKruskal algorithm\n";
  187.  
  188.     _edge* edge = new _edge[RealEdgeCount];
  189.  
  190.     for (int u = 0; u < n; u++)
  191.     {
  192.         for (int v = u + 1; v < n; v++)
  193.         {
  194.             if(matrix[u][v]!=inf)
  195.             {
  196.                 edge[i].cost = matrix[u][v];
  197.                 edge[i].parent1 = u;
  198.                 edge[i].parent2 = v;
  199.                 i++;
  200.             }
  201.            
  202.         }
  203.     }
  204.     cout << "\nUnsorted Edges: \n";
  205.     for (int k = 0; k < RealEdgeCount; k++)
  206.         cout << "\tEdge " << k + 1 << ": Cost = " << edge[k].cost << ", 1st Parent = " << edge[k].parent1 + 1 << ", 2nd Parent = " << edge[k].parent2 + 1 << "\n";
  207.     cout << "\n";
  208.  
  209.     ShellSort(&edge, RealEdgeCount);
  210.  
  211.     cout << "\nSorted Edges: \n";
  212.     for (int k = 0; k < RealEdgeCount; k++)
  213.         cout << "\tEdge " << k + 1 << ": Cost = " << edge[k].cost << ", 1st Parent = " << edge[k].parent1 + 1 << ", 2nd Parent = " << edge[k].parent2 + 1 << "\n";
  214.     cout << "\n";
  215.  
  216.     for (i = 0; i < n; i++)
  217.     {
  218.         make_set(i);
  219.     }
  220.  
  221.     for (i = 0; i < RealEdgeCount; i++)
  222.     {
  223.         if (edge[i].cost == inf) continue;
  224.         if (union_sets(edge[i].parent1, edge[i].parent2) == 1)
  225.         {
  226.             newMatrix[edge[i].parent1][edge[i].parent2] = edge[i].cost;
  227.             newMatrix[edge[i].parent2][edge[i].parent1] = edge[i].cost;
  228.         }
  229.     }
  230.  
  231.     for (int u = 0; u < n; u++)
  232.         for (int v = 0; v < n; v++)
  233.             matrix[u][v] = newMatrix[u][v];
  234. }
  235.  
  236. int main()
  237. {
  238.  
  239.     setlocale(LC_ALL, "RUS");
  240.  
  241.     int shortest[n][n][n + 1] = { 0 };
  242.     int pred[n][n][n + 1] = { 0 };
  243.  
  244.     int RealEdgeCount = 0;
  245.    
  246.     int matrix[n][n] =
  247.     {
  248.     {   0,  -1, inf,   2, inf, inf, inf, inf, inf, inf,  8, inf, inf, inf, inf },
  249.     { inf,   0, inf,   3, inf,  10, inf, inf, inf, inf, inf, inf, inf, inf, inf },
  250.     { inf,   4,   0, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf },
  251.     { inf, inf, inf,   0, inf, inf,  13, inf, inf, inf,  -7, inf, inf, inf, inf },
  252.     { inf, inf, inf, inf,   0, inf, inf,   3, inf,  11, inf, inf, inf, inf, inf },
  253.     { inf, inf, inf, inf, inf,   0, inf, inf,  18, inf, inf, inf, inf, inf, inf },
  254.     { inf, inf, inf, inf, inf, inf,   0, inf, inf,  15, inf, inf, inf, inf, inf },
  255.     { inf, inf, inf, inf, inf,  -8, inf,  0, inf, inf, inf, inf, inf, inf, inf },
  256.     { inf, inf, inf, inf, inf, inf, inf, inf,   0, inf, inf, inf, inf, inf, inf },
  257.     { inf, inf, inf, inf, inf, inf, inf, inf,  19,   0, inf, inf, inf, inf, inf },
  258.     { inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,   0, inf, inf,   2,  11 },
  259.     { inf, inf, inf, inf,  16, inf, inf, inf, inf, inf, inf,   0,  12, inf, inf },
  260.     { inf, inf, inf, inf, inf, inf,  17, inf, inf, inf, inf, inf,   0, inf, inf },
  261.     { inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,   0,   9 },
  262.     { inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,   1,  -7, inf,   0 }
  263.     };
  264.  
  265.     Floyd_Warshall(matrix, &shortest, &pred);
  266.  
  267.     outputFW(shortest, pred);
  268.  
  269.     cout << "\nTransforming graph into unoriented one...\n\n";
  270.  
  271.     makeGraphUnoriented(&matrix, &RealEdgeCount);
  272.  
  273.     cout << "\nResulting matrix: \n\n";
  274.  
  275.     outputMatrix(matrix);
  276.  
  277.     graphKruskalAlgorithm(matrix, RealEdgeCount);
  278.  
  279.     cout << "\nResulting graph: \n\n";
  280.  
  281.     outputMatrix(matrix);
  282.  
  283.     cout << "\n";
  284.  
  285.     system("pause");
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement