Guruh talabalari: -guruh: – Dadajonov Hikmatillo


Kruskal va Prim algoritmi o'rtasidagi farq nima?



Download 465,38 Kb.
bet9/24
Sana06.07.2022
Hajmi465,38 Kb.
#750621
1   ...   5   6   7   8   9   10   11   12   ...   24
Bog'liq
Abdulhamidov Abduqodir 613-20 guruhi

Kruskal va Prim algoritmi o'rtasidagi farq nima?
• Prim algoritmi tugun bilan boshlanadi, Kruskal algoritmi chekka bilan boshlanadi.
• Prim algoritmlari bir tugundan ikkinchisigacha cho'zilgan, Kruskal algoritmi esa qirralarning joylashuvi oxirgi bosqichga asoslanmagan tarzda tanlangan.
• Prim algoritmida grafik bog'langan grafik bo'lishi kerak, Kruskal esa uzilgan grafiklarda ham ishlashi mumkin.
• Prim algoritmining vaqt murakkabligi O (V 2 ), Kruskalning vaqt murakkabligi O (logV).
Amaliy qism







// Kruskal's algorithm in C++


#include
#include
#include
using namespace std;


#define edge pair


class Graph {
private:
vector
int, edge> > G; // graph
vector
int, edge> > T; // mst
int *parent;
int V; // number of vertices/nodes in graph
public:
Graph(int V);
void AddWeightedEdge(int u, int v, int w);
int find_set(int i);
void union_set(int u, int v);
void kruskal();
void print();
};
Graph::Graph(int V) {
parent = new int[V];


//i 0 1 2 3 4 5
//parent[i] 0 1 2 3 4 5
for (int i = 0; i < V; i++)
parent[i] = i;


G.clear();
T.clear();
}
void Graph::AddWeightedEdge(int u, int v, int w) {
G.push_back(make_pair(w, edge(u, v)));
}
int Graph::find_set(int i) {
// If i is the parent of itself
if (i == parent[i])
return i;
else
// Else if i is not the parent of itself
// Then i is not the representative of his set,
// so we recursively call Find on its parent
return find_set(parent[i]);
}


void Graph::union_set(int u, int v) {
parent[u] = parent[v];
}
void Graph::kruskal() {
int i, uRep, vRep;
sort(G.begin(), G.end()); // increasing weight
for (i = 0; i < G.size(); i++) {
uRep = find_set(G[i].second.first);
vRep = find_set(G[i].second.second);
if (uRep != vRep) {
T.push_back(G[i]); // add to tree
union_set(uRep, vRep);
}
}
}
void Graph::print() {
cout << "Edge :"
<< " Weight" << endl;
for (int i = 0; i < T.size(); i++) {
cout << T[i].second.first << " - " << T[i].second.second << " : "
<< T[i].first;
cout << endl;
}
}
int main() {
Graph g(6);
g.AddWeightedEdge(0, 1, 4);
g.AddWeightedEdge(0, 2, 4);
g.AddWeightedEdge(1, 2, 2);
g.AddWeightedEdge(1, 0, 4);
g.AddWeightedEdge(2, 0, 4);
g.AddWeightedEdge(2, 1, 2);
g.AddWeightedEdge(2, 3, 3);
g.AddWeightedEdge(2, 5, 2);
g.AddWeightedEdge(2, 4, 4);
g.AddWeightedEdge(3, 2, 3);
g.AddWeightedEdge(3, 4, 3);
g.AddWeightedEdge(4, 2, 4);
g.AddWeightedEdge(4, 3, 3);
g.AddWeightedEdge(5, 2, 2);
g.AddWeightedEdge(5, 4, 3);
g.kruskal();
g.print();
return 0;
}



// C++ tilida Kruskal algoritmi

#include


#include
#o'z ichiga
std nom maydonidan foydalanish;

#chek juftligini aniqlang


sinf grafigi {


shaxsiy:
vektor > G; // grafik
vektor > T; // mst
int *ota-ona;
int V; // grafikdagi uchlari/tugunlar soni
ommaviy:
Grafik (int V);
void AddWeightedEdge(int u, int v, int w);
int find_set (int i);
void union_set (int u, int v);
void kruskal();
bekor chop etish();
};
Grafik::Graph(int V) {
ota = new int[V];

//i 0 1 2 3 4 5


//ota-ona[i] 0 1 2 3 4 5
uchun (int i = 0; i < V; i++)
ota-ona [i] = i;

G.clear();


T.clear();
}
void Grafik ::AddWeightedEdge(int u, int v, int w) {
G.surish_orqa(juftlik_make(w, chekka(u, v)));
}
int Grafik ::find_set(int i) {
// Agar i o'zining ota-onasi bo'lsa
agar (i == ota-ona[i])
qaytish i;
boshqa
// Aks holda, agar i o'zining ota-onasi bo'lmasa
// Unda men uning to'plamining vakili emasman,
// shuning uchun biz uning ota-onasini rekursiv ravishda Find deb chaqiramiz
find_setni qaytarish (ota-ona [i]);
}

bekor Grafik::union_set(int u, int v) {


ota-ona[u] = ota-ona[v];
}
void Grafik ::kruskal() {
int i, uRep, vRep;
sort(G.begin(), G.end()); // vaznni oshirish
uchun (i = 0; i < G.size(); i++) {
uRep = find_set(G[i].ikkinchi.birinchi);
vRep = find_set(G[i].second.second);
agar (uRep != vRep) {
T.push_back(G[i]); // daraxtga qo'shish
union_set(uRep, vRep);
}
}
}
void Graph::print() {
cout << "Chet:"
<< "Og'irlik" << endl;
uchun (int i = 0; i < T.size(); i++) {
cout << T[i].ikkinchi.birinchi << " - " << T[i].ikkinchi.soniya << " : "
<< T[i].birinchi;
cout << endl;
}
}
int main() {
Grafik g(6);
g.AddWeightedEdge(0, 1, 4);
g.AddWeightedEdge(0, 2, 4);
g.AddWeightedEdge(1, 2, 2);
g.AddWeightedEdge(1, 0, 4);
g.AddWeightedEdge(2, 0, 4);
g.AddWeightedEdge(2, 1, 2);
g.AddWeightedEdge(2, 3, 3);
g.AddWeightedEdge(2, 5, 2);
g.AddWeightedEdge(2, 4, 4);
g.AddWeightedEdge(3, 2, 3);
g.AddWeightedEdge(3, 4, 3);
g.AddWeightedEdge(4, 2, 4);
g.AddWeightedEdge(4, 3, 3);
g.AddWeightedEdge(5, 2, 2);
g.AddWeightedEdge(5, 4, 3);
g.kruskal();
g.print();
qaytish 0;
}



Download 465,38 Kb.

Do'stlaringiz bilan baham:
1   ...   5   6   7   8   9   10   11   12   ...   24




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish