Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. inf = 100000
  2.  
  3. dijkstra  ::Int->Int->[(Int,Int,Int)]->[(Int,Int)]->[(Int,Int)]
  4. dijkstra  s d adjList visited
  5.         | (p,q) == (inf, inf) = visited
  6.         | otherwise  = dijkstra p d adjList ((p,q):visited)
  7.         where (p,q) = exploreNeighbours s visited adjList
  8.  
  9. exploreNeighbours ::Int->[(Int,Int)]->[(Int,Int,Int)]->(Int,Int)
  10. exploreNeighbours source visited adjList =
  11.   retMinNode (relax [(x,y,z) | (x,y,z) <- [(s,d,e) | (s,d,e)<-adjList, (belongsTo source visited)], not (belongsTo y visited)] visited)
  12.  
  13. belongsTo::Int->[(Int,Int)]->Bool
  14. belongsTo _ [] = False
  15. belongsTo s ((x,_):xs)
  16.     | (x==s) = True
  17.     | otherwise = belongsTo s xs
  18.  
  19. relax :: [(Int,Int,Int)]->[(Int,Int)]->[(Int,Int)]
  20. relax [] visited = []
  21. relax ((x,y,z):ns) visited = (y, (currDist x) + z):relax ns visited
  22.     where currDist n = foldl(\acc t -> if (fst t == n) then (snd t) else acc) inf visited
  23.  
  24. retMinNode ::[(Int,Int)]->(Int,Int)
  25. retMinNode [] = (inf,inf)
  26. retMinNode arr = foldl (\acc t -> if ((snd t) < (snd acc)) then t else acc) (inf,inf) arr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement