-
Notifications
You must be signed in to change notification settings - Fork 0
/
topol.ml
76 lines (53 loc) · 1.57 KB
/
topol.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(*******************************************)
(* *)
(* Autor: Amadeusz Iwanowski *)
(* Code review: Adam Jedrych *)
(* *)
(*******************************************)
open PMap;;
exception Cykliczne;;
type visit = Visited | Visiting | Not_visited;;
(*
Wejscie: (a, b) list, dodaje wszystkie wartosci z listy
do mapy i ustawia na Not_visited
*)
let add_all l =
let rec pom acu li =
match li with
| [] -> acu
| (a, b) :: t -> pom (add a (ref Not_visited, b) acu) t
in pom empty l;;
let rec iter2 funct li =
match li with
| [] -> ();
| (a, b) :: t -> funct a; iter2 funct t;;
let topol l =
let map = ref (add_all l) in
let acu = ref ([]) in
let rec dfs vertice =
if exists vertice !map then
let (c, li) = find vertice (!map) in
match !c with
| Visiting -> raise Cykliczne
| Visited -> ()
| Not_visited ->
begin
c := Visiting;
List.iter dfs li;
c := Visited;
acu := vertice :: (!acu);
end
else
begin
map:= add vertice (ref Visited, []) !map;
acu := vertice :: !acu;
end
in iter2 dfs l; !acu;;
let lista = [("kurtka", ["szalik"; "czapka"]); ("szalik", ["czapka"]);
("buty", ["kurtka"])];;
assert (topol lista = ["buty"; "kurtka"; "szalik"; "czapka"]);; (*prosty test*)
let lista = [("kurtka", ["szalik"; "czapka"]); ("szalik", ["czapka"]);
("buty", ["kurtka"]); ("czapka", ["kurtka"])];;
let c = try topol lista with
| Cykliczne -> [];;
assert (c = []); (*cykliczne*)