{-# 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 = undefined

-- Each edge is stored in both directions.
fromEdges :: (Ord v) => [(v, v)] -> Graph v
fromEdges = undefined

friends :: Graph String
friends = fromEdges friendships

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

-- Ana, then everyone her friends can reach. Try it on `friends`.
reachNaive :: (Ord v) => Graph v -> v -> [v]
reachNaive = undefined

-- Remember the nodes already seen, and keep a to-do list of nodes to visit.
dfs :: (Ord v) => Graph v -> v -> [v]
dfs = undefined

bfs :: (Ord v) => Graph v -> v -> [v]
bfs = undefined

connected :: (Ord v) => Graph v -> v -> v -> Bool
connected = undefined

-- The number of steps from the start to every node it can reach.
distances :: (Ord v) => Graph v -> v -> Map.Map v Int
distances = undefined

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

distancesBy :: (Ord v) => (v -> [v]) -> v -> Map.Map v Int
distancesBy = undefined

-- 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
  ]

-- Silences the warning on the unused import until `dfs` is written.
_seen :: Set.Set ()
_seen = Set.empty
