{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Topograph (
G (..),
runG,
runG',
transpose,
reduction,
closure,
dfs,
dfsTree,
allPaths,
allPaths',
allPathsTree,
shortestPathLengths,
longestPathLengths,
edgesSet,
adjacencyMap,
adjacencyList,
pairs,
treePairs,
) where
import Data.Orphans ()
import Prelude ()
import Prelude.Compat
import Control.Monad.ST (ST, runST)
import Data.Foldable (for_)
import Data.List (sort)
import Data.Map (Map)
import Data.Maybe (catMaybes, mapMaybe)
import Data.Monoid (First (..))
import Data.Ord (Down (..))
import Data.Set (Set)
import qualified Data.Graph as G
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Tree as T
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as MU
data G v i = G
{ G v i -> [i]
gVertices :: [i]
, G v i -> i -> v
gFromVertex :: i -> v
, G v i -> v -> Maybe i
gToVertex :: v -> Maybe i
, G v i -> i -> [i]
gEdges :: i -> [i]
, G v i -> i -> i -> Int
gDiff :: i -> i -> Int
, G v i -> Int
gVerticeCount :: Int
, G v i -> i -> Int
gVertexIndex :: i -> Int
}
runG
:: forall v r. Ord v
=> Map v (Set v)
-> (forall i. Ord i => G v i -> r)
-> Either [v] r
runG :: Map v (Set v) -> (forall i. Ord i => G v i -> r) -> Either [v] r
runG Map v (Set v)
m forall i. Ord i => G v i -> r
f
| Just [Int]
l <- Maybe [Int]
loop = [v] -> Either [v] r
forall a b. a -> Either a b
Left ((Int -> v) -> [Int] -> [v]
forall a b. (a -> b) -> [a] -> [b]
map (Vector v
indices Vector v -> Int -> v
forall a. Vector a -> Int -> a
V.!) [Int]
l)
| Bool
otherwise = r -> Either [v] r
forall a b. b -> Either a b
Right (G v Int -> r
forall i. Ord i => G v i -> r
f G v Int
g)
where
gr :: G.Graph
r :: G.Vertex -> ((), v, [v])
_t :: v -> Maybe G.Vertex
(Graph
gr, Int -> ((), v, [v])
r, v -> Maybe Int
_t) = [((), v, [v])] -> (Graph, Int -> ((), v, [v]), v -> Maybe Int)
forall key node.
Ord key =>
[(node, key, [key])]
-> (Graph, Int -> (node, key, [key]), key -> Maybe Int)
G.graphFromEdges [ ((), v
v, Set v -> [v]
forall a. Set a -> [a]
Set.toAscList Set v
us) | (v
v, Set v
us) <- Map v (Set v) -> [(v, Set v)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map v (Set v)
m ]
r' :: G.Vertex -> v
r' :: Int -> v
r' Int
i = case Int -> ((), v, [v])
r Int
i of (()
_, v
v, [v]
_) -> v
v
topo :: [G.Vertex]
topo :: [Int]
topo = Graph -> [Int]
G.topSort Graph
gr
indices :: V.Vector v
indices :: Vector v
indices = [v] -> Vector v
forall a. [a] -> Vector a
V.fromList ((Int -> v) -> [Int] -> [v]
forall a b. (a -> b) -> [a] -> [b]
map Int -> v
r' [Int]
topo)
revIndices :: Map v Int
revIndices :: Map v Int
revIndices = [(v, Int)] -> Map v Int
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(v, Int)] -> Map v Int) -> [(v, Int)] -> Map v Int
forall a b. (a -> b) -> a -> b
$ [v] -> [Int] -> [(v, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip ((Int -> v) -> [Int] -> [v]
forall a b. (a -> b) -> [a] -> [b]
map Int -> v
r' [Int]
topo) [Int
0..]
edges :: V.Vector [Int]
edges :: Vector [Int]
edges = (v -> [Int]) -> Vector v -> Vector [Int]
forall a b. (a -> b) -> Vector a -> Vector b
V.map
(\v
v -> [Int] -> (Set v -> [Int]) -> Maybe (Set v) -> [Int]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
[]
(\Set v
sv -> [Int] -> [Int]
forall a. Ord a => [a] -> [a]
sort ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ (v -> Maybe Int) -> [v] -> [Int]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\v
v' -> v -> Map v Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup v
v' Map v Int
revIndices) ([v] -> [Int]) -> [v] -> [Int]
forall a b. (a -> b) -> a -> b
$ Set v -> [v]
forall a. Set a -> [a]
Set.toList Set v
sv)
(v -> Map v (Set v) -> Maybe (Set v)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup v
v Map v (Set v)
m))
Vector v
indices
loop :: Maybe [Int]
loop :: Maybe [Int]
loop = First [Int] -> Maybe [Int]
forall a. First a -> Maybe a
getFirst (First [Int] -> Maybe [Int]) -> First [Int] -> Maybe [Int]
forall a b. (a -> b) -> a -> b
$ (Int -> First [Int]) -> [Int] -> First [Int]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\Int
a -> (Int -> First [Int]) -> [Int] -> First [Int]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Int -> Int -> First [Int]
check Int
a) (G v Int -> Int -> [Int]
forall v i. G v i -> i -> [i]
gEdges G v Int
g Int
a)) (G v Int -> [Int]
forall v i. G v i -> [i]
gVertices G v Int
g)
where
check :: Int -> Int -> First [Int]
check Int
a Int
b
| Int
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
b = Maybe [Int] -> First [Int]
forall a. Maybe a -> First a
First Maybe [Int]
forall a. Maybe a
Nothing
| Bool
otherwise = Maybe [Int] -> First [Int]
forall a. Maybe a -> First a
First (Maybe [Int] -> First [Int]) -> Maybe [Int] -> First [Int]
forall a b. (a -> b) -> a -> b
$ case G v Int -> Int -> Int -> [[Int]]
forall v i. Ord i => G v i -> i -> i -> [[i]]
allPaths G v Int
g Int
b Int
a of
[] -> Maybe [Int]
forall a. Maybe a
Nothing
([Int]
p : [[Int]]
_) -> [Int] -> Maybe [Int]
forall a. a -> Maybe a
Just [Int]
p
g :: G v Int
g :: G v Int
g = G :: forall v i.
[i]
-> (i -> v)
-> (v -> Maybe i)
-> (i -> [i])
-> (i -> i -> Int)
-> Int
-> (i -> Int)
-> G v i
G
{ gVertices :: [Int]
gVertices = [Int
0 .. Vector v -> Int
forall a. Vector a -> Int
V.length Vector v
indices Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
, gFromVertex :: Int -> v
gFromVertex = (Vector v
indices Vector v -> Int -> v
forall a. Vector a -> Int -> a
V.!)
, gToVertex :: v -> Maybe Int
gToVertex = (v -> Map v Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map v Int
revIndices)
, gDiff :: Int -> Int -> Int
gDiff = \Int
a Int
b -> Int
b Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
a
, gEdges :: Int -> [Int]
gEdges = (Vector [Int]
edges Vector [Int] -> Int -> [Int]
forall a. Vector a -> Int -> a
V.!)
, gVerticeCount :: Int
gVerticeCount = Vector v -> Int
forall a. Vector a -> Int
V.length Vector v
indices
, gVertexIndex :: Int -> Int
gVertexIndex = Int -> Int
forall a. a -> a
id
}
runG'
:: forall v r. Ord v
=> Map v (Set v)
-> (forall i. Ord i => G v i -> r)
-> Maybe r
runG' :: Map v (Set v) -> (forall i. Ord i => G v i -> r) -> Maybe r
runG' Map v (Set v)
m forall i. Ord i => G v i -> r
f = ([v] -> Maybe r) -> (r -> Maybe r) -> Either [v] r -> Maybe r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe r -> [v] -> Maybe r
forall a b. a -> b -> a
const Maybe r
forall a. Maybe a
Nothing) r -> Maybe r
forall a. a -> Maybe a
Just (Map v (Set v) -> (forall i. Ord i => G v i -> r) -> Either [v] r
forall v r.
Ord v =>
Map v (Set v) -> (forall i. Ord i => G v i -> r) -> Either [v] r
runG Map v (Set v)
m forall i. Ord i => G v i -> r
f)
allPaths :: forall v i. Ord i => G v i -> i -> i -> [[i]]
allPaths :: G v i -> i -> i -> [[i]]
allPaths G v i
g i
a i
b = ([i] -> [i]) -> [[i]] -> [[i]]
forall a b. (a -> b) -> [a] -> [b]
map (\[i]
p -> i
a i -> [i] -> [i]
forall a. a -> [a] -> [a]
: [i]
p) (G v i -> i -> i -> [i] -> [[i]]
forall v i. Ord i => G v i -> i -> i -> [i] -> [[i]]
allPaths' G v i
g i
a i
b [i
b])
allPaths' :: forall v i. Ord i => G v i -> i -> i -> [i] -> [[i]]
allPaths' :: G v i -> i -> i -> [i] -> [[i]]
allPaths' G {Int
[i]
v -> Maybe i
i -> v
i -> Int
i -> [i]
i -> i -> Int
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} i
a i
b [i]
end = (i -> [[i]]) -> [i] -> [[i]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap i -> [[i]]
go (i -> [i]
gEdges i
a) where
go :: i -> [[i]]
go :: i -> [[i]]
go i
i
| i
i i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
b = [[i]
end]
| Bool
otherwise =
let js :: [i]
js :: [i]
js = (i -> Bool) -> [i] -> [i]
forall a. (a -> Bool) -> [a] -> [a]
filter (i -> i -> Bool
forall a. Ord a => a -> a -> Bool
<= i
b) ([i] -> [i]) -> [i] -> [i]
forall a b. (a -> b) -> a -> b
$ i -> [i]
gEdges i
i
js2b :: [[i]]
js2b :: [[i]]
js2b = (i -> [[i]]) -> [i] -> [[i]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap i -> [[i]]
go [i]
js
in ([i] -> [i]) -> [[i]] -> [[i]]
forall a b. (a -> b) -> [a] -> [b]
map (i
ii -> [i] -> [i]
forall a. a -> [a] -> [a]
:) [[i]]
js2b
allPathsTree :: forall v i. Ord i => G v i -> i -> i -> Maybe (T.Tree i)
allPathsTree :: G v i -> i -> i -> Maybe (Tree i)
allPathsTree G {Int
[i]
v -> Maybe i
i -> v
i -> Int
i -> [i]
i -> i -> Int
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} i
a i
b = i -> Maybe (Tree i)
go i
a where
go :: i -> Maybe (T.Tree i)
go :: i -> Maybe (Tree i)
go i
i
| i
i i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
b = Tree i -> Maybe (Tree i)
forall a. a -> Maybe a
Just (i -> Forest i -> Tree i
forall a. a -> Forest a -> Tree a
T.Node i
b [])
| Bool
otherwise = case (i -> Maybe (Tree i)) -> [i] -> Forest i
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe i -> Maybe (Tree i)
go ([i] -> Forest i) -> [i] -> Forest i
forall a b. (a -> b) -> a -> b
$ (i -> Bool) -> [i] -> [i]
forall a. (a -> Bool) -> [a] -> [a]
filter (i -> i -> Bool
forall a. Ord a => a -> a -> Bool
<= i
b) ([i] -> [i]) -> [i] -> [i]
forall a b. (a -> b) -> a -> b
$ i -> [i]
gEdges i
i of
[] -> Maybe (Tree i)
forall a. Maybe a
Nothing
Forest i
js -> Tree i -> Maybe (Tree i)
forall a. a -> Maybe a
Just (i -> Forest i -> Tree i
forall a. a -> Forest a -> Tree a
T.Node i
i Forest i
js)
dfs :: forall v i. Ord i => G v i -> i -> [[i]]
dfs :: G v i -> i -> [[i]]
dfs G {Int
[i]
v -> Maybe i
i -> v
i -> Int
i -> [i]
i -> i -> Int
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} = i -> [[i]]
go where
go :: i -> [[i]]
go :: i -> [[i]]
go i
a = case i -> [i]
gEdges i
a of
[] -> [[i
a]]
[i]
bs -> (i -> [[i]]) -> [i] -> [[i]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\i
b -> ([i] -> [i]) -> [[i]] -> [[i]]
forall a b. (a -> b) -> [a] -> [b]
map (i
a i -> [i] -> [i]
forall a. a -> [a] -> [a]
:) (i -> [[i]]
go i
b)) [i]
bs
dfsTree :: forall v i. Ord i => G v i -> i -> T.Tree i
dfsTree :: G v i -> i -> Tree i
dfsTree G {Int
[i]
v -> Maybe i
i -> v
i -> Int
i -> [i]
i -> i -> Int
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} = i -> Tree i
go where
go :: i -> T.Tree i
go :: i -> Tree i
go i
a = case i -> [i]
gEdges i
a of
[] -> i -> Forest i -> Tree i
forall a. a -> Forest a -> Tree a
T.Node i
a []
[i]
bs -> i -> Forest i -> Tree i
forall a. a -> Forest a -> Tree a
T.Node i
a (Forest i -> Tree i) -> Forest i -> Tree i
forall a b. (a -> b) -> a -> b
$ (i -> Tree i) -> [i] -> Forest i
forall a b. (a -> b) -> [a] -> [b]
map i -> Tree i
go [i]
bs
shortestPathLengths :: Ord i => G v i -> i -> [Int]
shortestPathLengths :: G v i -> i -> [Int]
shortestPathLengths = (Int -> Int -> Int) -> G v i -> i -> [Int]
forall v i. Ord i => (Int -> Int -> Int) -> G v i -> i -> [Int]
pathLenghtsImpl Int -> Int -> Int
forall a. (Num a, Ord a) => a -> a -> a
min' where
min' :: a -> a -> a
min' a
0 a
y = a
y
min' a
x a
y = a -> a -> a
forall a. Ord a => a -> a -> a
min a
x a
y
longestPathLengths :: Ord i => G v i -> i -> [Int]
longestPathLengths :: G v i -> i -> [Int]
longestPathLengths = (Int -> Int -> Int) -> G v i -> i -> [Int]
forall v i. Ord i => (Int -> Int -> Int) -> G v i -> i -> [Int]
pathLenghtsImpl Int -> Int -> Int
forall a. Ord a => a -> a -> a
max
pathLenghtsImpl :: forall v i. Ord i => (Int -> Int -> Int) -> G v i -> i -> [Int]
pathLenghtsImpl :: (Int -> Int -> Int) -> G v i -> i -> [Int]
pathLenghtsImpl Int -> Int -> Int
merge G {Int
[i]
v -> Maybe i
i -> v
i -> Int
i -> [i]
i -> i -> Int
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} i
a = (forall s. ST s [Int]) -> [Int]
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s [Int]) -> [Int])
-> (forall s. ST s [Int]) -> [Int]
forall a b. (a -> b) -> a -> b
$ do
MVector s Int
v <- Int -> Int -> ST s (MVector (PrimState (ST s)) Int)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> a -> m (MVector (PrimState m) a)
MU.replicate ([i] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [i]
gVertices) (Int
0 :: Int)
MVector s Int -> Set i -> ST s ()
forall s. MVector s Int -> Set i -> ST s ()
go MVector s Int
v (i -> Set i
forall a. a -> Set a
Set.singleton i
a)
Vector Int
v' <- MVector (PrimState (ST s)) Int -> ST s (Vector Int)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
MVector (PrimState m) a -> m (Vector a)
U.freeze MVector s Int
MVector (PrimState (ST s)) Int
v
[Int] -> ST s [Int]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector Int -> [Int]
forall a. Unbox a => Vector a -> [a]
U.toList Vector Int
v')
where
go :: MU.MVector s Int -> Set i -> ST s ()
go :: MVector s Int -> Set i -> ST s ()
go MVector s Int
v Set i
xs = do
case Set i -> Maybe (i, Set i)
forall a. Set a -> Maybe (a, Set a)
Set.minView Set i
xs of
Maybe (i, Set i)
Nothing -> () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just (i
x, Set i
xs') -> do
Int
c <- MVector (PrimState (ST s)) Int -> Int -> ST s Int
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> m a
MU.unsafeRead MVector s Int
MVector (PrimState (ST s)) Int
v (i -> Int
gVertexIndex i
x)
let ys :: Set i
ys = [i] -> Set i
forall a. Ord a => [a] -> Set a
Set.fromList ([i] -> Set i) -> [i] -> Set i
forall a b. (a -> b) -> a -> b
$ i -> [i]
gEdges i
x
Set i -> (i -> ST s ()) -> ST s ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ Set i
ys ((i -> ST s ()) -> ST s ()) -> (i -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \i
y ->
((Int -> Int) -> Int -> ST s ()) -> Int -> (Int -> Int) -> ST s ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (MVector (PrimState (ST s)) Int -> (Int -> Int) -> Int -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> (a -> a) -> Int -> m ()
MU.unsafeModify MVector s Int
MVector (PrimState (ST s)) Int
v) (i -> Int
gVertexIndex i
y) ((Int -> Int) -> ST s ()) -> (Int -> Int) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int
d -> Int -> Int -> Int
merge Int
d (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
MVector s Int -> Set i -> ST s ()
forall s. MVector s Int -> Set i -> ST s ()
go MVector s Int
v (Set i
xs' Set i -> Set i -> Set i
forall a. Ord a => Set a -> Set a -> Set a
`Set.union` Set i
ys)
transpose :: forall v i. Ord i => G v i -> G v (Down i)
transpose :: G v i -> G v (Down i)
transpose G {Int
[i]
v -> Maybe i
i -> v
i -> Int
i -> [i]
i -> i -> Int
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} = G :: forall v i.
[i]
-> (i -> v)
-> (v -> Maybe i)
-> (i -> [i])
-> (i -> i -> Int)
-> Int
-> (i -> Int)
-> G v i
G
{ gVertices :: [Down i]
gVertices = (i -> Down i) -> [i] -> [Down i]
forall a b. (a -> b) -> [a] -> [b]
map i -> Down i
forall a. a -> Down a
Down ([i] -> [Down i]) -> [i] -> [Down i]
forall a b. (a -> b) -> a -> b
$ [i] -> [i]
forall a. [a] -> [a]
reverse [i]
gVertices
, gFromVertex :: Down i -> v
gFromVertex = i -> v
gFromVertex (i -> v) -> (Down i -> i) -> Down i -> v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down i -> i
forall a. Down a -> a
getDown
, gToVertex :: v -> Maybe (Down i)
gToVertex = (i -> Down i) -> Maybe i -> Maybe (Down i)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap i -> Down i
forall a. a -> Down a
Down (Maybe i -> Maybe (Down i))
-> (v -> Maybe i) -> v -> Maybe (Down i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> Maybe i
gToVertex
, gEdges :: Down i -> [Down i]
gEdges = Down i -> [Down i]
gEdges'
, gDiff :: Down i -> Down i -> Int
gDiff = \(Down i
a) (Down i
b) -> i -> i -> Int
gDiff i
b i
a
, gVerticeCount :: Int
gVerticeCount = Int
gVerticeCount
, gVertexIndex :: Down i -> Int
gVertexIndex = \(Down i
a) -> Int
gVerticeCount Int -> Int -> Int
forall a. Num a => a -> a -> a
- i -> Int
gVertexIndex i
a Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
}
where
gEdges' :: Down i -> [Down i]
gEdges' :: Down i -> [Down i]
gEdges' (Down i
a) = Vector [Down i]
es Vector [Down i] -> Int -> [Down i]
forall a. Vector a -> Int -> a
V.! i -> Int
gVertexIndex i
a
es :: V.Vector [Down i]
es :: Vector [Down i]
es = [[Down i]] -> Vector [Down i]
forall a. [a] -> Vector a
V.fromList ([[Down i]] -> Vector [Down i]) -> [[Down i]] -> Vector [Down i]
forall a b. (a -> b) -> a -> b
$ (i -> [Down i]) -> [i] -> [[Down i]]
forall a b. (a -> b) -> [a] -> [b]
map ((i -> Down i) -> [i] -> [Down i]
forall a b. (a -> b) -> [a] -> [b]
map i -> Down i
forall a. a -> Down a
Down ([i] -> [Down i]) -> (i -> [i]) -> i -> [Down i]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> [i]
revEdges) [i]
gVertices
revEdges :: i -> [i]
revEdges :: i -> [i]
revEdges i
x = (i -> [i]) -> [i] -> [i]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\i
y -> [i
y | i
x i -> [i] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` i -> [i]
gEdges i
y ]) [i]
gVertices
reduction :: Ord i => G v i -> G v i
reduction :: G v i -> G v i
reduction = (Int -> Bool) -> G v i -> G v i
forall v i. Ord i => (Int -> Bool) -> G v i -> G v i
transitiveImpl (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1)
closure :: Ord i => G v i -> G v i
closure :: G v i -> G v i
closure = (Int -> Bool) -> G v i -> G v i
forall v i. Ord i => (Int -> Bool) -> G v i -> G v i
transitiveImpl (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0)
transitiveImpl :: forall v i. Ord i => (Int -> Bool) -> G v i -> G v i
transitiveImpl :: (Int -> Bool) -> G v i -> G v i
transitiveImpl Int -> Bool
pre g :: G v i
g@G {Int
[i]
v -> Maybe i
i -> v
i -> Int
i -> [i]
i -> i -> Int
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} = G v i
g { gEdges :: i -> [i]
gEdges = i -> [i]
gEdges' } where
gEdges' :: i -> [i]
gEdges' :: i -> [i]
gEdges' i
a = Vector [i]
es Vector [i] -> Int -> [i]
forall a. Vector a -> Int -> a
V.! i -> Int
gVertexIndex i
a
es :: V.Vector [i]
es :: Vector [i]
es = [[i]] -> Vector [i]
forall a. [a] -> Vector a
V.fromList ([[i]] -> Vector [i]) -> [[i]] -> Vector [i]
forall a b. (a -> b) -> a -> b
$ (i -> [i]) -> [i] -> [[i]]
forall a b. (a -> b) -> [a] -> [b]
map i -> [i]
f [i]
gVertices where
f :: i -> [i]
f :: i -> [i]
f i
x = [Maybe i] -> [i]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe i] -> [i]) -> [Maybe i] -> [i]
forall a b. (a -> b) -> a -> b
$ (i -> Int -> Maybe i) -> [i] -> [Int] -> [Maybe i]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith i -> Int -> Maybe i
forall a. a -> Int -> Maybe a
edge [i]
gVertices (G v i -> i -> [Int]
forall i v. Ord i => G v i -> i -> [Int]
longestPathLengths G v i
g i
x)
edge :: a -> Int -> Maybe a
edge a
y Int
i
| Int -> Bool
pre Int
i = a -> Maybe a
forall a. a -> Maybe a
Just a
y
| Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
adjacencyMap :: Ord v => G v i -> Map v (Set v)
adjacencyMap :: G v i -> Map v (Set v)
adjacencyMap G {Int
[i]
v -> Maybe i
i -> v
i -> Int
i -> [i]
i -> i -> Int
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} = [(v, Set v)] -> Map v (Set v)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(v, Set v)] -> Map v (Set v)) -> [(v, Set v)] -> Map v (Set v)
forall a b. (a -> b) -> a -> b
$ (i -> (v, Set v)) -> [i] -> [(v, Set v)]
forall a b. (a -> b) -> [a] -> [b]
map i -> (v, Set v)
f [i]
gVertices where
f :: i -> (v, Set v)
f i
x = (i -> v
gFromVertex i
x, [v] -> Set v
forall a. Ord a => [a] -> Set a
Set.fromList ([v] -> Set v) -> [v] -> Set v
forall a b. (a -> b) -> a -> b
$ (i -> v) -> [i] -> [v]
forall a b. (a -> b) -> [a] -> [b]
map i -> v
gFromVertex ([i] -> [v]) -> [i] -> [v]
forall a b. (a -> b) -> a -> b
$ i -> [i]
gEdges i
x)
adjacencyList :: Ord v => G v i -> [(v, [v])]
adjacencyList :: G v i -> [(v, [v])]
adjacencyList = Map v (Set v) -> [(v, [v])]
forall a. Map a (Set a) -> [(a, [a])]
flattenAM (Map v (Set v) -> [(v, [v])])
-> (G v i -> Map v (Set v)) -> G v i -> [(v, [v])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. G v i -> Map v (Set v)
forall v i. Ord v => G v i -> Map v (Set v)
adjacencyMap
flattenAM :: Map a (Set a) -> [(a, [a])]
flattenAM :: Map a (Set a) -> [(a, [a])]
flattenAM = ((a, Set a) -> (a, [a])) -> [(a, Set a)] -> [(a, [a])]
forall a b. (a -> b) -> [a] -> [b]
map ((Set a -> [a]) -> (a, Set a) -> (a, [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Set a -> [a]
forall a. Set a -> [a]
Set.toList) ([(a, Set a)] -> [(a, [a])])
-> (Map a (Set a) -> [(a, Set a)]) -> Map a (Set a) -> [(a, [a])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map a (Set a) -> [(a, Set a)]
forall k a. Map k a -> [(k, a)]
Map.toList
edgesSet :: Ord i => G v i -> Set (i, i)
edgesSet :: G v i -> Set (i, i)
edgesSet G {Int
[i]
i -> v
i -> Int
i -> [i]
i -> i -> Int
v -> Maybe i
gVertexIndex :: i -> Int
gVerticeCount :: Int
gDiff :: i -> i -> Int
gEdges :: i -> [i]
gToVertex :: v -> Maybe i
gFromVertex :: i -> v
gVertices :: [i]
gVertexIndex :: forall v i. G v i -> i -> Int
gVerticeCount :: forall v i. G v i -> Int
gDiff :: forall v i. G v i -> i -> i -> Int
gEdges :: forall v i. G v i -> i -> [i]
gToVertex :: forall v i. G v i -> v -> Maybe i
gFromVertex :: forall v i. G v i -> i -> v
gVertices :: forall v i. G v i -> [i]
..} = [(i, i)] -> Set (i, i)
forall a. Ord a => [a] -> Set a
Set.fromList
[ (i
x, i
y)
| i
x <- [i]
gVertices
, i
y <- i -> [i]
gEdges i
x
]
#if !(MIN_VERSION_base(4,14,0))
getDown :: Down a -> a
getDown (Down a) = a
#endif
treePairs :: T.Tree a -> [(a,a)]
treePairs :: Tree a -> [(a, a)]
treePairs (T.Node a
i Forest a
js) =
[ (a
i, a
j) | T.Node a
j Forest a
_ <- Forest a
js ] [(a, a)] -> [(a, a)] -> [(a, a)]
forall a. [a] -> [a] -> [a]
++ (Tree a -> [(a, a)]) -> Forest a -> [(a, a)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Tree a -> [(a, a)]
forall a. Tree a -> [(a, a)]
treePairs Forest a
js
pairs :: [a] -> [(a, a)]
pairs :: [a] -> [(a, a)]
pairs [] = []
pairs [a]
xs = [a] -> [a] -> [(a, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
xs ([a] -> [a]
forall a. [a] -> [a]
tail [a]
xs)