Sunteți pe pagina 1din 12

Ministerul Educației, Culturii și Cercetării al Republicii Moldova

Universitatea Tehnică a Moldovei


Facultatea Calculatoare, Informatică și Microelectronică

Raport la
Lucrarea de laborator Nr. 4
Disciplina: Algoritmi si proiectarea algoritmilor

Tema: ” Algoritmi Greedy.”

Au efectuat: Botnaru Alexandru

A verificat: Mihai Gaidau

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.*;

public class Main {

public static void main(String[] args) {

PrimMST t = new PrimMST();

int graph1[][] = new int[][]{

{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);

int V = 5; // Number of vertices in graph

int E = 7; // Number of edges in graph

KruskalMST graph2 = new KruskalMST(V, E);

// add edge 0-1

graph2.edge[0].src = 0;

graph2.edge[0].dest = 1;

graph2.edge[0].weight = 2;

// add edge 0-3

graph2.edge[1].src = 0;

graph2.edge[1].dest = 3;

graph2.edge[1].weight = 6;

// add edge 1-3

graph2.edge[2].src = 1;

graph2.edge[2].dest = 3;

graph2.edge[2].weight = 8;

// add edge 1-2

graph2.edge[3].src = 1;

graph2.edge[3].dest = 2;

graph2.edge[3].weight = 3;

// add edge 1-4

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;

// add edge 3-4

graph2.edge[6].src = 3;

graph2.edge[6].dest = 4;

graph2.edge[6].weight = 9;

// Function call

graph2.KruskalMST();

class PrimMST {

private static final int V = 5;

int minKey(int key[], Boolean mstSet[])

int min = Integer.MAX_VALUE, min_index = -1;

for (int v = 0; v < V; v++)

if (mstSet[v] == false && key[v] < min) {

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");

for (int i = 1; i < V; i++) {

System.out.println(parent[i] + " - " + i + "\t" + graph[i]


[parent[i]]);

cost += graph[i][parent[i]];

System.out.println("Costul total: " + cost);

void primMST(int graph[][])

long start = System.nanoTime();

int parent[] = new int[V];

int key[] = new int[V];

Boolean mstSet[] = new Boolean[V];

for (int i = 0; i < V; i++) {

key[i] = Integer.MAX_VALUE;

mstSet[i] = false;

key[0] = 0;

parent[0] = -1;
for (int count = 0; count < V - 1; count++) {

int u = minKey(key, mstSet);

mstSet[u] = true;

for (int v = 0; v < V; v++)

if (graph[u][v] != 0 && mstSet[v] == false && graph[u][v] <


key[v]) {

parent[v] = u;

key[v] = graph[u][v];

printMST(parent, graph);

long end = System.nanoTime();

System.out.println("Timpul executiei :" + (end - start));

class KruskalMST {

// A class to represent a graph edge

class Edge implements Comparable<Edge> {

int src, dest, weight;

// Comparator function used for

// sorting edgesbased on their weight


public int compareTo(Edge compareEdge) {

return this.weight - compareEdge.weight;

// A class to represent a subset for

// union-find

class subset {

int parent, rank;

};

int V, E; // V-> no. of vertices & E->no.of edges

Edge edge[]; // collection of all edges

// Creates a graph with V vertices and E edges

KruskalMST(int v, int e) {

V = v;

E = e;

edge = new Edge[E];

for (int i = 0; i < e; ++i)

edge[i] = new Edge();

// A utility function to find set of an

// element i (uses path compression technique)

int find(subset subsets[], int i) {

// find root and make root as parent of i

// (path compression)
if (subsets[i].parent != i)

subsets[i].parent

= find(subsets, subsets[i].parent);

return subsets[i].parent;

// A function that does union of two sets

// of x and y (uses union by rank)

void Union(subset subsets[], int x, int y) {

int xroot = find(subsets, x);

int yroot = find(subsets, y);

// Attach smaller rank tree under root

// of high rank tree (Union by Rank)

if (subsets[xroot].rank

< subsets[yroot].rank)

subsets[xroot].parent = yroot;

else if (subsets[xroot].rank

> subsets[yroot].rank)

subsets[yroot].parent = xroot;

// If ranks are same, then make one as

// root and increment its rank by one

else {

subsets[yroot].parent = xroot;

subsets[xroot].rank++;

// The main function to construct MST using Kruskal's

// algorithm
void KruskalMST() {

long start = System.nanoTime();

// Tnis will store the resultant MST

Edge result[] = new Edge[V];

// An index variable, used for result[]

int e = 0;

// An index variable, used for sorted edges

int i = 0;

for (i = 0; i < V; ++i)

result[i] = new Edge();

// Step 1: Sort all the edges in non-decreasing

// order of their weight. If we are not allowed to

// change the given graph, we can create a copy of

// array of edges

Arrays.sort(edge);

// Allocate memory for creating V ssubsets

subset subsets[] = new subset[V];

for (i = 0; i < V; ++i)

subsets[i] = new subset();

// Create V subsets with single elements

for (int v = 0; v < V; ++v) {

subsets[v].parent = v;

subsets[v].rank = 0;

i = 0; // Index used to pick next edge


// Number of edges to be taken is equal to V-1

while (e < V - 1) {

// Step 2: Pick the smallest edge. And increment

// the index for next iteration

Edge next_edge = new Edge();

next_edge = edge[i++];

int x = find(subsets, next_edge.src);

int y = find(subsets, next_edge.dest);

// If including this edge does't cause cycle,

// include it in result and increment the index

// of result for next edge

if (x != y) {

result[e++] = next_edge;

Union(subsets, x, y);

// Else discard the next_edge

// print the contents of result[] to display

// the built MST

System.out.println("\nKruskal: ");

System.out.println("Muchia \t Costul");

int minimumCost = 0;

for (i = 0; i < e; ++i) {

System.out.println(result[i].src + " - "

+ result[i].dest

+ " \t " + result[i].weight);

minimumCost += result[i].weight;

}
System.out.println("Costul total: "

+ minimumCost);

long end = System.nanoTime();

System.out.println("Timpul executiei: " + (end-start));

180000000

160000000

140000000

120000000
Numarul de iteratii

100000000

80000000

60000000

40000000

20000000

0
n=100 n=1000 n=2000 n=5000

GnomeSort MergeSort QuickSort

Gnome Sort : O(N^2), O(n) in cel mai bun caz


Merge Sort : O(nLogn) – pentru toate cazurile
Quick Sort: O(n log n) ,O(n^2) in cel mai rau caz

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.

S-ar putea să vă placă și

  • SSM Tema 1FFCIM
    SSM Tema 1FFCIM
    Document17 pagini
    SSM Tema 1FFCIM
    Nastea13
    Încă nu există evaluări
  • Anul III EvP1 1
    Anul III EvP1 1
    Document3 pagini
    Anul III EvP1 1
    AlexandruBotnaru
    Încă nu există evaluări
  • Lab 1 Docx
    Lab 1 Docx
    Document6 pagini
    Lab 1 Docx
    AlexandruBotnaru
    Încă nu există evaluări
  • Lab 2
    Lab 2
    Document4 pagini
    Lab 2
    AlexandruBotnaru
    Încă nu există evaluări
  • Lab 1 Docx
    Lab 1 Docx
    Document6 pagini
    Lab 1 Docx
    AlexandruBotnaru
    Încă nu există evaluări
  • Lab 2
    Lab 2
    Document4 pagini
    Lab 2
    AlexandruBotnaru
    Încă nu există evaluări
  • Lab 1 Docx
    Lab 1 Docx
    Document6 pagini
    Lab 1 Docx
    AlexandruBotnaru
    Încă nu există evaluări
  • Cap1-3 PPE
    Cap1-3 PPE
    Document81 pagini
    Cap1-3 PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Test2proba PPE
    Test2proba PPE
    Document30 pagini
    Test2proba PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Cap1-3 PPE
    Cap1-3 PPE
    Document81 pagini
    Cap1-3 PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Cap1-3 PPE
    Cap1-3 PPE
    Document81 pagini
    Cap1-3 PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Cap1-3 PPE
    Cap1-3 PPE
    Document81 pagini
    Cap1-3 PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Cap1-3 PPE
    Cap1-3 PPE
    Document81 pagini
    Cap1-3 PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Cap1-3 PPE
    Cap1-3 PPE
    Document81 pagini
    Cap1-3 PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Cap1-3 PPE
    Cap1-3 PPE
    Document81 pagini
    Cap1-3 PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Laborator 3 2020
    Laborator 3 2020
    Document1 pagină
    Laborator 3 2020
    AlexandruBotnaru
    Încă nu există evaluări
  • Cap1-3 PPE
    Cap1-3 PPE
    Document81 pagini
    Cap1-3 PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Atestarea 1 AC
    Atestarea 1 AC
    Document18 pagini
    Atestarea 1 AC
    AlexandruBotnaru
    Încă nu există evaluări
  • Atestare RC 2
    Atestare RC 2
    Document6 pagini
    Atestare RC 2
    AlexandruBotnaru
    Încă nu există evaluări
  • Raspunsuri
     Raspunsuri
    Document39 pagini
    Raspunsuri
    Munteanu Doinelush
    Încă nu există evaluări
  • Raspunsuri Teste Examen
    Raspunsuri Teste Examen
    Document14 pagini
    Raspunsuri Teste Examen
    Damean Alexandra
    Încă nu există evaluări
  • Raspunsuri
     Raspunsuri
    Document39 pagini
    Raspunsuri
    Munteanu Doinelush
    Încă nu există evaluări
  • Test1proba PPE
    Test1proba PPE
    Document20 pagini
    Test1proba PPE
    AlexandruBotnaru
    Încă nu există evaluări
  • Laborator 2
    Laborator 2
    Document6 pagini
    Laborator 2
    AlexandruBotnaru
    Încă nu există evaluări
  • Laborator 1
    Laborator 1
    Document5 pagini
    Laborator 1
    AlexandruBotnaru
    Încă nu există evaluări
  • Cristian Frasinaru-Curs Practic de Java
    Cristian Frasinaru-Curs Practic de Java
    Document462 pagini
    Cristian Frasinaru-Curs Practic de Java
    anon-331879
    100% (14)
  • Laborator 4 OOP
    Laborator 4 OOP
    Document4 pagini
    Laborator 4 OOP
    AlexandruBotnaru
    Încă nu există evaluări
  • Laboratorul 8 LFA
    Laboratorul 8 LFA
    Document4 pagini
    Laboratorul 8 LFA
    AlexandruBotnaru
    Încă nu există evaluări
  • Laborator 2
    Laborator 2
    Document6 pagini
    Laborator 2
    AlexandruBotnaru
    Încă nu există evaluări