Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. let rec isAdjacent graphe sommet1 sommet2 = match graphe with
  2. | [] -> false
  3. | (a,b)::l -> if (((a=sommet1) && (b=sommet2)) || ((b=sommet1) && (a=sommet2))) then true else isAdjacent l sommet1 sommet2
  4.  
  5. let rec listeSommets graphe =
  6. let rec listeSommets2 graphe liste = match graphe with
  7. | [] -> liste
  8. | (sommet1,sommet2)::l when not (List.mem sommet1 liste) -> if not(List.mem sommet2 liste) then listeSommets2 l (sommet1::sommet2::liste)
  9. else listeSommets2 l (sommet1::liste)
  10. | (sommet1,sommet2)::l when not (List.mem sommet2 liste) -> if not(List.mem sommet1 liste) then listeSommets2 l (sommet1::sommet2::liste)
  11. else listeSommets2 l (sommet2::liste)
  12. | (_,_)::l -> listeSommets2 l liste
  13.  
  14. in listeSommets2 graphe [];;
  15.  
  16.  
  17. let listeVoisins graphe sommet =
  18. let rec listeVoisins2 graphe liste = match graphe with
  19. | [] -> liste
  20. | (a,b)::l when (a=sommet) -> listeVoisins2 l (List.sort_uniq compare (b::liste))
  21. | (a,b)::l when (b=sommet) -> listeVoisins2 l (List.sort_uniq compare (a::liste))
  22. | (_,_)::l -> listeVoisins2 l liste
  23.  
  24. in listeVoisins2 graphe [];;
  25.  
  26.  
  27. let rec plusPetiteCoulPossible couleur = function
  28. | [] -> couleur
  29. | coul::l -> if (couleur=coul) then plusPetiteCoulPossible (coul+1) l else plusPetiteCoulPossible couleur l;;
  30.  
  31.  
  32. let rec insere elem liste = match liste with
  33. | [] -> elem::[]
  34. | tete::l -> if elem < tete then elem :: liste else tete :: insere elem l;;
  35.  
  36.  
  37.  
  38. let rec siVoisinsDejaColores listeCouleurBis couleur sommet graphe = match listeCouleurBis with
  39. | [] -> plusPetiteCoulPossible 1 couleur
  40. | (element,coul)::l -> if (isAdjacent graphe element sommet) then
  41. siVoisinsDejaColores l (insere coul couleur) sommet graphe
  42. else siVoisinsDejaColores l couleur sommet graphe;;
  43.  
  44. (* fonction aux *)
  45. let exists_left list value = List.exists (fun (x, _) -> x = value) list;;
  46.  
  47.  
  48.  
  49. (* val gloutonSansH: (’a * ’a) list -> (’a * int) list * int * ’a list, pas d’heuristique : l’ordre de coloriage des sommets est indifférent *)
  50.  
  51. let gloutonSansH graphe =
  52. let rec gloutonSansH2 listeSommets listeCouleur nbCouleur listeCroissante = match listeSommets with
  53. | [] -> (listeCouleur, nbCouleur, listeCroissante)
  54. | sommet::listeSommets -> if (exists_left listeCouleur sommet)
  55. then gloutonSansH2 listeSommets listeCouleur nbCouleur listeCroissante
  56. else let couleurLocale = (siVoisinsDejaColores listeCouleur [] sommet graphe) in
  57. (gloutonSansH2 listeSommets ((sommet,couleurLocale)::listeCouleur) (if nbCouleur<=couleurLocale then couleurLocale else (nbCouleur)) (listeCroissante@[sommet]))
  58.  
  59. in gloutonSansH2 (listeSommets graphe) [] 0 [];;
  60.  
  61.  
  62.  
  63. (* val gloutonDeg: (’a * ’a) list -> (’a * int) list * int * ’a, listheuristique statique : les sommets sont colorés par ordre de degré décroissant *)
  64.  
  65.  
  66.  
  67. (* renvoie le degre du sommet *)
  68. let degre graphe sommet = List.length (listeVoisins graphe sommet);;
  69.  
  70. (* insert l'element au bon endroit dans la liste *)
  71.  
  72. let sommetEnCoupleAvecDegre graphe sommet = (sommet,(degre graphe sommet));;
  73.  
  74.  
  75. let rec insereDegre graphe elem liste = match liste with
  76. | [] -> elem::liste
  77. | element::l -> if ((degre graphe elem) > degre graphe element) then elem::liste else element::(insereDegre graphe elem l);;
  78.  
  79.  
  80.  
  81. (* pour chaque sommet, on calcul sont degre et on l'insert dans la liste *)
  82.  
  83. let tri_croissantDegresSommets graphe =
  84. let rec tri_croissantDegresSommets2 listeSommets liste = match listeSommets with
  85. | [] -> liste
  86. | element::l -> tri_croissantDegresSommets2 l (insereDegre graphe element liste)
  87. in tri_croissantDegresSommets2 (listeSommets graphe) [];;
  88.  
  89.  
  90. let gloutonDeg graphe =
  91. let rec gloutonDeg2 listeSommets listeCouleur nbCouleur listeCroissante = match listeSommets with
  92. | [] -> (listeCouleur, nbCouleur, listeCroissante)
  93. | sommet::listeSommets -> if (exists_left listeCouleur sommet)
  94. then gloutonDeg2 listeSommets listeCouleur nbCouleur listeCroissante
  95. else let couleurLocale = (siVoisinsDejaColores listeCouleur [] sommet graphe) in
  96. (gloutonDeg2 listeSommets ((sommet,couleurLocale)::listeCouleur) (if nbCouleur<=couleurLocale then couleurLocale else (nbCouleur)) (listeCroissante@[sommet]))
  97.  
  98. in gloutonDeg2 (tri_croissantDegresSommets graphe) [] 0 [];;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement