Pouknouki

Graphes

Feb 9th, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.10 KB | None | 0 0
  1. type Arete = {a : int; b : int };;
  2. type Graphe = {n : int; A : Arete list};;
  3.  
  4. let Gexl = {n = 5; A = [{a = 0; b = 4}; {a = 4; b = 3}; {a = 0; b = 3}; {a = 1; b = 3}; {a = 0; b = 3}; { a = 3; b = 0}]};;
  5.  
  6. let rec afficherListe liste = match liste with
  7.     | [] -> ();
  8.     | t::q -> print_int t; afficherListe q;;
  9.  
  10. (* Complexité : |O(liste)|*)
  11. let rec insere liste valeur = match liste with
  12.     | [] -> [valeur];
  13.     | t::q -> if t = valeur then liste else (
  14.                      if t < valeur then t::(insere q valeur)
  15.                      else valeur :: (t::q)
  16.                      );;
  17.  
  18. let maListe = [1;2;3;5;6];;
  19. let maListe = insere maListe 4;;
  20. afficherListe maListe;;
  21.  
  22. let voisins graphe sommet =
  23.     let rec sommetsAux liste element = match liste with
  24.         | [] -> []
  25.         | t::q -> if t.a == element then (insere (sommetsAux q element) t.b)
  26.                      else (
  27.                         if t.b == element then (insere (sommetsAux q element) t.a)
  28.                         else (sommetsAux q element)
  29.                         )
  30.         in sommetsAux graphe.A sommet;;
  31.  
  32. afficherListe (voisins Gexl 3);;
  33.  
  34. let rec trouverLePremierTrouDansLaListe liste = match liste with
  35.     | [] -> 0;
  36.     | [t] -> t + 1;
  37.     | t::q -> if t < hd(q) - 1 then t + 1
  38.                  else trouverLePremierTrouDansLaListe q;;
  39.  
  40. let rec getColor liste vec = match liste with
  41.     | []   -> [];
  42.     (*| t::q -> vec.(t) :: getColor q vec;;*)
  43.     | t::q -> insere (getColor q vec) (vec.(t));;
  44.  
  45. let coloration graphe =
  46.     let couleurs = make_vect graphe.n (0) in
  47.     for i = 0 to graphe.n - 1 do
  48.         let listeDeCouleurs = getColor (voisins graphe i) couleurs in
  49.         couleurs.(i) <- trouverLePremierTrouDansLaListe listeDeCouleurs;
  50.     done;
  51.     couleurs;;
  52.  
  53. let test = coloration Gexl;;
  54.  
  55. let coloration graphe = let rec trouverLePremierTrouDansLaListe liste = match liste with | [] -> 0; | [t] -> t + 1; | t::q -> if t < hd(q) - 1 then t + 1 else trouverLePremierTrouDansLaListe q in let rec getColor liste vec = match liste with | []   -> []; | t::q -> vec.(t) :: (getColor q vec); in let couleurs = make_vect graphe.n (0) in for i = 0 to graphe.n - 1 do let listeDeCouleurs = getColor (voisins graphe i) couleurs in couleurs.(i) <- trouverLePremierTrouDansLaListe listeDeCouleurs; done; couleurs;;
Advertisement
Add Comment
Please, Sign In to add comment