-- |
-- Module      : RecorridoEnAnchura
-- Description : Recorrido de grafos en anchura.
-- License     : Creative Commons
-- Maintainer  : José A. Alonso
-- 
-- = Recorrido de grafos en anchura
-- 
-- En los ejemplos se usará el siguiente grafo
-- 
-- >   +---> 2 <---+
-- >   |           |
-- >   |           |
-- >   1 --> 3 --> 6 --> 5
-- >   |                 |
-- >   |                 |
-- >   +---> 4 <---------+
-- definido por
-- > g = creaGrafo D (1,6) 
-- >               [(1,2,0),(1,3,0),(1,4,0),(3,6,0),(5,4,0),(6,2,0),(6,5,0)]

module I1M.RecorridoEnAnchura (recorridoEnAnchura) where

-- ---------------------------------------------------------------------
-- Librerías auxiliares                                               --
-- ---------------------------------------------------------------------

import Data.Ix
import I1M.Grafo

-- ---------------------------------------------------------------------
-- Ejemplo de grafo                                                   --
-- ---------------------------------------------------------------------

-- g es el grafo
--    +---> 2 <---+
--    |           |
--    |           |
--    1 --> 3 --> 6 --> 5
--    |                 |
--    |                 |
--    +---> 4 <---------+
-- g = creaGrafo D (1,6) 
--               [(1,2,0),(1,3,0),(1,4,0),(3,6,0),(5,4,0),(6,2,0),(6,5,0)]

-- ---------------------------------------------------------------------
-- Recorrido en anchura con colas                                      --
-- ---------------------------------------------------------------------

-- | (recorridoEnAnchura i g) es el recorrido en anchura del grafo g
-- desde el vértice i, usando colas. Por ejemplo, 
-- 
-- > recorridoEnAnchura 1 g  ==  [1,4,3,2,6,5]
recorridoEnAnchura :: (Num p, Ix v) => v -> Grafo v p -> [v]
recorridoEnAnchura :: forall p v. (Num p, Ix v) => v -> Grafo v p -> [v]
recorridoEnAnchura v
i Grafo v p
g = [v] -> [v]
forall a. [a] -> [a]
reverse ([v] -> [v] -> [v]
ra [v
i] []) where 
  ra :: [v] -> [v] -> [v]
ra [] [v]
vis    = [v]
vis
  ra (v
c:[v]
cs) [v]
vis 
    | v
c v -> [v] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [v]
vis = [v] -> [v] -> [v]
ra [v]
cs [v]
vis
    | Bool
otherwise    = [v] -> [v] -> [v]
ra ([v]
cs [v] -> [v] -> [v]
forall a. [a] -> [a] -> [a]
++ Grafo v p -> v -> [v]
forall v p. (Ix v, Num p) => Grafo v p -> v -> [v]
adyacentes Grafo v p
g v
c) (v
cv -> [v] -> [v]
forall a. a -> [a] -> [a]
:[v]
vis)

-- Traza del cálculo de (recorridoEnAnchura 1 g)
--    RecorridoEnAnchura 1 g
--    = ra [1]     []
--    = ra [2,3,4] [1]
--    = ra [3,4]   [2,1]
--    = ra [4,6]   [3,2,1]
--    = ra [6]     [4,3,2,1]
--    = ra [2,5]   [6,4,3,2,1]
--    = ra [5]     [6,4,3,2,1]
--    = ra [4]     [5,6,4,3,2,1]
--    = ra []      [5,6,4,3,2,1]
--    = [1,2,3,4,6,5]