public static IPath FindPath(this IGraph graph, IEnumerable <Node> source, Func <Node, bool> target, Dfs.Direction direction) { PathDfs pathDfs = new PathDfs(); pathDfs.PathDirection = direction; pathDfs.IsTarget = target; PathDfs pathDfs2 = pathDfs; pathDfs2.Run(graph, source); if (pathDfs2.EndNode == Node.Invalid) { return(null); } Path path = new Path(graph); path.Begin(pathDfs2.StartNode); foreach (Arc item in pathDfs2.Path) { path.AddLast(item); } return(path); }
/// \anchor FindPath_Main Finds a path in a graph from a source node to a target node. /// \param source The set of source nodes. /// \param target A function determining whether a node belongs to the set of target nodes. /// \param direction The direction of the Dfs used to search for the path. /// \return A path from a source node to a target node, or \e null if none exists. public static IPath FindPath(this IGraph graph, IEnumerable <Node> source, Func <Node, bool> target, Dfs.Direction direction) { var dfs = new PathDfs() { PathDirection = direction, IsTarget = target }; dfs.Run(graph, source); if (dfs.EndNode == Node.Invalid) { return(null); } var result = new Path(graph); result.Begin(dfs.StartNode); foreach (var arc in dfs.Path) { result.AddLast(arc); } return(result); }