(********************************************************************) (* 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 101 on page 424 Shortest path (Dijkstra’s algorithm) *) module VertexDistance = struct type t = vertex * float let compare (_, d1) (_, d2) = Stdlib.compare d1 d2 end module P = PriorityQueue(VertexDistance) let rec dijkstra f g s = let visited = H.create () in let distance = H.create () in let queue = P.create () in let add v d = H.replace distance v d; P.add queue (v, d) in add s 0.; while not (P.is_empty queue) do let (u, du) = P.extract_min queue in if not (H.mem visited u) then begin H.add visited u (); f u du; let visit v = let d = du +. weight g u v in if not (H.mem distance v) || d < H.find distance v then add v d in iter_succ visit g u end done
This document was generated using caml2html