module Algebra.Graph (
Graph (..),
empty, vertex, edge, overlay, connect, vertices, edges, overlays, connects,
foldg, buildg,
isSubgraphOf, (===),
isEmpty, size, hasVertex, hasEdge, vertexCount, edgeCount, vertexList,
edgeList, vertexSet, edgeSet, adjacencyList,
path, circuit, clique, biclique, star, stars, tree, forest, mesh, torus,
deBruijn,
removeVertex, removeEdge, replaceVertex, mergeVertices, splitVertex,
transpose, induce, induceJust, simplify, sparsify, sparsifyKL,
compose, box,
Context (..), context
) where
import Control.Applicative (Alternative)
import Control.DeepSeq
import Control.Monad (MonadPlus (..))
import Control.Monad.Trans.State (runState, get, put)
import Data.Foldable (toList)
import Data.Maybe (fromMaybe)
import Data.String
import Data.Tree
import GHC.Generics
import Algebra.Graph.Internal
import qualified Control.Applicative
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.Set as Set
import qualified Data.Tree as Tree
import qualified GHC.Exts as Exts
data Graph a = Empty
| Vertex a
| Overlay (Graph a) (Graph a)
| Connect (Graph a) (Graph a)
deriving (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, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Graph a) x -> Graph a
forall a x. Graph a -> Rep (Graph a) x
$cto :: forall a x. Rep (Graph a) x -> Graph a
$cfrom :: forall a x. Graph a -> Rep (Graph a) x
Generic)
instance Functor Graph where
fmap :: forall a b. (a -> b) -> Graph a -> Graph b
fmap a -> b
f Graph a
g = Graph a
g forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall a. a -> Graph a
vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
{-# INLINE fmap #-}
instance NFData a => NFData (Graph a) where
rnf :: Graph a -> ()
rnf Graph a
Empty = ()
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 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 b. a -> b -> a
const forall a. Graph a
Empty
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
eqR
instance Ord a => Ord (Graph a) where
compare :: Graph a -> Graph a -> Ordering
compare = forall a. Ord a => Graph a -> Graph a -> Ordering
ordR
eqR :: Ord a => Graph a -> Graph a -> Bool
eqR :: forall a. Ord a => Graph a -> Graph a -> Bool
eqR Graph a
x Graph a
y = forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
x forall a. Eq a => a -> a -> Bool
== forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
y
{-# INLINE [2] eqR #-}
{-# RULES "eqR/Int" eqR = eqIntR #-}
eqIntR :: Graph Int -> Graph Int -> Bool
eqIntR :: Graph Int -> Graph Int -> Bool
eqIntR Graph Int
x Graph Int
y = Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
x forall a. Eq a => a -> a -> Bool
== Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
y
{-# INLINE eqIntR #-}
ordR :: Ord a => Graph a -> Graph a -> Ordering
ordR :: forall a. Ord a => Graph a -> Graph a -> Ordering
ordR Graph a
x Graph a
y = forall a. Ord a => a -> a -> Ordering
compare (forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
x) (forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
y)
{-# INLINE [2] ordR #-}
{-# RULES "ordR/Int" ordR = ordIntR #-}
ordIntR :: Graph Int -> Graph Int -> Ordering
ordIntR :: Graph Int -> Graph Int -> Ordering
ordIntR Graph Int
x Graph Int
y = forall a. Ord a => a -> a -> Ordering
compare (Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
x) (Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
y)
{-# INLINE ordIntR #-}
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 = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e b -> r
v r -> r -> r
o r -> r -> r
c -> forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e (\a -> b
w -> forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e (b -> r
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
w) r -> r -> r
o r -> r -> r
c Graph a
x) r -> r -> r
o r -> r -> r
c Graph (a -> b)
f
{-# INLINE (<*>) #-}
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.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e b -> r
v r -> r -> r
o r -> r -> r
c -> forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e (forall b c a. (b -> c) -> (a -> b) -> a -> c
composeR (forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e b -> r
v r -> r -> r
o r -> r -> r
c) a -> Graph b
f) r -> r -> r
o r -> r -> r
c Graph a
g
{-# INLINE (>>=) #-}
instance Alternative Graph where
empty :: forall a. Graph a
empty = forall a. Graph a
Empty
<|> :: forall a. Graph a -> Graph a -> Graph a
(<|>) = forall a. Graph a -> Graph a -> Graph a
Overlay
instance MonadPlus Graph where
mzero :: forall a. Graph a
mzero = forall a. Graph a
Empty
mplus :: forall a. Graph a -> Graph a -> Graph a
mplus = forall a. Graph a -> Graph a -> Graph a
Overlay
instance Semigroup (Graph a) where
<> :: Graph a -> Graph a -> Graph a
(<>) = forall a. Graph a -> Graph a -> Graph a
overlay
instance Monoid (Graph a) where
mempty :: Graph a
mempty = forall a. Graph a
empty
empty :: Graph a
empty :: forall a. Graph a
empty = forall a. Graph a
Empty
{-# INLINE empty #-}
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
x a
y = forall a. Graph a -> Graph a -> Graph a
connect (forall a. a -> Graph a
vertex a
x) (forall a. a -> Graph a
vertex a
y)
{-# INLINE edge #-}
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 #-}
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 #-}
vertices :: [a] -> Graph a
vertices :: forall a. [a] -> Graph a
vertices [a]
xs = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
_ -> forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
o a -> r
v [a]
xs
{-# INLINE vertices #-}
edges :: [(a, a)] -> Graph a
edges :: forall a. [(a, a)] -> Graph a
edges [(a, a)]
xs = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
o (\(a
x, a
y) -> r -> r -> r
c (a -> r
v a
x) (a -> r
v a
y)) [(a, a)]
xs
{-# INLINE edges #-}
overlays :: [Graph a] -> Graph a
overlays :: forall a. [Graph a] -> Graph a
overlays [Graph a]
xs = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
o (forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c) [Graph a]
xs
{-# INLINE overlays #-}
connects :: [Graph a] -> Graph a
connects :: forall a. [Graph a] -> Graph a
connects [Graph a]
xs = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
c (forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c) [Graph a]
xs
{-# INLINE connects #-}
combineR :: c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR :: forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR c
e c -> c -> c
o a -> c
f = forall a. a -> Maybe a -> a
fromMaybe c
e forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe c -> c -> c
o forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map a -> c
f
{-# INLINE combineR #-}
foldg :: b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg :: forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg b
e a -> b
v b -> b -> b
o b -> b -> b
c = Graph a -> b
go
where
go :: Graph a -> b
go Graph a
Empty = b
e
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)
{-# INLINE [0] foldg #-}
{-# RULES
"foldg/Empty" forall e v o c.
foldg e v o c Empty = e
"foldg/Vertex" forall e v o c x.
foldg e v o c (Vertex x) = v x
"foldg/Overlay" forall e v o c x y.
foldg e v o c (Overlay x y) = o (foldg e v o c x) (foldg e v o c y)
"foldg/Connect" forall e v o c x y.
foldg e v o c (Connect x y) = c (foldg e v o c x) (foldg e v o c y)
#-}
buildg :: (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r) -> Graph a
buildg :: forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r
f = forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r
f forall a. Graph a
Empty forall a. a -> Graph a
Vertex forall a. Graph a -> Graph a -> Graph a
Overlay forall a. Graph a -> Graph a -> Graph a
Connect
{-# INLINE [1] buildg #-}
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 a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
x) (forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
y)
{-# INLINE [2] 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
toAdjacencyIntMap Graph Int
x) (Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
y)
{-# INLINE isSubgraphOfIntR #-}
(===) :: Eq a => Graph a -> Graph a -> Bool
Graph a
Empty === :: forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
Empty = Bool
True
(Vertex a
x1 ) === (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 ===
isEmpty :: Graph a -> Bool
isEmpty :: forall a. Graph a -> Bool
isEmpty = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Bool
True (forall a b. a -> b -> a
const Bool
False) Bool -> Bool -> Bool
(&&) Bool -> Bool -> Bool
(&&)
{-# INLINE isEmpty #-}
size :: Graph a -> Int
size :: forall a. Graph a -> Int
size = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Int
1 (forall a b. a -> b -> a
const Int
1) forall a. Num a => a -> a -> a
(+) forall a. Num a => a -> a -> a
(+)
{-# INLINE size #-}
hasVertex :: Eq a => a -> Graph a -> Bool
hasVertex :: forall a. Eq a => a -> Graph a -> Bool
hasVertex a
x = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Bool
False (forall a. Eq a => a -> a -> Bool
==a
x) Bool -> Bool -> Bool
(||) Bool -> Bool -> Bool
(||)
{-# INLINE hasVertex #-}
{-# 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 b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall a. a -> a
id 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 }
{-# INLINE hasEdge #-}
{-# SPECIALISE hasEdge :: Int -> Int -> Graph Int -> Bool #-}
vertexCount :: Ord a => Graph a -> Int
vertexCount :: forall a. Ord a => Graph a -> Int
vertexCount = forall a. Set a -> Int
Set.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => Graph a -> Set a
vertexSet
{-# INLINE [2] vertexCount #-}
{-# RULES "vertexCount/Int" vertexCount = vertexIntCountR #-}
vertexIntCountR :: Graph Int -> Int
vertexIntCountR :: Graph Int -> Int
vertexIntCountR = IntSet -> Int
IntSet.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> IntSet
vertexIntSetR
{-# INLINE vertexIntCountR #-}
edgeCount :: Ord a => Graph a -> Int
edgeCount :: forall a. Ord a => Graph a -> Int
edgeCount = forall a. AdjacencyMap a -> Int
AM.edgeCount forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap
{-# INLINE [2] edgeCount #-}
{-# RULES "edgeCount/Int" edgeCount = edgeCountIntR #-}
edgeCountIntR :: Graph Int -> Int
edgeCountIntR :: Graph Int -> Int
edgeCountIntR = AdjacencyIntMap -> Int
AIM.edgeCount forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> AdjacencyIntMap
toAdjacencyIntMap
{-# INLINE edgeCountIntR #-}
vertexList :: Ord a => Graph a -> [a]
vertexList :: forall a. Ord a => Graph a -> [a]
vertexList = 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
{-# INLINE [2] vertexList #-}
{-# RULES "vertexList/Int" vertexList = vertexIntListR #-}
vertexIntListR :: Graph Int -> [Int]
vertexIntListR :: Graph Int -> [Int]
vertexIntListR = IntSet -> [Int]
IntSet.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> IntSet
vertexIntSetR
{-# INLINE vertexIntListR #-}
edgeList :: Ord a => Graph a -> [(a, a)]
edgeList :: forall a. Ord a => Graph a -> [(a, a)]
edgeList = forall a. AdjacencyMap a -> [(a, a)]
AM.edgeList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap
{-# INLINE [2] edgeList #-}
{-# RULES "edgeList/Int" edgeList = edgeIntListR #-}
edgeIntListR :: Graph Int -> [(Int, Int)]
edgeIntListR :: Graph Int -> [Edge]
edgeIntListR = AdjacencyIntMap -> [Edge]
AIM.edgeList forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> AdjacencyIntMap
toAdjacencyIntMap
{-# INLINE edgeIntListR #-}
vertexSet :: Ord a => Graph a -> Set.Set a
vertexSet :: forall a. Ord a => Graph a -> Set a
vertexSet = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall a. Set a
Set.empty forall a. a -> Set a
Set.singleton forall a. Ord a => Set a -> Set a -> Set a
Set.union forall a. Ord a => Set a -> Set a -> Set a
Set.union
{-# INLINE vertexSet #-}
vertexIntSetR :: Graph Int -> IntSet.IntSet
vertexIntSetR :: Graph Int -> IntSet
vertexIntSetR = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg IntSet
IntSet.empty Int -> IntSet
IntSet.singleton IntSet -> IntSet -> IntSet
IntSet.union IntSet -> IntSet -> IntSet
IntSet.union
{-# INLINE vertexIntSetR #-}
edgeSet :: Ord a => Graph a -> Set.Set (a, a)
edgeSet :: forall a. Ord a => Graph a -> Set (a, a)
edgeSet = forall a. Eq a => AdjacencyMap a -> Set (a, a)
AM.edgeSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap
{-# INLINE [2] edgeSet #-}
{-# RULES "edgeSet/Int" edgeSet = edgeIntSetR #-}
edgeIntSetR :: Graph Int -> Set.Set (Int,Int)
edgeIntSetR :: Graph Int -> Set Edge
edgeIntSetR = AdjacencyIntMap -> Set Edge
AIM.edgeSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> AdjacencyIntMap
toAdjacencyIntMap
{-# INLINE edgeIntSetR #-}
adjacencyList :: Ord a => Graph a -> [(a, [a])]
adjacencyList :: forall a. Ord a => Graph a -> [(a, [a])]
adjacencyList = forall a. AdjacencyMap a -> [(a, [a])]
AM.adjacencyList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap
{-# INLINE adjacencyList #-}
{-# SPECIALISE adjacencyList :: Graph Int -> [(Int, [Int])] #-}
toAdjacencyMap :: Ord a => Graph a -> AM.AdjacencyMap a
toAdjacencyMap :: forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall a. AdjacencyMap a
AM.empty forall a. a -> AdjacencyMap a
AM.vertex forall a.
Ord a =>
AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a
AM.overlay forall a.
Ord a =>
AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a
AM.connect
{-# INLINE toAdjacencyMap #-}
toAdjacencyIntMap :: Graph Int -> AIM.AdjacencyIntMap
toAdjacencyIntMap :: Graph Int -> AdjacencyIntMap
toAdjacencyIntMap = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg AdjacencyIntMap
AIM.empty Int -> AdjacencyIntMap
AIM.vertex AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap
AIM.overlay AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap
AIM.connect
{-# INLINE toAdjacencyIntMap #-}
path :: [a] -> Graph a
path :: forall a. [a] -> Graph a
path [a]
xs = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> case [a]
xs of
[] -> r
e
[a
x] -> a -> r
v a
x
(a
_ : [a]
ys) -> forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c forall a b. (a -> b) -> a -> b
$ forall a. [(a, a)] -> Graph a
edges (forall a b. [a] -> [b] -> [(a, b)]
zip [a]
xs [a]
ys)
{-# INLINE path #-}
circuit :: [a] -> Graph a
circuit :: forall a. [a] -> Graph a
circuit [a]
xs = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> case [a]
xs of
[] -> r
e
(a
x : [a]
xs) -> forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Graph a
path forall a b. (a -> b) -> a -> b
$ [a
x] forall a. [a] -> [a] -> [a]
++ [a]
xs forall a. [a] -> [a] -> [a]
++ [a
x]
{-# INLINE circuit #-}
clique :: [a] -> Graph a
clique :: forall a. [a] -> Graph a
clique [a]
xs = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
_ r -> r -> r
c -> forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
c a -> r
v [a]
xs
{-# INLINE clique #-}
biclique :: [a] -> [a] -> Graph a
biclique :: forall a. [a] -> [a] -> Graph a
biclique [a]
xs [a]
ys = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> case forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe r -> r -> r
o (forall a b. (a -> b) -> [a] -> [b]
map a -> r
v [a]
xs) of
Maybe r
Nothing -> forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Graph a
vertices [a]
ys
Just r
xs -> case forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe r -> r -> r
o (forall a b. (a -> b) -> [a] -> [b]
map a -> r
v [a]
ys) of
Maybe r
Nothing -> r
xs
Just r
ys -> r -> r -> r
c r
xs r
ys
{-# INLINE biclique #-}
star :: a -> [a] -> Graph a
star :: forall a. a -> [a] -> Graph a
star a
x [a]
ys = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
_ a -> r
v r -> r -> r
o r -> r -> r
c -> case forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe r -> r -> r
o (forall a b. (a -> b) -> [a] -> [b]
map a -> r
v [a]
ys) of
Maybe r
Nothing -> a -> r
v a
x
Just r
ys -> r -> r -> r
c (a -> r
v a
x) r
ys
{-# INLINE star #-}
stars :: [(a, [a])] -> Graph a
stars :: forall a. [(a, [a])] -> Graph a
stars [(a, [a])]
xs = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
o (forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. a -> [a] -> Graph a
star) [(a, [a])]
xs
{-# INLINE stars #-}
tree :: Tree.Tree a -> Graph a
tree :: forall a. Tree a -> Graph a
tree (Node a
x []) = forall a. a -> Graph a
vertex a
x
tree (Node a
x [Tree a]
f ) = forall a. a -> [a] -> Graph a
star a
x (forall a b. (a -> b) -> [a] -> [b]
map forall a. Tree a -> a
rootLabel [Tree a]
f)
forall a. Graph a -> Graph a -> Graph a
`overlay` forall a. Forest a -> Graph a
forest (forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Tree a -> [Tree a]
subForest) [Tree a]
f)
forest :: Tree.Forest a -> Graph a
forest :: forall a. Forest a -> Graph a
forest = forall a. [Graph a] -> Graph a
overlays forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a. Tree a -> Graph a
tree
mesh :: [a] -> [b] -> Graph (a, b)
mesh :: forall a b. [a] -> [b] -> Graph (a, b)
mesh [] [b]
_ = forall a. Graph a
empty
mesh [a]
_ [] = forall a. Graph a
empty
mesh [a
x] [b
y] = forall a. a -> Graph a
vertex (a
x, b
y)
mesh [a]
xs [b]
ys = forall a. [(a, [a])] -> Graph a
stars forall a b. (a -> b) -> a -> b
$
[ ((a
a1, b
b1), [(a
a1, b
b2), (a
a2, b
b1)]) | (a
a1, a
a2) <- [(a, a)]
ix, (b
b1, b
b2) <- [(b, b)]
iy ]
forall a. [a] -> [a] -> [a]
++ [ ((a
lx, b
y1), [(a
lx, b
y2)]) | (b
y1, b
y2) <- [(b, b)]
iy ]
forall a. [a] -> [a] -> [a]
++ [ ((a
x1, b
ly), [(a
x2, b
ly)]) | (a
x1, a
x2) <- [(a, a)]
ix ]
where
lx :: a
lx = forall a. [a] -> a
last [a]
xs
ly :: b
ly = forall a. [a] -> a
last [b]
ys
ix :: [(a, a)]
ix = forall a. [a] -> [a]
init (forall a. [a] -> [(a, a)]
pairs [a]
xs)
iy :: [(b, b)]
iy = forall a. [a] -> [a]
init (forall a. [a] -> [(a, a)]
pairs [b]
ys)
torus :: [a] -> [b] -> Graph (a, b)
torus :: forall a b. [a] -> [b] -> Graph (a, b)
torus [a]
xs [b]
ys = forall a. [(a, [a])] -> Graph a
stars
[ ((a
a1, b
b1), [(a
a1, b
b2), (a
a2, b
b1)]) | (a
a1, a
a2) <- forall a. [a] -> [(a, a)]
pairs [a]
xs, (b
b1, b
b2) <- forall a. [a] -> [(a, a)]
pairs [b]
ys ]
pairs :: [a] -> [(a, a)]
pairs :: forall a. [a] -> [(a, a)]
pairs [] = []
pairs as :: [a]
as@(a
x:[a]
xs) = forall a b. [a] -> [b] -> [(a, b)]
zip [a]
as ([a]
xs forall a. [a] -> [a] -> [a]
++ [a
x])
deBruijn :: Int -> [a] -> Graph [a]
deBruijn :: forall a. Int -> [a] -> Graph [a]
deBruijn Int
0 [a]
_ = forall a. a -> a -> Graph a
edge [] []
deBruijn Int
len [a]
alphabet = Graph (Either [a] [a])
skeleton forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either [a] [a] -> Graph [a]
expand
where
overlaps :: [[a]]
overlaps = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall a b. a -> b -> a
const [a]
alphabet) [Int
2..Int
len]
skeleton :: Graph (Either [a] [a])
skeleton = forall a. [(a, a)] -> Graph a
edges [ (forall a b. a -> Either a b
Left [a]
s, forall a b. b -> Either a b
Right [a]
s) | [a]
s <- [[a]]
overlaps ]
expand :: Either [a] [a] -> Graph [a]
expand Either [a] [a]
v = forall a. [a] -> Graph a
vertices [ forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([a
a] forall a. [a] -> [a] -> [a]
++) (forall a. [a] -> [a] -> [a]
++ [a
a]) Either [a] [a]
v | a
a <- [a]
alphabet ]
removeVertex :: Eq a => a -> Graph a -> Graph a
removeVertex :: forall a. Eq a => a -> Graph a -> Graph a
removeVertex a
v = forall a. (a -> Bool) -> Graph a -> Graph a
induce (forall a. Eq a => a -> a -> Bool
/= a
v)
{-# SPECIALISE removeVertex :: Int -> Graph Int -> 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)
context (forall a. Eq a => a -> a -> Bool
==a
s) Graph a
g
where
go :: Context a -> Graph a
go (Context [a]
is [a]
os) = forall a. (a -> Bool) -> Graph a -> Graph a
induce (forall a. Eq a => a -> a -> Bool
/=a
s) Graph a
g forall a. Graph a -> Graph a -> Graph a
`overlay` 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
{-# INLINE replaceVertex #-}
{-# 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
{-# INLINE mergeVertices #-}
splitVertex :: Eq a => a -> [a] -> Graph a -> Graph a
splitVertex :: forall a. Eq a => a -> [a] -> Graph a -> Graph a
splitVertex a
x [a]
us Graph a
g = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c ->
let split :: a -> r
split a
y = if a
x forall a. Eq a => a -> a -> Bool
== a
y then forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c (forall a. [a] -> Graph a
vertices [a]
us) else a -> r
v a
y in
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
split r -> r -> r
o r -> r -> r
c Graph a
g
{-# INLINE splitVertex #-}
{-# SPECIALISE splitVertex :: Int -> [Int] -> Graph Int -> Graph Int #-}
transpose :: Graph a -> Graph a
transpose :: forall a. Graph a -> Graph a
transpose Graph a
g = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o (forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> r -> r
c) Graph a
g
{-# INLINE transpose #-}
induce :: (a -> Bool) -> Graph a -> Graph a
induce :: forall a. (a -> Bool) -> Graph a -> Graph a
induce a -> Bool
p = forall a. Graph (Maybe a) -> Graph a
induceJust 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)
{-# INLINE induce #-}
induceJust :: Graph (Maybe a) -> Graph a
induceJust :: forall a. Graph (Maybe a) -> Graph a
induceJust Graph (Maybe a)
g = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> forall a. a -> Maybe a -> a
fromMaybe r
e forall a b. (a -> b) -> a -> b
$
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall a. Maybe a
Nothing (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> r
v) (forall {t}. (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k r -> r -> r
o) (forall {t}. (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k r -> r -> r
c) Graph (Maybe a)
g
where
k :: (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k t -> t -> t
_ Maybe t
x Maybe t
Nothing = Maybe t
x
k t -> t -> t
_ Maybe t
Nothing Maybe t
y = Maybe t
y
k t -> t -> t
f (Just t
x) (Just t
y) = forall a. a -> Maybe a
Just (t -> t -> t
f t
x t
y)
{-# INLINE induceJust #-}
simplify :: Ord a => Graph a -> Graph a
simplify :: forall a. Ord a => Graph a -> Graph a
simplify = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall a. Graph a
Empty 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)
{-# INLINE simplify #-}
{-# 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 :: (Int -> Int -> Int) -> Int -> Int -> Int #-}
compose :: Ord a => Graph a -> Graph a -> Graph a
compose :: forall a. Ord a => Graph a -> Graph a -> Graph a
compose Graph a
x Graph a
y = forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> forall a. a -> Maybe a -> a
fromMaybe r
e forall a b. (a -> b) -> a -> b
$
forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe r -> r -> r
o
[ forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c (forall a. [a] -> [a] -> Graph a
biclique [a]
xs [a]
ys)
| a
ve <- forall a. Set a -> [a]
Set.toList (forall a. AdjacencyMap a -> Set a
AM.vertexSet AdjacencyMap a
mx forall a. Ord a => Set a -> Set a -> Set a
`Set.union` forall a. AdjacencyMap a -> Set a
AM.vertexSet AdjacencyMap a
my)
, let xs :: [a]
xs = forall a. Set a -> [a]
Set.toList (forall a. Ord a => a -> AdjacencyMap a -> Set a
AM.postSet a
ve AdjacencyMap a
mx), Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs)
, let ys :: [a]
ys = forall a. Set a -> [a]
Set.toList (forall a. Ord a => a -> AdjacencyMap a -> Set a
AM.postSet a
ve AdjacencyMap a
my), Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
ys) ]
where
mx :: AdjacencyMap a
mx = forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap (forall a. Graph a -> Graph a
transpose Graph a
x)
my :: AdjacencyMap a
my = forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
y
{-# INLINE compose #-}
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 b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall a. Graph a
empty (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 b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall a. Graph a
empty (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 b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall {m :: * -> *} {a} {b}.
Monad m =>
a -> a -> m (Graph (Either a b))
e 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
e :: a -> a -> m (Graph (Either a b))
e a
s a
t = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Graph a
path [forall a b. a -> Either a b
Left a
s, forall a b. a -> Either a b
Left a
t]
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. [a] -> Graph a
clique [forall a b. a -> Either a b
Left a
s, 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 b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall {m :: * -> *} {a} {p} {p}.
(Monad m, IsList a) =>
p -> p -> m a
e 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)
e :: p -> p -> m a
e p
_ p
_ = 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 []
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
composeR :: (b -> c) -> (a -> b) -> a -> c
composeR :: forall b c a. (b -> c) -> (a -> b) -> a -> c
composeR = forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)
{-# INLINE [1] composeR #-}
{-# RULES
-- Fuse a 'foldg' followed by a 'buildg':
"foldg/buildg" forall e v o c (g :: forall b. b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> b).
foldg e v o c (buildg g) = g e v o c
-- Fuse 'composeR' chains (see the definition of the bind operator).
"composeR/composeR" forall c f g.
composeR (composeR c f) g = composeR c (f . g)
-- Rewrite identity (which can appear in the inlining of 'buildg') to a more
-- efficient one.
"foldg/id"
foldg Empty Vertex Overlay Connect = id
#-}
focus :: (a -> Bool) -> Graph a -> Focus a
focus :: forall a. (a -> Bool) -> Graph a -> Focus a
focus a -> Bool
f = forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg forall a. Focus a
emptyFocus (forall a. (a -> Bool) -> a -> Focus a
vertexFocus a -> Bool
f) forall a. Focus a -> Focus a -> Focus a
overlayFoci forall a. Focus a -> Focus a -> Focus a
connectFoci
{-# INLINE focus #-}
data Context a = Context { forall a. Context a -> [a]
inputs :: [a], forall a. Context a -> [a]
outputs :: [a] }
deriving (Context a -> Context a -> Bool
forall a. Eq a => Context a -> Context a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Context a -> Context a -> Bool
$c/= :: forall a. Eq a => Context a -> Context a -> Bool
== :: Context a -> Context a -> Bool
$c== :: forall a. Eq a => Context a -> Context a -> Bool
Eq, Int -> Context a -> ShowS
forall a. Show a => Int -> Context a -> ShowS
forall a. Show a => [Context a] -> ShowS
forall a. Show a => Context a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Context a] -> ShowS
$cshowList :: forall a. Show a => [Context a] -> ShowS
show :: Context a -> String
$cshow :: forall a. Show a => Context a -> String
showsPrec :: Int -> Context a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Context a -> ShowS
Show)
context :: (a -> Bool) -> Graph a -> Maybe (Context a)
context :: forall a. (a -> Bool) -> Graph a -> Maybe (Context a)
context a -> Bool
p Graph a
g | forall a. Focus a -> Bool
ok Focus a
f = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a] -> Context a
Context (forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall a b. (a -> b) -> a -> b
$ forall a. Focus a -> List a
is Focus a
f) (forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall a b. (a -> b) -> a -> b
$ forall a. Focus a -> List a
os Focus a
f)
| Bool
otherwise = forall a. Maybe a
Nothing
where
f :: Focus a
f = forall a. (a -> Bool) -> Graph a -> Focus a
focus a -> Bool
p Graph a
g
{-# INLINE context #-}