Advertisement
juaniisuar

Dijkstra ML

Sep 23rd, 2015
2,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. load "Int";
  2.  
  3. fun listaNodos g = let fun clash [] = [] (* funcion para saber los nodos de un grafo *)
  4.                            | clash ((a,b,c)::d) = [a,b]@(clash d)
  5.                        fun eliminar [] k  = []
  6.                            | eliminar (a::b) k = if k = a then eliminar b k else a::(eliminar b k)
  7.                        fun listaNodosAux [] = []
  8.                            | listaNodosAux (a::b) = a::(listaNodosAux (eliminar b a))
  9.                    in listaNodosAux (clash g) end
  10.  
  11. fun quitarAristas [] nodo restGraph lista = (restGraph, lista) (* separar grafo en la tupla (grafo - conjunto de aristas de 'nodo', conjunto de aristas de 'nodo') *)
  12.     | quitarAristas ((a,b,c)::d) nodo restGraph lista = if nodo = a (* nodo coincide *)
  13.                                                             then quitarAristas d nodo restGraph ((a,b,c)::lista)
  14.                                                             else quitarAristas d nodo ((a,b,c)::restGraph) lista;
  15.  
  16. fun push [] (w,z) = [(w,z)] (* priority queue de menor a mayor *)
  17.     | push ((a,d)::rest) (w,z) = if z > d then (a,d)::(push rest (w,z)) else [(w,z),(a,d)]@rest;
  18.  
  19. fun pushListPlusValue [] luckingFist value = luckingFist (* push de una lista a la PQ *)
  20.     | pushListPlusValue ((a,b,c)::d) luckingFist value = pushListPlusValue d (push luckingFist (b,c+value)) value;
  21.  
  22. fun dijkstraAux graph [] destino = valOf(Int.maxInt) (* devolver infinito *)
  23.     | dijkstraAux graph ((x,y)::PQ) destino = if destino = x (* encontre el destino en la PQ y termina *)
  24.                                                 then y
  25.                                                 else let val (p,k) = quitarAristas graph x [] []
  26.                                                      in dijkstraAux p (pushListPlusValue k PQ y) destino end
  27.  
  28. fun dijkstra graph origen myBool =  let fun invertGraph [] = [] (* a la funcion dijkstra se le pasa el grafo, el punto de origen y un bool que es true si el grafo es dirigido, false si no *)
  29.                                             | invertGraph ((a,b,c)::d) = (b,a,c)::(invertGraph d)
  30.                                         val nonDirected = if myBool then graph else graph @ (invertGraph graph)
  31.                                         fun callNodeList [] = []
  32.                                             | callNodeList (a::b) = (a,(dijkstraAux nonDirected [(origen,0)] a)) :: (callNodeList b)
  33.                                     in callNodeList (listaNodos graph) end
  34.  
  35. (* tests below *)
  36.  
  37. val G = [(1,2,1), (1,3,1), (2,4,2), (3,4,3), (4,5,9)];
  38. dijkstra G 3 false; (* false -> grafo no dirigido *)
  39.  
  40. val G = [(1,2,1), (1,3,1), (2,4,2), (3,4,3), (4,5,9)];
  41. dijkstra G 4 true; (* true -> grafo dirigido *)
  42.  
  43. val G = [(1,2,2), (2,3,3), (3,1,1), (1,4,5), (4,5,8), (5,3,1), (3,4,1)];
  44. dijkstra G 1 false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement