fork download
  1. open System.Collections.Generic
  2.  
  3. let groupConnected initId idTups =
  4. let mergeGroups projectIds input =
  5. (List.empty<SortedSet<_>>, input)
  6. ||> List.fold (fun groups x ->
  7. let ids = projectIds x
  8. match groups |> List.tryFind (fun g -> g.Overlaps ids) with
  9. | Some g -> g.UnionWith ids
  10. groups
  11. | _ -> ids::groups)
  12. idTups
  13. |> mergeGroups (fun (a, b) -> SortedSet([| a; b |]))
  14. |> mergeGroups id
  15. |> List.sortBy (fun g -> g.Min)
  16. |> Seq.mapi (fun i g -> initId + i, List.ofSeq g)
  17. |> Map.ofSeq
  18.  
  19. do
  20. let test tups =
  21. tups |> groupConnected 100 |> printfn "%A"
  22. stdout.WriteLine ()
  23. test [(1M, 2M); (2M, 3M); (3M, 3M); (4M, 5M); (5M, 6M); (7M, 6M); (8M, 9M); (10M, 9M)]
  24. test [(1M, 2M); (7M, 8M); (2M, 3M); (8M, 9M); (3M, 4M); (10M, 11M); (4M, 5M); (13M, 11M); (5M, 6M)]
  25. test [(1, 1); (2, 18); (3, 3); (4, 5); (5, 24); (24, 6); (7, 6); (8, 9); (10, 9)]
  26.  
Success #stdin #stdout 0.08s 31568KB
stdin
Standard input is empty
stdout
map [(100, [1M; 2M; 3M]); (101, [4M; 5M; 6M; 7M]); (102, [8M; 9M; 10M])]

map
  [(100, [1M; 2M; 3M; 4M; 5M; 6M]); (101, [7M; 8M; 9M]); (102, [10M; 11M; 13M])]

map
  [(100, [1]); (101, [2; 18]); (102, [3]); (103, [4; 5; 6; 7; 24]);
   (104, [8; 9; 10])]