Professional Documents
Culture Documents
Raport: Lucrare de Laborator Nr. 4
Raport: Lucrare de Laborator Nr. 4
Raport
Analiza și proiectarea algoritmilor
Realizat de :
Verificat de :
Chișinau 2020
1. De implementat într-un limbaj de programare algoritmii Prim şi Kruskal.
#include <iostream>
#include <vector>
#include <ctime>
int find(int i)
{
while (set[i] != i)
i = set[i];
return i;
}
int edge_count = 0;
while (edge_count < V - 1) {
int min = MAX_INT, a = -1, b = -1;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (find(i) != find(j) && matrix[i][j] < min && matrix[i][j] != 0) {
min = matrix[i][j];
a = i;
b = j;
}
}
}
union_set(a, b);
edge_count++;
cout << "Connecting edge " << a << " with " << b << endl;
weight += min;
}
cout << endl;
cout << "Total weight = " << weight << endl;
}
inMST[0] = true;
int main()
{
int matrix[][V] = {
{0,1,0,0,0,1,0,0,0,0},
{1,0,2,0,1,0,0,0,0,0},
{0,2,0,3,0,0,0,0,0,0},
{0,0,3,0,2,0,0,5,0,0},
{0,1,0,2,0,5,0,4,0,0},
{1,0,0,0,5,0,4,0,0,0},
{0,0,0,0,0,4,0,3,5,0},
{0,0,0,5,4,0,3,0,2,3},
{0,0,0,0,0,0,5,2,0,1},
{0,0,0,0,0,0,0,3,1,0}
};
return 0;
}
Matrice 50 x 50:
Kruskal's algorithm:
Connecting edge 0 with 4
Connecting edge 0 with 8
Connecting edge 0 with 23
Connecting edge 0 with 29
Connecting edge 0 with 36
Connecting edge 0 with 41
Connecting edge 0 with 46
Connecting edge 1 with 2
Connecting edge 2 with 18
Connecting edge 2 with 28
Connecting edge 3 with 1
Connecting edge 3 with 22
Connecting edge 3 with 24
Connecting edge 3 with 29
Connecting edge 4 with 21
Connecting edge 4 with 47
Connecting edge 5 with 9
Connecting edge 5 with 13
Connecting edge 5 with 23
Connecting edge 5 with 27
Connecting edge 5 with 31
Connecting edge 5 with 42
Connecting edge 5 with 49
Connecting edge 6 with 4
Connecting edge 6 with 19
Connecting edge 6 with 40
Connecting edge 7 with 27
Connecting edge 7 with 44
Connecting edge 8 with 10
Connecting edge 8 with 30
Connecting edge 9 with 16
Connecting edge 10 with 20
Connecting edge 11 with 25
Connecting edge 11 with 32
Connecting edge 12 with 7
Connecting edge 12 with 15
Connecting edge 13 with 37
Connecting edge 14 with 0
Connecting edge 14 with 38
Connecting edge 15 with 43
Connecting edge 17 with 4
Connecting edge 19 with 26
Connecting edge 25 with 10
Connecting edge 26 with 33
Connecting edge 27 with 39
Connecting edge 34 with 8
Connecting edge 35 with 13
Connecting edge 38 with 45
Connecting edge 45 with 48
Total weight = 49
Execution time: 0.011911
Prim's algorithm:
Connecting edge 0 with 4
Connecting edge 0 with 8
Connecting edge 0 with 23
Connecting edge 0 with 29
Connecting edge 0 with 36
Connecting edge 0 with 41
Connecting edge 0 with 46
Connecting edge 3 with 29
Connecting edge 3 with 1
Connecting edge 1 with 2
Connecting edge 2 with 18
Connecting edge 2 with 28
Connecting edge 3 with 22
Connecting edge 3 with 24
Connecting edge 4 with 21
Connecting edge 4 with 47
Connecting edge 5 with 23
Connecting edge 5 with 9
Connecting edge 5 with 13
Connecting edge 5 with 27
Connecting edge 5 with 31
Connecting edge 5 with 42
Connecting edge 5 with 49
Connecting edge 6 with 4
Connecting edge 6 with 19
Connecting edge 6 with 40
Connecting edge 7 with 27
Connecting edge 7 with 44
Connecting edge 8 with 10
Connecting edge 8 with 30
Connecting edge 9 with 16
Connecting edge 10 with 20
Connecting edge 12 with 7
Connecting edge 12 with 15
Connecting edge 13 with 37
Connecting edge 14 with 0
Connecting edge 14 with 38
Connecting edge 15 with 43
Connecting edge 17 with 4
Connecting edge 19 with 26
Connecting edge 25 with 10
Connecting edge 11 with 25
Connecting edge 11 with 32
Connecting edge 26 with 33
Connecting edge 27 with 39
Connecting edge 34 with 8
Connecting edge 35 with 13
Connecting edge 38 with 45
Connecting edge 45 with 48
Minimum cost= 49
Execution time: 0.022137
int m;
int matrix[V][V];
for(int i = 0; i < V; i++){
for(int j = 0; j < V; j++){
for (int k = 1; k <= 13; k++){
m = rand()% 13;
matrix[i][j]= m;
}
}
}
for(int i=0;i< V;i++){
for(int j = 0;j < V; j++){
cout<< matrix[i][j]<<" ";
}
cout<<"\n";
}
2. De făcut analiza empirică a algoritmilor Kruskal şi Prim.
Pentru valori mari :
Kruskal – verde, Prim – roșu;
Concluzie :
În această lucrare de laborator am studiat algoritmii greedy și mai ales arborii de acoperire minimi
Kruskal și Prim. Algoritmul Kruskal alege muchia de cost minim nealeasă anterior și care nu formează
un ciclu, iar disjoint sets ne permite ușor să executăm algoritmul dat verificând fiecare criteriu cerut. În
algoritmul Prim am inițializat lungimea vârfurile neconectate cu infinit pentru a putea mai ușor afla
compara cu valoarea min în timpul executării algoritmului. Se creează 2 mulțimi, una care conține toate
muchiile grafului iar alta inițial vidă, dar care mai târziu va conține muchiile cu cost minim. La fiecare
pas se alege o muchie de cost minim care se va adăuga în mulțimea u. În ambele cazuri de creează un
arbore, care conectează toate vârfurile grafului. Totuși, după timpul de execuție, putem observa că
algoritmul Kruskal e puțin mai rapid decât Prim, deoarece Prim pierde timp pentru a verifica existența
conexiunii, în timp ce Kruskal deja unește vârfurile în aceleași seturi.