forked from MarwanOriginals/IPF-Graph-Coloring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coloring.ml
101 lines (75 loc) · 2.08 KB
/
coloring.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(* 1 *)
module StringSet = Set.Make(String)
module StringMap = Map.Make(String)
type graph = StringSet.t StringMap.t
(* 2 *)
let add_edge u v g =
let add_oriented_edge v1 v2 g =
let s =
try
StringMap.find v1 g
with
Not_found -> StringSet.empty
in
let s' = StringSet.add v2 s in
StringMap.add v1 s' g in
let g = add_oriented_edge u v g in
add_oriented_edge v u g
(* 3 *)
let remove_vertex u g =
let g' = StringMap.remove u g in
StringMap.map
(fun s -> StringSet.remove u s)
g'
(* 4 *)
module Int = struct
type t = int
let compare = fun x y -> x - y
end
module IntSet = Set.Make(Int)
(* 5 *)
let rec range a b =
if a = b then IntSet.singleton a
else IntSet.add a (range (a+1) b)
let color_set n =
range 1 n
(* 6 *)
type disp_color = IntSet.t StringMap.t
(* 7 *)
let init_colors g k =
StringMap.map
(fun s -> color_set k)
g
(* 8 *)
let remove_color i v c =
let s = StringMap.find v c in
StringMap.add v (IntSet.remove i s) c
(* 9 *)
exception Failed
(* 10 *)
let rec try_first f s =
if IntSet.is_empty s then raise (Failed) else
let i = IntSet.choose s in
try
f i
with
Failed -> try_first f (IntSet.remove i s)
(* 11 *)
type coloring = int StringMap.t
(* 12 *)
let rec color g c =
if StringMap.is_empty g = false then (* g non vide du coup on continue *)
let (v, vn) = StringMap.choose g in (* sommet v choisi, et vn ses neighbors *)
let traitement i =
let c' = StringMap.mapi ( (* on prend chaque couleur dispo *)
fun v' s' ->
if StringSet.mem v' vn (* si le sommet actuel [v'] est voisin du sommet v choisi plus haut *)
then IntSet.remove i s' (* => enlever cette couleur des couleurs possibles *)
else s' ) c in (* sinon on change rien *)
let g' = remove_vertex v g in (* on enleve le sommet v choisi plus haut, au graphe *)
StringMap.add v i (color g' c') (* et on applique la fonction color sur ce graphe resultant *)
in
let s = StringMap.find v c in
try_first traitement s
else
StringMap.empty (* g vide pas de coloriage possible *)