Documente Academic
Documente Profesional
Documente Cultură
Raport la
Lucrarea de laborator Nr. 4
Disciplina: Algoritmi si proiectarea algoritmilor
Chișinău – 2020
Scopul lucrării:
1. Studierea tehnicii greedy.
2. Analiza şi implementarea algoritmilor greedy
SARCINA DE BAZĂ:
1. De studiat tehnica greedy de proiectare a algoritmilor.
2. De implementat într-un limbaj de programare algoritmii
Prim şi Kruskal.
3. De făcut analiza empirică a algoritmilor Kruskal şi Prim.
4. De alcătuit un raport.
Programul (JAVA):
(web)
import java.util.*;
import java.lang.*;
import java.io.*;
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}};
t.primMST(graph1);
graph2.edge[0].src = 0;
graph2.edge[0].dest = 1;
graph2.edge[0].weight = 2;
graph2.edge[1].src = 0;
graph2.edge[1].dest = 3;
graph2.edge[1].weight = 6;
graph2.edge[2].src = 1;
graph2.edge[2].dest = 3;
graph2.edge[2].weight = 8;
graph2.edge[3].src = 1;
graph2.edge[3].dest = 2;
graph2.edge[3].weight = 3;
graph2.edge[4].src = 1;
graph2.edge[4].dest = 4;
graph2.edge[4].weight = 5;
// add edge 2-4
graph2.edge[5].src = 2;
graph2.edge[5].dest = 4;
graph2.edge[5].weight = 7;
graph2.edge[6].src = 3;
graph2.edge[6].dest = 4;
graph2.edge[6].weight = 9;
// Function call
graph2.KruskalMST();
class PrimMST {
min = key[v];
min_index = v;
return min_index;
}
void printMST(int parent[], int graph[][])
int cost = 0;
System.out.println("\nPrim: ");
System.out.println("Muchia \tCostul");
cost += graph[i][parent[i]];
key[i] = Integer.MAX_VALUE;
mstSet[i] = false;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
mstSet[u] = true;
parent[v] = u;
key[v] = graph[u][v];
printMST(parent, graph);
class KruskalMST {
// union-find
class subset {
};
KruskalMST(int v, int e) {
V = v;
E = e;
// (path compression)
if (subsets[i].parent != i)
subsets[i].parent
= find(subsets, subsets[i].parent);
return subsets[i].parent;
if (subsets[xroot].rank
< subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank
> subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
// algorithm
void KruskalMST() {
int e = 0;
int i = 0;
// array of edges
Arrays.sort(edge);
subsets[v].parent = v;
subsets[v].rank = 0;
while (e < V - 1) {
next_edge = edge[i++];
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
System.out.println("\nKruskal: ");
System.out.println("Muchia \t Costul");
int minimumCost = 0;
+ result[i].dest
minimumCost += result[i].weight;
}
System.out.println("Costul total: "
+ minimumCost);
180000000
160000000
140000000
120000000
Numarul de iteratii
100000000
80000000
60000000
40000000
20000000
0
n=100 n=1000 n=2000 n=5000
Concluzie:
Scopul acestei lucrari de laborator a fost de a efectua analiza
empirica a 3 algoritmi destinati rezolvarii aceleiasi probleme : de a sorta
un tablou de numere in ordine crescatoare.Conform rezultatelor
obtinute,pot spune ca Merge Sort este cel mai optim algoritm dintre
acestea 3.Quick Sortul este eficient la sortarea unui tablou cu putine
elemente, iar Gnome Sortul este foarte eficient in cazul in care tabloul
este deja sortat, Merge Sortul avand o complexitate buna in toate
cazurile.