module I1M.Grafo
(Orientacion (..),
Grafo,
creaGrafo,
dirigido,
adyacentes,
nodos,
aristas,
aristaEn,
peso
) where
import Data.Array
data Orientacion = D | ND
deriving (Orientacion -> Orientacion -> Bool
(Orientacion -> Orientacion -> Bool)
-> (Orientacion -> Orientacion -> Bool) -> Eq Orientacion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Orientacion -> Orientacion -> Bool
== :: Orientacion -> Orientacion -> Bool
$c/= :: Orientacion -> Orientacion -> Bool
/= :: Orientacion -> Orientacion -> Bool
Eq, Int -> Orientacion -> ShowS
[Orientacion] -> ShowS
Orientacion -> String
(Int -> Orientacion -> ShowS)
-> (Orientacion -> String)
-> ([Orientacion] -> ShowS)
-> Show Orientacion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Orientacion -> ShowS
showsPrec :: Int -> Orientacion -> ShowS
$cshow :: Orientacion -> String
show :: Orientacion -> String
$cshowList :: [Orientacion] -> ShowS
showList :: [Orientacion] -> ShowS
Show)
data Grafo v p = G Orientacion (Array v [(v,p)])
deriving (Grafo v p -> Grafo v p -> Bool
(Grafo v p -> Grafo v p -> Bool)
-> (Grafo v p -> Grafo v p -> Bool) -> Eq (Grafo v p)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall v p. (Ix v, Eq p) => Grafo v p -> Grafo v p -> Bool
$c== :: forall v p. (Ix v, Eq p) => Grafo v p -> Grafo v p -> Bool
== :: Grafo v p -> Grafo v p -> Bool
$c/= :: forall v p. (Ix v, Eq p) => Grafo v p -> Grafo v p -> Bool
/= :: Grafo v p -> Grafo v p -> Bool
Eq, Int -> Grafo v p -> ShowS
[Grafo v p] -> ShowS
Grafo v p -> String
(Int -> Grafo v p -> ShowS)
-> (Grafo v p -> String)
-> ([Grafo v p] -> ShowS)
-> Show (Grafo v p)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall v p. (Ix v, Show v, Show p) => Int -> Grafo v p -> ShowS
forall v p. (Ix v, Show v, Show p) => [Grafo v p] -> ShowS
forall v p. (Ix v, Show v, Show p) => Grafo v p -> String
$cshowsPrec :: forall v p. (Ix v, Show v, Show p) => Int -> Grafo v p -> ShowS
showsPrec :: Int -> Grafo v p -> ShowS
$cshow :: forall v p. (Ix v, Show v, Show p) => Grafo v p -> String
show :: Grafo v p -> String
$cshowList :: forall v p. (Ix v, Show v, Show p) => [Grafo v p] -> ShowS
showList :: [Grafo v p] -> ShowS
Show)
creaGrafo :: (Ix v, Num p) => Orientacion -> (v,v) -> [(v,v,p)] -> Grafo v p
creaGrafo :: forall v p.
(Ix v, Num p) =>
Orientacion -> (v, v) -> [(v, v, p)] -> Grafo v p
creaGrafo Orientacion
o (v, v)
cs [(v, v, p)]
vs =
Orientacion -> Array v [(v, p)] -> Grafo v p
forall v p. Orientacion -> Array v [(v, p)] -> Grafo v p
G Orientacion
o (([(v, p)] -> (v, p) -> [(v, p)])
-> [(v, p)] -> (v, v) -> [(v, (v, p))] -> Array v [(v, p)]
forall i e a.
Ix i =>
(e -> a -> e) -> e -> (i, i) -> [(i, a)] -> Array i e
accumArray
(\[(v, p)]
xs (v, p)
x -> [(v, p)]
xs[(v, p)] -> [(v, p)] -> [(v, p)]
forall a. [a] -> [a] -> [a]
++[(v, p)
x]) [] (v, v)
cs
((if Orientacion
o Orientacion -> Orientacion -> Bool
forall a. Eq a => a -> a -> Bool
== Orientacion
D then []
else [(v
x2,(v
x1,p
p))|(v
x1,v
x2,p
p) <- [(v, v, p)]
vs, v
x1 v -> v -> Bool
forall a. Eq a => a -> a -> Bool
/= v
x2]) [(v, (v, p))] -> [(v, (v, p))] -> [(v, (v, p))]
forall a. [a] -> [a] -> [a]
++
[(v
x1,(v
x2,p
p)) | (v
x1,v
x2,p
p) <- [(v, v, p)]
vs]))
dirigido :: (Ix v,Num p) => Grafo v p -> Bool
dirigido :: forall v p. (Ix v, Num p) => Grafo v p -> Bool
dirigido (G Orientacion
o Array v [(v, p)]
_) = Orientacion
o Orientacion -> Orientacion -> Bool
forall a. Eq a => a -> a -> Bool
== Orientacion
D
nodos :: (Ix v,Num p) => Grafo v p -> [v]
nodos :: forall v p. (Ix v, Num p) => Grafo v p -> [v]
nodos (G Orientacion
_ Array v [(v, p)]
g) = Array v [(v, p)] -> [v]
forall i e. Ix i => Array i e -> [i]
indices Array v [(v, p)]
g
adyacentes :: (Ix v,Num p) => Grafo v p -> v -> [v]
adyacentes :: forall v p. (Ix v, Num p) => Grafo v p -> v -> [v]
adyacentes (G Orientacion
_ Array v [(v, p)]
g) v
v = ((v, p) -> v) -> [(v, p)] -> [v]
forall a b. (a -> b) -> [a] -> [b]
map (v, p) -> v
forall a b. (a, b) -> a
fst (Array v [(v, p)]
gArray v [(v, p)] -> v -> [(v, p)]
forall i e. Ix i => Array i e -> i -> e
!v
v)
aristaEn :: (Ix v,Num p) => Grafo v p -> (v,v) -> Bool
aristaEn :: forall v p. (Ix v, Num p) => Grafo v p -> (v, v) -> Bool
aristaEn Grafo v p
g (v
x,v
y) = v
y v -> [v] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Grafo v p -> v -> [v]
forall v p. (Ix v, Num p) => Grafo v p -> v -> [v]
adyacentes Grafo v p
g v
x
peso :: (Ix v,Num p) => v -> v -> Grafo v p -> p
peso :: forall v p. (Ix v, Num p) => v -> v -> Grafo v p -> p
peso v
x v
y (G Orientacion
_ Array v [(v, p)]
g) = [p] -> p
forall a. HasCallStack => [a] -> a
head [p
c | (v
a,p
c) <- Array v [(v, p)]
gArray v [(v, p)] -> v -> [(v, p)]
forall i e. Ix i => Array i e -> i -> e
!v
x , v
a v -> v -> Bool
forall a. Eq a => a -> a -> Bool
== v
y]
aristas :: (Ix v,Num p) => Grafo v p -> [(v,v,p)]
aristas :: forall v p. (Ix v, Num p) => Grafo v p -> [(v, v, p)]
aristas (G Orientacion
o Array v [(v, p)]
g) = [(v
v1,v
v2,p
w) | v
v1 <- Grafo v p -> [v]
forall v p. (Ix v, Num p) => Grafo v p -> [v]
nodos (Orientacion -> Array v [(v, p)] -> Grafo v p
forall v p. Orientacion -> Array v [(v, p)] -> Grafo v p
G Orientacion
o Array v [(v, p)]
g) , (v
v2,p
w) <- Array v [(v, p)]
gArray v [(v, p)] -> v -> [(v, p)]
forall i e. Ix i => Array i e -> i -> e
!v
v1]