add CCGraph.is_dag

This commit is contained in:
Simon Cruanes 2016-05-05 18:16:23 +02:00
parent 3190278d86
commit e08fc88e13
2 changed files with 24 additions and 0 deletions

View file

@ -43,6 +43,10 @@ module Seq = struct
a (fun x -> acc := f !acc x);
!acc
let to_list seq = fold (fun acc x->x::acc) [] seq |> List.rev
exception Exit_
let exists_ f seq =
try seq (fun x -> if f x then raise Exit_); false
with Exit_ -> true
end
(** {2 Interfaces for graphs} *)
@ -315,6 +319,15 @@ module Traverse = struct
end
end
(** {2 Cycles} *)
let is_dag ?(tbl=mk_table 128) ~graph vs =
Traverse.Event.dfs ~tbl ~graph vs
|> Seq.exists_
(function
| `Edge (_, `Back) -> true
| _ -> false)
(** {2 Topological Sort} *)
exception Has_cycle

View file

@ -224,6 +224,17 @@ module Traverse : sig
end
end
(** {2 Cycles} *)
val is_dag :
?tbl:'v set ->
graph:('v, _) t ->
'v sequence ->
bool
(** [is_dag ~graph vs] returns [true] if the subset of [graph] reachable
from [vs] is acyclic.
@since NEXT_RELEASE *)
(** {2 Topological Sort} *)
exception Has_cycle