Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all shortest paths using Dijkstra's algorithm #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ struct

module PQ = Heap.Imperative(Elt)

(* Gets the shortest path between v1 and v2 in graph g *)
let shortest_path g v1 v2 =
(* Create a hashtable (which is essentially a set...) of visited nodes *)
let visited = H.create 97 in
(* Create a hashtable of distances from each of the nodes *)
let dist = H.create 97 in
(* Create a priority queue *)
let q = PQ.create 17 in
let rec loop () =
if PQ.is_empty q then raise Not_found;
Expand Down Expand Up @@ -98,6 +102,44 @@ struct
H.add dist v1 W.zero;
loop ()



let all_shortest_paths g v1 =
(* Create a hashtable (which is essentially a set...) of visited nodes *)
let visited = H.create 97 in
(* Create a hashtable of distances from each of the nodes *)
let dist = H.create 97 in
(* Create a priority queue *)
let q = PQ.create 17 in
let rec loop () =
if PQ.is_empty q then
dist
else
let (w,v,p) = PQ.pop_maximum q in
begin
if not (H.mem visited v) then begin
H.add visited v ();
G.iter_succ_e
(fun e ->
let ev = dst e in
if not (H.mem visited ev) then begin
let dev = W.add w (W.weight e) in
let improvement =
try W.compare dev (H.find dist ev) < 0 with Not_found -> true
in
if improvement then begin
H.replace dist ev dev;
PQ.add q (dev, ev, e :: p)
end
end)
g v
end;
loop ()
end
in
PQ.add q (W.zero, v1, []);
H.add dist v1 W.zero;
loop ()
end

(* The following module is a contribution of Yuto Takei (University of Tokyo) *)
Expand Down
5 changes: 5 additions & 0 deletions src/path.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ module Dijkstra
(W: Sig.WEIGHT with type edge = G.E.t) :
sig

module H : Hashtbl.S with type key = G.V.t

val shortest_path : G.t -> G.V.t -> G.V.t -> G.E.t list * W.t
(** [shortest_path g v1 v2] computes the shortest path from vertex [v1]
to vertex [v2] in graph [g]. The path is returned as the list of
Expand All @@ -53,6 +55,9 @@ sig

Complexity: at most O((V+E)log(V)) *)

val all_shortest_paths : G.t -> G.V.t -> W.t H.t


end

(* The following module is a contribution of Yuto Takei (University of Tokyo) *)
Expand Down