private static Tuple <int[], double[]> ShortestPaths(IEnumerable <Tuple <int, int> > edges, Func <int, int, double> weight, int source) { var neighborsOf = edges.ToNeighborhoodLists(); var vertices = Enumerable.Range(0, neighborsOf.Count); // distanceTo will store the distance from source to any other vertex. At the beginning according to Dijkstra algorithm it's infinity for all // vertices except the source var distanceTo = Enumerable.Repeat(double.PositiveInfinity, neighborsOf.Count).ToArray(); distanceTo[source] = 0; // distance from the source to itself // previous[v] will store the previous vertex in the shortest path from source to v. Initialized to -1, which means no previous vertex // as we don't know the shortest path yet. var previous = Enumerable.Repeat(-1, neighborsOf.Count).ToArray(); var priorityQueue = new SortedSet <int>(DelegateComparer.Create <int>((x, y) => // compare vertices according to their distance from source { var weightCompare = distanceTo[x].CompareTo(distanceTo[y]); if (weightCompare == 0) { return(x.CompareTo(y)); } else { return(weightCompare); } })); foreach (var v in vertices) { priorityQueue.Add(v); } // the main loop of the Dijkstra algorithm while (priorityQueue.Count > 0) { // we remove the min-distance vertex from the priority queue var v = priorityQueue.First(); priorityQueue.Remove(v); // update all neighbors of v with new distances foreach (var u in neighborsOf[v]) { var newDistance = distanceTo[v] + weight(v, u); if (newDistance < distanceTo[u]) { // we have to add and remove u from the priority queue during distance update, otherwise the priority queue will incorrectly // return the minimal-distance node. priorityQueue.Remove(u); distanceTo[u] = newDistance; priorityQueue.Add(u); previous[u] = v; } } } return(Tuple.Create(previous, distanceTo)); }
/// <summary> /// Creates a new Observable List View /// </summary> /// <param name="source">The source list to wrap</param> /// <param name="comparison">A delegate to perform the comparison with</param> /// <param name="filter">A filter to apply to the source list</param> public ObservableListView(IList <T> source, Comparison <T> comparison, Predicate <T>?filter) : this(source, comparison != null ? DelegateComparer.Create(comparison) : null, filter, null) { }
/// <summary> /// Creates a new Observable List View /// </summary> /// <param name="source">The source list to wrap</param> /// <param name="comparison">A delegate to perform the comparison with</param> public ObservableListView(IList <T> source, Comparison <T> comparison) : this(source, DelegateComparer.Create(comparison), null, null) { }
private void SortGridLines() { _sortedLines.Sort(DelegateComparer.Create <TGridLine>((l, r) => Comparer <int> .Default.Compare(l.Step, r.Step))); }