【发布时间】:2012-02-15 04:10:07
【问题描述】:
我有这个简单的图表:
name -> string
^
|
v
label
let matrix = [|
[|false; false; false |];
[|true; false; true |];
[|false; true; false|] |]
(* compute transitive closure of matrix*)
let transClosure m =
let n = Array.length m in
for k = 0 to n - 1 do
let mk = m.(k) in
for i = 0 to n - 1 do
let mi = m.(i) in
for j = 0 to n - 1 do
mi.(j) <- max mi.(j) (min mi.(k) mk.(j))
done;
done;
done;
m;;
传递闭包矩阵的输出为:
假假假
真真真假
真真真假
函数比较等价类:
let cmp_classes m i j =
match m.(i).(j), m.(j).(i) with
(* same class: there is a path between i and j, and between j and i *)
| true, true -> 0
(* there is a path between i and j *)
| true, false -> -1
(* there is a path between j and i *)
| false, true -> 1
(* i and j are not compareable *)
| false, false -> raise Not_found
let sort_eq_classes m = List.sort (cmp_classes m);;
函数计算等价类:
let eq_class m i =
let column = m.(i)
and set = ref [] in
Array.iteri begin fun j _ ->
if j = i || column.(j) && m.(j).(i) then
set := j :: !set
end column;
!set;;
let eq_classes m =
let classes = ref [] in
Array.iteri begin fun e _ ->
if not (List.exists (List.mem e) !classes) then
classes := eq_class m e :: !classes
end m;
!classes;;
(* compute transitive closure of given matrix *)
let tc_xsds = transClosure matrix
(* finding equivalence classes in transitive closure matrix *)
let eq_xsds = eq_classes tc_xsds
(* sorting all equivalence classes with transitive closure matrix *)
let sort_eq_xsds = sort_eq_classes tc_xsds (List.flatten eq_xsds)
它给了我命令:label, name, string,意思是正确的命令。
问题是,当我用另一个图表进行测试时,例如:
name -> string
^
|
v
label -> int
或
name -> int
^ \
| \
v v
label string
或
name -> string
|
v
label -> int
输出是 raise Not_found
您能帮我解释一下为什么它不能给出正确的顺序吗?谢谢。
【问题讨论】:
标签: ocaml