Dsa 7
Dsa 7
PROBLEM STATEMENT:
You have a business with several offices; you want to lease phone lines to connect them up
with each other; and the phone company charges different amounts of money to connect
different pairs of cities. You want a set of lines that connects all your offices with
minimum total cost.
*/
#include<iostream>
class tree
int a[20][20],l,u,w,i,j,v,e,visited[20];
public:
void input();
void display();
void minimum();
};
void tree::input()
cin>>v;
for(i=0;i<v;i++)
visited[i]=0;
for(j=0;j<v;j++)
a[i][j]=999;
cin>>e;
for(i=0;i<e;i++)
cin>>l>>u;
cin>>w;
a[l-1][u-1]=a[u-1][l-1]=w;
void tree::display()
cout<<"\nAdjacency matrix:";
for(i=0;i<v;i++)
cout<<endl;
for(j=0;j<v;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
void tree::minimum()
int p=0,q=0,total=0,min;
visited[0]=1;
for(int count=0;count<(v-1);count++)
min=999;
for(i=0;i<v;i++)
if(visited[i]==1)
for(j=0;j<v;j++)
if(visited[j]!=1)
min=a[i][j];
p=i;
q=j;
visited[p]=1;
visited[q]=1;
total=total+min;
cout<<"Minimum cost connection is"<<(p+1)<<" -> "<<(q+1)<<" with charge :
"<<min<< endl;
int main()
int ch;
tree t;
do
cout<<"==========PRIM'S ALGORITHM================="<<endl;
cin>>ch;
switch(ch)
t.input();
break;
t.display();
break;
case 3: cout<<"MINIMUM*"<<endl;
t.minimum();
break;
}
}while(ch!=4);
return 0;
}