-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20150423.hs
53 lines (40 loc) · 1.82 KB
/
20150423.hs
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
-- ## Trabalho 09 (23/04/2015) ##
-- Questão 1: fazer os exercícios de todos os slides vistos até esta data (arquivo .txt)
-- Questão 2 (a completar)
type Vertex t = t
type Edge t = (Vertex t, Vertex t, Int) -- Vértices adjacentes + Peso da aresta
type Node t = (Vertex t, Int) -- Vértice + Distância até a origem
type Pair t = (Vertex t, Vertex t) -- Aresta sem pesos
data Graph t = Graph [Vertex t] [Edge t] deriving (Eq, Ord, Show)
getFirst :: Edge t -> Vertex t
getFirst (u, _, _) = u
getSecond :: Edge t -> Vertex t
getSecond (_, v, _) = v
getThird :: Edge t -> Int
getThird (_, _, w) = w
refresh :: (Eq t) => Vertex t -> [Edge t] -> [Node t] -> [Pair t] -> ([Node t], [Pair t])
refresh vertex edges queue prev
| edges == [] = (queue, prev)
| (getFirst (head edges) == vertex) &&
where
-- alt = dist[vertex] + length(vertex, adjVertex)
extractPath :: (Eq t) => Vertex t -> Vertex t -> [Pair t] -> [Pair t] -> [Pair t]
extractPath src target incompletePath realPath
| target == src = realPath
| otherwise = extractPath src parent incompletePath ((parent, target):realPath)
where
auxList = [x | x <- incompletePath, (snd x) == target]
parent = fst (head auxList)
aux :: (Eq t) => Graph t -> Vertex t -> Vertex t -> [Node t] -> [Pair t] -> [Pair t]
aux (Graph vertices edges) src target minQueue prev
| minQueue == [] = extractPath src target prev []
|
dijkstra :: (Eq t) => Graph t -> Vertex t -> Vertex t -> [Pair t]
dijkstra (Graph vertices edges) src target = aux (Graph vertices edges) src target queue []
where
maxDist = foldr ((+).(getThird)) 0 edges
queue = (src, 0):[(x, maxDist) | x <- vertices, not(x == src)]
geraFuncaoMenorCaminho :: (Eq t) => Graph t -> (Vertex t -> Vertex t -> [Pair t])
geraFuncaoMenorCaminho graph = findShortestPath
where
findShortestPath src target = dijkstra graph src target