5-amaliy mashg’ulot Ishdan maqsad


Download 89.15 Kb.
bet3/3
Sana23.10.2023
Hajmi89.15 Kb.
#1717612
1   2   3
this->n = n;
parent = new int[n+1];
rnk = new int[n+1];
for (int i = 0; i <= n; i++)
{
rnk[i] = 0;
//every element is parent of itself
parent[i] = i;
}
}
int find(int u)
{
/* Make the parent of the nodes in the path
from u--> parent[u] point to parent[u] */
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
// Union by rank
void merge(int x, int y)
{
x = find(x), y = find(y);
/* Make tree with smaller height
a subtree of the other tree */
if (rnk[x] > rnk[y])
parent[y] = x;
else // If rnk[x] <= rnk[y]
parent[x] = y;
if (rnk[x] == rnk[y])
rnk[y]++;
}
};
/* Functions returns weight of the MST*/
int Graph::kruskalMST()
{
int mst_wt = 0; // Initialize result
sort(edges.begin(), edges.end());
DisjointSets ds(V);
vector< pair >::iterator it;
for (it=edges.begin(); it!=edges.end(); it++)
{
int u = it->second.first;
int v = it->second.second;
int set_u = ds.find(u);
int set_v = ds.find(v);
if (set_u != set_v)
{
// Current edge will be in the MST
// so print it
cout << u << " - " << v << endl;
// Update MST weight
mst_wt += it->first;
// Merge two sets
ds.merge(set_u, set_v);
}
}
return mst_wt;
}
int main()
{
int V = 9, E = 14;
Graph g(V, E);
g.addEdge(0, 1, 4);
g.addEdge(0, 7, 8);
g.addEdge(1, 2, 8);
g.addEdge(1, 7, 11);
g.addEdge(2, 3, 7);
g.addEdge(2, 8, 2);
g.addEdge(2, 5, 4);
g.addEdge(3, 4, 9);
g.addEdge(3, 5, 14);
g.addEdge(4, 5, 10);
g.addEdge(5, 6, 2);
g.addEdge(6, 7, 1);
g.addEdge(6, 8, 6);
g.addEdge(7, 8, 7);
cout << "Edges of MST are \n";
int mst_wt = g.kruskalMST();
cout << "\nWeight of MST is " << mst_wt;
return 0;
}
Quyida tasvirlangan graf uchun minimal narxli daraxt skeletini qurish usullaridan Primo usuli yordamida yechilsin va dasturini tuzilsin.

Prim algoritmini amalga oshirish bosqichlari quyidagicha:

  1. Minimal uzunlikdagi daraxtni tasodifiy tanlangan tugun bilan boshlang.

  2. Daraxtni yangi tugunlar bilan bog'laydigan barcha qirralarni toping, minimalini toping va daraxtga qo'shing

  3. Minimal daraxt daraxti olinmaguncha, 2-bosqichni takrorlang




Tasvir

U tanlangan tugunlar to’plami

(u,v)yoylar

v\u tanlanmagan tugunlar

izox



{}




{A,B,C,D,E,F,G}

Dastlab daraxt tugunlar to’plami bo’sh



{D}

(D,A) = 5 V
(D,B) = 9
(D,E) = 15
(D,F) = 6

{A,B,C,E,F,G}

Ilk tugun sifatida Dolindi. Unga tegishli yoylar A,B,E,F tugunlarga olib boradi. Ularning minimal yoyligini tanlaymiz. ya’ni A



{A,D}

(D,B) = 9
(D,E) = 15
(D,F) = 6 V
(A,B) = 7

{B,C,E,F,G}






{A,D,F}

(D,B) = 9
(D,E) = 15
(A,B) = 7 V
(F,E) = 8
(F,G) = 11

{B,C,E,G}






{A,B,D,F}

(B,C) = 8
(B,E) = 7 V
(D,B) = 9 sikl
(D,E) = 15
(F,E) = 8
(F,G) = 11

{C,E,G}

(D,B)yoy tanlansa xalqa xosil bo’ladi. Shuning uchun uni tanlay olmaymiz.



{A,B,D,E,F}

(B,C) = 8
(D,B) = 9 sikl
(D,E) = 15 sikl
(E,C) = 5 V
(E,G) = 9
(F,E) = 8 sikl
(F,G) = 11

{C,G}






{A,B,C,D,E,F}

(B,C) = 8 sikl
(D,B) = 9 sikl
(D,E) = 15 sikl
(E,G) = 9 V
(F,E) = 8 sikl
(F,G) = 11

{G}






{A,B,C,D,E,F,G}

(B,C) = 8 sikl
(D,B) = 9 sikl
(D,E) = 15 sikl
(F,E) = 8 sikl
(F,G) = 11 sikl

{}

Grafdagi barcha tugunlar tekshirib bo’ldi.

// Prim's Algorithm in C++


#include
#include
using namespace std;
#define INF 9999999
#define V 5
int G[V][V] = {
{0, 9, 75, 0, 0},
{9, 0, 95, 19, 42},
{75, 95, 0, 51, 66},
{0, 19, 51, 0, 31},
{0, 42, 66, 31, 0}};
int main() {
int no_edge; // number of edge
int selected[V];
memset(selected, false, sizeof(selected));
no_edge = 0;
selected[0] = true;
int x; // row number
int y; // col number
cout << "Edge" << " : " << "Weight";
cout << endl;
while (no_edge < V - 1) {
int min = INF;
x = 0;
y = 0;
for (int i = 0; i < V; i++) {
if (selected[i]) {
for (int j = 0; j < V; j++) {
if (!selected[j] && G[i][j]) { // not in selected and there is an edge
if (min > G[i][j]) {
min = G[i][j];
x = i;
y = j;
}
}
}
}
}
cout << x << " - " << y << " : " << G[x][y];
cout << endl;
selected[y] = true;
no_edge++;
}
return 0;
}


Topshiriq
1. Har bir talaba yo‘naltirilgan va yo‘naltirilmagan graf yasasin.Tugunlar soni 10-12 ta. Unga mos qo‘shma , munosabat matrisalari va qo‘shnichilik va yoylar ro‘yxati tuzilsin.
2. Har bir talaba vaznli graf yasasin.Tugunlar soni 10-12 ta. Graf uchun eng qisqa yo’lni aniqlash algoritmlaridan bo’lgan Primo va Kruskal usullarini qo’llab, eng qisqa yo’l aniqlansin.
Download 89.15 Kb.

Do'stlaringiz bilan baham:
1   2   3




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