finiding min in array till satisfies condition explained in prbm stmnt

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 12
Reputation: tosh123 is an unknown quantity at this point 
Solved Threads: 0
tosh123 tosh123 is offline Offline
Newbie Poster

finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #1
Apr 3rd, 2008
hello.
i have a set of 50 points. (array of 50 objects (arrayOfNodes[]) having x, y locations and distance and nearest neighbor and neighbor table ). The points are given X-Y location randomly in a range of 300 X300. and there is a center point called BaseStaion (BS). For every point A, I have a neghbor table (array of int) where if the other point B is in range of 50mts then its a neighbor of A, so value in (arrayOfNodes[A].neighborTable[B]) is set to distance of point B to BS for all other points value is set to 9999. From this neighbor table I want to find a nearest neighbor (any point that is neighbor of A and its distance from BS is minimum). So the code finds that fine. But there are some cases where 2 points are nearest neighbors of each other. I dont want that to happen. so I need to find the next neighbor wich can be closer to the BS amongst teh other points. I wrote a code to find the second min in array but at times even the point at second minimum are neighbore of each other. So i have to look into many neighbors to decide the next nearest one!. I am not able to figure that out.

I am attaching some part of code here. Sorry! my problem description can be weird. Please let me know if i can make myself more clear.


  1. // set the vlaues in nebor table
  2. for (int i = 0; i < noOfNodes; i++)
  3. {
  4. for (int j =0; j<noOfNodes; j++)
  5. {
  6. if(i==j)
  7. arrayOfNodes[i].neighborTable[j] = 9999; //self node
  8. else
  9. if (euclideanDistance(arrayOfNodes[i].xLocation, arrayOfNodes[j].xLocation, arrayOfNodes[i].yLocation,arrayOfNodes[j].yLocation) < txRange)
  10. arrayOfNodes[i].neighborTable[j] = arrayOfNodes[j].distance;
  11. // if neighbor node, save distance from BS
  12. else
  13. arrayOfNodes[i].neighborTable[j] = 9999; // not neighbor
  14. }//end of for j
  15. }//end of for i
  16.  
  17. //find the neighbor closest to the BS and set it as nearestNeigborNode
  18.  
  19. for (int i = 0; i < noOfNodes; i++)
  20. {
  21.  
  22. /* for some nodes wich are in range of 50 from BS, their nearest nebor is set to BS. Did that in some other part of code */
  23.  
  24. if (arrayOfNodes[i].nearestNeighborNode != baseStation.nodeID)
  25. {
  26. int minValue = arrayOfNodes[i].neighborTable[0];
  27. for (int j =1; j<noOfNodes; j++)
  28. {
  29. if (arrayOfNodes[i].neighborTable[j] < minValue)
  30. {
  31. minValue = arrayOfNodes[i].neighborTable[j];
  32. arrayOfNodes[i].nearestNeighborNode = j;
  33. }
  34. }
  35. } //end of for i
  36.  
  37. /* I am facing problem here here i want to keep looking recursively for other nebors of A till i find B such that A and B are not nebors of each other. I did only to find second min, but i might have to look beyond second min. */
  38.  
  39. //for the case when 2 nodes are nearest neighbors of each other
  40. for (int i=0;i<noOfNodes;i++)
  41. for (int j =0 ; j<noOfNodes; j++)
  42. {
  43. if ((arrayOfNodes[i].nearestNeighborNode == j)&& (arrayOfNodes[j].nearestNeighborNode == i))
  44. { arrayOfNodes[i].neighborTable[j] == 1000;
  45.  
  46. int minValue2 = arrayOfNodes[i].neighborTable[0];
  47. for (int k =1; k<noOfNodes; k++)
  48. {
  49. if (arrayOfNodes[i].neighborTable[k] < minValue2)
  50. {
  51. minValue2 = arrayOfNodes[i].neighborTable[k];
  52. arrayOfNodes[i].nearestNeighborNode = k;
  53. }
  54. } //end of for k
  55. }//end of if
  56. }
  57. }//end of if (arrayOfNodes[i].nearestNeighborNode = baseStation.nodeID */
  58. } // end of i for nearest neighbor
Last edited by tosh123; Apr 3rd, 2008 at 7:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #2
Apr 3rd, 2008
How about if you sort the neighbors by distance. Then search the array from closest to furthest looking for closest unique distance determined something like this:

WARNING: Following snippett not rigorously tested
  1. type neighbors[capacity]
  2. int size = number of neighbors
  3. for(i = 0; i < size - 1; ++i)
  4. {
  5. if(neighbors[i] == neighbors[i + 1])
  6. while((i < size - 1) && (neighbors[i] == neighbors[i + 1]))
  7. ++i;
  8. else
  9. indexOfClosestUniqueNeighbor = i;
  10. break;
  11. }
  12. if((i == size -1) && (neighbors[i] != neighbors[i - 1]))
  13. indexOfClosestUniqueNeighbor = i;
  14. else
  15. cout << "no unique closest Neighbor"
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,839
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #3
Apr 3rd, 2008
I'm trying to make sense of this. Can you post the Node struct and your declarations of the BaseStation variable and the arrayOfNodes array?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: tosh123 is an unknown quantity at this point 
Solved Threads: 0
tosh123 tosh123 is offline Offline
Newbie Poster

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #4
Apr 4th, 2008
Originally Posted by VernonDozier View Post
I'm trying to make sense of this. Can you post the Node struct and your declarations of the BaseStation variable and the arrayOfNodes array?
  1. class node {
  2. public:
  3. int nodeID;
  4. int xLocation; //for the xy coordinates of the node
  5. int yLocation;
  6. float energyLevelOfNode;
  7. int hopCount;
  8. float distance; // distance of node form the base station
  9. int routeTable[][2];
  10. int nearestNeighborNode;
  11. int neighborTable [1000]; // saves the distance of neighbore node from BS. inorder to calc the nearest neighbor
  12. // int sendPacket();
  13. int neighborNode;
  14.  
  15. // initialization
  16. node (){
  17. xLocation = 100;
  18. yLocation = 100;
  19. hopCount = 0;
  20. distance = 0.0;
  21. energyLevelOfNode = 1000.0;// this value is just for the Base station. will reset for all the other nodes
  22. nodeID = 1000; //this value for BS. will set for other nodes
  23. }
  24. }; /*End of Class*/
  25.  
  26. node arrayOfNodes[1000], baseStation;
Last edited by tosh123; Apr 4th, 2008 at 12:05 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: tosh123 is an unknown quantity at this point 
Solved Threads: 0
tosh123 tosh123 is offline Offline
Newbie Poster

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #5
Apr 4th, 2008
Originally Posted by Lerner View Post
How about if you sort the neighbors by distance. Then search the array from closest to furthest looking for closest unique distance determined something like this:

WARNING: Following snippett not rigorously tested
  1. type neighbors[capacity]
  2. int size = number of neighbors
  3. for(i = 0; i < size - 1; ++i)
  4. {
  5. if(neighbors[i] == neighbors[i + 1])
  6. while((i < size - 1) && (neighbors[i] == neighbors[i + 1]))
  7. ++i;
  8. else
  9. indexOfClosestUniqueNeighbor = i;
  10. break;
  11. }
  12. if((i == size -1) && (neighbors[i] != neighbors[i - 1]))
  13. indexOfClosestUniqueNeighbor = i;
  14. else
  15. cout << "no unique closest Neighbor"
Thanks a lot! I really appreaciate your efforts. But I have some dbts -
The index of the array neighborTable[] is important as it reflects the node. So in that case if have to sort this array then how do i retain the original indices? Do i keep make it a 2D array and store the index as well as distance? how do i sort in thsi case?

secondly- the distances are not same. So even if i sort the nebor table of node A and have the index also accordinglyl, when i find the next closest neighbor, i will have to again check if the nearest nebor of that node is not equal to A. i am trying to figure out a way to do that!

any suggestions or inputs are most welcome!
thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: tosh123 is an unknown quantity at this point 
Solved Threads: 0
tosh123 tosh123 is offline Offline
Newbie Poster

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #6
Apr 4th, 2008
Originally Posted by Lerner View Post
How about if you sort the neighbors by distance. Then search the array from closest to furthest looking for closest unique distance determined something like this:

WARNING: Following snippett not rigorously tested
  1. type neighbors[capacity]
  2. int size = number of neighbors
  3. for(i = 0; i < size - 1; ++i)
  4. {
  5. if(neighbors[i] == neighbors[i + 1])
  6. while((i < size - 1) && (neighbors[i] == neighbors[i + 1]))
  7. ++i;
  8. else
  9. indexOfClosestUniqueNeighbor = i;
  10. break;
  11. }
  12. if((i == size -1) && (neighbors[i] != neighbors[i - 1]))
  13. indexOfClosestUniqueNeighbor = i;
  14. else
  15. cout << "no unique closest Neighbor"
I think i figured out someway to do this!
if i make nebortable as 2D array where
neighbortable[][1] = distance
neighbortable[][2] = nodeID
Assuming that this array is sorted by distance, i am really not concerned with actual distance value if array is sorted. i am only concerned with corresponding index/nodeID
then

for (int i =0;i<noOfNodes; i++)
for(int j=0;i<noOfNodes;j++)
if(arr[i].nearestNebor==j)&&(arr[j].nearestnebor==i)&&(i!=j)
{
int k=1;// (as i assume that arr[i].neborTable[0][2] == arr[i].nearestnebor==j)
while(arr[arr[i].neborTable[k][2]].nearestnebor == i)
{
k++;
}

arr[i].nearestnebor= arr[i].neborTable[k][2];

}

i hope this will work. not sure though. suggestions welcome
sorry for the complicated code.
Last edited by tosh123; Apr 4th, 2008 at 1:21 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,839
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #7
Apr 4th, 2008
I guess this comment doesn't so much address your particular task, but more the design of your class. I think you have too much duplicate storage of distances between Nodes and the base station. In particular, I think having this array as a data member of Node is a bad idea:
  1. int neighborTable [1000]; // saves the distance of neighbore node from BS. inorder to calc the nearest neighbor
This is an array of distances from other nodes to the base stateion. I think it would make sense if it was the distance between the node and some other node, but it isn't:

if the other point B is in range of 50mts then its a neighbor of A, so value in (arrayOfNodes[A].neighborTable[b]) is set to distance of point B to BS for all other points value is set to 9999.
I think you should have each Node store its own distance to the base station. Don't store the distance from Node B to the base station in Node A. It's already stored in Node B:
  1. float distance; // distance of node form the base station

What I think you SHOULD have in Node is a list of neighbor nodes. That could be a boolean array that is true if the Node in the corresponding arrayOfNodes index is a neighbor to this Node. Or it could a linked list of pointers to neighbor nodes. Or something. But somehow keep a list of neighbors so that you can reference a neighbor Node. Then reference that Node and get the "distance" data member from that neighbor Node. That's better than storing the same distances in a whole bunch of Nodes when it only needs to be stored once.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: tosh123 is an unknown quantity at this point 
Solved Threads: 0
tosh123 tosh123 is offline Offline
Newbie Poster

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #8
Apr 4th, 2008
Originally Posted by VernonDozier View Post
I guess this comment doesn't so much address your particular task, but more the design of your class. I think you have too much duplicate storage of distances between Nodes and the base station. In particular, I think having this array as a data member of Node is a bad idea:
  1. int neighborTable [1000]; // saves the distance of neighbore node from BS. inorder to calc the nearest neighbor
This is an array of distances from other nodes to the base stateion. I think it would make sense if it was the distance between the node and some other node, but it isn't:



I think you should have each Node store its own distance to the base station. Don't store the distance from Node B to the base station in Node A. It's already stored in Node B:
  1. float distance; // distance of node form the base station

What I think you SHOULD have in Node is a list of neighbor nodes. That could be a boolean array that is true if the Node in the corresponding arrayOfNodes index is a neighbor to this Node. Or it could a linked list of pointers to neighbor nodes. Or something. But somehow keep a list of neighbors so that you can reference a neighbor Node. Then reference that Node and get the "distance" data member from that neighbor Node. That's better than storing the same distances in a whole bunch of Nodes when it only needs to be stored once.
I know i have plenty of duplicate storage. But i did not want to use pointers. And I wanted to maintain a nebor table of every node wherein in need to know the distance of that nebor from the BS so that i get a nebor nearest to BS called nearestNeighborNode. i am doing this by simply finding a min in the neborTable array. So i thot that storing this way wud make simpler with the downside of duplicate storage. Wat i basically want to find is how many nodes i need from a node A to reach the BS.

Thanks for your comments.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,839
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #9
Apr 4th, 2008
Originally Posted by tosh123 View Post
I know i have plenty of duplicate storage. But i did not want to use pointers. And I wanted to maintain a nebor table of every node wherein in need to know the distance of that nebor from the BS so that i get a nebor nearest to BS called nearestNeighborNode. i am doing this by simply finding a min in the neborTable array. So i thot that storing this way wud make simpler with the downside of duplicate storage. Wat i basically want to find is how many nodes i need from a node A to reach the BS.

Thanks for your comments.
O.K. That's fine. It's doable without pointers. The extra storage won't kill you. I would make neighborTable type float though since "distance" in Node is type float.

You have an array of integers called neighborTable. One of them contains your the distance from your nearest neighbor to the base station. nearestNeighborNode is the index of the neighbor that is closest to the base station.

  1. nearestNeighborNode = -1; // initialize to bogus value;
  2. int shortestDistance = 10000; // initialize to bogus value;
  3. for (int i = 0; i < numNodes; i++)
  4. {
  5. // compare distance of node i to base
  6. // to shortestDistance. If less than,
  7. // set shortestDistance to the distance to
  8. // base from node i and set
  9. // nearestNeighorNode to i
  10. }
  11. // nearestNeighborNode is now set.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: tosh123 is an unknown quantity at this point 
Solved Threads: 0
tosh123 tosh123 is offline Offline
Newbie Poster

Re: finiding min in array till satisfies condition explained in prbm stmnt

 
0
  #10
Apr 4th, 2008
Originally Posted by VernonDozier View Post
O.K. That's fine. It's doable without pointers. The extra storage won't kill you. I would make neighborTable type float though since "distance" in Node is type float.

You have an array of integers called neighborTable. One of them contains your the distance from your nearest neighbor to the base station. nearestNeighborNode is the index of the neighbor that is closest to the base station.

  1. nearestNeighborNode = -1; // initialize to bogus value;
  2. int shortestDistance = 10000; // initialize to bogus value;
  3. for (int i = 0; i < numNodes; i++)
  4. {
  5. // compare distance of node i to base
  6. // to shortestDistance. If less than,
  7. // set shortestDistance to the distance to
  8. // base from node i and set
  9. // nearestNeighorNode to i
  10. }
  11. // nearestNeighborNode is now set.
thanks...now if u can refer to my earlier posts...inorder to solve my original Q, I might have to sort this neighbortable in ascending order based on distance. and also maintain the index(as it refers to the nodeID) . To do this now I will have to make neborTable as a 2 D array
neighbortable[][1] = distance
neighbortable[][2] = nodeID

how do i sort this array on distance?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 1996 | Replies: 21
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC