module Algebra.Graph.Labelled.AdjacencyMap (
AdjacencyMap, adjacencyMap,
empty, vertex, edge, (-<), (>-), overlay, connect, vertices, edges,
overlays, fromAdjacencyMaps,
isSubgraphOf,
isEmpty, hasVertex, hasEdge, edgeLabel, vertexCount, edgeCount, vertexList,
edgeList, vertexSet, edgeSet, preSet, postSet, skeleton,
removeVertex, removeEdge, replaceVertex, replaceEdge, transpose, gmap,
emap, induce, induceJust,
closure, reflexiveClosure, symmetricClosure, transitiveClosure,
consistent
) where
import Control.DeepSeq
import Data.Maybe
import Data.Map (Map)
import Data.Monoid (Sum (..))
import Data.Set (Set, (\\))
import Data.String
import GHC.Generics
import Algebra.Graph.Label
import qualified Algebra.Graph.AdjacencyMap as AM
import qualified Algebra.Graph.ToGraph as T
import qualified Data.IntSet as IntSet
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
newtype AdjacencyMap e a = AM {
forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap :: Map a (Map a e) } deriving (AdjacencyMap e a -> AdjacencyMap e a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall e a.
(Eq a, Eq e) =>
AdjacencyMap e a -> AdjacencyMap e a -> Bool
/= :: AdjacencyMap e a -> AdjacencyMap e a -> Bool
$c/= :: forall e a.
(Eq a, Eq e) =>
AdjacencyMap e a -> AdjacencyMap e a -> Bool
== :: AdjacencyMap e a -> AdjacencyMap e a -> Bool
$c== :: forall e a.
(Eq a, Eq e) =>
AdjacencyMap e a -> AdjacencyMap e a -> Bool
Eq, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall e a x. Rep (AdjacencyMap e a) x -> AdjacencyMap e a
forall e a x. AdjacencyMap e a -> Rep (AdjacencyMap e a) x
$cto :: forall e a x. Rep (AdjacencyMap e a) x -> AdjacencyMap e a
$cfrom :: forall e a x. AdjacencyMap e a -> Rep (AdjacencyMap e a) x
Generic, AdjacencyMap e a -> ()
forall a. (a -> ()) -> NFData a
forall e a. (NFData a, NFData e) => AdjacencyMap e a -> ()
rnf :: AdjacencyMap e a -> ()
$crnf :: forall e a. (NFData a, NFData e) => AdjacencyMap e a -> ()
NFData)
instance (Ord a, Show a, Ord e, Show e) => Show (AdjacencyMap e a) where
showsPrec :: Int -> AdjacencyMap e a -> ShowS
showsPrec Int
p lam :: AdjacencyMap e a
lam@(AM Map a (Map a e)
m)
| forall a. Set a -> Bool
Set.null Set a
vs = String -> ShowS
showString String
"empty"
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(e, a, a)]
es = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$ forall {a}. Show a => Set a -> ShowS
vshow Set a
vs
| Set a
vs forall a. Eq a => a -> a -> Bool
== Set a
used = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$ forall {a} {a} {a}.
(Show a, Show a, Show a) =>
[(a, a, a)] -> ShowS
eshow [(e, a, a)]
es
| Bool
otherwise = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"overlay (" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. Show a => Set a -> ShowS
vshow (Set a
vs forall a. Ord a => Set a -> Set a -> Set a
\\ Set a
used) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> ShowS
showString String
") (" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a} {a} {a}.
(Show a, Show a, Show a) =>
[(a, a, a)] -> ShowS
eshow [(e, a, a)]
es forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
")"
where
vs :: Set a
vs = forall e a. AdjacencyMap e a -> Set a
vertexSet AdjacencyMap e a
lam
es :: [(e, a, a)]
es = forall e a. AdjacencyMap e a -> [(e, a, a)]
edgeList AdjacencyMap e a
lam
used :: Set a
used = forall a e. Ord a => Map a (Map a e) -> Set a
referredToVertexSet Map a (Map a e)
m
vshow :: Set a -> ShowS
vshow Set a
vs = case forall a. Set a -> [a]
Set.toAscList Set a
vs of
[a
x] -> String -> ShowS
showString String
"vertex " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
x
[a]
xs -> String -> ShowS
showString String
"vertices " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 [a]
xs
eshow :: [(a, a, a)] -> ShowS
eshow [(a, a, a)]
es = case [(a, a, a)]
es of
[(a
e, a
x, a
y)] -> String -> ShowS
showString String
"edge " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
e forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> ShowS
showString String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> ShowS
showString String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
y
[(a, a, a)]
xs -> String -> ShowS
showString String
"edges " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 [(a, a, a)]
xs
instance (Ord e, Monoid e, Ord a) => Ord (AdjacencyMap e a) where
compare :: AdjacencyMap e a -> AdjacencyMap e a -> Ordering
compare AdjacencyMap e a
x AdjacencyMap e a
y = forall a. Monoid a => [a] -> a
mconcat
[ forall a. Ord a => a -> a -> Ordering
compare (forall e a. AdjacencyMap e a -> Int
vertexCount AdjacencyMap e a
x) (forall e a. AdjacencyMap e a -> Int
vertexCount AdjacencyMap e a
y)
, forall a. Ord a => a -> a -> Ordering
compare (forall e a. AdjacencyMap e a -> Set a
vertexSet AdjacencyMap e a
x) (forall e a. AdjacencyMap e a -> Set a
vertexSet AdjacencyMap e a
y)
, forall a. Ord a => a -> a -> Ordering
compare (forall e a. AdjacencyMap e a -> Int
edgeCount AdjacencyMap e a
x) (forall e a. AdjacencyMap e a -> Int
edgeCount AdjacencyMap e a
y)
, forall a. Ord a => a -> a -> Ordering
compare (AdjacencyMap e a -> Set (a, a)
eSet AdjacencyMap e a
x) (AdjacencyMap e a -> Set (a, a)
eSet AdjacencyMap e a
y)
, Ordering
cmp ]
where
eSet :: AdjacencyMap e a -> Set (a, a)
eSet = forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map (\(e
_, a
x, a
y) -> (a
x, a
y)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e. (Eq a, Eq e) => AdjacencyMap e a -> Set (e, a, a)
edgeSet
cmp :: Ordering
cmp | AdjacencyMap e a
x forall a. Eq a => a -> a -> Bool
== AdjacencyMap e a
y = Ordering
EQ
| forall e a.
(Eq e, Monoid e, Ord a) =>
[AdjacencyMap e a] -> AdjacencyMap e a
overlays [AdjacencyMap e a
x, AdjacencyMap e a
y] forall a. Eq a => a -> a -> Bool
== AdjacencyMap e a
y = Ordering
LT
| Bool
otherwise = forall a. Ord a => a -> a -> Ordering
compare AdjacencyMap e a
x AdjacencyMap e a
y
instance (Eq e, Dioid e, Num a, Ord a) => Num (AdjacencyMap e a) where
fromInteger :: Integer -> AdjacencyMap e a
fromInteger = forall a e. a -> AdjacencyMap e a
vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
+ :: AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
(+) = forall e a.
(Eq e, Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
overlay
* :: AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
(*) = forall e a.
(Eq e, Monoid e, Ord a) =>
e -> AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
connect forall a. Monoid a => a
mempty
signum :: AdjacencyMap e a -> AdjacencyMap e a
signum = forall a b. a -> b -> a
const forall e a. AdjacencyMap e a
empty
abs :: AdjacencyMap e a -> AdjacencyMap e a
abs = forall a. a -> a
id
negate :: AdjacencyMap e a -> AdjacencyMap e a
negate = forall a. a -> a
id
instance IsString a => IsString (AdjacencyMap e a) where
fromString :: String -> AdjacencyMap e a
fromString = forall a e. a -> AdjacencyMap e a
vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString
instance (Ord a, Eq e, Monoid e) => Semigroup (AdjacencyMap e a) where
<> :: AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
(<>) = forall e a.
(Eq e, Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
overlay
instance (Ord a, Eq e, Monoid e) => Monoid (AdjacencyMap e a) where
mempty :: AdjacencyMap e a
mempty = forall e a. AdjacencyMap e a
empty
instance (Eq e, Monoid e, Ord a) => T.ToGraph (AdjacencyMap e a) where
type ToVertex (AdjacencyMap e a) = a
toGraph :: AdjacencyMap e a -> Graph (ToVertex (AdjacencyMap e a))
toGraph = forall t. ToGraph t => t -> Graph (ToVertex t)
T.toGraph forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
foldg :: forall r.
r
-> (ToVertex (AdjacencyMap e a) -> r)
-> (r -> r -> r)
-> (r -> r -> r)
-> AdjacencyMap e a
-> r
foldg r
e ToVertex (AdjacencyMap e a) -> r
v r -> r -> r
o r -> r -> r
c = forall t r.
ToGraph t =>
r -> (ToVertex t -> r) -> (r -> r -> r) -> (r -> r -> r) -> t -> r
T.foldg r
e ToVertex (AdjacencyMap e a) -> r
v r -> r -> r
o r -> r -> r
c forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
isEmpty :: AdjacencyMap e a -> Bool
isEmpty = forall e a. AdjacencyMap e a -> Bool
isEmpty
hasVertex :: Eq (ToVertex (AdjacencyMap e a)) =>
ToVertex (AdjacencyMap e a) -> AdjacencyMap e a -> Bool
hasVertex = forall a e. Ord a => a -> AdjacencyMap e a -> Bool
hasVertex
hasEdge :: Eq (ToVertex (AdjacencyMap e a)) =>
ToVertex (AdjacencyMap e a)
-> ToVertex (AdjacencyMap e a) -> AdjacencyMap e a -> Bool
hasEdge = forall a e. Ord a => a -> a -> AdjacencyMap e a -> Bool
hasEdge
vertexCount :: Ord (ToVertex (AdjacencyMap e a)) => AdjacencyMap e a -> Int
vertexCount = forall e a. AdjacencyMap e a -> Int
vertexCount
edgeCount :: Ord (ToVertex (AdjacencyMap e a)) => AdjacencyMap e a -> Int
edgeCount = forall e a. AdjacencyMap e a -> Int
edgeCount
vertexList :: Ord (ToVertex (AdjacencyMap e a)) =>
AdjacencyMap e a -> [ToVertex (AdjacencyMap e a)]
vertexList = forall e a. AdjacencyMap e a -> [a]
vertexList
vertexSet :: Ord (ToVertex (AdjacencyMap e a)) =>
AdjacencyMap e a -> Set (ToVertex (AdjacencyMap e a))
vertexSet = forall e a. AdjacencyMap e a -> Set a
vertexSet
vertexIntSet :: (ToVertex (AdjacencyMap e a) ~ Int) => AdjacencyMap e a -> IntSet
vertexIntSet = [Int] -> IntSet
IntSet.fromAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> [a]
vertexList
edgeList :: Ord (ToVertex (AdjacencyMap e a)) =>
AdjacencyMap e a
-> [(ToVertex (AdjacencyMap e a), ToVertex (AdjacencyMap e a))]
edgeList = 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 a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
edgeSet :: Ord (ToVertex (AdjacencyMap e a)) =>
AdjacencyMap e a
-> Set (ToVertex (AdjacencyMap e a), ToVertex (AdjacencyMap e a))
edgeSet = forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> Set (ToVertex t, ToVertex t)
T.edgeSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
adjacencyList :: Ord (ToVertex (AdjacencyMap e a)) =>
AdjacencyMap e a
-> [(ToVertex (AdjacencyMap e a), [ToVertex (AdjacencyMap e a)])]
adjacencyList = forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> [(ToVertex t, [ToVertex t])]
T.adjacencyList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
preSet :: Ord (ToVertex (AdjacencyMap e a)) =>
ToVertex (AdjacencyMap e a)
-> AdjacencyMap e a -> Set (ToVertex (AdjacencyMap e a))
preSet = forall a e. Ord a => a -> AdjacencyMap e a -> Set a
preSet
postSet :: Ord (ToVertex (AdjacencyMap e a)) =>
ToVertex (AdjacencyMap e a)
-> AdjacencyMap e a -> Set (ToVertex (AdjacencyMap e a))
postSet = forall a e. Ord a => a -> AdjacencyMap e a -> Set a
postSet
toAdjacencyMap :: Ord (ToVertex (AdjacencyMap e a)) =>
AdjacencyMap e a -> AdjacencyMap (ToVertex (AdjacencyMap e a))
toAdjacencyMap = forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
toAdjacencyIntMap :: (ToVertex (AdjacencyMap e a) ~ Int) =>
AdjacencyMap e a -> AdjacencyIntMap
toAdjacencyIntMap = forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
toAdjacencyMapTranspose :: Ord (ToVertex (AdjacencyMap e a)) =>
AdjacencyMap e a -> AdjacencyMap (ToVertex (AdjacencyMap e a))
toAdjacencyMapTranspose = forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMapTranspose forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
toAdjacencyIntMapTranspose :: (ToVertex (AdjacencyMap e a) ~ Int) =>
AdjacencyMap e a -> AdjacencyIntMap
toAdjacencyIntMapTranspose = forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMapTranspose forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton
empty :: AdjacencyMap e a
empty :: forall e a. AdjacencyMap e a
empty = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall k a. Map k a
Map.empty
vertex :: a -> AdjacencyMap e a
vertex :: forall a e. a -> AdjacencyMap e a
vertex a
x = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall k a. k -> a -> Map k a
Map.singleton a
x forall k a. Map k a
Map.empty
edge :: (Eq e, Monoid e, Ord a) => e -> a -> a -> AdjacencyMap e a
edge :: forall e a.
(Eq e, Monoid e, Ord a) =>
e -> a -> a -> AdjacencyMap e a
edge e
e a
x a
y | e
e forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
zero = forall a e. Ord a => [a] -> AdjacencyMap e a
vertices [a
x, a
y]
| a
x forall a. Eq a => a -> a -> Bool
== a
y = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall k a. k -> a -> Map k a
Map.singleton a
x (forall k a. k -> a -> Map k a
Map.singleton a
x e
e)
| Bool
otherwise = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(a
x, forall k a. k -> a -> Map k a
Map.singleton a
y e
e), (a
y, forall k a. Map k a
Map.empty)]
(-<) :: a -> e -> (a, e)
a
g -< :: forall a e. a -> e -> (a, e)
-< e
e = (a
g, e
e)
(>-) :: (Eq e, Monoid e, Ord a) => (a, e) -> a -> AdjacencyMap e a
(a
x, e
e) >- :: forall e a.
(Eq e, Monoid e, Ord a) =>
(a, e) -> a -> AdjacencyMap e a
>- a
y = forall e a.
(Eq e, Monoid e, Ord a) =>
e -> a -> a -> AdjacencyMap e a
edge e
e a
x a
y
infixl 5 -<
infixl 5 >-
overlay :: (Eq e, Monoid e, Ord a) => AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
overlay :: forall e a.
(Eq e, Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
overlay (AM Map a (Map a e)
x) (AM Map a (Map a e)
y) = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith forall e a.
(Eq e, Monoid e, Ord a) =>
Map a e -> Map a e -> Map a e
nonZeroUnion Map a (Map a e)
x Map a (Map a e)
y
nonZeroUnion :: (Eq e, Monoid e, Ord a) => Map a e -> Map a e -> Map a e
nonZeroUnion :: forall e a.
(Eq e, Monoid e, Ord a) =>
Map a e -> Map a e -> Map a e
nonZeroUnion Map a e
x Map a e
y = forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (forall a. Eq a => a -> a -> Bool
/= forall a. Monoid a => a
zero) forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith forall a. Monoid a => a -> a -> a
mappend Map a e
x Map a e
y
trimZeroes :: (Eq e, Monoid e) => Map a (Map a e) -> Map a (Map a e)
trimZeroes :: forall e a. (Eq e, Monoid e) => Map a (Map a e) -> Map a (Map a e)
trimZeroes = forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (forall a. Eq a => a -> a -> Bool
/= forall a. Monoid a => a
zero))
connect :: (Eq e, Monoid e, Ord a) => e -> AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
connect :: forall e a.
(Eq e, Monoid e, Ord a) =>
e -> AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
connect e
e (AM Map a (Map a e)
x) (AM Map a (Map a e)
y)
| e
e forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
mempty = forall e a.
(Eq e, Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
overlay (forall e a. Map a (Map a e) -> AdjacencyMap e a
AM Map a (Map a e)
x) (forall e a. Map a (Map a e) -> AdjacencyMap e a
AM Map a (Map a e)
y)
| Bool
otherwise = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith forall e a.
(Eq e, Monoid e, Ord a) =>
Map a e -> Map a e -> Map a e
nonZeroUnion forall a b. (a -> b) -> a -> b
$ Map a (Map a e)
x forall a. a -> [a] -> [a]
: Map a (Map a e)
y forall a. a -> [a] -> [a]
:
[ forall k a. (k -> a) -> Set k -> Map k a
Map.fromSet (forall a b. a -> b -> a
const Map a e
targets) (forall k a. Map k a -> Set k
Map.keysSet Map a (Map a e)
x) ]
where
targets :: Map a e
targets = forall k a. (k -> a) -> Set k -> Map k a
Map.fromSet (forall a b. a -> b -> a
const e
e) (forall k a. Map k a -> Set k
Map.keysSet Map a (Map a e)
y)
vertices :: Ord a => [a] -> AdjacencyMap e a
vertices :: forall a e. Ord a => [a] -> AdjacencyMap e a
vertices = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (, forall k a. Map k a
Map.empty)
edges :: (Eq e, Monoid e, Ord a) => [(e, a, a)] -> AdjacencyMap e a
edges :: forall e a.
(Eq e, Monoid e, Ord a) =>
[(e, a, a)] -> AdjacencyMap e a
edges [(e, a, a)]
es = forall e a.
(Eq e, Monoid e, Ord a) =>
[(a, Map a e)] -> AdjacencyMap e a
fromAdjacencyMaps [ (a
x, forall k a. k -> a -> Map k a
Map.singleton a
y e
e) | (e
e, a
x, a
y) <- [(e, a, a)]
es ]
overlays :: (Eq e, Monoid e, Ord a) => [AdjacencyMap e a] -> AdjacencyMap e a
overlays :: forall e a.
(Eq e, Monoid e, Ord a) =>
[AdjacencyMap e a] -> AdjacencyMap e a
overlays = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith forall e a.
(Eq e, Monoid e, Ord a) =>
Map a e -> Map a e -> Map a e
nonZeroUnion forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
fromAdjacencyMaps :: (Eq e, Monoid e, Ord a) => [(a, Map a e)] -> AdjacencyMap e a
fromAdjacencyMaps :: forall e a.
(Eq e, Monoid e, Ord a) =>
[(a, Map a e)] -> AdjacencyMap e a
fromAdjacencyMaps [(a, Map a e)]
xs = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall e a. (Eq e, Monoid e) => Map a (Map a e) -> Map a (Map a e)
trimZeroes forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith forall a. Monoid a => a -> a -> a
mappend Map a (Map a e)
vs Map a (Map a e)
es
where
vs :: Map a (Map a e)
vs = forall k a. (k -> a) -> Set k -> Map k a
Map.fromSet (forall a b. a -> b -> a
const forall k a. Map k a
Map.empty) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall k a. Map k a -> Set k
Map.keysSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(a, Map a e)]
xs
es :: Map a (Map a e)
es = forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith (forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith forall a. Monoid a => a -> a -> a
mappend) [(a, Map a e)]
xs
isSubgraphOf :: (Eq e, Monoid e, Ord a) => AdjacencyMap e a -> AdjacencyMap e a -> Bool
isSubgraphOf :: forall e a.
(Eq e, Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a -> Bool
isSubgraphOf (AM Map a (Map a e)
x) (AM Map a (Map a e)
y) = forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
Map.isSubmapOfBy (forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
Map.isSubmapOfBy forall {a}. (Eq a, Monoid a) => a -> a -> Bool
le) Map a (Map a e)
x Map a (Map a e)
y
where
le :: a -> a -> Bool
le a
x a
y = forall a. Monoid a => a -> a -> a
mappend a
x a
y forall a. Eq a => a -> a -> Bool
== a
y
isEmpty :: AdjacencyMap e a -> Bool
isEmpty :: forall e a. AdjacencyMap e a -> Bool
isEmpty = forall k a. Map k a -> Bool
Map.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
hasVertex :: Ord a => a -> AdjacencyMap e a -> Bool
hasVertex :: forall a e. Ord a => a -> AdjacencyMap e a -> Bool
hasVertex a
x = forall k a. Ord k => k -> Map k a -> Bool
Map.member a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
hasEdge :: Ord a => a -> a -> AdjacencyMap e a -> Bool
hasEdge :: forall a e. Ord a => a -> a -> AdjacencyMap e a -> Bool
hasEdge a
x a
y (AM Map a (Map a e)
m) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (forall k a. Ord k => k -> Map k a -> Bool
Map.member a
y) (forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup a
x Map a (Map a e)
m)
edgeLabel :: (Monoid e, Ord a) => a -> a -> AdjacencyMap e a -> e
edgeLabel :: forall e a. (Monoid e, Ord a) => a -> a -> AdjacencyMap e a -> e
edgeLabel a
x a
y (AM Map a (Map a e)
m) = forall a. a -> Maybe a -> a
fromMaybe forall a. Monoid a => a
zero (forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup a
x Map a (Map a e)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup a
y)
vertexCount :: AdjacencyMap e a -> Int
vertexCount :: forall e a. AdjacencyMap e a -> Int
vertexCount = forall k a. Map k a -> Int
Map.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
edgeCount :: AdjacencyMap e a -> Int
edgeCount :: forall e a. AdjacencyMap e a -> Int
edgeCount = forall a. Sum a -> a
getSum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (forall a. a -> Sum a
Sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> Int
Map.size) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
vertexList :: AdjacencyMap e a -> [a]
vertexList :: forall e a. AdjacencyMap e a -> [a]
vertexList = forall k a. Map k a -> [k]
Map.keys forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
edgeList :: AdjacencyMap e a -> [(e, a, a)]
edgeList :: forall e a. AdjacencyMap e a -> [(e, a, a)]
edgeList (AM Map a (Map a e)
m) =
[ (e
e, a
x, a
y) | (a
x, Map a e
ys) <- forall k a. Map k a -> [(k, a)]
Map.toAscList Map a (Map a e)
m, (a
y, e
e) <- forall k a. Map k a -> [(k, a)]
Map.toAscList Map a e
ys ]
vertexSet :: AdjacencyMap e a -> Set a
vertexSet :: forall e a. AdjacencyMap e a -> Set a
vertexSet = forall k a. Map k a -> Set k
Map.keysSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
edgeSet :: (Eq a, Eq e) => AdjacencyMap e a -> Set (e, a, a)
edgeSet :: forall a e. (Eq a, Eq e) => AdjacencyMap e a -> Set (e, a, a)
edgeSet = forall a. Eq a => [a] -> Set a
Set.fromAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> [(e, a, a)]
edgeList
preSet :: Ord a => a -> AdjacencyMap e a -> Set a
preSet :: forall a e. Ord a => a -> AdjacencyMap e a -> Set a
preSet a
x (AM Map a (Map a e)
m) = forall a. Eq a => [a] -> Set a
Set.fromAscList
[ a
a | (a
a, Map a e
es) <- forall k a. Map k a -> [(k, a)]
Map.toAscList Map a (Map a e)
m, forall k a. Ord k => k -> Map k a -> Bool
Map.member a
x Map a e
es ]
postSet :: Ord a => a -> AdjacencyMap e a -> Set a
postSet :: forall a e. Ord a => a -> AdjacencyMap e a -> Set a
postSet a
x = forall k a. Map k a -> Set k
Map.keysSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault forall k a. Map k a
Map.empty a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
skeleton :: Ord a => AdjacencyMap e a -> AM.AdjacencyMap a
skeleton :: forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
skeleton (AM Map a (Map a e)
m) = forall a. Ord a => [(a, Set a)] -> AdjacencyMap a
AM.fromAdjacencySets forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
Map.toAscList forall a b. (a -> b) -> a -> b
$ forall a b k. (a -> b) -> Map k a -> Map k b
Map.map forall k a. Map k a -> Set k
Map.keysSet Map a (Map a e)
m
removeVertex :: Ord a => a -> AdjacencyMap e a -> AdjacencyMap e a
removeVertex :: forall a e. Ord a => a -> AdjacencyMap e a -> AdjacencyMap e a
removeVertex a
x = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (forall k a. Ord k => k -> Map k a -> Map k a
Map.delete a
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => k -> Map k a -> Map k a
Map.delete a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
removeEdge :: Ord a => a -> a -> AdjacencyMap e a -> AdjacencyMap e a
removeEdge :: forall a e. Ord a => a -> a -> AdjacencyMap e a -> AdjacencyMap e a
removeEdge a
x a
y = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
Map.adjust (forall k a. Ord k => k -> Map k a -> Map k a
Map.delete a
y) a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
replaceVertex :: (Eq e, Monoid e, Ord a) => a -> a -> AdjacencyMap e a -> AdjacencyMap e a
replaceVertex :: forall e a.
(Eq e, Monoid e, Ord a) =>
a -> a -> AdjacencyMap e a -> AdjacencyMap e a
replaceVertex a
u a
v = forall e a b.
(Eq e, Monoid e, Ord a, Ord b) =>
(a -> b) -> AdjacencyMap e a -> AdjacencyMap e b
gmap 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
replaceEdge :: (Eq e, Monoid e, Ord a) => e -> a -> a -> AdjacencyMap e a -> AdjacencyMap e a
replaceEdge :: forall e a.
(Eq e, Monoid e, Ord a) =>
e -> a -> a -> AdjacencyMap e a -> AdjacencyMap e a
replaceEdge e
e a
x a
y
| e
e forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
zero = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map a (Map a e) -> Map a (Map a e)
addY forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall k a. Map k a
Map.empty (forall k a. Ord k => k -> Map k a -> Map k a
Map.delete a
y)) a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
| Bool
otherwise = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map a (Map a e) -> Map a (Map a e)
addY forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter Maybe (Map a e) -> Maybe (Map a e)
replace a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
where
addY :: Map a (Map a e) -> Map a (Map a e)
addY = forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe forall k a. Map k a
Map.empty) a
y
replace :: Maybe (Map a e) -> Maybe (Map a e)
replace (Just Map a e
m) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert a
y e
e Map a e
m
replace Maybe (Map a e)
Nothing = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall k a. k -> a -> Map k a
Map.singleton a
y e
e
transpose :: (Monoid e, Ord a) => AdjacencyMap e a -> AdjacencyMap e a
transpose :: forall e a.
(Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a
transpose (AM Map a (Map a e)
m) = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey forall {k} {k} {a}.
(Ord k, Ord k, Monoid a) =>
k -> Map k a -> Map k (Map k a) -> Map k (Map k a)
combine Map a (Map a e)
vs Map a (Map a e)
m
where
combine :: k -> Map k a -> Map k (Map k a) -> Map k (Map k a)
combine k
v Map k a
es = forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith (forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith forall a. Monoid a => a -> a -> a
mappend) forall a b. (a -> b) -> a -> b
$
forall k a. Eq k => [(k, a)] -> Map k a
Map.fromAscList [ (k
u, forall k a. k -> a -> Map k a
Map.singleton k
v a
e) | (k
u, a
e) <- forall k a. Map k a -> [(k, a)]
Map.toAscList Map k a
es ]
vs :: Map a (Map a e)
vs = forall k a. (k -> a) -> Set k -> Map k a
Map.fromSet (forall a b. a -> b -> a
const forall k a. Map k a
Map.empty) (forall k a. Map k a -> Set k
Map.keysSet Map a (Map a e)
m)
gmap :: (Eq e, Monoid e, Ord a, Ord b) => (a -> b) -> AdjacencyMap e a -> AdjacencyMap e b
gmap :: forall e a b.
(Eq e, Monoid e, Ord a, Ord b) =>
(a -> b) -> AdjacencyMap e a -> AdjacencyMap e b
gmap a -> b
f = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. (Eq e, Monoid e) => Map a (Map a e) -> Map a (Map a e)
trimZeroes forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (forall k2 a k1.
Ord k2 =>
(a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a
Map.mapKeysWith forall a. Monoid a => a -> a -> a
mappend a -> b
f) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall k2 a k1.
Ord k2 =>
(a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a
Map.mapKeysWith (forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith forall a. Monoid a => a -> a -> a
mappend) a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
emap :: (Eq f, Monoid f) => (e -> f) -> AdjacencyMap e a -> AdjacencyMap f a
emap :: forall f e a.
(Eq f, Monoid f) =>
(e -> f) -> AdjacencyMap e a -> AdjacencyMap f a
emap e -> f
h = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. (Eq e, Monoid e) => Map a (Map a e) -> Map a (Map a e)
trimZeroes forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (forall a b k. (a -> b) -> Map k a -> Map k b
Map.map e -> f
h) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
induce :: (a -> Bool) -> AdjacencyMap e a -> AdjacencyMap e a
induce :: forall a e. (a -> Bool) -> AdjacencyMap e a -> AdjacencyMap e a
induce a -> Bool
p = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey (\a
k e
_ -> a -> Bool
p a
k)) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey (\a
k Map a e
_ -> a -> Bool
p a
k) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
induceJust :: Ord a => AdjacencyMap e (Maybe a) -> AdjacencyMap e a
induceJust :: forall a e. Ord a => AdjacencyMap e (Maybe a) -> AdjacencyMap e a
induceJust = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b k. (a -> b) -> Map k a -> Map k b
Map.map forall {a}. Map (Maybe a) a -> Map a a
catMaybesMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. Map (Maybe a) a -> Map a a
catMaybesMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. AdjacencyMap e a -> Map a (Map a e)
adjacencyMap
where
catMaybesMap :: Map (Maybe a) a -> Map a a
catMaybesMap = forall k1 k2 a. (k1 -> k2) -> Map k1 a -> Map k2 a
Map.mapKeysMonotonic forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => k -> Map k a -> Map k a
Map.delete forall a. Maybe a
Nothing
closure :: (Eq e, Ord a, StarSemiring e) => AdjacencyMap e a -> AdjacencyMap e a
closure :: forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
closure = forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
goWarshallFloydKleene forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a e.
(Ord a, Semiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
reflexiveClosure
reflexiveClosure :: (Ord a, Semiring e) => AdjacencyMap e a -> AdjacencyMap e a
reflexiveClosure :: forall a e.
(Ord a, Semiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
reflexiveClosure (AM Map a (Map a e)
m) = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey (\a
k -> forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith forall a. Semigroup a => a -> a -> a
(<+>) a
k forall a. Semiring a => a
one) Map a (Map a e)
m
symmetricClosure :: (Eq e, Monoid e, Ord a) => AdjacencyMap e a -> AdjacencyMap e a
symmetricClosure :: forall e a.
(Eq e, Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a
symmetricClosure AdjacencyMap e a
m = forall e a.
(Eq e, Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
overlay AdjacencyMap e a
m (forall e a.
(Monoid e, Ord a) =>
AdjacencyMap e a -> AdjacencyMap e a
transpose AdjacencyMap e a
m)
transitiveClosure :: (Eq e, Ord a, StarSemiring e) => AdjacencyMap e a -> AdjacencyMap e a
transitiveClosure :: forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
transitiveClosure = forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
goWarshallFloydKleene
goWarshallFloydKleene :: (Eq e, Ord a, StarSemiring e) => AdjacencyMap e a -> AdjacencyMap e a
goWarshallFloydKleene :: forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
goWarshallFloydKleene (AM Map a (Map a e)
m) = forall e a. Map a (Map a e) -> AdjacencyMap e a
AM forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> Map a (Map a e) -> Map a (Map a e)
update Map a (Map a e)
m [a]
vs
where
vs :: [a]
vs = forall a. Set a -> [a]
Set.toAscList (forall k a. Map k a -> Set k
Map.keysSet Map a (Map a e)
m)
update :: a -> Map a (Map a e) -> Map a (Map a e)
update a
k Map a (Map a e)
cur = forall k a. Eq k => [(k, a)] -> Map k a
Map.fromAscList [ (a
i, a -> e -> Map a e
go a
i (a -> a -> e
get a
i a
k forall a. Semiring a => a -> a -> a
<.> e
starkk)) | a
i <- [a]
vs ]
where
get :: a -> a -> e
get a
i a
j = forall e a. (Monoid e, Ord a) => a -> a -> AdjacencyMap e a -> e
edgeLabel a
i a
j (forall e a. Map a (Map a e) -> AdjacencyMap e a
AM Map a (Map a e)
cur)
starkk :: e
starkk = forall a. StarSemiring a => a -> a
star (a -> a -> e
get a
k a
k)
go :: a -> e -> Map a e
go a
i e
ik = forall k a. Eq k => [(k, a)] -> Map k a
Map.fromAscList
[ (a
j, e
e) | a
j <- [a]
vs, let e :: e
e = a -> a -> e
get a
i a
j forall a. Semigroup a => a -> a -> a
<+> e
ik forall a. Semiring a => a -> a -> a
<.> a -> a -> e
get a
k a
j, e
e forall a. Eq a => a -> a -> Bool
/= forall a. Monoid a => a
zero ]
consistent :: (Ord a, Eq e, Monoid e) => AdjacencyMap e a -> Bool
consistent :: forall a e. (Ord a, Eq e, Monoid e) => AdjacencyMap e a -> Bool
consistent (AM Map a (Map a e)
m) = forall a e. Ord a => Map a (Map a e) -> Set a
referredToVertexSet Map a (Map a e)
m forall a. Ord a => Set a -> Set a -> Bool
`Set.isSubsetOf` forall k a. Map k a -> Set k
Map.keysSet Map a (Map a e)
m
Bool -> Bool -> Bool
&& forall (t :: * -> *). Foldable t => t Bool -> Bool
and [ e
e forall a. Eq a => a -> a -> Bool
/= forall a. Monoid a => a
zero | (a
_, Map a e
es) <- forall k a. Map k a -> [(k, a)]
Map.toAscList Map a (Map a e)
m, (a
_, e
e) <- forall k a. Map k a -> [(k, a)]
Map.toAscList Map a e
es ]
referredToVertexSet :: Ord a => Map a (Map a e) -> Set a
referredToVertexSet :: forall a e. Ord a => Map a (Map a e) -> Set a
referredToVertexSet Map a (Map a e)
m = forall a. Ord a => [a] -> Set a
Set.fromList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [a
x, a
y] | (a
x, Map a e
ys) <- forall k a. Map k a -> [(k, a)]
Map.toAscList Map a (Map a e)
m, (a
y, e
_) <- forall k a. Map k a -> [(k, a)]
Map.toAscList Map a e
ys ]