Sa se determine un arbore partial de cost minim folosind algoritmul Kruskal
Pentru memorarea muchiilor grafului si a costurilor acestora se defineste o structura de date cu trei campuri (nodurile muchiei si costul ei) pe care o numim muchie
#include<iostream.h>
#include<fstream.h>
typedef struct{int u,v,c;} muchie;
int l[30],n,m;
muchie e[30];
void citire()
{int i;
fstream f(“apm.txt”, ios::in);
f>>n;
f>>m;
for(i=1;i<=m;i++)
f>>e[i].u>>e[i].v>>e[i].c;
f.close();}
void main()
{int k,ultim,i,u,v,ct,ind,ms,lu,lv;
muchie aux;
citire();
for(i=1;i<=n;i++)
l[i]=i;
ultim=m;
while(ultim>1)
{k=0;
for(i=1;i<=ultim-1;i++)
if(e[i].c>e[i+1].c)
{aux=e[i];
e[i]=e[i+1];
e[i+1]=aux;
k=1;
}
ultim=k;}
cout<<“APM contine muchiile:”<<endl;
ct=0;ms=0;ind=0;
while(ms<n-1)
{
do
ind++;
while(l[e[ind].u]==l[e[ind].v]);
u=e[ind].u;
lu=l[u];
v=e[ind].v;
lv=l[v];
cout<<u<<” “<<v<<endl;
ct=ct+e[ind].c;
ms++;
for(i=1;i<=n;i++)
if(l[i]==lu)
l[i]=lv;}
cout<<“costul APM este:”<<ct;}