{-# OPTIONS_GHC -Wall #-}

-- Graphs as adjacency maps, and searching them.
module Graph where

import qualified Data.Map as Map
import qualified Data.Set as Set

-- For each node, the list of its neighbours.
type Graph v = Map.Map v [v]

--------------------------------------------------------------------------------
-- Building graphs

--     Ana ---- Ben             Gus ---- Hugo
--      |        |
--     Cleo --- Dan ---- Eva ---- Finn
friendships :: [(String, String)]
friendships =
  [ ("Ana", "Ben"), ("Ana", "Cleo"), ("Ben", "Dan"), ("Cleo", "Dan"),
    ("Dan", "Eva"), ("Eva", "Finn"), ("Gus", "Hugo")
  ]

-- A node with no entry in the map has no neighbours.
neighbours :: (Ord v) => Graph v -> v -> [v]
neighbours g v = Map.findWithDefault [] v g

-- Each edge is stored in both directions.
-- `wordFreq` again: there the counts were combined with (+), here the lists with (++).
fromEdges :: (Ord v) => [(v, v)] -> Graph v
fromEdges es =
  Map.fromListWith (++) ([(a, [b]) | (a, b) <- es] ++ [(b, [a]) | (a, b) <- es])

friends :: Graph String
friends = fromEdges friendships

--------------------------------------------------------------------------------
-- Searching

-- Loops forever on a cycle: Cleo is Ana's friend, and Ana is Cleo's.
--   ghci> take 8 (reachNaive friends "Ana")
reachNaive :: (Ord v) => Graph v -> v -> [v]
reachNaive g v = v : concatMap (reachNaive g) (neighbours g v)

-- New neighbours at the front of the to-do list: a stack, depth-first.
dfs :: (Ord v) => Graph v -> v -> [v]
dfs g start = go Set.empty [start]
  where
    go _ [] = []
    go seen (v : todo)
      | Set.member v seen = go seen todo
      | otherwise = v : go (Set.insert v seen) (neighbours g v ++ todo)

-- New neighbours at the back: a queue, breadth-first.
-- `todo ++ ...` is slow on long lists: lab 9's `Queue` fixes it.
bfs :: (Ord v) => Graph v -> v -> [v]
bfs g start = go Set.empty [start]
  where
    go _ [] = []
    go seen (v : todo)
      | Set.member v seen = go seen todo
      | otherwise = v : go (Set.insert v seen) (todo ++ neighbours g v)

-- Laziness: `elem` stops the search as soon as `b` is found.
connected :: (Ord v) => Graph v -> v -> v -> Bool
connected g a b = b `elem` dfs g a

-- Breadth-first, with the distance carried in the to-do list.
-- The map doubles as the set of seen nodes.
distances :: (Ord v) => Graph v -> v -> Map.Map v Int
distances g start = go Map.empty [(start, 0)]
  where
    go dist [] = dist
    go dist ((v, d) : todo)
      | Map.member v dist = go dist todo
      | otherwise =
          go (Map.insert v d dist) (todo ++ [(w, d + 1) | w <- neighbours g v])

--------------------------------------------------------------------------------
-- A graph nobody stores

-- `distances` only used the graph through `neighbours g`: ask for that function instead.
--   distances g = distancesBy (neighbours g)
distancesBy :: (Ord v) => (v -> [v]) -> v -> Map.Map v Int
distancesBy next start = go Map.empty [(start, 0)]
  where
    go dist [] = dist
    go dist ((v, d) : todo)
      | Map.member v dist = go dist todo
      | otherwise =
          go (Map.insert v d dist) (todo ++ [(w, d + 1) | w <- next v])

-- The squares a knight can jump to, on an 8x8 board.
knight :: (Int, Int) -> [(Int, Int)]
knight (x, y) =
  [ (x + dx, y + dy)
  | (dx, dy) <- [(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)],
    x + dx >= 1, x + dx <= 8, y + dy >= 1, y + dy <= 8
  ]

main :: IO ()
main = do
  print (take 8 (reachNaive friends "Ana"))
  print (dfs friends "Ana")
  print (bfs friends "Ana")
  print (connected friends "Ana" "Finn", connected friends "Ana" "Hugo")
  print (distances friends "Ana")
  print (Map.lookup (8, 8) (distancesBy knight (1, 1)))
