| 1 |
module Node = struct |
| 2 |
type 'a t = { |
| 3 |
mutable pred : 'a t list; |
| 4 |
name : 'a; |
| 5 |
mutable succ : 'a t list; |
| 6 |
} |
| 7 |
|
| 8 |
let init name = { pred = []; name; succ = [] } |
| 9 |
|
| 10 |
let remove_succ rem_node n = |
| 11 |
n.succ <- List.filter (fun succ -> succ.name <> rem_node.name) n.succ |
| 12 |
|
| 13 |
let add_succ current succ = |
| 14 |
current.succ <- succ :: current.succ; |
| 15 |
succ.pred <- current :: succ.pred |
| 16 |
|
| 17 |
let remove_pred rem_node n = |
| 18 |
n.pred <- List.filter (fun pred -> pred.name <> rem_node.name) n.pred |
| 19 |
|
| 20 |
let clear node = node.pred <- []; node.succ <- [] |
| 21 |
|
| 22 |
let remove current = |
| 23 |
List.iter (fun succ -> remove_pred current succ) current.succ; |
| 24 |
List.iter (fun pred -> remove_succ current pred) current.pred; |
| 25 |
clear current |
| 26 |
|
| 27 |
let print pf n = |
| 28 |
let pf e = Printf.sprintf "%s " (pf e.name) in |
| 29 |
let pf_fold e = List.fold_left (fun acc e -> Printf.sprintf "%s%s, " acc (pf e)) "" e in |
| 30 |
Printf.printf "%s : [ succ: %s ;; pred : %s ]\n%!" |
| 31 |
(pf n) (pf_fold n.succ) (pf_fold n.pred); |
| 32 |
end |
| 33 |
|
| 34 |
module Graph = struct |
| 35 |
|
| 36 |
type 'a graph = { nodes : ('a, 'a Node.t) Hashtbl.t } |
| 37 |
|
| 38 |
exception Invalid_graph of string |
| 39 |
|
| 40 |
let rec queue_to_list q = |
| 41 |
if Queue.is_empty q then [] |
| 42 |
else |
| 43 |
let elt = Queue.pop q in |
| 44 |
elt :: queue_to_list q |
| 45 |
|
| 46 |
let add_node graph node_name = |
| 47 |
if Hashtbl.mem graph.nodes node_name then |
| 48 |
raise (Invalid_graph "The nodes of the graph should be unique") |
| 49 |
else |
| 50 |
let node = Node.init node_name in |
| 51 |
Hashtbl.add graph.nodes node_name node; |
| 52 |
node |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
let build l : 'a graph = |
| 59 |
let graph = { nodes = Hashtbl.create (List.length l) } in |
| 60 |
List.iter (fun (node, _) -> add_node graph node |> ignore) l; |
| 61 |
List.iter |
| 62 |
(fun (current_name, succ_names) -> |
| 63 |
let current = Hashtbl.find graph.nodes current_name in |
| 64 |
List.iter |
| 65 |
(fun succ_name -> |
| 66 |
let succ = |
| 67 |
try Hashtbl.find graph.nodes succ_name |
| 68 |
with Not_found -> add_node graph succ_name in |
| 69 |
Node.add_succ current succ |
| 70 |
) |
| 71 |
succ_names) |
| 72 |
l; |
| 73 |
graph |
| 74 |
|
| 75 |
let topo_sort graph : 'a list = |
| 76 |
let res = Queue.create () in |
| 77 |
let to_treat = Queue.create () in |
| 78 |
Hashtbl.iter |
| 79 |
(fun _ (n: 'a Node.t) -> if List.length n.pred = 0 then Queue.add n to_treat) |
| 80 |
graph.nodes; |
| 81 |
while Queue.is_empty to_treat |> not do |
| 82 |
let current_node = Queue.pop to_treat in |
| 83 |
Queue.push current_node.name res; |
| 84 |
let succ = current_node.succ in |
| 85 |
Node.remove current_node; |
| 86 |
List.iter |
| 87 |
(fun (succ: 'a Node.t) -> if List.length succ.pred = 0 then Queue.push succ to_treat) |
| 88 |
succ |
| 89 |
done; |
| 90 |
if Queue.length res <> Hashtbl.length graph.nodes then |
| 91 |
raise (Invalid_graph "Cannot do topological sort on cyclic graph") |
| 92 |
else queue_to_list res |
| 93 |
|
| 94 |
let print pf g = |
| 95 |
Hashtbl.iter (fun _ n -> Node.print pf n) |
| 96 |
g.nodes |
| 97 |
end |