spacepaste

  1.  
  2. class ['a, 'b] graph (edge_list: ('a * 'b) list) =
  3. let adjacency_list = Hashtbl.create (List.length edge_list) in
  4. let () =
  5. List.iter (fun (u, v) ->
  6. try
  7. (* it exists *)
  8. let nodes = Hashtbl.find adjacency_list u in
  9. Hashtbl.replace adjacency_list u (v :: nodes)
  10. with Not_found ->
  11. (* init the u -> v *)
  12. Hashtbl.add adjacency_list u [v]
  13. ) edge_list
  14. in
  15. object
  16. (* Create an adjacency list of at least "edge_list" size *)
  17. val adjacency_list = adjacency_list
  18. method aj = adjacency_list
  19. end
  20.