public Tuple <ISet <T>, ISet <T> > Partition(Tuple <T, T> promoted, ICollection <T> dataSet, DistanceMetric <T> distanceMetric) { List <T> queue1 = new List <T>(dataSet); queue1.Sort((data1, data2) => { Double distance1 = distanceMetric.Invoke(data1, promoted.Item1); Double distance2 = distanceMetric.Invoke(data2, promoted.Item1); return(distance1.CompareTo(distance2)); }); List <T> queue2 = new List <T>(dataSet); queue2.Sort((data1, data2) => { Double distance1 = distanceMetric.Invoke(data1, promoted.Item2); Double distance2 = distanceMetric.Invoke(data2, promoted.Item2); return(distance1.CompareTo(distance2)); }); ISet <T> first = new HashSet <T>(); ISet <T> second = new HashSet <T>(); int index1 = 0; int index2 = 0; while (index1 < queue1.Count || index2 != queue2.Count) { while (index1 < queue1.Count) { T data = queue1[index1++]; if (!second.Contains(data)) { first.Add(data); break; } } while (index2 < queue2.Count) { T data = queue2[index2++]; if (!first.Contains(data)) { second.Add(data); break; } } } return(new Tuple <ISet <T>, ISet <T> >(first, second)); }
public Tuple <ISet <T>, ISet <T> > Partition(Tuple <T, T> promoted, ICollection <T> dataSet, DistanceMetric <T> distanceMetric) { ISet <T> first = new HashSet <T>(); ISet <T> second = new HashSet <T>(); foreach (T data in dataSet) { if (distanceMetric.Invoke(data, promoted.Item1) <= distanceMetric.Invoke(data, promoted.Item2)) { first.Add(data); } else { second.Add(data); } } return(new Tuple <ISet <T>, ISet <T> >(first, second)); }
/// <summary> /// Chooses the two items from the <c>dataSet</c> which are the farthest from each other. /// </summary> /// <param name="dataSet">The data set.</param> /// <param name="distanceMetric">The distance metric.</param> /// <returns> /// a pair of promoted items /// </returns> public Tuple <T, T> Promote(ICollection <T> dataSet, DistanceMetric <T> distanceMetric) { T[] data = new T[dataSet.Count]; dataSet.CopyTo(data, 0); T first = data[0]; T second = data[1]; Double maxDistance = distanceMetric.Invoke(first, second); for (int i = 0; i < data.Length; i++) { for (int j = i + 1; j < data.Length; j++) { Double distance = distanceMetric.Invoke(data[i], data[j]); if (distance > maxDistance) { maxDistance = distance; first = data[i]; second = data[j]; } } } return(new Tuple <T, T>(first, second)); }