```-- Copyright (c) David Amos, 2009. All rights reserved.

module Math.Combinatorics.Hypergraph where

import qualified Data.List as L
import Math.Common.ListSet
import Math.Combinatorics.Graph hiding (incidenceMatrix)
import Math.Algebra.Group.PermutationGroup (orbitB, p) -- needed for construction of Coxeter group

-- not used in this module, only in GHCi
import Math.Algebra.Field.Base
import Math.Algebra.Field.Extension
import Math.Combinatorics.Design hiding (incidenceMatrix, incidenceGraph, dual, isSubset, fanoPlane)

-- set system or hypergraph
data Hypergraph a = H [a] [[a]] deriving (Eq,Ord,Show)

hypergraph xs bs | isSetSystem xs bs = H xs bs

toHypergraph xs bs = H xs' bs' where
xs' = L.sort xs
bs' = L.sort \$ map L.sort bs
-- this still doesn't guarantee that all bs are subset of xs

-- uniform hypergraph - all blocks are same size
isUniform h@(H xs bs) = isSetSystem xs bs && same (map length bs)

same (x:xs) = all (==x) xs
same [] = True

fromGraph (G vs es) = H vs es
fromDesign (D xs bs) = H xs (L.sort bs)
-- !! should insist that designs have blocks in order
-- !! dual probably doesn't guarantee this at present

{-
dual (H xs bs) = toHypergraph (bs, map beta xs) where
beta x = filter (x `elem`) bs
-}

-- INCIDENCE GRAPH

data Incidence a = P a | B [a] deriving (Eq, Ord, Show)

-- compare Design, where we just use Left, Right

-- Also called the Levi graph
incidenceGraph (H xs bs) = G vs es where
vs = map P xs ++ map B bs
es = L.sort [ [P x, B b] | b <- bs, x <- b]

-- INCIDENCE MATRIX

-- !! why are we doing this the other way round to the literature ??

-- incidence matrix of a hypergraph
-- (rows and columns indexed by edges and vertices respectively)
-- (warning: in the literature it is often the other way round)
incidenceMatrix (H vs es) = [ [if v `elem` e then 1 else 0 | v <- vs] | e <- es]

fromIncidenceMatrix m = H vs es where
n = L.genericLength \$ head m
vs = [1..n]
es = L.sort \$ map edge m
edge row = [v | (1,v) <- zip row vs]

-- isTwoGraph

-- We can represent various incidence structures as hypergraphs,
-- by identifying the lines with the sets of points that they contain

isPartialLinearSpace h@(H ps ls) =
isSetSystem ps ls &&
all ( (<=1) . length ) [filter (pair `isSubset`) ls | pair <- combinationsOf 2 ps]
-- any two points are incident with at most one line

-- Godsil & Royle, p79
isProjectivePlane h@(H ps ls) =
isSetSystem ps ls &&
all ( (==1) . length) [intersect l1 l2 | [l1,l2] <- combinationsOf 2 ls] && -- any two lines meet in a unique point
all ( (==1) . length) [ filter ([p1,p2] `isSubset`) ls | [p1,p2] <- combinationsOf 2 ps] -- any two points lie in a unique line

-- a projective plane with a triangle
-- this is a weak non-degeneracy condition, which eliminates all points on the same line, or all lines through the same point
isProjectivePlaneTri h@(H ps ls) =
isProjectivePlane h && any triangle (combinationsOf 3 ps)
where triangle t@[p1,p2,p3] =
(not . null) [l | l <- ls, [p1,p2] `isSubset` l, p3 `notElem` l] && -- there is a line containing p1,p2 but not p3
(not . null) [l | l <- ls, [p1,p3] `isSubset` l, p2 `notElem` l] &&
(not . null) [l | l <- ls, [p2,p3] `isSubset` l, p1 `notElem` l]

-- a projective plane with a quadrangle
-- this is a stronger non-degeneracy condition
isProjectivePlaneQuad h@(H ps ls) =
isProjectivePlane h && any quadrangle (combinationsOf 4 ps)
where quadrangle q = all (not . collinear) (combinationsOf 3 q) -- no three points collinear
collinear ps = any (ps `isSubset`) ls

-- > isProjectivePlaneQuad \$ fromDesign \$ pg2 f2
-- True

-- GENERALIZED QUADRANGLES

-- Godsil & Royle p81
isGeneralizedQuadrangle h@(H ps ls) =
isPartialLinearSpace h &&
all (\(l,p) -> unique [p' | p' <- l, collinear (pair p p')]) [(l,p) | l <- ls, p <- ps, p `notElem` l] &&
-- given any line l and point p not on l, there is a unique point p' on l with p and p' collinear
any (not . collinear) (powerset ps) && -- there are non collinear points
any (not . concurrent) (powerset ls) -- there are non concurrent lines
where unique xs = length xs == 1
pair x y = if x < y then [x,y] else [y,x]
collinear ps = any (ps `isSubset`) ls
concurrent ls = any (\p -> all (p `elem`) ls) ps

grid m n = H ps ls where
ps = [(i,j) | i <- [1..m], j <- [1..n] ]
ls = L.sort \$ [ [(i,j) | i <- [1..m] ] | j <- [1..n] ] -- horizontal lines
++ [ [(i,j) | j <- [1..n] ] | i <- [1..m] ] -- vertical lines

dualGrid m n = fromGraph \$ kb m n
-- the lines of the grid are the points of the dual, and the points of the grid are the lines of the dual

isGenQuadrangle' h = diameter g == 4 && girth g == 8 -- !! plus non-degeneracy conditions
where g = incidenceGraph h

-- CONFIGURATIONS

-- http://en.wikipedia.org/wiki/Projective_configuration
isConfiguration h@(H ps ls) =
isUniform h && -- a set system, with each line incident with the same number of points
same [length (filter (p `elem`) ls) | p <- ps] -- each point is incident with the same number of lines

fanoPlane = toHypergraph [1..7] [[1,2,4],[2,3,5],[3,4,6],[4,5,7],[5,6,1],[6,7,2],[7,1,3]]

heawoodGraph = incidenceGraph fanoPlane

desarguesConfiguration = H xs bs where
xs = combinationsOf 2 [1..5]
bs = [ [x | x <- xs, x `isSubset` b] | b <- combinationsOf 3 [1..5] ]

desarguesGraph = incidenceGraph desarguesConfiguration

pappusConfiguration = H xs bs where
xs = [1..9]
bs = L.sort [ [1,2,3], [4,5,6], [7,8,9], [1,5,9], [1,6,8], [2,4,9], [3,4,8], [2,6,7], [3,5,7] ]

pappusGraph = incidenceGraph pappusConfiguration

-- !! no particular reason why the following is here rather than elsewhere
{-
triples = combinationsOf 3 [1..7]

heptads = [ [a,b,c,d,e,f,g] | a <- triples,
b <- triples, a < b, meetOne b a,
c <- triples, b < c, all (meetOne c) [a,b],
d <- triples, c < d, all (meetOne d) [a,b,c],
e <- triples, d < e, all (meetOne e) [a,b,c,d],
f <- triples, e < f, all (meetOne f) [a,b,c,d,e],
g <- triples, f < g, all (meetOne g) [a,b,c,d,e,f],
foldl intersect [1..7] [a,b,c,d,e,f,g] == [] ]
where meetOne x y = length (intersect x y) == 1
-- each pair of triples meet in exactly one point, and there is no point in all of them - Godsil & Royle p69
-- (so these are the projective planes over 7 points)
-}
-- Godsil & Royle p69
coxeterGraph = G vs es where
g = p [[1..7]]
vs = L.sort \$ concatMap (orbitB [g]) [[1,2,4],[3,5,7],[3,6,7],[5,6,7]]
es = [ e | e@[v1,v2] <- combinationsOf 2 vs, disjoint v1 v2]

-- is this the incidence graph of a hypergraph involving heptads over triples?

-- edges of K6
duads = combinationsOf 2 [1..6]

-- 1-factors of K6
-- 15 different ways to pick three disjoint duads from [1..6]
synthemes = [ [d1,d2,d3] | d1 <- duads,
d2 <- duads, d2 > d1, disjoint d1 d2,
d3 <- duads, d3 > d2, disjoint d1 d3, disjoint d2 d3 ]

-- Tutte 8-cage
tutteCoxeterGraph = incidenceGraph \$ H duads synthemes

-- Also known as line graph
intersectionGraph (H xs bs) = G vs es where
vs = bs
es = [pair | pair@[b1,b2] <- combinationsOf 2 bs, not (disjoint b1 b2)]
```