private static VertexDFSData DFS(Vertex from, Vertex to, HashSet <Point> avoid) { var queue = new Queue <VertexDFSData>(); var visited = new HashSet <Point>(avoid); var q = new VertexDFSData { Vertex = from, Length = 1 }; queue.Enqueue(q); while (queue.Count > 0 && q.Vertex != to) { var x = queue.Dequeue(); foreach (var c in x.Vertex.Neighbors) { var p = new Point(x.Vertex.Id, c.Id); if (!visited.Contains(p)) { q = new VertexDFSData { Vertex = c, Previous = x, Length = x.Length + 1 }; queue.Enqueue(q); visited.Add(p); if (c == to) { break; } } } } return(q); }
private static Vertex[] ExtractPath(VertexDFSData q) { var path = new Vertex[q.Length]; int i = path.Length; while (--i >= 0) { path[i] = q.Vertex; if (i > 0) { q = q.Previous; } } return(path); }