) Map.empty
let graphCount (g : Map<int, Set<int>>) =
-
let rec visit (current : int) (visited : Set<int>) : Set<int> =
if visited |> Set.contains current then
Set.empty
seq { yield g.[current]; for neighbor in g.[current] -> visit neighbor (visited |> Set.add current) } |> Set.unionMany
let rec nbRoots (vertices : Set<int>) =
- if Set.isEmpty vertices then
- 0
- else
- 1 + nbRoots (vertices - (visit (vertices |> Set.minElement) Set.empty))
+ if Set.isEmpty vertices then 0 else 1 + nbRoots (vertices - (visit (vertices |> Set.minElement) Set.empty))
visit 0 Set.empty |> Set.count, g |> Map.toList |> List.map fst |> Set.ofList |> nbRoots
\ No newline at end of file