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