Data structure in C# for neighbor-nearest?

Asked

Viewed 76 times

-1

Currently I’m putting all positions in an ordered vector. When I want the nearest neighbor, step through the whole vector.

I want to know if there’s a structure in any namespace from C# to this. Like a hash that when it does not find a value, returns the nearest.

  • 1

    how so "nearest neighbor"? if it does not find a value it is not in the array, how can it have a neighbor? would make sense if it found the value, oh yes it would have at least one neighbor (before or after), you mean the nearest value (higher or lower) of a value that was not found?

  • Yes, the closest if the value is not in the array (which is almost always the case)

  • and what values are in this vector? simple values, numbers, strings or are objects?

  • Objects. I made a class that stores information from a given point in space. Hence if an X point is not in the vector (which is almost always the case), I look for the element in the vector that has the nearest point

  • so you need to put this class in your question to help. If you were a simple guy, a list of integers for example, you could do this with yourself OrderBy, or Aggregate, now as it is an object, you will have to write your own logic to do this, using Icomparer` for example, but only seeing what has in its object to give a more accurate example, the rest would only be guesses

  • I don’t know Fuzzy lists in C#. But the algorithm I know the name, it’s called Distancia Levenshtein it measures how many transformations one string needs to match another. All you have to do is scroll through a list applying this algorithm to the string that displays the Mno transformation coefficient is the nearest string.

Show 1 more comment

1 answer

1

Not much information was given so I can’t help with something more detailed, but there is no proper data structure for this.

Indeed for its precise description of classified data structure (is a wrong term there), then you can do this manually every time before you search (or once if you can guarantee the immutability of the data collection), or you can use a structure that classifies itself as the SortedDictionary, without this just analyzing the entire collection. But to find the next one is algorithm and not data structure. If you don’t know how to do it you have a chance to get ready using LINQ (probably Aggregate()) or at least in the library Morelinq, there is to find a method that overcomes your need.

Then in comment something was said about space, if that is the problem is much more complex and if this is clarified it is too wide.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.