public ICollection <Way> RemoveDeadNodes() { var edges = GetEdges(); var deadWays = new HashSet <Way>(); var visited = new HashSet <long>(); var queue = new Queue <long>(); queue.Enqueue(Ways.First().Start); while (queue.Any()) { var node = queue.Dequeue(); foreach (var neighbour in edges[node]) { if (visited.Contains(neighbour)) { continue; } visited.Add(neighbour); queue.Enqueue(neighbour); } } foreach (var way in Ways.ToList()) { if (visited.Contains(way.Start)) { continue; } Ways.Remove(way); deadWays.Add(way); } return(deadWays); }