module Algebra.Graph.NonEmpty (
Graph (..), toNonEmpty,
vertex, edge, overlay, overlay1, connect, vertices1, edges1, overlays1,
connects1,
foldg1,
isSubgraphOf, (===),
size, hasVertex, hasEdge, vertexCount, edgeCount, vertexList1, edgeList,
vertexSet, edgeSet,
path1, circuit1, clique1, biclique1, star, stars1, tree, mesh1, torus1,
removeVertex1, removeEdge, replaceVertex, mergeVertices, splitVertex1,
transpose, induce1, induceJust1, simplify, sparsify, sparsifyKL,
box
) where
import Control.DeepSeq
import Control.Monad
import Control.Monad.Trans.State
import Data.List.NonEmpty (NonEmpty (..))
import Data.String
import Algebra.Graph.Internal
import qualified Algebra.Graph as G
import qualified Algebra.Graph.ToGraph as T
import qualified Algebra.Graph.AdjacencyMap as AM
import qualified Algebra.Graph.AdjacencyIntMap as AIM
import qualified Data.Graph as KL
import qualified Data.IntSet as IntSet
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Set as Set
import qualified Data.Tree as Tree
import qualified GHC.Exts as Exts
data Graph a = Vertex a
| Overlay (Graph a) (Graph a)
| Connect (Graph a) (Graph a)
deriving (forall a b. a -> Graph b -> Graph a
forall a b. (a -> b) -> Graph a -> Graph b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Graph b -> Graph a
$c<$ :: forall a b. a -> Graph b -> Graph a
fmap :: forall a b. (a -> b) -> Graph a -> Graph b
$cfmap :: forall a b. (a -> b) -> Graph a -> Graph b
Functor, Int -> Graph a -> ShowS
forall a. Show a => Int -> Graph a -> ShowS
forall a. Show a => [Graph a] -> ShowS
forall a. Show a => Graph a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Graph a] -> ShowS
$cshowList :: forall a. Show a => [Graph a] -> ShowS
show :: Graph a -> String
$cshow :: forall a. Show a => Graph a -> String
showsPrec :: Int -> Graph a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Graph a -> ShowS
Show)
instance NFData a => NFData (Graph a) where
rnf :: Graph a -> ()
rnf (Vertex a
x ) = forall a. NFData a => a -> ()
rnf a
x
rnf (Overlay Graph a
x Graph a
y) = forall a. NFData a => a -> ()
rnf Graph a
x seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf Graph a
y
rnf (Connect Graph a
x Graph a
y) = forall a. NFData a => a -> ()
rnf Graph a
x seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf Graph a
y
instance T.ToGraph (Graph a) where
type ToVertex (Graph a) = a
foldg :: forall r.
r
-> (ToVertex (Graph a) -> r)
-> (r -> r -> r)
-> (r -> r -> r)
-> Graph a
-> r
foldg r
_ = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1
hasEdge :: Eq (ToVertex (Graph a)) =>
ToVertex (Graph a) -> ToVertex (Graph a) -> Graph a -> Bool
hasEdge = forall a. Eq a => a -> a -> Graph a -> Bool
hasEdge
instance Num a => Num (Graph a) where
fromInteger :: Integer -> Graph a
fromInteger = forall a. a -> Graph a
Vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
+ :: Graph a -> Graph a -> Graph a
(+) = forall a. Graph a -> Graph a -> Graph a
Overlay
* :: Graph a -> Graph a -> Graph a
(*) = forall a. Graph a -> Graph a -> Graph a
Connect
signum :: Graph a -> Graph a
signum = forall a. HasCallStack => String -> a
error String
"NonEmpty.Graph.signum cannot be implemented."
abs :: Graph a -> Graph a
abs = forall a. a -> a
id
negate :: Graph a -> Graph a
negate = forall a. a -> a
id
instance IsString a => IsString (Graph a) where
fromString :: String -> Graph a
fromString = forall a. a -> Graph a
Vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString
instance Ord a => Eq (Graph a) where
== :: Graph a -> Graph a -> Bool
(==) = forall a. Ord a => Graph a -> Graph a -> Bool
eq
instance Ord a => Ord (Graph a) where
compare :: Graph a -> Graph a -> Ordering
compare = forall a. Ord a => Graph a -> Graph a -> Ordering
ord
instance Semigroup (Graph a) where
<> :: Graph a -> Graph a -> Graph a
(<>) = forall a. Graph a -> Graph a -> Graph a
overlay
eq :: Ord a => Graph a -> Graph a -> Bool
eq :: forall a. Ord a => Graph a -> Graph a -> Bool
eq Graph a
x Graph a
y = forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMap Graph a
x forall a. Eq a => a -> a -> Bool
== forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMap Graph a
y
{-# NOINLINE [1] eq #-}
{-# RULES "eqInt" eq = eqInt #-}
eqInt :: Graph Int -> Graph Int -> Bool
eqInt :: Graph Int -> Graph Int -> Bool
eqInt Graph Int
x Graph Int
y = forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap Graph Int
x forall a. Eq a => a -> a -> Bool
== forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap Graph Int
y
ord :: Ord a => Graph a -> Graph a -> Ordering
ord :: forall a. Ord a => Graph a -> Graph a -> Ordering
ord Graph a
x Graph a
y = forall a. Ord a => a -> a -> Ordering
compare (forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMap Graph a
x) (forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMap Graph a
y)
{-# NOINLINE [1] ord #-}
{-# RULES "ordInt" ord = ordInt #-}
ordInt :: Graph Int -> Graph Int -> Ordering
ordInt :: Graph Int -> Graph Int -> Ordering
ordInt Graph Int
x Graph Int
y = forall a. Ord a => a -> a -> Ordering
compare (forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap Graph Int
x) (forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap Graph Int
y)
instance Applicative Graph where
pure :: forall a. a -> Graph a
pure = forall a. a -> Graph a
Vertex
Graph (a -> b)
f <*> :: forall a b. Graph (a -> b) -> Graph a -> Graph b
<*> Graph a
x = Graph (a -> b)
f forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Graph a
x)
instance Monad Graph where
return :: forall a. a -> Graph a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
Graph a
g >>= :: forall a b. Graph a -> (a -> Graph b) -> Graph b
>>= a -> Graph b
f = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 a -> Graph b
f forall a. Graph a -> Graph a -> Graph a
Overlay forall a. Graph a -> Graph a -> Graph a
Connect Graph a
g
toNonEmpty :: G.Graph a -> Maybe (Graph a)
toNonEmpty :: forall a. Graph a -> Maybe (Graph a)
toNonEmpty = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
G.foldg forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Graph a
Vertex) (forall {t}. (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
go forall a. Graph a -> Graph a -> Graph a
Overlay) (forall {t}. (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
go forall a. Graph a -> Graph a -> Graph a
Connect)
where
go :: (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
go t -> t -> t
_ Maybe t
Nothing Maybe t
y = Maybe t
y
go t -> t -> t
_ Maybe t
x Maybe t
Nothing = Maybe t
x
go t -> t -> t
f (Just t
x) (Just t
y) = forall a. a -> Maybe a
Just (t -> t -> t
f t
x t
y)
vertex :: a -> Graph a
vertex :: forall a. a -> Graph a
vertex = forall a. a -> Graph a
Vertex
{-# INLINE vertex #-}
edge :: a -> a -> Graph a
edge :: forall a. a -> a -> Graph a
edge a
u a
v = forall a. Graph a -> Graph a -> Graph a
connect (forall a. a -> Graph a
vertex a
u) (forall a. a -> Graph a
vertex a
v)
overlay :: Graph a -> Graph a -> Graph a
overlay :: forall a. Graph a -> Graph a -> Graph a
overlay = forall a. Graph a -> Graph a -> Graph a
Overlay
{-# INLINE overlay #-}
overlay1 :: G.Graph a -> Graph a -> Graph a
overlay1 :: forall a. Graph a -> Graph a -> Graph a
overlay1 = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id forall a. Graph a -> Graph a -> Graph a
overlay forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Graph a -> Maybe (Graph a)
toNonEmpty
connect :: Graph a -> Graph a -> Graph a
connect :: forall a. Graph a -> Graph a -> Graph a
connect = forall a. Graph a -> Graph a -> Graph a
Connect
{-# INLINE connect #-}
vertices1 :: NonEmpty a -> Graph a
vertices1 :: forall a. NonEmpty a -> Graph a
vertices1 = forall a. NonEmpty (Graph a) -> Graph a
overlays1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Graph a
vertex
{-# NOINLINE [1] vertices1 #-}
edges1 :: NonEmpty (a, a) -> Graph a
edges1 :: forall a. NonEmpty (a, a) -> Graph a
edges1 = forall a. NonEmpty (Graph a) -> Graph a
overlays1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. a -> a -> Graph a
edge)
overlays1 :: NonEmpty (Graph a) -> Graph a
overlays1 :: forall a. NonEmpty (Graph a) -> Graph a
overlays1 = forall a.
(Graph a -> Graph a -> Graph a) -> NonEmpty (Graph a) -> Graph a
concatg1 forall a. Graph a -> Graph a -> Graph a
overlay
{-# INLINE [2] overlays1 #-}
connects1 :: NonEmpty (Graph a) -> Graph a
connects1 :: forall a. NonEmpty (Graph a) -> Graph a
connects1 = forall a.
(Graph a -> Graph a -> Graph a) -> NonEmpty (Graph a) -> Graph a
concatg1 forall a. Graph a -> Graph a -> Graph a
connect
{-# INLINE [2] connects1 #-}
concatg1 :: (Graph a -> Graph a -> Graph a) -> NonEmpty (Graph a) -> Graph a
concatg1 :: forall a.
(Graph a -> Graph a -> Graph a) -> NonEmpty (Graph a) -> Graph a
concatg1 Graph a -> Graph a -> Graph a
combine (Graph a
x :| [Graph a]
xs) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Graph a
x (Graph a -> Graph a -> Graph a
combine Graph a
x) forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe Graph a -> Graph a -> Graph a
combine [Graph a]
xs
foldg1 :: (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 :: forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 a -> b
v b -> b -> b
o b -> b -> b
c = Graph a -> b
go
where
go :: Graph a -> b
go (Vertex a
x ) = a -> b
v a
x
go (Overlay Graph a
x Graph a
y) = b -> b -> b
o (Graph a -> b
go Graph a
x) (Graph a -> b
go Graph a
y)
go (Connect Graph a
x Graph a
y) = b -> b -> b
c (Graph a -> b
go Graph a
x) (Graph a -> b
go Graph a
y)
isSubgraphOf :: Ord a => Graph a -> Graph a -> Bool
isSubgraphOf :: forall a. Ord a => Graph a -> Graph a -> Bool
isSubgraphOf Graph a
x Graph a
y = forall a. Ord a => AdjacencyMap a -> AdjacencyMap a -> Bool
AM.isSubgraphOf (forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMap Graph a
x) (forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMap Graph a
y)
{-# NOINLINE [1] isSubgraphOf #-}
{-# RULES "isSubgraphOf/Int" isSubgraphOf = isSubgraphOfIntR #-}
isSubgraphOfIntR :: Graph Int -> Graph Int -> Bool
isSubgraphOfIntR :: Graph Int -> Graph Int -> Bool
isSubgraphOfIntR Graph Int
x Graph Int
y = AdjacencyIntMap -> AdjacencyIntMap -> Bool
AIM.isSubgraphOf (forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap Graph Int
x) (forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap Graph Int
y)
(===) :: Eq a => Graph a -> Graph a -> Bool
(Vertex a
x1 ) === :: forall a. Eq a => Graph a -> Graph a -> Bool
=== (Vertex a
x2 ) = a
x1 forall a. Eq a => a -> a -> Bool
== a
x2
(Overlay Graph a
x1 Graph a
y1) === (Overlay Graph a
x2 Graph a
y2) = Graph a
x1 forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
x2 Bool -> Bool -> Bool
&& Graph a
y1 forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
y2
(Connect Graph a
x1 Graph a
y1) === (Connect Graph a
x2 Graph a
y2) = Graph a
x1 forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
x2 Bool -> Bool -> Bool
&& Graph a
y1 forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
y2
Graph a
_ === Graph a
_ = Bool
False
{-# SPECIALISE (===) :: Graph Int -> Graph Int -> Bool #-}
infix 4 ===
size :: Graph a -> Int
size :: forall a. Graph a -> Int
size = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 (forall a b. a -> b -> a
const Int
1) forall a. Num a => a -> a -> a
(+) forall a. Num a => a -> a -> a
(+)
hasVertex :: Eq a => a -> Graph a -> Bool
hasVertex :: forall a. Eq a => a -> Graph a -> Bool
hasVertex a
v = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 (forall a. Eq a => a -> a -> Bool
==a
v) Bool -> Bool -> Bool
(||) Bool -> Bool -> Bool
(||)
{-# SPECIALISE hasVertex :: Int -> Graph Int -> Bool #-}
hasEdge :: Eq a => a -> a -> Graph a -> Bool
hasEdge :: forall a. Eq a => a -> a -> Graph a -> Bool
hasEdge a
s a
t Graph a
g = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 a -> Int -> Int
v forall {a} {t}. (Eq a, Num a) => (t -> a) -> (t -> Int) -> t -> Int
o forall {t} {a} {t}.
(Eq t, Num t, Num a) =>
(t -> t) -> (t -> a) -> t -> a
c Graph a
g Int
0 forall a. Eq a => a -> a -> Bool
== Int
2
where
v :: a -> Int -> Int
v a
x Int
0 = if a
x forall a. Eq a => a -> a -> Bool
== a
s then Int
1 else Int
0
v a
x Int
_ = if a
x forall a. Eq a => a -> a -> Bool
== a
t then Int
2 else Int
1
o :: (t -> a) -> (t -> Int) -> t -> Int
o t -> a
x t -> Int
y t
a = case t -> a
x t
a of
a
0 -> t -> Int
y t
a
a
1 -> if t -> Int
y t
a forall a. Eq a => a -> a -> Bool
== Int
2 then Int
2 else Int
1
a
_ -> Int
2 :: Int
c :: (t -> t) -> (t -> a) -> t -> a
c t -> t
x t -> a
y t
a = case t -> t
x t
a of { t
2 -> a
2; t
res -> t -> a
y t
res }
{-# SPECIALISE hasEdge :: Int -> Int -> Graph Int -> Bool #-}
vertexCount :: Ord a => Graph a -> Int
vertexCount :: forall a. Ord a => Graph a -> Int
vertexCount = forall t. (ToGraph t, Ord (ToVertex t)) => t -> Int
T.vertexCount
{-# RULES "vertexCount/Int" vertexCount = vertexIntCount #-}
{-# INLINE [1] vertexCount #-}
vertexIntCount :: Graph Int -> Int
vertexIntCount :: Graph Int -> Int
vertexIntCount = IntSet -> Int
IntSet.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> IntSet
vertexIntSet
edgeCount :: Ord a => Graph a -> Int
edgeCount :: forall a. Ord a => Graph a -> Int
edgeCount = forall t. (ToGraph t, Ord (ToVertex t)) => t -> Int
T.edgeCount
{-# INLINE [1] edgeCount #-}
{-# RULES "edgeCount/Int" edgeCount = edgeCountInt #-}
edgeCountInt :: Graph Int -> Int
edgeCountInt :: Graph Int -> Int
edgeCountInt = forall t. (ToGraph t, Ord (ToVertex t)) => t -> Int
T.edgeCount forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap
vertexList1 :: Ord a => Graph a -> NonEmpty a
vertexList1 :: forall a. Ord a => Graph a -> NonEmpty a
vertexList1 = forall a. [a] -> NonEmpty a
NonEmpty.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Set a -> [a]
Set.toAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => Graph a -> Set a
vertexSet
{-# RULES "vertexList1/Int" vertexList1 = vertexIntList1 #-}
{-# INLINE [1] vertexList1 #-}
vertexIntList1 :: Graph Int -> NonEmpty Int
vertexIntList1 :: Graph Int -> NonEmpty Int
vertexIntList1 = forall a. [a] -> NonEmpty a
NonEmpty.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntSet -> [Int]
IntSet.toAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> IntSet
vertexIntSet
edgeList :: Ord a => Graph a -> [(a, a)]
edgeList :: forall a. Ord a => Graph a -> [(a, a)]
edgeList = forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> [(ToVertex t, ToVertex t)]
T.edgeList
{-# RULES "edgeList/Int" edgeList = edgeIntList #-}
{-# INLINE [1] edgeList #-}
edgeIntList :: Graph Int -> [(Int, Int)]
edgeIntList :: Graph Int -> [Edge]
edgeIntList = forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> [(ToVertex t, ToVertex t)]
T.edgeList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap
vertexSet :: Ord a => Graph a -> Set.Set a
vertexSet :: forall a. Ord a => Graph a -> Set a
vertexSet = forall t. (ToGraph t, Ord (ToVertex t)) => t -> Set (ToVertex t)
T.vertexSet
vertexIntSet :: Graph Int -> IntSet.IntSet
vertexIntSet :: Graph Int -> IntSet
vertexIntSet = forall t. (ToGraph t, ToVertex t ~ Int) => t -> IntSet
T.vertexIntSet
edgeSet :: Ord a => Graph a -> Set.Set (a, a)
edgeSet :: forall a. Ord a => Graph a -> Set (a, a)
edgeSet = forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> Set (ToVertex t, ToVertex t)
T.edgeSet
path1 :: NonEmpty a -> Graph a
path1 :: forall a. NonEmpty a -> Graph a
path1 (a
x :| [] ) = forall a. a -> Graph a
vertex a
x
path1 (a
x :| (a
y:[a]
ys)) = forall a. NonEmpty (a, a) -> Graph a
edges1 ((a
x, a
y) forall a. a -> [a] -> NonEmpty a
:| forall a b. [a] -> [b] -> [(a, b)]
zip (a
yforall a. a -> [a] -> [a]
:[a]
ys) [a]
ys)
circuit1 :: NonEmpty a -> Graph a
circuit1 :: forall a. NonEmpty a -> Graph a
circuit1 (a
x :| [a]
xs) = forall a. NonEmpty a -> Graph a
path1 (a
x forall a. a -> [a] -> NonEmpty a
:| [a]
xs forall a. [a] -> [a] -> [a]
++ [a
x])
clique1 :: NonEmpty a -> Graph a
clique1 :: forall a. NonEmpty a -> Graph a
clique1 = forall a. NonEmpty (Graph a) -> Graph a
connects1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Graph a
vertex
{-# NOINLINE [1] clique1 #-}
biclique1 :: NonEmpty a -> NonEmpty a -> Graph a
biclique1 :: forall a. NonEmpty a -> NonEmpty a -> Graph a
biclique1 NonEmpty a
xs NonEmpty a
ys = forall a. Graph a -> Graph a -> Graph a
connect (forall a. NonEmpty a -> Graph a
vertices1 NonEmpty a
xs) (forall a. NonEmpty a -> Graph a
vertices1 NonEmpty a
ys)
star :: a -> [a] -> Graph a
star :: forall a. a -> [a] -> Graph a
star a
x [] = forall a. a -> Graph a
vertex a
x
star a
x (a
y:[a]
ys) = forall a. Graph a -> Graph a -> Graph a
connect (forall a. a -> Graph a
vertex a
x) (forall a. NonEmpty a -> Graph a
vertices1 forall a b. (a -> b) -> a -> b
$ a
y forall a. a -> [a] -> NonEmpty a
:| [a]
ys)
{-# INLINE star #-}
stars1 :: NonEmpty (a, [a]) -> Graph a
stars1 :: forall a. NonEmpty (a, [a]) -> Graph a
stars1 = forall a. NonEmpty (Graph a) -> Graph a
overlays1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. a -> [a] -> Graph a
star)
{-# INLINE stars1 #-}
tree :: Tree.Tree a -> Graph a
tree :: forall a. Tree a -> Graph a
tree (Tree.Node a
x [Tree a]
f) = forall a. NonEmpty (Graph a) -> Graph a
overlays1 forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> Graph a
star a
x (forall a b. (a -> b) -> [a] -> [b]
map forall a. Tree a -> a
Tree.rootLabel [Tree a]
f) forall a. a -> [a] -> NonEmpty a
:| forall a b. (a -> b) -> [a] -> [b]
map forall a. Tree a -> Graph a
tree [Tree a]
f
mesh1 :: NonEmpty a -> NonEmpty b -> Graph (a, b)
mesh1 :: forall a b. NonEmpty a -> NonEmpty b -> Graph (a, b)
mesh1 (a
x :| []) NonEmpty b
ys = (a
x, ) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. NonEmpty a -> Graph a
path1 NonEmpty b
ys
mesh1 NonEmpty a
xs (b
y :| []) = (, b
y) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. NonEmpty a -> Graph a
path1 NonEmpty a
xs
mesh1 xs :: NonEmpty a
xs@(a
x1 :| a
x2 : [a]
xt) ys :: NonEmpty b
ys@(b
y1 :| b
y2 : [b]
yt) =
let star :: a -> a -> a -> Graph a
star a
i a
j a
o = (forall a. a -> Graph a
vertex a
i forall a. Graph a -> Graph a -> Graph a
`overlay` forall a. a -> Graph a
vertex a
j) forall a. Graph a -> Graph a -> Graph a
`connect` forall a. a -> Graph a
vertex a
o
innerStars :: Graph (a, b)
innerStars = forall a. NonEmpty (Graph a) -> Graph a
overlays1 forall a b. (a -> b) -> a -> b
$ do
(a
x1, a
x2) <- forall a b. NonEmpty a -> NonEmpty b -> NonEmpty (a, b)
NonEmpty.zip NonEmpty a
xs (a
x2 forall a. a -> [a] -> NonEmpty a
:| [a]
xt)
(b
y1, b
y2) <- forall a b. NonEmpty a -> NonEmpty b -> NonEmpty (a, b)
NonEmpty.zip NonEmpty b
ys (b
y2 forall a. a -> [a] -> NonEmpty a
:| [b]
yt)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall {a}. a -> a -> a -> Graph a
star (a
x1, b
y2) (a
x2, b
y1) (a
x2, b
y2)
in
((a
x1, ) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. NonEmpty a -> Graph a
path1 NonEmpty b
ys) forall a. Graph a -> Graph a -> Graph a
`overlay` ((, b
y1) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. NonEmpty a -> Graph a
path1 NonEmpty a
xs) forall a. Graph a -> Graph a -> Graph a
`overlay` Graph (a, b)
innerStars
torus1 :: NonEmpty a -> NonEmpty b -> Graph (a, b)
torus1 :: forall a b. NonEmpty a -> NonEmpty b -> Graph (a, b)
torus1 NonEmpty a
xs NonEmpty b
ys = forall a. NonEmpty (a, [a]) -> Graph a
stars1 forall a b. (a -> b) -> a -> b
$ do
(a
x1, a
x2) <- forall a. NonEmpty a -> NonEmpty (a, a)
pairs1 NonEmpty a
xs
(b
y1, b
y2) <- forall a. NonEmpty a -> NonEmpty (a, a)
pairs1 NonEmpty b
ys
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
x1, b
y1), [(a
x1, b
y2), (a
x2, b
y1)])
where
pairs1 :: NonEmpty a -> NonEmpty (a, a)
pairs1 :: forall a. NonEmpty a -> NonEmpty (a, a)
pairs1 as :: NonEmpty a
as@(a
x :| [a]
xs) = forall a b. NonEmpty a -> NonEmpty b -> NonEmpty (a, b)
NonEmpty.zip NonEmpty a
as forall a b. (a -> b) -> a -> b
$
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (a
x forall a. a -> [a] -> NonEmpty a
:| []) (forall a. NonEmpty a -> [a] -> NonEmpty a
`append1` [a
x]) (forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty [a]
xs)
append1 :: NonEmpty a -> [a] -> NonEmpty a
append1 :: forall a. NonEmpty a -> [a] -> NonEmpty a
append1 (a
x :| [a]
xs) [a]
ys = a
x forall a. a -> [a] -> NonEmpty a
:| ([a]
xs forall a. [a] -> [a] -> [a]
++ [a]
ys)
removeVertex1 :: Eq a => a -> Graph a -> Maybe (Graph a)
removeVertex1 :: forall a. Eq a => a -> Graph a -> Maybe (Graph a)
removeVertex1 a
x = forall a. (a -> Bool) -> Graph a -> Maybe (Graph a)
induce1 (forall a. Eq a => a -> a -> Bool
/= a
x)
{-# SPECIALISE removeVertex1 :: Int -> Graph Int -> Maybe (Graph Int) #-}
removeEdge :: Eq a => a -> a -> Graph a -> Graph a
removeEdge :: forall a. Eq a => a -> a -> Graph a -> Graph a
removeEdge a
s a
t = forall a.
Eq a =>
a -> (a -> Bool) -> (a -> Bool) -> Graph a -> Graph a
filterContext a
s (forall a. Eq a => a -> a -> Bool
/=a
s) (forall a. Eq a => a -> a -> Bool
/=a
t)
{-# SPECIALISE removeEdge :: Int -> Int -> Graph Int -> Graph Int #-}
filterContext :: Eq a => a -> (a -> Bool) -> (a -> Bool) -> Graph a -> Graph a
filterContext :: forall a.
Eq a =>
a -> (a -> Bool) -> (a -> Bool) -> Graph a -> Graph a
filterContext a
s a -> Bool
i a -> Bool
o Graph a
g = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Graph a
g Context a -> Graph a
go forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> Graph a -> Maybe (Context a)
G.context (forall a. Eq a => a -> a -> Bool
==a
s) (forall t. ToGraph t => t -> Graph (ToVertex t)
T.toGraph Graph a
g)
where
go :: Context a -> Graph a
go (G.Context [a]
is [a]
os) = forall a. (a -> Bool) -> Graph a -> Graph a
G.induce (forall a. Eq a => a -> a -> Bool
/=a
s) (forall t. ToGraph t => t -> Graph (ToVertex t)
T.toGraph Graph a
g) forall a. Graph a -> Graph a -> Graph a
`overlay1`
forall a. Graph a -> Graph a
transpose (forall a. a -> [a] -> Graph a
star a
s (forall a. (a -> Bool) -> [a] -> [a]
filter a -> Bool
i [a]
is)) forall a. Graph a -> Graph a -> Graph a
`overlay` forall a. a -> [a] -> Graph a
star a
s (forall a. (a -> Bool) -> [a] -> [a]
filter a -> Bool
o [a]
os)
{-# SPECIALISE filterContext :: Int -> (Int -> Bool) -> (Int -> Bool) -> Graph Int -> Graph Int #-}
replaceVertex :: Eq a => a -> a -> Graph a -> Graph a
replaceVertex :: forall a. Eq a => a -> a -> Graph a -> Graph a
replaceVertex a
u a
v = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a -> b) -> a -> b
$ \a
w -> if a
w forall a. Eq a => a -> a -> Bool
== a
u then a
v else a
w
{-# SPECIALISE replaceVertex :: Int -> Int -> Graph Int -> Graph Int #-}
mergeVertices :: (a -> Bool) -> a -> Graph a -> Graph a
mergeVertices :: forall a. (a -> Bool) -> a -> Graph a -> Graph a
mergeVertices a -> Bool
p a
v = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a -> b) -> a -> b
$ \a
w -> if a -> Bool
p a
w then a
v else a
w
splitVertex1 :: Eq a => a -> NonEmpty a -> Graph a -> Graph a
splitVertex1 :: forall a. Eq a => a -> NonEmpty a -> Graph a -> Graph a
splitVertex1 a
v NonEmpty a
us Graph a
g = Graph a
g forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
w -> if a
w forall a. Eq a => a -> a -> Bool
== a
v then forall a. NonEmpty a -> Graph a
vertices1 NonEmpty a
us else forall a. a -> Graph a
vertex a
w
{-# SPECIALISE splitVertex1 :: Int -> NonEmpty Int -> Graph Int -> Graph Int #-}
transpose :: Graph a -> Graph a
transpose :: forall a. Graph a -> Graph a
transpose = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 forall a. a -> Graph a
vertex forall a. Graph a -> Graph a -> Graph a
overlay (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Graph a -> Graph a -> Graph a
connect)
{-# NOINLINE [1] transpose #-}
{-# RULES
"transpose/Vertex" forall x. transpose (Vertex x) = Vertex x
"transpose/Overlay" forall g1 g2. transpose (Overlay g1 g2) = Overlay (transpose g1) (transpose g2)
"transpose/Connect" forall g1 g2. transpose (Connect g1 g2) = Connect (transpose g2) (transpose g1)
"transpose/overlays1" forall xs. transpose (overlays1 xs) = overlays1 (fmap transpose xs)
"transpose/connects1" forall xs. transpose (connects1 xs) = connects1 (NonEmpty.reverse (fmap transpose xs))
"transpose/vertices1" forall xs. transpose (vertices1 xs) = vertices1 xs
"transpose/clique1" forall xs. transpose (clique1 xs) = clique1 (NonEmpty.reverse xs)
#-}
induce1 :: (a -> Bool) -> Graph a -> Maybe (Graph a)
induce1 :: forall a. (a -> Bool) -> Graph a -> Maybe (Graph a)
induce1 a -> Bool
p = forall a. Graph (Maybe a) -> Maybe (Graph a)
induceJust1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
a -> if a -> Bool
p a
a then forall a. a -> Maybe a
Just a
a else forall a. Maybe a
Nothing)
induceJust1 :: Graph (Maybe a) -> Maybe (Graph a)
induceJust1 :: forall a. Graph (Maybe a) -> Maybe (Graph a)
induceJust1 = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Graph a
Vertex) (forall {t}. (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k forall a. Graph a -> Graph a -> Graph a
Overlay) (forall {t}. (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k forall a. Graph a -> Graph a -> Graph a
Connect)
where
k :: (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k t -> t -> t
_ Maybe t
Nothing Maybe t
a = Maybe t
a
k t -> t -> t
_ Maybe t
a Maybe t
Nothing = Maybe t
a
k t -> t -> t
f (Just t
a) (Just t
b) = forall a. a -> Maybe a
Just (t -> t -> t
f t
a t
b)
simplify :: Ord a => Graph a -> Graph a
simplify :: forall a. Ord a => Graph a -> Graph a
simplify = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 forall a. a -> Graph a
Vertex (forall g. Eq g => (g -> g -> g) -> g -> g -> g
simple forall a. Graph a -> Graph a -> Graph a
Overlay) (forall g. Eq g => (g -> g -> g) -> g -> g -> g
simple forall a. Graph a -> Graph a -> Graph a
Connect)
{-# SPECIALISE simplify :: Graph Int -> Graph Int #-}
simple :: Eq g => (g -> g -> g) -> g -> g -> g
simple :: forall g. Eq g => (g -> g -> g) -> g -> g -> g
simple g -> g -> g
op g
x g
y
| g
x forall a. Eq a => a -> a -> Bool
== g
z = g
x
| g
y forall a. Eq a => a -> a -> Bool
== g
z = g
y
| Bool
otherwise = g
z
where
z :: g
z = g -> g -> g
op g
x g
y
{-# SPECIALISE simple :: (Graph Int -> Graph Int -> Graph Int) -> Graph Int -> Graph Int -> Graph Int #-}
box :: Graph a -> Graph b -> Graph (a, b)
box :: forall a b. Graph a -> Graph b -> Graph (a, b)
box Graph a
x Graph b
y = forall a. Graph a -> Graph a -> Graph a
overlay (Graph (b -> (a, b))
fx forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Graph b
y) (Graph (a -> (a, b))
fy forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Graph a
x)
where
fx :: Graph (b -> (a, b))
fx = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 (forall a. a -> Graph a
vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,)) forall a. Graph a -> Graph a -> Graph a
overlay forall a. Graph a -> Graph a -> Graph a
overlay Graph a
x
fy :: Graph (a -> (a, b))
fy = forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 (forall a. a -> Graph a
vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip (,)) forall a. Graph a -> Graph a -> Graph a
overlay forall a. Graph a -> Graph a -> Graph a
overlay Graph b
y
sparsify :: Graph a -> Graph (Either Int a)
sparsify :: forall a. Graph a -> Graph (Either Int a)
sparsify Graph a
graph = Graph (Either Int a)
res
where
(Graph (Either Int a)
res, Int
end) = forall s a. State s a -> s -> (a, s)
runState (forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 forall {m :: * -> *} {b} {a}.
Monad m =>
b -> a -> a -> m (Graph (Either a b))
v forall {f :: * -> *} {t} {t} {a}.
Applicative f =>
(t -> t -> f (Graph a))
-> (t -> t -> f (Graph a)) -> t -> t -> f (Graph a)
o forall {m :: * -> *} {t} {t} {a} {t}.
(Monad m, Num t) =>
(t -> t -> StateT t m (Graph a))
-> (t -> t -> StateT t m (Graph a))
-> t
-> t
-> StateT t m (Graph a)
c Graph a
graph Int
0 Int
end) Int
1
v :: b -> a -> a -> m (Graph (Either a b))
v b
x a
s a
t = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. NonEmpty a -> Graph a
clique1 (forall a b. a -> Either a b
Left a
s forall a. a -> [a] -> NonEmpty a
:| [forall a b. b -> Either a b
Right b
x, forall a b. a -> Either a b
Left a
t])
o :: (t -> t -> f (Graph a))
-> (t -> t -> f (Graph a)) -> t -> t -> f (Graph a)
o t -> t -> f (Graph a)
x t -> t -> f (Graph a)
y t
s t
t = forall a. Graph a -> Graph a -> Graph a
overlay forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t
s t -> t -> f (Graph a)
`x` t
t forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t
s t -> t -> f (Graph a)
`y` t
t
c :: (t -> t -> StateT t m (Graph a))
-> (t -> t -> StateT t m (Graph a))
-> t
-> t
-> StateT t m (Graph a)
c t -> t -> StateT t m (Graph a)
x t -> t -> StateT t m (Graph a)
y t
s t
t = do
t
m <- forall (m :: * -> *) s. Monad m => StateT s m s
get
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put (t
m forall a. Num a => a -> a -> a
+ t
1)
forall a. Graph a -> Graph a -> Graph a
overlay forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t
s t -> t -> StateT t m (Graph a)
`x` t
m forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t
m t -> t -> StateT t m (Graph a)
`y` t
t
sparsifyKL :: Int -> Graph Int -> KL.Graph
sparsifyKL :: Int -> Graph Int -> Graph
sparsifyKL Int
n Graph Int
graph = Edge -> [Edge] -> Graph
KL.buildG (Int
1, Int
next forall a. Num a => a -> a -> a
- Int
1) ((Int
n forall a. Num a => a -> a -> a
+ Int
1, Int
n forall a. Num a => a -> a -> a
+ Int
2) forall a. a -> [a] -> [a]
: forall l. IsList l => l -> [Item l]
Exts.toList (List Edge
res :: List KL.Edge))
where
(List Edge
res, Int
next) = forall s a. State s a -> s -> (a, s)
runState (forall a b.
(a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg1 forall {a} {b} {m :: * -> *}.
(Item a ~ (b, b), Monad m, IsList a) =>
b -> b -> b -> m a
v forall {f :: * -> *} {b} {t} {t}.
(Applicative f, Semigroup b) =>
(t -> t -> f b) -> (t -> t -> f b) -> t -> t -> f b
o forall {b} {t} {m :: * -> *}.
(Item b ~ (t, t), Monad m, Num t, Semigroup b, IsList b) =>
(t -> t -> StateT t m b)
-> (t -> t -> StateT t m b) -> t -> t -> StateT t m b
c Graph Int
graph (Int
n forall a. Num a => a -> a -> a
+ Int
1) (Int
n forall a. Num a => a -> a -> a
+ Int
2)) (Int
n forall a. Num a => a -> a -> a
+ Int
3)
v :: b -> b -> b -> m a
v b
x b
s b
t = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall l. IsList l => [Item l] -> l
Exts.fromList [(b
s,b
x), (b
x,b
t)]
o :: (t -> t -> f b) -> (t -> t -> f b) -> t -> t -> f b
o t -> t -> f b
x t -> t -> f b
y t
s t
t = forall a. Semigroup a => a -> a -> a
(<>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t
s t -> t -> f b
`x` t
t forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t
s t -> t -> f b
`y` t
t
c :: (t -> t -> StateT t m b)
-> (t -> t -> StateT t m b) -> t -> t -> StateT t m b
c t -> t -> StateT t m b
x t -> t -> StateT t m b
y t
s t
t = do
t
m <- forall (m :: * -> *) s. Monad m => StateT s m s
get
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put (t
m forall a. Num a => a -> a -> a
+ t
1)
(\b
xs b
ys -> forall l. IsList l => [Item l] -> l
Exts.fromList [(t
s,t
m), (t
m,t
t)] forall a. Semigroup a => a -> a -> a
<> b
xs forall a. Semigroup a => a -> a -> a
<> b
ys) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t
s t -> t -> StateT t m b
`x` t
m forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t
m t -> t -> StateT t m b
`y` t
t