Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

shortest path tree using prim’s algorithm/ {condition of IF statment}

$
0
0
Hello
I am working to find shortest path tree using prim’s algorithm
I used Number of children metric to find the shortest tree

In this part of the code I tried to find number of children for each node
Its work correctly


/ Here i found number of children for each node
//Some nodes are not appearing as parent that’s ok because they don’t have any linke (no childern’s)

int childe[100]={0}, s=0;

for(i=0;i<n;i++)
{ s=0;
  for(j=i+1;j<n;j++)
  {
   if(new_wght[i][j]!=INF)
   { Parent[j] = i;
      if(Parent[j]==i)
	{
		s++;
		childe[i]=s;
	 }
cout<<"parent :"<<i<<" has  "<<childe[i]<<" childern "<<endl;
}
} 
} 


Now I need to put a condition so that the tree will be rearrange …eg: if I have a node want to choose a parent and it can connect directly to sink(root) but the weight of the sink is 5, and there is another nod of weigh 2 , I will connect with the lowest weight (the lowest weight here is number of children for any parent )

:notify: I am thinking to check what is the parent of each node
See how many children for each parent
Save the result of the previous step in some where
Then compare the result with the parent array , then take the lowest parent as parent for the node


/ here I  tried to find the hops from any node to the sink//
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++){
	if(Parent[j]==i){
Hops[j] = Hops[i] + 1;
myfile<<"I= "<<j<<"   Parent: "<<Parent[j]<<" Hops : "<<Hops[j]<<endl<<endl;
}
}
}




After rearrange the tree I have to calculate number of hops from each node in the tree to the sink


Any suggestion will be appreciated

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>