(********************************************************************)
(*  OCaml code from the book ``Learn Programming with OCaml''       *)
(*  See https://usr.lmf.cnrs.fr/lpo/                                *)
(*                                                                  *)
(*  Sylvain Conchon and Jean-Christophe Filliâtre                   *)
(*  Copyright 2025 Université Paris-Saclay and CNRS                 *)
(*                                                                  *)
(*  Openly licensed via CC BY SA 4.0                                *)
(*  See https://creativecommons.org/licenses/by-sa/4.0/deed.en      *)
(********************************************************************)

(* Program 99 on page 416
   Breadth-First Search *)

  let iter_bfs f g s =
    let visited = H.create () in
    let queue = Queue.create () in
    let add w = H.add visited w (); Queue.add w queue in
    add s;
    while not (Queue.is_empty queue) do
      let v = Queue.pop queue in
      f v;
      iter_succ (fun w -> if not (H.mem visited w) then add w) g v
    done

This document was generated using caml2html