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