module Math.Combinatorics.GraphAuts where
import qualified Data.List as L
import qualified Data.Map as M
import qualified Data.Set as S
import Math.Common.ListSet
import Math.Combinatorics.Graph
import Math.Algebra.Group.PermutationGroup
import Math.Algebra.Group.SchreierSims as SS
isVertexTransitive (G [] []) = True
isVertexTransitive g@(G (v:vs) es) = orbitV auts v == v:vs where
auts = graphAuts g
isEdgeTransitive (G _ []) = True
isEdgeTransitive g@(G vs (e:es)) = orbitE auts e == e:es where
auts = graphAuts g
arc ->^ g = map (.^ g) arc
isArcTransitive (G _ []) = True
isArcTransitive g@(G vs es) = orbit (->^) a auts == a:as where
a:as = L.sort $ es ++ map reverse es
auts = graphAuts g
isArcTransitive' g@(G (v:vs) es) =
orbitP auts v == v:vs &&
orbitP stab n == n:ns
where auts = graphAuts g
stab = dropWhile (\p -> v .^ p /= v) auts
n:ns = nbrs g v
findArcs g@(G vs es) x l = map reverse $ dfs [ ([x],0) ] where
dfs ( (z1:z2:zs,l') : nodes)
| l == l' = (z1:z2:zs) : dfs nodes
| otherwise = dfs $ [(w:z1:z2:zs,l'+1) | w <- nbrs g z1, w /= z2] ++ nodes
dfs ( ([z],l') : nodes)
| l == l' = [z] : dfs nodes
| otherwise = dfs $ [([w,z],l'+1) | w <- nbrs g z] ++ nodes
dfs [] = []
isnArcTransitive _ (G [] []) = True
isnArcTransitive n g@(G (v:vs) es) =
orbitP auts v == v:vs &&
orbit (->^) a stab == a:as
where auts = graphAuts g
stab = dropWhile (\p -> v .^ p /= v) auts
a:as = findArcs g v n
is2ArcTransitive g = isnArcTransitive 2 g
is3ArcTransitive g = isnArcTransitive 3 g
isDistanceTransitive (G [] []) = True
isDistanceTransitive g@(G (v:vs) es)
| isConnected g =
orbitP auts v == v:vs &&
length stabOrbits == diameter g + 1
| otherwise = error "isDistanceTransitive: only defined for connected graphs"
where auts = graphAuts g
stab = dropWhile (\p -> v .^ p /= v) auts
stabOrbits = let os = orbits stab in os ++ map (:[]) ((v:vs) L.\\ concat os)
refine p1 p2 = concat [ [c1 `intersect` c2 | c2 <- p2] | c1 <- p1]
isGraphAut (G vs es) h = all (`S.member` es') [e -^ h | e <- es]
where es' = S.fromList es
graphAuts1 (G vs es) = dfs [] vs vs
where dfs xys (x:xs) ys =
concat [dfs ((x,y):xys) xs (L.delete y ys) | y <- ys, isCompatible (x,y) xys]
dfs xys [] [] = [fromPairs xys]
isCompatible (x,y) xys = and [([x',x] `S.member` es') == (L.sort [y,y'] `S.member` es') | (x',y') <- xys]
es' = S.fromList es
graphAuts2 (G vs es) = graphAuts' [] vs
where graphAuts' us (v:vs) =
let uus = zip us us
in concat [take 1 $ dfs ((v,w):uus) vs (v : L.delete w vs) | w <- vs, isCompatible (v,w) uus]
++ graphAuts' (v:us) vs
graphAuts' _ [] = []
dfs xys (x:xs) ys =
concat [dfs ((x,y):xys) xs (L.delete y ys) | y <- ys, isCompatible (x,y) xys]
dfs xys [] [] = [fromPairs xys]
isCompatible (x,y) xys = and [([x',x] `S.member` es') == (L.sort [y,y'] `S.member` es') | (x',y') <- xys]
es' = S.fromList es
graphAuts3 g@(G vs es) = graphAuts' [] [vs] where
graphAuts' us ((x:ys):pt) =
let px = refine (ys : pt) (dps M.! x)
p y = refine ((x : L.delete y ys) : pt) (dps M.! y)
uus = zip us us
p' = L.sort $ filter (not . null) $ px
in concat [take 1 $ dfs ((x,y):uus) px (p y) | y <- ys]
++ graphAuts' (x:us) p'
graphAuts' us ([]:pt) = graphAuts' us pt
graphAuts' _ [] = []
dfs xys p1 p2
| map length p1 /= map length p2 = []
| otherwise =
let p1' = filter (not . null) p1
p2' = filter (not . null) p2
in if all isSingleton p1'
then let xys' = xys ++ zip (concat p1') (concat p2')
in if isCompatible xys' then [fromPairs' xys'] else []
else let (x:xs):p1'' = p1'
ys:p2'' = p2'
in concat [dfs ((x,y):xys)
(refine (xs : p1'') (dps M.! x))
(refine ((L.delete y ys):p2'') (dps M.! y))
| y <- ys]
isCompatible xys = and [([x,x'] `S.member` es') == (L.sort [y,y'] `S.member` es') | (x,y) <- xys, (x',y') <- xys, x < x']
dps = M.fromList [(v, distancePartition g v) | v <- vs]
es' = S.fromList es
isSingleton [_] = True
isSingleton _ = False
graphAuts g@(G vs es) = graphAuts' [] [vs] where
graphAuts' us p@((x:ys):pt) =
let p' = L.sort $ filter (not . null) $ refine (ys:pt) (dps M.! x)
in level us p x ys []
++ graphAuts' (x:us) p'
graphAuts' us ([]:pt) = graphAuts' us pt
graphAuts' _ [] = []
level us p@(ph:pt) x (y:ys) hs =
let px = refine (L.delete x ph : pt) (dps M.! x)
py = refine (L.delete y ph : pt) (dps M.! y)
uus = zip us us
in case dfs ((x,y):uus) px py of
[] -> level us p x ys hs
h:_ -> let hs' = h:hs in h : level us p x (ys L.\\ (x .^^ hs')) hs'
level _ _ _ [] _ = []
dfs xys p1 p2
| map length p1 /= map length p2 = []
| otherwise =
let p1' = filter (not . null) p1
p2' = filter (not . null) p2
in if all isSingleton p1'
then let xys' = xys ++ zip (concat p1') (concat p2')
in if isCompatible xys' then [fromPairs' xys'] else []
else let (x:xs):p1'' = p1'
ys:p2'' = p2'
in concat [dfs ((x,y):xys)
(refine (xs : p1'') (dps M.! x))
(refine ((L.delete y ys):p2'') (dps M.! y))
| y <- ys]
isCompatible xys = and [([x,x'] `S.member` es') == (L.sort [y,y'] `S.member` es') | (x,y) <- xys, (x',y') <- xys, x < x']
dps = M.fromList [(v, distancePartition g v) | v <- vs]
es' = S.fromList es
removeGens x gs = removeGens' [] gs where
baseOrbit = x .^^ gs
removeGens' ls (r:rs) =
if x .^^ (ls++rs) == baseOrbit
then removeGens' ls rs
else removeGens' (r:ls) rs
removeGens' ls [] = reverse ls
graphAutsNew g@(G vs es) = graphAuts' [] [] [vs] where
graphAuts' us hs p@((x:ys):pt) =
let ys' = ys L.\\ (x .^^ hs)
hs' = level us p x ys' []
p' = L.sort $ filter (not . null) $ refine (ys:pt) (dps M.! x)
reps = cosetRepsGx (hs'++hs) x
schreierGens = removeGens x $ schreierGeneratorsGx (x,reps) (hs'++hs)
in hs' ++ graphAuts' (x:us) schreierGens p'
graphAuts' us hs ([]:pt) = graphAuts' us hs pt
graphAuts' _ _ [] = []
level us p@(ph:pt) x (y:ys) hs =
let px = refine (L.delete x ph : pt) (dps M.! x)
py = refine (L.delete y ph : pt) (dps M.! y)
uus = zip us us
in if map length px /= map length py
then level us p x ys hs
else case dfs ((x,y):uus) (filter (not . null) px) (filter (not . null) py) of
[] -> level us p x ys hs
h:_ -> let hs' = h:hs in h : level us p x (ys L.\\ (x .^^ hs')) hs'
level _ _ _ [] _ = []
dfs xys p1 p2
| map length p1 /= map length p2 = []
| otherwise =
let p1' = filter (not . null) p1
p2' = filter (not . null) p2
in if all isSingleton p1'
then let xys' = xys ++ zip (concat p1') (concat p2')
in if isCompatible xys' then [fromPairs' xys'] else []
else let (x:xs):p1'' = p1'
ys:p2'' = p2'
in concat [dfs ((x,y):xys)
(refine (xs : p1'') (dps M.! x))
(refine ((L.delete y ys):p2'') (dps M.! y))
| y <- ys]
isCompatible xys = and [([x,x'] `S.member` es') == (L.sort [y,y'] `S.member` es') | (x,y) <- xys, (x',y') <- xys, x < x']
dps = M.fromList [(v, distancePartition g v) | v <- vs]
es' = S.fromList es
graphIsos g1 g2 = concat [dfs [] (distancePartition g1 v1) (distancePartition g2 v2) | v2 <- vertices g2] where
v1 = head $ vertices g1
dfs xys p1 p2
| map length p1 /= map length p2 = []
| otherwise =
let p1' = filter (not . null) p1
p2' = filter (not . null) p2
in if all isSingleton p1'
then let xys' = xys ++ zip (concat p1') (concat p2')
in if isCompatible xys' then [xys'] else []
else let (x:xs):p1'' = p1'
ys:p2'' = p2'
in concat [dfs ((x,y):xys)
(refine (xs : p1'') (dps1 M.! x))
(refine ((L.delete y ys):p2'') (dps2 M.! y))
| y <- ys]
isCompatible xys = and [([x,x'] `S.member` es1) == (L.sort [y,y'] `S.member` es2) | (x,y) <- xys, (x',y') <- xys, x < x']
dps1 = M.fromList [(v, distancePartition g1 v) | v <- vertices g1]
dps2 = M.fromList [(v, distancePartition g2 v) | v <- vertices g2]
es1 = S.fromList $ edges g1
es2 = S.fromList $ edges g2
isIso g1 g2 = (not . null) (graphIsos g1 g2)