Advertisement
Guest User

Alg. Kruskal

a guest
Oct 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. ifstream f("graf.txt");
  8.  
  9. int m, n, viz[100];
  10.  
  11. struct muchie {
  12.     int vf1, vf2, cost;
  13. }x[100];
  14.  
  15. void citire() {
  16.     f >> n >> m;
  17.  
  18.     for(int i = 1; i <= m; i++)
  19.         f >> x[i].vf1 >> x[i].vf2 >> x[i].cost;
  20. }
  21.  
  22. void sortare() {
  23.     for(int i = 1; i < m; i++)
  24.         for(int j = i; j <= m; j++)
  25.             swap(x[i],x[j]);
  26. }
  27.  
  28. /* void citire1() { citire de tripleti de pe fiecare rand
  29.     f >> n;
  30.     m = 0;
  31.     while(f >> t >> y >> z) {
  32.         m++;
  33.         x[m].vf1 = t;
  34.         x[m].vf2 = y;
  35.         x[m].cost = z;
  36.     }
  37. } */
  38.  
  39. void APM2() { // algoritmul lui Prim
  40.    
  41. }
  42.  
  43. void APM() { // algoritmul lui kruskal -> def. APM
  44.     int c[20], i, k, comp2, comp1, v1, v2;
  45.  
  46.     for(i = 1; i <= n; i++)
  47.         c[i] = i;
  48.  
  49.     sortare();
  50.  
  51.     k = 0; // numara muchiile alese sa faca parte din arborele partial de cost minim
  52.     i = 1; // urmareste la rand muchiile care sunt sortate dupa cost
  53.  
  54.     while(k < n-1) {
  55.         v1 = x[i].vf1; // notez cu v1 o extremitate a muchiei 13
  56.         v2 = x[i].vf2;
  57.         if(c[v1] != c[v2]) {
  58.             printf("vf1: %d, vf2: %d, cost: %d", x[i].vf1, x[i].vf2, x[i].cost);
  59.             k ++;
  60.             comp1 = c[v1];
  61.             comp2 = c[v2];
  62.             for(int j = 1; j <= n; i++) { // mut toate varfurile care fac parte din c[v2] in c[v1]
  63.                 if(c[j] == comp2) c[j] = comp1;
  64.             }
  65.         }
  66.         i++;
  67.     }
  68. }
  69.  
  70. int main()
  71. {
  72.     citire();
  73.     APM();
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement