Class KnnGraph<TItem, TDistance>
This class implements the navigable world graph, constructing the K-NN graph structures.
Inheritance
System.Object
KnnGraph<TItem, TDistance>
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Mars.Common.Collections.KNNGraph
Assembly: Mars.Common.dll
Syntax
public class KnnGraph<TItem, TDistance>
where TDistance : struct, IComparable<TDistance>
Type Parameters
Name | Description |
---|---|
TItem | The type of items to connect into small world. |
TDistance | The type of distance between items (expect any numeric type: float, double, decimal, int, ...). |
Constructors
| Improve this Doc View SourceKnnGraph(Func<TItem, TItem, TDistance>, IProvideRandomValues, KnnGraph<TItem, TDistance>.Parameters)
Initializes a new instance of the KnnGraph<TItem, TDistance> class.
Declaration
public KnnGraph(Func<TItem, TItem, TDistance> distance, IProvideRandomValues generator, KnnGraph<TItem, TDistance>.Parameters parameters)
Parameters
Type | Name | Description |
---|---|---|
System.Func<TItem, TItem, TDistance> | distance | The distance function to use in the small world. |
IProvideRandomValues | generator | The random number generator for building graph. |
KnnGraph.Parameters<> | parameters | Parameters of the algorithm. |
Properties
| Improve this Doc View SourceItems
Gets a copy of the list of items currently held by the SmallWorld graph.
This call is protected by a read-lock and is safe to be called from multiple threads.
Declaration
public IReadOnlyList<TItem> Items { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IReadOnlyList<TItem> |
Methods
| Improve this Doc View SourceAddItems(IReadOnlyList<TItem>, IProgressReporter, CancellationToken)
Builds KNN graph from the given items.
Declaration
public IReadOnlyList<int> AddItems(IReadOnlyList<TItem> items, IProgressReporter progressReporter = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyList<TItem> | items | The items to connect into the graph. |
IProgressReporter | progressReporter | Optional progress callback during construction. |
System.Threading.CancellationToken | cancellationToken | The cancellation flag. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IReadOnlyList<System.Int32> |
DeserializeGraph(IReadOnlyList<TItem>, Func<TItem, TItem, TDistance>, IProvideRandomValues, Stream)
Deserializes the graph from byte array.
Declaration
public static KnnGraph<TItem, TDistance> DeserializeGraph(IReadOnlyList<TItem> items, Func<TItem, TItem, TDistance> distance, IProvideRandomValues generator, Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyList<TItem> | items | The items to assign to the graph's nodes.. |
System.Func<TItem, TItem, TDistance> | distance | The correlation function for similarity or distance of given data objects. |
IProvideRandomValues | generator | The random number generator constructing an equally distance space. |
System.IO.Stream | stream | The input stream containing the parameters and serialized network. |
Returns
Type | Description |
---|---|
KnnGraph<TItem, TDistance> |
KnnSearch(TItem, Int32)
Run knn search for a given item.
Declaration
public IList<KnnGraph<TItem, TDistance>.KnnSearchResult> KnnSearch(TItem item, int k)
Parameters
Type | Name | Description |
---|---|---|
TItem | item | The item to search nearest neighbours. |
System.Int32 | k | The number of nearest neighbours. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IList<KnnGraph.KnnSearchResult<>> | The list of found nearest neighbours. |
ResizeDistanceCache(Int32)
Frees the memory used by the Distance Cache
Declaration
public void ResizeDistanceCache(int newSize)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | newSize |
SerializeGraph(Stream)
Serializes the graph WITHOUT linked items.
Declaration
public void SerializeGraph(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream |