26 using System.Collections.Generic;
29 using System.Threading.Tasks;
49 public sealed
class Bfs
54 private readonly Dictionary<Node, Arc> parentArc;
55 private readonly Dictionary<Node, int> level;
56 private readonly Queue<Node> queue;
62 parentArc =
new Dictionary<Node, Arc>();
63 level =
new Dictionary<Node, int>();
64 queue =
new Queue<Node>();
87 public bool Step(Func<Node, bool> isTarget, out
Node reachedTargetNode)
90 if (queue.Count == 0)
return false;
92 Node node = queue.Dequeue();
93 int d = level[node] + 1;
97 if (parentArc.ContainsKey(child))
continue;
101 parentArc[child] = arc;
103 if (isTarget != null && isTarget(child))
105 reachedTargetNode = child;
116 while (
Step(null, out dummy)) ;
124 if (
Reached(target))
return target;
125 Node reachedTargetNode;
126 while (
Step(node => node == target, out reachedTargetNode)) ;
127 return reachedTargetNode;
135 if (reachedTargetNode !=
Node.
Invalid)
return reachedTargetNode;
136 while (
Step(isTarget, out reachedTargetNode)) ;
137 return reachedTargetNode;
147 return parentArc.ContainsKey(x);
152 public IEnumerable<Node>
ReachedNodes {
get {
return parentArc.Keys; } }
160 return level.TryGetValue(node, out result) ? result : -1;
168 return parentArc.TryGetValue(node, out result) ? result :
Arc.
Invalid;
175 if (!
Reached(node))
return null;
183 result.AddFirst(arc);
184 node =
Graph.Other(arc, node);