Xom9ik

Alg_Lab_11 (lll semester)

Dec 5th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. void Prima(int* graph[], int V)
  7. {
  8.     int *parent = new int[V];
  9.     int *key = new int[V];
  10.     bool *mstSet = new bool[V];
  11.     for (int i = 0; i < V; i++)
  12.         key[i] = INT_MAX, mstSet[i] = false;
  13.     key[0] = 0;
  14.     parent[0] = -1;
  15.     for (int count = 0; count < V - 1; count++)
  16.     {
  17.         int min = INT_MAX, min_index;
  18.         for (int v = 0; v < V; v++)
  19.             if (mstSet[v] == false && key[v] < min)
  20.                 min = key[v], min_index = v;
  21.         int u = min_index;
  22.         mstSet[u] = true;
  23.         for (int v = 0; v < V; v++)
  24.             if (graph[u][v] && mstSet[v] == false && graph[u][v] <  key[v])
  25.                 parent[v] = u, key[v] = graph[u][v];
  26.     }
  27.     int sum = 0;
  28.     for (int i = 1; i < V; i++)
  29.     {
  30.         cout << i << " edge " << parent[i] + 1 << "->" << i + 1 << "\t" << graph[i][parent[i]] << endl;
  31.         sum += graph[i][parent[i]];
  32.     }
  33.     cout << "Weight of the minimal skeleton of the graph(Prima): " << sum << endl;
  34. }
  35. int parent[100];
  36. void Kraskal(int* graph[], int V)
  37. {
  38.     int min, mincost = 0;
  39.     int i, j, k, a, b, u, v, ne = 1;
  40.     while (ne < V)
  41.     {
  42.         for (i = 0, min = INT_MAX; i < V; i++)
  43.         {
  44.             for (j = 0; j < V; j++)
  45.             {
  46.                 if (graph[i][j] == 0)
  47.                     graph[i][j] = INT_MAX;
  48.                 if (graph[i][j] < min)
  49.                 {
  50.                     min = graph[i][j];
  51.                     a = u = i;
  52.                     b = v = j;
  53.                 }
  54.             }
  55.         }
  56.         while (parent[u])
  57.             u = parent[i];
  58.         while (parent[v])
  59.             u = parent[i];
  60.         if (u != v)
  61.         {
  62.             parent[v] = u;
  63.             cout << ne++ << " edge " << (a + 1) << "->" << (b + 1) << "\t" << min << endl;
  64.             mincost += min;
  65.         }
  66.         graph[a][b] = graph[b][a] = INT_MAX;
  67.     }
  68.     cout << "Weight of the minimal skeleton of the graph(Kraskal): " << mincost << endl;
  69. }
  70.  
  71. int main()
  72. {
  73.     int V;
  74.     cout << "Enter the dimension of the graph: ";
  75.     cin >> V;
  76.     int **VES = new int*[V];
  77.     for (int i = 0; i < V; i++)
  78.         VES[i] = new int[V];
  79.     cout << "Enter weight:" << endl;
  80.     for (int i = 0; i < V; i++)
  81.         cout << "|x" << i + 1;
  82.     cout << endl;
  83.     for (int i = 0; i < V; i++)
  84.     {
  85.         cout << "X" << i + 1 << '|';
  86.         for (int j = 0; j < V; j++)
  87.             cin >> VES[i][j];
  88.     }
  89.     cout << "Edge\t\tWeight" << endl;
  90.     Prima(VES, V);
  91.     Kraskal(VES, V);
  92.     system("pause");
  93. }
Advertisement
Add Comment
Please, Sign In to add comment