public Node(T value) { Value = value; _distanceVector = new DistanceVector <T>(this); _neighbours = new HashSet <Node <T> >(); _distances = new Dictionary <Node <T>, long>(); _neighbourDistanceVectors = new Dictionary <Node <T>, DistanceVector <T> >(); }
private void UpdateDistanceVector() { var distanceVector = new DistanceVector <T>(this); List <Node <T> > destinations = _neighbours.SelectMany( x => x.DistanceVector .Entries .Select(y => y.Destination) ).Where(x => x != this).Distinct().ToList(); foreach (var destination in destinations) { long bestDist = long.MaxValue; Node <T> next = null; foreach (var neighbour in _neighbours) { long distToNeigh = _distances[neighbour]; var distToDest = neighbour.DistanceVector .Entries .Where(x => x.Destination == destination) .Where(x => x.Next != this) .Select(x => x.Distance); if (distToDest.Count() == 0) { continue; } if (distToNeigh + distToDest.First() < bestDist) { bestDist = distToNeigh + distToDest.First(); next = neighbour; } } if (next != null) { distanceVector.AddEntry(destination, bestDist, next); } } _distanceVector = distanceVector; }