I'm trying to implement the Prim's algorithm. I am taking the input from a file as follows.
3 3 // Number of vertices and edges
1 2 3 // edge 1 edge 2 cost
2 3 4 // edge 2 edge 3 cost
1 3 4 // edge 1 edge 3 cost
I create a cost matrix as follows. Initially, every weight in the cost matrix is infinity (9999 in this case).
for(i = 0; i < n; i++)
{
for( j = 0; j < n; j++)
{
cost[i][j] = 9999;
}
}
Now, i need to update the weights of the cost matrix by reading the weights from the file. So, I am reading the file as follows.
ifstream fin;
fin.open("input.txt",ios::in);
fin >> n; //nodes
fin >> e; //edges
while(fin)
{
fin>>a>>b>>w;
cost[a-1][b-1] =cost[b-1][a-1]= w;
}
fin.close();
So, a and b are the edges and w is the weight for that edge. So, Suppose I have edge(1,2) and its weight is 3. So, my cost matrix cost[1][2]
and cost[2][1]
should update to 3. I am not able to figure out how I should update the cost matrix using file operations.
To put it again, I have a text file like the file I mentioned above. The first line of the file contains number of vertices in edges. I want to read the vertices in a variable v and edges in variable e. Then, I have an initial cost matrix cost[i][i]
where all values are infinity. I want to update the edges in this cost matrix from the file. So, i will read the second line from the file and update the cost[1][2]
= 3. I have no idea how to do this still.
Here's the full program I have now:
#include<iostream>
#include<fstream>
using namespace std;
int n,e,a,b,w;
int **cost = new int*[n];
void prim()
{
int i,j,k,l,x,nr[10],temp,min_cost=0;
int **tree = new int*[n];
for(i = 0; i < n; i++)
tree[i]=new int[n];
/* For first smallest edge */
temp=cost[0][0];
for(i=0;i< n;i++)
{
for(j=0;j< n;j++)
{
if(temp>cost[i][j])
{
temp=cost[i][j];
k=i;
l=j;
}
}
}
/* Now we have fist smallest edge in graph */
tree[0][0]=k;
tree[0][1]=l;
tree[0][2]=temp;
min_cost=temp;
/* Now we have to find min dis of each
vertex from either k or l
by initialising nr[] array
*/
for(i=0;i< n;i++)
{
if(cost[i][k]< cost[i][l])
nr[i]=k;
else
nr[i]=l;
}
/* To indicate visited vertex initialise nr[] for them to 100 */
nr[k]=100;
nr[l]=100;
/* Now find out remaining n-2 edges */
temp=99;
for(i=1;i< n-1;i++)
{
for(j=0;j< n;j++)
{
if(nr[j]!=100 && cost[j][nr[j]] < temp)
{
temp=cost[j][nr[j]];
x=j;
}
}
/* Now i have got next vertex */
tree[i][0]=x;
tree[i][1]=nr[x];
tree[i][2]=cost[x][nr[x]];
min_cost=min_cost+cost[x][nr[x]];
nr[x]=100;
/* Now find if x is nearest to any vertex
than its previous near value */
for(j=0;j< n;j++)
{
if(nr[j]!=100 && cost[j][nr[j]] > cost[j][x])
nr[j]=x;
}
temp=9999;
}
/* Now i have the answer, just going to print it */
cout<<"\n The minimum spanning tree is:"<<endl;
for(i=0;i< n-1;i++)
{
for(j=0;j< 3;j++)
cout<<tree[i][j];
cout<<endl;
}
cout<<"\nMinimum cost:";
cout<<min_cost;
}
int main()
{
int i,j;
for(i = 0; i < n; i++)
cost[i]=new int[n];
for(i = 0; i < n; i++)
{
for( j = 0; j < n; j++)
{
cost[i][j] = 9999;
}
}
ifstream fin;
fin.open("input.txt",ios::in);
//cout<<n<<e;
fin>>n>>e;
while(fin>>a>>b>>w)
{
cost[a-1][b-1] = w;
}
fin.close();
prim();
system("pause");
}