| | |
finiding min in array till satisfies condition explained in prbm stmnt
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
// set the vlaues in nebor table for (int i = 0; i < noOfNodes; i++) { for (int j =0; j<noOfNodes; j++) { if(i==j) arrayOfNodes[i].neighborTable[j] = 9999; //self node else if (euclideanDistance(arrayOfNodes[i].xLocation, arrayOfNodes[j].xLocation, arrayOfNodes[i].yLocation,arrayOfNodes[j].yLocation) < txRange) arrayOfNodes[i].neighborTable[j] = arrayOfNodes[j].distance; // if neighbor node, save distance from BS else arrayOfNodes[i].neighborTable[j] = 9999; // not neighbor }//end of for j }//end of for i //find the neighbor closest to the BS and set it as nearestNeigborNode for (int i = 0; i < noOfNodes; i++) { /* 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 */ if (arrayOfNodes[i].nearestNeighborNode != baseStation.nodeID) { int minValue = arrayOfNodes[i].neighborTable[0]; for (int j =1; j<noOfNodes; j++) { if (arrayOfNodes[i].neighborTable[j] < minValue) { minValue = arrayOfNodes[i].neighborTable[j]; arrayOfNodes[i].nearestNeighborNode = j; } } } //end of for i /* 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. */ //for the case when 2 nodes are nearest neighbors of each other for (int i=0;i<noOfNodes;i++) for (int j =0 ; j<noOfNodes; j++) { if ((arrayOfNodes[i].nearestNeighborNode == j)&& (arrayOfNodes[j].nearestNeighborNode == i)) { arrayOfNodes[i].neighborTable[j] == 1000; int minValue2 = arrayOfNodes[i].neighborTable[0]; for (int k =1; k<noOfNodes; k++) { if (arrayOfNodes[i].neighborTable[k] < minValue2) { minValue2 = arrayOfNodes[i].neighborTable[k]; arrayOfNodes[i].nearestNeighborNode = k; } } //end of for k }//end of if } }//end of if (arrayOfNodes[i].nearestNeighborNode = baseStation.nodeID */ } // end of i for nearest neighbor
Last edited by tosh123; Apr 3rd, 2008 at 7:37 pm.
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
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
WARNING: Following snippett not rigorously tested
C++ Syntax (Toggle Plain Text)
type neighbors[capacity] int size = number of neighbors for(i = 0; i < size - 1; ++i) { if(neighbors[i] == neighbors[i + 1]) while((i < size - 1) && (neighbors[i] == neighbors[i + 1])) ++i; else indexOfClosestUniqueNeighbor = i; break; } if((i == size -1) && (neighbors[i] != neighbors[i - 1])) indexOfClosestUniqueNeighbor = i; else cout << "no unique closest Neighbor"
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
class node { public: int nodeID; int xLocation; //for the xy coordinates of the node int yLocation; float energyLevelOfNode; int hopCount; float distance; // distance of node form the base station int routeTable[][2]; int nearestNeighborNode; int neighborTable [1000]; // saves the distance of neighbore node from BS. inorder to calc the nearest neighbor // int sendPacket(); int neighborNode; // initialization node (){ xLocation = 100; yLocation = 100; hopCount = 0; distance = 0.0; energyLevelOfNode = 1000.0;// this value is just for the Base station. will reset for all the other nodes nodeID = 1000; //this value for BS. will set for other nodes } }; /*End of Class*/ node arrayOfNodes[1000], baseStation;
Last edited by tosh123; Apr 4th, 2008 at 12:05 am.
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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 testedC++ Syntax (Toggle Plain Text)
type neighbors[capacity] int size = number of neighbors for(i = 0; i < size - 1; ++i) { if(neighbors[i] == neighbors[i + 1]) while((i < size - 1) && (neighbors[i] == neighbors[i + 1])) ++i; else indexOfClosestUniqueNeighbor = i; break; } if((i == size -1) && (neighbors[i] != neighbors[i - 1])) indexOfClosestUniqueNeighbor = i; else cout << "no unique closest Neighbor"
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
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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 testedC++ Syntax (Toggle Plain Text)
type neighbors[capacity] int size = number of neighbors for(i = 0; i < size - 1; ++i) { if(neighbors[i] == neighbors[i + 1]) while((i < size - 1) && (neighbors[i] == neighbors[i + 1])) ++i; else indexOfClosestUniqueNeighbor = i; break; } if((i == size -1) && (neighbors[i] != neighbors[i - 1])) indexOfClosestUniqueNeighbor = i; else cout << "no unique closest Neighbor"
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.
•
•
Join Date: Jan 2008
Posts: 3,839
Reputation:
Solved Threads: 503
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:
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:
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.
C++ Syntax (Toggle Plain Text)
int neighborTable [1000]; // saves the distance of neighbore node from BS. inorder to calc the nearest neighbor
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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:
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:C++ Syntax (Toggle Plain Text)
int neighborTable [1000]; // saves the distance of neighbore node from BS. inorder to calc the nearest neighbor
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:
C++ Syntax (Toggle Plain Text)
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.
Thanks for your comments.
•
•
Join Date: Jan 2008
Posts: 3,839
Reputation:
Solved Threads: 503
•
•
•
•
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.
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.
C++ Syntax (Toggle Plain Text)
nearestNeighborNode = -1; // initialize to bogus value; int shortestDistance = 10000; // initialize to bogus value; for (int i = 0; i < numNodes; i++) { // compare distance of node i to base // to shortestDistance. If less than, // set shortestDistance to the distance to // base from node i and set // nearestNeighorNode to i } // nearestNeighborNode is now set.
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
nearestNeighborNode = -1; // initialize to bogus value; int shortestDistance = 10000; // initialize to bogus value; for (int i = 0; i < numNodes; i++) { // compare distance of node i to base // to shortestDistance. If less than, // set shortestDistance to the distance to // base from node i and set // nearestNeighorNode to i } // nearestNeighborNode is now set.
neighbortable[][1] = distance
neighbortable[][2] = nodeID
how do i sort this array on distance?
![]() |
Views: 1996 | Replies: 21
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






