heuristic-search 0.2.1

Explicit-state heuristic graph search: A*, weighted and anytime, bounded-suboptimal, bidirectional, incremental and real-time search
Documentation
//! Breadth-first search ([`bfs`]): fewest edges, ignoring edge costs.

use alloc::collections::VecDeque;
use alloc::vec::Vec;
use core::hash::Hash;

use search_core::{Budget, Goal};

use crate::best_first::GraphResult;
use crate::outcome::{Outcome, SearchResult, Stats};
use crate::problem::{Successors, collect_successors};
use crate::space::{NodeId, Reach, SearchSpace};

/// Breadth-first search from `start` to the goal with the fewest edges.
///
/// Edge costs are ignored for the order but summed into
/// [`Solution::cost`](crate::Solution::cost). The goal test runs when a state is generated,
/// not when it is expanded, which saves the last layer of expansions and keeps the result
/// shortest. Each state is expanded at most once.
///
/// # Examples
///
/// ```
/// use heuristic_search::{Limits, Successors, bfs};
///
/// /// Words over `a` and `b` up to length 4; appending a letter costs its position.
/// struct Words;
///
/// impl Successors for Words {
///     type State = String;
///     type Action = char;
///     type Cost = u32;
///
///     fn successors(&self, word: &String, mut emit: impl FnMut(char, String, u32)) {
///         if word.len() < 4 {
///             for letter in ['a', 'b'] {
///                 emit(letter, format!("{word}{letter}"), u32::from(letter) - 96);
///             }
///         }
///     }
/// }
///
/// let result = bfs(&Words, String::new(), &|w: &String| w == "bab", Limits::NONE);
/// let solution = result.into_solution().expect("bab is reachable");
///
/// assert_eq!(solution.actions, ['b', 'a', 'b']);
/// assert_eq!(solution.cost, 5);
/// ```
pub fn bfs<G, T>(graph: &G, start: G::State, goal: &T, budget: impl Budget<Stats>) -> GraphResult<G>
where
    G: Successors,
    G::State: Clone + Hash + Eq,
    G::Action: Clone,
    T: Goal<G::State> + ?Sized,
{
    bfs_multi(graph, [start], goal, budget)
}

/// Breadth-first search from several start states at once, to the goal with the fewest
/// edges from any of them.
///
/// Every start is at depth zero. Repeated starts count once, an empty set of starts is
/// [`Outcome::Unsolvable`], and a start that is a goal is returned as a path without edges.
/// Otherwise behaves like [`bfs()`].
///
/// # Examples
///
/// ```
/// use heuristic_search::{Limits, Successors, bfs_multi};
///
/// /// Counting up by one, at cost 1.
/// struct Up;
///
/// impl Successors for Up {
///     type State = u32;
///     type Action = ();
///     type Cost = u32;
///
///     fn successors(&self, n: &u32, mut emit: impl FnMut((), u32, u32)) {
///         emit((), n + 1, 1);
///     }
/// }
///
/// let result = bfs_multi(&Up, [0, 6, 3], &|n: &u32| *n == 8, Limits::NONE);
/// let solution = result.into_solution().expect("8 is reachable");
///
/// assert_eq!(solution.states, [6, 7, 8]);
/// ```
pub fn bfs_multi<G, T>(
    graph: &G,
    starts: impl IntoIterator<Item = G::State>,
    goal: &T,
    mut budget: impl Budget<Stats>,
) -> GraphResult<G>
where
    G: Successors,
    G::State: Clone + Hash + Eq,
    G::Action: Clone,
    T: Goal<G::State> + ?Sized,
{
    let mut counters = Stats::default();
    let (mut space, start_goal) = SearchSpace::seed(starts, goal);
    if let Some(found) = start_goal {
        return finish(Outcome::Found(space.solution(found)), counters);
    }

    let mut queue: VecDeque<NodeId> = (0..space.len()).collect();
    let mut buffer = Vec::new();
    while let Some(node) = queue.pop_front() {
        if budget.exhausted(&counters) {
            return finish(Outcome::LimitReached, counters);
        }

        counters.expanded += 1;
        counters.generated += collect_successors(graph, space.state(node), &mut buffer);
        if let Some(found) = enqueue_new(&mut space, &mut queue, &mut buffer, node, goal) {
            return finish(Outcome::Found(space.solution(found)), counters);
        }
    }

    finish(Outcome::Unsolvable, counters)
}

/// Adds the unseen successors in `buffer` (emptying it) to the search space and the back of
/// `queue`, and returns the first one that is a goal.
fn enqueue_new<S, A, C, T>(
    space: &mut SearchSpace<S, A, C>,
    queue: &mut VecDeque<NodeId>,
    buffer: &mut Vec<(A, S, C)>,
    parent: NodeId,
    goal: &T,
) -> Option<NodeId>
where
    S: Hash + Eq,
    C: crate::Cost,
    T: Goal<S> + ?Sized,
{
    for (action, state, cost) in buffer.drain(..) {
        let Reach::New(id) = space.reach(state, parent, action, cost, false) else {
            continue;
        };

        if goal.is_goal(space.state(id)) {
            return Some(id);
        }

        queue.push_back(id);
    }

    None
}

/// Builds the result from `outcome` and `stats`.
fn finish<S, A, C>(outcome: Outcome<S, A, C>, stats: Stats) -> SearchResult<S, A, C> {
    SearchResult { outcome, stats }
}