{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}
# if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE Safe #-}
# else
{-# LANGUAGE Trustworthy #-}
# endif
#endif

#include "containers.h"

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Graph
-- Copyright   :  (c) The University of Glasgow 2002
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Portability :  portable
--
-- = Finite Graphs
--
-- The @'Graph'@ type is an adjacency list representation of a finite, directed
-- graph with vertices of type @Int@.
--
-- The @'SCC'@ type represents a
-- <https://en.wikipedia.org/wiki/Strongly_connected_component strongly-connected component>
-- of a graph.
--
-- == Implementation
--
-- The implementation is based on
--
--   * /Structuring Depth-First Search Algorithms in Haskell/,
--     by David King and John Launchbury, <http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.52.6526>
--
-----------------------------------------------------------------------------

module Data.Graph (

    -- * Graphs
      Graph
    , Bounds
    , Edge
    , Vertex
    , Table

    -- ** Graph Construction
    , graphFromEdges
    , graphFromEdges'
    , buildG

    -- ** Graph Properties
    , vertices
    , edges
    , outdegree
    , indegree

    -- ** Graph Transformations
    , transposeG

    -- ** Graph Algorithms
    , dfs
    , dff
    , topSort
    , reverseTopSort
    , components
    , scc
    , bcc
    , reachable
    , path


    -- * Strongly Connected Components
    , SCC(..)

    -- ** Construction
    , stronglyConnComp
    , stronglyConnCompR

    -- ** Conversion
    , flattenSCC
    , flattenSCCs

    -- * Trees
    , module Data.Tree

    ) where

#if USE_ST_MONAD
import Control.Monad.ST
import Data.Array.ST.Safe (newArray, readArray, writeArray)
# if USE_UNBOXED_ARRAYS
import Data.Array.ST.Safe (STUArray)
# else
import Data.Array.ST.Safe (STArray)
# endif
#else
import Data.IntSet (IntSet)
import qualified Data.IntSet as Set
#endif
import Data.Tree (Tree(Node), Forest)

-- std interfaces
import Control.Applicative
#if !MIN_VERSION_base(4,8,0)
import qualified Data.Foldable as F
import Data.Traversable
#else
import Data.Foldable as F
#endif
import Control.DeepSeq (NFData(rnf))
import Data.Maybe
import Data.Array
#if USE_UNBOXED_ARRAYS
import qualified Data.Array.Unboxed as UA
import Data.Array.Unboxed ( UArray )
#else
import qualified Data.Array as UA
#endif
import qualified Data.List as L
#if MIN_VERSION_base(4,9,0)
import Data.Functor.Classes
#endif
#if (!MIN_VERSION_base(4,11,0)) && MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup (..))
#endif
#ifdef __GLASGOW_HASKELL__
import GHC.Generics (Generic, Generic1)
import Data.Data (Data)
import Data.Typeable
#endif

-- Make sure we don't use Integer by mistake.
default ()

-------------------------------------------------------------------------
--                                                                      -
--      Strongly Connected Components
--                                                                      -
-------------------------------------------------------------------------

-- | Strongly connected component.
data SCC vertex = AcyclicSCC vertex     -- ^ A single vertex that is not
                                        -- in any cycle.
                | CyclicSCC  [vertex]   -- ^ A maximal set of mutually
                                        -- reachable vertices.
#if __GLASGOW_HASKELL__ >= 802
  deriving ( SCC vertex -> SCC vertex -> Bool
forall vertex. Eq vertex => SCC vertex -> SCC vertex -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SCC vertex -> SCC vertex -> Bool
$c/= :: forall vertex. Eq vertex => SCC vertex -> SCC vertex -> Bool
== :: SCC vertex -> SCC vertex -> Bool
$c== :: forall vertex. Eq vertex => SCC vertex -> SCC vertex -> Bool
Eq   -- ^ @since 0.5.9
           , Int -> SCC vertex -> ShowS
forall vertex. Show vertex => Int -> SCC vertex -> ShowS
forall vertex. Show vertex => [SCC vertex] -> ShowS
forall vertex. Show vertex => SCC vertex -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SCC vertex] -> ShowS
$cshowList :: forall vertex. Show vertex => [SCC vertex] -> ShowS
show :: SCC vertex -> String
$cshow :: forall vertex. Show vertex => SCC vertex -> String
showsPrec :: Int -> SCC vertex -> ShowS
$cshowsPrec :: forall vertex. Show vertex => Int -> SCC vertex -> ShowS
Show -- ^ @since 0.5.9
           , ReadPrec [SCC vertex]
ReadPrec (SCC vertex)
ReadS [SCC vertex]
forall vertex. Read vertex => ReadPrec [SCC vertex]
forall vertex. Read vertex => ReadPrec (SCC vertex)
forall vertex. Read vertex => Int -> ReadS (SCC vertex)
forall vertex. Read vertex => ReadS [SCC vertex]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [SCC vertex]
$creadListPrec :: forall vertex. Read vertex => ReadPrec [SCC vertex]
readPrec :: ReadPrec (SCC vertex)
$creadPrec :: forall vertex. Read vertex => ReadPrec (SCC vertex)
readList :: ReadS [SCC vertex]
$creadList :: forall vertex. Read vertex => ReadS [SCC vertex]
readsPrec :: Int -> ReadS (SCC vertex)
$creadsPrec :: forall vertex. Read vertex => Int -> ReadS (SCC vertex)
Read -- ^ @since 0.5.9
           )
#else
  deriving (Eq, Show, Read)
#endif

INSTANCE_TYPEABLE1(SCC)

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.9
deriving instance Data vertex => Data (SCC vertex)

-- | @since 0.5.9
deriving instance Generic1 SCC

-- | @since 0.5.9
deriving instance Generic (SCC vertex)
#endif

#if MIN_VERSION_base(4,9,0)
-- | @since 0.5.9
instance Eq1 SCC where
  liftEq :: forall a b. (a -> b -> Bool) -> SCC a -> SCC b -> Bool
liftEq a -> b -> Bool
eq (AcyclicSCC a
v1) (AcyclicSCC b
v2) = a -> b -> Bool
eq a
v1 b
v2
  liftEq a -> b -> Bool
eq (CyclicSCC [a]
vs1) (CyclicSCC [b]
vs2) = forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq [a]
vs1 [b]
vs2
  liftEq a -> b -> Bool
_ SCC a
_ SCC b
_ = Bool
False
-- | @since 0.5.9
instance Show1 SCC where
  liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> SCC a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
_sl Int
d (AcyclicSCC a
v) = forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith Int -> a -> ShowS
sp String
"AcyclicSCC" Int
d a
v
  liftShowsPrec Int -> a -> ShowS
_sp [a] -> ShowS
sl Int
d (CyclicSCC [a]
vs) = forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (forall a b. a -> b -> a
const [a] -> ShowS
sl) String
"CyclicSCC" Int
d [a]
vs
-- | @since 0.5.9
instance Read1 SCC where
  liftReadsPrec :: forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (SCC a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$
    forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith Int -> ReadS a
rp String
"AcyclicSCC" forall vertex. vertex -> SCC vertex
AcyclicSCC forall a. Semigroup a => a -> a -> a
<>
    forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall a b. a -> b -> a
const ReadS [a]
rl) String
"CyclicSCC" forall vertex. [vertex] -> SCC vertex
CyclicSCC
#endif

-- | @since 0.5.9
instance F.Foldable SCC where
  foldr :: forall a b. (a -> b -> b) -> b -> SCC a -> b
foldr a -> b -> b
c b
n (AcyclicSCC a
v) = a -> b -> b
c a
v b
n
  foldr a -> b -> b
c b
n (CyclicSCC [a]
vs) = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
c b
n [a]
vs

-- | @since 0.5.9
instance Traversable SCC where
  -- We treat the non-empty cyclic case specially to cut one
  -- fmap application.
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SCC a -> f (SCC b)
traverse a -> f b
f (AcyclicSCC a
vertex) = forall vertex. vertex -> SCC vertex
AcyclicSCC forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
vertex
  traverse a -> f b
_f (CyclicSCC []) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall vertex. [vertex] -> SCC vertex
CyclicSCC [])
  traverse a -> f b
f (CyclicSCC (a
x : [a]
xs)) =
    forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\b
x' [b]
xs' -> forall vertex. [vertex] -> SCC vertex
CyclicSCC (b
x' forall a. a -> [a] -> [a]
: [b]
xs')) (a -> f b
f a
x) (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f [a]
xs)

instance NFData a => NFData (SCC a) where
    rnf :: SCC a -> ()
rnf (AcyclicSCC a
v) = forall a. NFData a => a -> ()
rnf a
v
    rnf (CyclicSCC [a]
vs) = forall a. NFData a => a -> ()
rnf [a]
vs

-- | @since 0.5.4
instance Functor SCC where
    fmap :: forall a b. (a -> b) -> SCC a -> SCC b
fmap a -> b
f (AcyclicSCC a
v) = forall vertex. vertex -> SCC vertex
AcyclicSCC (a -> b
f a
v)
    fmap a -> b
f (CyclicSCC [a]
vs) = forall vertex. [vertex] -> SCC vertex
CyclicSCC (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f [a]
vs)

-- | The vertices of a list of strongly connected components.
flattenSCCs :: [SCC a] -> [a]
flattenSCCs :: forall a. [SCC a] -> [a]
flattenSCCs = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a. SCC a -> [a]
flattenSCC

-- | The vertices of a strongly connected component.
flattenSCC :: SCC vertex -> [vertex]
flattenSCC :: forall a. SCC a -> [a]
flattenSCC (AcyclicSCC vertex
v) = [vertex
v]
flattenSCC (CyclicSCC [vertex]
vs) = [vertex]
vs

-- | The strongly connected components of a directed graph, reverse topologically
-- sorted.
--
-- ==== __Examples__
--
-- > stronglyConnComp [("a",0,[1]),("b",1,[2,3]),("c",2,[1]),("d",3,[3])]
-- >   == [CyclicSCC ["d"],CyclicSCC ["b","c"],AcyclicSCC "a"]
stronglyConnComp
        :: Ord key
        => [(node, key, [key])]
                -- ^ The graph: a list of nodes uniquely identified by keys,
                -- with a list of keys of nodes this node has edges to.
                -- The out-list may contain keys that don't correspond to
                -- nodes of the graph; such edges are ignored.
        -> [SCC node]

stronglyConnComp :: forall key node. Ord key => [(node, key, [key])] -> [SCC node]
stronglyConnComp [(node, key, [key])]
edges0
  = forall a b. (a -> b) -> [a] -> [b]
map forall {vertex} {b} {c}. SCC (vertex, b, c) -> SCC vertex
get_node (forall key node.
Ord key =>
[(node, key, [key])] -> [SCC (node, key, [key])]
stronglyConnCompR [(node, key, [key])]
edges0)
  where
    get_node :: SCC (vertex, b, c) -> SCC vertex
get_node (AcyclicSCC (vertex
n, b
_, c
_)) = forall vertex. vertex -> SCC vertex
AcyclicSCC vertex
n
    get_node (CyclicSCC [(vertex, b, c)]
triples)     = forall vertex. [vertex] -> SCC vertex
CyclicSCC [vertex
n | (vertex
n,b
_,c
_) <- [(vertex, b, c)]
triples]

-- | The strongly connected components of a directed graph, reverse topologically
-- sorted.  The function is the same as 'stronglyConnComp', except that
-- all the information about each node retained.
-- This interface is used when you expect to apply 'SCC' to
-- (some of) the result of 'SCC', so you don't want to lose the
-- dependency information.
--
-- ==== __Examples__
--
-- > stronglyConnCompR [("a",0,[1]),("b",1,[2,3]),("c",2,[1]),("d",3,[3])]
-- >  == [CyclicSCC [("d",3,[3])],CyclicSCC [("b",1,[2,3]),("c",2,[1])],AcyclicSCC ("a",0,[1])]
stronglyConnCompR
        :: Ord key
        => [(node, key, [key])]
                -- ^ The graph: a list of nodes uniquely identified by keys,
                -- with a list of keys of nodes this node has edges to.
                -- The out-list may contain keys that don't correspond to
                -- nodes of the graph; such edges are ignored.
        -> [SCC (node, key, [key])]     -- ^ Reverse topologically sorted

stronglyConnCompR :: forall key node.
Ord key =>
[(node, key, [key])] -> [SCC (node, key, [key])]
stronglyConnCompR [] = []  -- added to avoid creating empty array in graphFromEdges -- SOF
stronglyConnCompR [(node, key, [key])]
edges0
  = forall a b. (a -> b) -> [a] -> [b]
map Tree Int -> SCC (node, key, [key])
decode Forest Int
forest
  where
    (Graph
graph, Int -> (node, key, [key])
vertex_fn,key -> Maybe Int
_) = forall key node.
Ord key =>
[(node, key, [key])]
-> (Graph, Int -> (node, key, [key]), key -> Maybe Int)
graphFromEdges [(node, key, [key])]
edges0
    forest :: Forest Int
forest             = Graph -> Forest Int
scc Graph
graph
    decode :: Tree Int -> SCC (node, key, [key])
decode (Node Int
v []) | Int -> Bool
mentions_itself Int
v = forall vertex. [vertex] -> SCC vertex
CyclicSCC [Int -> (node, key, [key])
vertex_fn Int
v]
                       | Bool
otherwise         = forall vertex. vertex -> SCC vertex
AcyclicSCC (Int -> (node, key, [key])
vertex_fn Int
v)
    decode Tree Int
other = forall vertex. [vertex] -> SCC vertex
CyclicSCC (Tree Int -> [(node, key, [key])] -> [(node, key, [key])]
dec Tree Int
other [])
                 where
                   dec :: Tree Int -> [(node, key, [key])] -> [(node, key, [key])]
dec (Node Int
v Forest Int
ts) [(node, key, [key])]
vs = Int -> (node, key, [key])
vertex_fn Int
v forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Tree Int -> [(node, key, [key])] -> [(node, key, [key])]
dec [(node, key, [key])]
vs Forest Int
ts
    mentions_itself :: Int -> Bool
mentions_itself Int
v = Int
v forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (Graph
graph forall i e. Ix i => Array i e -> i -> e
! Int
v)

-------------------------------------------------------------------------
--                                                                      -
--      Graphs
--                                                                      -
-------------------------------------------------------------------------

-- | Abstract representation of vertices.
type Vertex  = Int
-- | Table indexed by a contiguous set of vertices.
--
-- /Note: This is included for backwards compatibility./
type Table a = Array Vertex a
-- | Adjacency list representation of a graph, mapping each vertex to its
-- list of successors.
type Graph   = Array Vertex [Vertex]
-- | The bounds of an @Array@.
type Bounds  = (Vertex, Vertex)
-- | An edge from the first vertex to the second.
type Edge    = (Vertex, Vertex)

#if !USE_UNBOXED_ARRAYS
type UArray i a = Array i a
#endif

-- | Returns the list of vertices in the graph.
--
-- ==== __Examples__
--
-- > vertices (buildG (0,-1) []) == []
--
-- > vertices (buildG (0,2) [(0,1),(1,2)]) == [0,1,2]
vertices :: Graph -> [Vertex]
vertices :: Graph -> [Int]
vertices  = forall i e. Ix i => Array i e -> [i]
indices

-- | Returns the list of edges in the graph.
--
-- ==== __Examples__
--
-- > edges (buildG (0,-1) []) == []
--
-- > edges (buildG (0,2) [(0,1),(1,2)]) == [(0,1),(1,2)]
edges    :: Graph -> [Edge]
edges :: Graph -> [Edge]
edges Graph
g   = [ (Int
v, Int
w) | Int
v <- Graph -> [Int]
vertices Graph
g, Int
w <- Graph
gforall i e. Ix i => Array i e -> i -> e
!Int
v ]

-- | Build a graph from a list of edges.
--
-- Warning: This function will cause a runtime exception if a vertex in the edge
-- list is not within the given @Bounds@.
--
-- ==== __Examples__
--
-- > buildG (0,-1) [] == array (0,-1) []
-- > buildG (0,2) [(0,1), (1,2)] == array (0,1) [(0,[1]),(1,[2])]
-- > buildG (0,2) [(0,1), (0,2), (1,2)] == array (0,2) [(0,[2,1]),(1,[2]),(2,[])]
buildG :: Bounds -> [Edge] -> Graph
buildG :: Edge -> [Edge] -> Graph
buildG Edge
bounds0 [Edge]
edges0 = forall i e a.
Ix i =>
(e -> a -> e) -> e -> (i, i) -> [(i, a)] -> Array i e
accumArray (forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] Edge
bounds0 [Edge]
edges0

-- | The graph obtained by reversing all edges.
--
-- ==== __Examples__
--
-- > transposeG (buildG (0,2) [(0,1), (1,2)]) == array (0,2) [(0,[]),(1,[0]),(2,[1])]
transposeG  :: Graph -> Graph
transposeG :: Graph -> Graph
transposeG Graph
g = Edge -> [Edge] -> Graph
buildG (forall i e. Array i e -> (i, i)
bounds Graph
g) (Graph -> [Edge]
reverseE Graph
g)

reverseE    :: Graph -> [Edge]
reverseE :: Graph -> [Edge]
reverseE Graph
g   = [ (Int
w, Int
v) | (Int
v, Int
w) <- Graph -> [Edge]
edges Graph
g ]

-- | A table of the count of edges from each node.
--
-- ==== __Examples__
--
-- > outdegree (buildG (0,-1) []) == array (0,-1) []
--
-- > outdegree (buildG (0,2) [(0,1), (1,2)]) == array (0,2) [(0,1),(1,1),(2,0)]
outdegree :: Graph -> Array Vertex Int
-- This is bizarrely lazy. We build an array filled with thunks, instead
-- of actually calculating anything. This is the historical behavior, and I
-- suppose someone *could* be relying on it, but it might be worth finding
-- out. Note that we *can't* be so lazy with indegree.
outdegree :: Graph -> Array Int Int
outdegree  = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) a. Foldable t => t a -> Int
length

-- | A table of the count of edges into each node.
--
-- ==== __Examples__
--
-- > indegree (buildG (0,-1) []) == array (0,-1) []
--
-- > indegree (buildG (0,2) [(0,1), (1,2)]) == array (0,2) [(0,0),(1,1),(2,1)]
indegree :: Graph -> Array Vertex Int
indegree :: Graph -> Array Int Int
indegree Graph
g = forall i e a.
Ix i =>
(e -> a -> e) -> e -> (i, i) -> [(i, a)] -> Array i e
accumArray forall a. Num a => a -> a -> a
(+) Int
0 (forall i e. Array i e -> (i, i)
bounds Graph
g) [(Int
v, Int
1) | (Int
_, [Int]
outs) <- forall i e. Ix i => Array i e -> [(i, e)]
assocs Graph
g, Int
v <- [Int]
outs]

-- | Identical to 'graphFromEdges', except that the return value
-- does not include the function which maps keys to vertices.  This
-- version of 'graphFromEdges' is for backwards compatibility.
graphFromEdges'
        :: Ord key
        => [(node, key, [key])]
        -> (Graph, Vertex -> (node, key, [key]))
graphFromEdges' :: forall key node.
Ord key =>
[(node, key, [key])] -> (Graph, Int -> (node, key, [key]))
graphFromEdges' [(node, key, [key])]
x = (Graph
a,Int -> (node, key, [key])
b) where
    (Graph
a,Int -> (node, key, [key])
b,key -> Maybe Int
_) = forall key node.
Ord key =>
[(node, key, [key])]
-> (Graph, Int -> (node, key, [key]), key -> Maybe Int)
graphFromEdges [(node, key, [key])]
x

-- | Build a graph from a list of nodes uniquely identified by keys,
-- with a list of keys of nodes this node should have edges to.
--
-- This function takes an adjacency list representing a graph with vertices of
-- type @key@ labeled by values of type @node@ and produces a @Graph@-based
-- representation of that list. The @Graph@ result represents the /shape/ of the
-- graph, and the functions describe a) how to retrieve the label and adjacent
-- vertices of a given vertex, and b) how to retrieve a vertex given a key.
--
-- @(graph, nodeFromVertex, vertexFromKey) = graphFromEdges edgeList@
--
-- * @graph :: Graph@ is the raw, array based adjacency list for the graph.
-- * @nodeFromVertex :: Vertex -> (node, key, [key])@ returns the node
--   associated with the given 0-based @Int@ vertex; see /warning/ below.
-- * @vertexFromKey :: key -> Maybe Vertex@ returns the @Int@ vertex for the
--   key if it exists in the graph, @Nothing@ otherwise.
--
-- To safely use this API you must either extract the list of vertices directly
-- from the graph or first call @vertexFromKey k@ to check if a vertex
-- corresponds to the key @k@. Once it is known that a vertex exists you can use
-- @nodeFromVertex@ to access the labelled node and adjacent vertices. See below
-- for examples.
--
-- Note: The out-list may contain keys that don't correspond to nodes of the
-- graph; they are ignored.
--
-- Warning: The @nodeFromVertex@ function will cause a runtime exception if the
-- given @Vertex@ does not exist.
--
-- ==== __Examples__
--
-- An empty graph.
--
-- > (graph, nodeFromVertex, vertexFromKey) = graphFromEdges []
-- > graph = array (0,-1) []
--
-- A graph where the out-list references unspecified nodes (@\'c\'@), these are
-- ignored.
--
-- > (graph, _, _) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c'])]
-- > array (0,1) [(0,[1]),(1,[])]
--
--
-- A graph with 3 vertices: ("a") -> ("b") -> ("c")
--
-- > (graph, nodeFromVertex, vertexFromKey) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c']), ("c", 'c', [])]
-- > graph == array (0,2) [(0,[1]),(1,[2]),(2,[])]
-- > nodeFromVertex 0 == ("a",'a',"b")
-- > vertexFromKey 'a' == Just 0
--
-- Get the label for a given key.
--
-- > let getNodePart (n, _, _) = n
-- > (graph, nodeFromVertex, vertexFromKey) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c']), ("c", 'c', [])]
-- > getNodePart . nodeFromVertex <$> vertexFromKey 'a' == Just "A"
--
graphFromEdges
        :: Ord key
        => [(node, key, [key])]
        -> (Graph, Vertex -> (node, key, [key]), key -> Maybe Vertex)
graphFromEdges :: forall key node.
Ord key =>
[(node, key, [key])]
-> (Graph, Int -> (node, key, [key]), key -> Maybe Int)
graphFromEdges [(node, key, [key])]
edges0
  = (Graph
graph, \Int
v -> Array Int (node, key, [key])
vertex_map forall i e. Ix i => Array i e -> i -> e
! Int
v, key -> Maybe Int
key_vertex)
  where
    max_v :: Int
max_v           = forall (t :: * -> *) a. Foldable t => t a -> Int
length [(node, key, [key])]
edges0 forall a. Num a => a -> a -> a
- Int
1
    bounds0 :: Edge
bounds0         = (Int
0,Int
max_v) :: (Vertex, Vertex)
    sorted_edges :: [(node, key, [key])]
sorted_edges    = forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy forall {a} {a} {c} {a} {c}.
Ord a =>
(a, a, c) -> (a, a, c) -> Ordering
lt [(node, key, [key])]
edges0
    edges1 :: [(Int, (node, key, [key]))]
edges1          = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (,) [Int
0..] [(node, key, [key])]
sorted_edges

    graph :: Graph
graph           = forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array Edge
bounds0 [(,) Int
v (forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe key -> Maybe Int
key_vertex [key]
ks) | (,) Int
v (node
_,    key
_, [key]
ks) <- [(Int, (node, key, [key]))]
edges1]
    key_map :: Array Int key
key_map         = forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array Edge
bounds0 [(,) Int
v key
k                       | (,) Int
v (node
_,    key
k, [key]
_ ) <- [(Int, (node, key, [key]))]
edges1]
    vertex_map :: Array Int (node, key, [key])
vertex_map      = forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array Edge
bounds0 [(Int, (node, key, [key]))]
edges1

    (a
_,a
k1,c
_) lt :: (a, a, c) -> (a, a, c) -> Ordering
`lt` (a
_,a
k2,c
_) = a
k1 forall a. Ord a => a -> a -> Ordering
`compare` a
k2

    -- key_vertex :: key -> Maybe Vertex
    --  returns Nothing for non-interesting vertices
    key_vertex :: key -> Maybe Int
key_vertex key
k   = Int -> Int -> Maybe Int
findVertex Int
0 Int
max_v
                   where
                     findVertex :: Int -> Int -> Maybe Int
findVertex Int
a Int
b | Int
a forall a. Ord a => a -> a -> Bool
> Int
b
                              = forall a. Maybe a
Nothing
                     findVertex Int
a Int
b = case forall a. Ord a => a -> a -> Ordering
compare key
k (Array Int key
key_map forall i e. Ix i => Array i e -> i -> e
! Int
mid) of
                                   Ordering
LT -> Int -> Int -> Maybe Int
findVertex Int
a (Int
midforall a. Num a => a -> a -> a
-Int
1)
                                   Ordering
EQ -> forall a. a -> Maybe a
Just Int
mid
                                   Ordering
GT -> Int -> Int -> Maybe Int
findVertex (Int
midforall a. Num a => a -> a -> a
+Int
1) Int
b
                              where
                                mid :: Int
mid = Int
a forall a. Num a => a -> a -> a
+ (Int
b forall a. Num a => a -> a -> a
- Int
a) forall a. Integral a => a -> a -> a
`div` Int
2

-------------------------------------------------------------------------
--                                                                      -
--      Depth first search
--                                                                      -
-------------------------------------------------------------------------

-- | A spanning forest of the graph, obtained from a depth-first search of
-- the graph starting from each vertex in an unspecified order.
dff          :: Graph -> Forest Vertex
dff :: Graph -> Forest Int
dff Graph
g         = Graph -> [Int] -> Forest Int
dfs Graph
g (Graph -> [Int]
vertices Graph
g)

-- | A spanning forest of the part of the graph reachable from the listed
-- vertices, obtained from a depth-first search of the graph starting at
-- each of the listed vertices in order.
dfs          :: Graph -> [Vertex] -> Forest Vertex
dfs :: Graph -> [Int] -> Forest Int
dfs Graph
g [Int]
vs      = Edge -> Forest Int -> Forest Int
prune (forall i e. Array i e -> (i, i)
bounds Graph
g) (forall a b. (a -> b) -> [a] -> [b]
map (Graph -> Int -> Tree Int
generate Graph
g) [Int]
vs)

generate     :: Graph -> Vertex -> Tree Vertex
generate :: Graph -> Int -> Tree Int
generate Graph
g Int
v  = forall a. a -> [Tree a] -> Tree a
Node Int
v (forall a b. (a -> b) -> [a] -> [b]
map (Graph -> Int -> Tree Int
generate Graph
g) (Graph
gforall i e. Ix i => Array i e -> i -> e
!Int
v))

prune        :: Bounds -> Forest Vertex -> Forest Vertex
prune :: Edge -> Forest Int -> Forest Int
prune Edge
bnds Forest Int
ts = forall a. Edge -> (forall s. SetM s a) -> a
run Edge
bnds (forall s. Forest Int -> SetM s (Forest Int)
chop Forest Int
ts)

chop         :: Forest Vertex -> SetM s (Forest Vertex)
chop :: forall s. Forest Int -> SetM s (Forest Int)
chop []       = forall (m :: * -> *) a. Monad m => a -> m a
return []
chop (Node Int
v Forest Int
ts : Forest Int
us)
              = do
                Bool
visited <- forall s. Int -> SetM s Bool
contains Int
v
                if Bool
visited then
                  forall s. Forest Int -> SetM s (Forest Int)
chop Forest Int
us
                 else do
                  forall s. Int -> SetM s ()
include Int
v
                  Forest Int
as <- forall s. Forest Int -> SetM s (Forest Int)
chop Forest Int
ts
                  Forest Int
bs <- forall s. Forest Int -> SetM s (Forest Int)
chop Forest Int
us
                  forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> [Tree a] -> Tree a
Node Int
v Forest Int
as forall a. a -> [a] -> [a]
: Forest Int
bs)

-- A monad holding a set of vertices visited so far.
#if USE_ST_MONAD

-- Use the ST monad if available, for constant-time primitives.

#if USE_UNBOXED_ARRAYS
newtype SetM s a = SetM { forall s a. SetM s a -> STUArray s Int Bool -> ST s a
runSetM :: STUArray s Vertex Bool -> ST s a }
#else
newtype SetM s a = SetM { runSetM :: STArray  s Vertex Bool -> ST s a }
#endif

instance Monad (SetM s) where
    return :: forall a. a -> SetM s a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
    {-# INLINE return #-}
    SetM STUArray s Int Bool -> ST s a
v >>= :: forall a b. SetM s a -> (a -> SetM s b) -> SetM s b
>>= a -> SetM s b
f = forall s a. (STUArray s Int Bool -> ST s a) -> SetM s a
SetM forall a b. (a -> b) -> a -> b
$ \STUArray s Int Bool
s -> do { a
x <- STUArray s Int Bool -> ST s a
v STUArray s Int Bool
s; forall s a. SetM s a -> STUArray s Int Bool -> ST s a
runSetM (a -> SetM s b
f a
x) STUArray s Int Bool
s }
    {-# INLINE (>>=) #-}

instance Functor (SetM s) where
    a -> b
f fmap :: forall a b. (a -> b) -> SetM s a -> SetM s b
`fmap` SetM STUArray s Int Bool -> ST s a
v = forall s a. (STUArray s Int Bool -> ST s a) -> SetM s a
SetM forall a b. (a -> b) -> a -> b
$ \STUArray s Int Bool
s -> a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` STUArray s Int Bool -> ST s a
v STUArray s Int Bool
s
    {-# INLINE fmap #-}

instance Applicative (SetM s) where
    pure :: forall a. a -> SetM s a
pure a
x = forall s a. (STUArray s Int Bool -> ST s a) -> SetM s a
SetM forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const (forall (m :: * -> *) a. Monad m => a -> m a
return a
x)
    {-# INLINE pure #-}
    SetM STUArray s Int Bool -> ST s (a -> b)
f <*> :: forall a b. SetM s (a -> b) -> SetM s a -> SetM s b
<*> SetM STUArray s Int Bool -> ST s a
v = forall s a. (STUArray s Int Bool -> ST s a) -> SetM s a
SetM forall a b. (a -> b) -> a -> b
$ \STUArray s Int Bool
s -> STUArray s Int Bool -> ST s (a -> b)
f STUArray s Int Bool
s forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` STUArray s Int Bool -> ST s a
v STUArray s Int Bool
s)
    -- We could also use the following definition
    --   SetM f <*> SetM v = SetM $ \s -> f s <*> v s
    -- but Applicative (ST s) instance is present only in GHC 7.2+
    {-# INLINE (<*>) #-}

run          :: Bounds -> (forall s. SetM s a) -> a
run :: forall a. Edge -> (forall s. SetM s a) -> a
run Edge
bnds forall s. SetM s a
act  = forall a. (forall s. ST s a) -> a
runST (forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray Edge
bnds Bool
False forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s a. SetM s a -> STUArray s Int Bool -> ST s a
runSetM forall s. SetM s a
act)

contains     :: Vertex -> SetM s Bool
contains :: forall s. Int -> SetM s Bool
contains Int
v    = forall s a. (STUArray s Int Bool -> ST s a) -> SetM s a
SetM forall a b. (a -> b) -> a -> b
$ \ STUArray s Int Bool
m -> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Bool
m Int
v

include      :: Vertex -> SetM s ()
include :: forall s. Int -> SetM s ()
include Int
v     = forall s a. (STUArray s Int Bool -> ST s a) -> SetM s a
SetM forall a b. (a -> b) -> a -> b
$ \ STUArray s Int Bool
m -> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Bool
m Int
v Bool
True

#else /* !USE_ST_MONAD */

-- Portable implementation using IntSet.

newtype SetM s a = SetM { runSetM :: IntSet -> (a, IntSet) }

instance Monad (SetM s) where
    return x     = SetM $ \s -> (x, s)
    SetM v >>= f = SetM $ \s -> case v s of (x, s') -> runSetM (f x) s'

instance Functor (SetM s) where
    f `fmap` SetM v = SetM $ \s -> case v s of (x, s') -> (f x, s')
    {-# INLINE fmap #-}

instance Applicative (SetM s) where
    pure x = SetM $ \s -> (x, s)
    {-# INLINE pure #-}
    SetM f <*> SetM v = SetM $ \s -> case f s of (k, s') -> case v s' of (x, s'') -> (k x, s'')
    {-# INLINE (<*>) #-}

run          :: Bounds -> SetM s a -> a
run _ act     = fst (runSetM act Set.empty)

contains     :: Vertex -> SetM s Bool
contains v    = SetM $ \ m -> (Set.member v m, m)

include      :: Vertex -> SetM s ()
include v     = SetM $ \ m -> ((), Set.insert v m)

#endif /* !USE_ST_MONAD */

-------------------------------------------------------------------------
--                                                                      -
--      Algorithms
--                                                                      -
-------------------------------------------------------------------------

------------------------------------------------------------
-- Algorithm 1: depth first search numbering
------------------------------------------------------------

preorder' :: Tree a -> [a] -> [a]
preorder' :: forall a. Tree a -> [a] -> [a]
preorder' (Node a
a [Tree a]
ts) = (a
a forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Forest a -> [a] -> [a]
preorderF' [Tree a]
ts

preorderF' :: Forest a -> [a] -> [a]
preorderF' :: forall a. Forest a -> [a] -> [a]
preorderF' Forest a
ts = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) forall a. a -> a
id forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Tree a -> [a] -> [a]
preorder' Forest a
ts

preorderF :: Forest a -> [a]
preorderF :: forall a. Forest a -> [a]
preorderF Forest a
ts = forall a. Forest a -> [a] -> [a]
preorderF' Forest a
ts []

tabulate        :: Bounds -> [Vertex] -> UArray Vertex Int
tabulate :: Edge -> [Int] -> UArray Int Int
tabulate Edge
bnds [Int]
vs = forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [(i, e)] -> a i e
UA.array Edge
bnds (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (forall a b c. (a -> b -> c) -> b -> a -> c
flip (,)) [Int
1..] [Int]
vs)
-- Why zipWith (flip (,)) instead of just using zip with the
-- arguments in the other order? We want the [1..] to fuse
-- away, and these days that only happens when it's the first
-- list argument.

preArr          :: Bounds -> Forest Vertex -> UArray Vertex Int
preArr :: Edge -> Forest Int -> UArray Int Int
preArr Edge
bnds      = Edge -> [Int] -> UArray Int Int
tabulate Edge
bnds forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Forest a -> [a]
preorderF

------------------------------------------------------------
-- Algorithm 2: topological sorting
------------------------------------------------------------

postorder :: Tree a -> [a] -> [a]
postorder :: forall a. Tree a -> [a] -> [a]
postorder (Node a
a [Tree a]
ts) = forall a. Forest a -> [a] -> [a]
postorderF [Tree a]
ts forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
a forall a. a -> [a] -> [a]
:)

postorderF   :: Forest a -> [a] -> [a]
postorderF :: forall a. Forest a -> [a] -> [a]
postorderF Forest a
ts = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) forall a. a -> a
id forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Tree a -> [a] -> [a]
postorder Forest a
ts

postOrd :: Graph -> [Vertex]
postOrd :: Graph -> [Int]
postOrd Graph
g = forall a. Forest a -> [a] -> [a]
postorderF (Graph -> Forest Int
dff Graph
g) []

-- | A topological sort of the graph.
-- The order is partially specified by the condition that a vertex /i/
-- precedes /j/ whenever /j/ is reachable from /i/ but not vice versa.
topSort      :: Graph -> [Vertex]
topSort :: Graph -> [Int]
topSort       = forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph -> [Int]
postOrd

-- | Reverse ordering of `topSort`.
--
-- @since 0.6.4
reverseTopSort :: Graph -> [Vertex]
reverseTopSort :: Graph -> [Int]
reverseTopSort = Graph -> [Int]
postOrd

------------------------------------------------------------
-- Algorithm 3: connected components
------------------------------------------------------------

-- | The connected components of a graph.
-- Two vertices are connected if there is a path between them, traversing
-- edges in either direction.
components   :: Graph -> Forest Vertex
components :: Graph -> Forest Int
components    = Graph -> Forest Int
dff forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph -> Graph
undirected

undirected   :: Graph -> Graph
undirected :: Graph -> Graph
undirected Graph
g  = Edge -> [Edge] -> Graph
buildG (forall i e. Array i e -> (i, i)
bounds Graph
g) (Graph -> [Edge]
edges Graph
g forall a. [a] -> [a] -> [a]
++ Graph -> [Edge]
reverseE Graph
g)

-- Algorithm 4: strongly connected components

-- | The strongly connected components of a graph, in reverse topological order.
--
-- ==== __Examples__
--
-- > scc (buildG (0,3) [(3,1),(1,2),(2,0),(0,1)])
-- >   == [Node {rootLabel = 0, subForest = [Node {rootLabel = 1, subForest = [Node {rootLabel = 2, subForest = []}]}]}
-- >      ,Node {rootLabel = 3, subForest = []}]

scc  :: Graph -> Forest Vertex
scc :: Graph -> Forest Int
scc Graph
g = Graph -> [Int] -> Forest Int
dfs Graph
g (forall a. [a] -> [a]
reverse (Graph -> [Int]
postOrd (Graph -> Graph
transposeG Graph
g)))

------------------------------------------------------------
-- Algorithm 5: Classifying edges
------------------------------------------------------------

{-
XXX unused code

tree              :: Bounds -> Forest Vertex -> Graph
tree bnds ts       = buildG bnds (concat (map flat ts))
 where flat (Node v ts') = [ (v, w) | Node w _us <- ts' ]
                        ++ concat (map flat ts')

back              :: Graph -> Table Int -> Graph
back g post        = mapT select g
 where select v ws = [ w | w <- ws, post!v < post!w ]

cross             :: Graph -> Table Int -> Table Int -> Graph
cross g pre post   = mapT select g
 where select v ws = [ w | w <- ws, post!v > post!w, pre!v > pre!w ]

forward           :: Graph -> Graph -> Table Int -> Graph
forward g tree' pre = mapT select g
 where select v ws = [ w | w <- ws, pre!v < pre!w ] \\ tree' ! v

mapT    :: (Vertex -> a -> b) -> Array Vertex a -> Array Vertex b
mapT f t = array (bounds t) [ (,) v (f v (t!v)) | v <- indices t ]
-}

------------------------------------------------------------
-- Algorithm 6: Finding reachable vertices
------------------------------------------------------------

-- | Returns the list of vertices reachable from a given vertex.
--
-- ==== __Examples__
--
-- > reachable (buildG (0,0) []) 0 == [0]
--
-- > reachable (buildG (0,2) [(0,1), (1,2)]) 0 == [0,1,2]
reachable :: Graph -> Vertex -> [Vertex]
reachable :: Graph -> Int -> [Int]
reachable Graph
g Int
v = forall a. Forest a -> [a]
preorderF (Graph -> [Int] -> Forest Int
dfs Graph
g [Int
v])

-- | Returns @True@ if the second vertex reachable from the first.
--
-- ==== __Examples__
--
-- > path (buildG (0,0) []) 0 0 == True
--
-- > path (buildG (0,2) [(0,1), (1,2)]) 0 2 == True
--
-- > path (buildG (0,2) [(0,1), (1,2)]) 2 0 == False
path :: Graph -> Vertex -> Vertex -> Bool
path :: Graph -> Int -> Int -> Bool
path Graph
g Int
v Int
w    = Int
w forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (Graph -> Int -> [Int]
reachable Graph
g Int
v)

------------------------------------------------------------
-- Algorithm 7: Biconnected components
------------------------------------------------------------

-- | The biconnected components of a graph.
-- An undirected graph is biconnected if the deletion of any vertex
-- leaves it connected.
bcc :: Graph -> Forest [Vertex]
bcc :: Graph -> Forest [Int]
bcc Graph
g = (forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Tree (Int, Int, Int) -> Forest [Int]
bicomps forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (Graph -> UArray Int Int -> Tree Int -> Tree (Int, Int, Int)
do_label Graph
g UArray Int Int
dnum)) Forest Int
forest
 where forest :: Forest Int
forest = Graph -> Forest Int
dff Graph
g
       dnum :: UArray Int Int
dnum   = Edge -> Forest Int -> UArray Int Int
preArr (forall i e. Array i e -> (i, i)
bounds Graph
g) Forest Int
forest

do_label :: Graph -> UArray Vertex Int -> Tree Vertex -> Tree (Vertex,Int,Int)
do_label :: Graph -> UArray Int Int -> Tree Int -> Tree (Int, Int, Int)
do_label Graph
g UArray Int Int
dnum (Node Int
v Forest Int
ts) = forall a. a -> [Tree a] -> Tree a
Node (Int
v, UArray Int Int
dnum forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
UA.! Int
v, Int
lv) [Tree (Int, Int, Int)]
us
 where us :: [Tree (Int, Int, Int)]
us = forall a b. (a -> b) -> [a] -> [b]
map (Graph -> UArray Int Int -> Tree Int -> Tree (Int, Int, Int)
do_label Graph
g UArray Int Int
dnum) Forest Int
ts
       lv :: Int
lv = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum ([UArray Int Int
dnum forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
UA.! Int
v] forall a. [a] -> [a] -> [a]
++ [UArray Int Int
dnum forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
UA.! Int
w | Int
w <- Graph
gforall i e. Ix i => Array i e -> i -> e
!Int
v]
                     forall a. [a] -> [a] -> [a]
++ [Int
lu | Node (Int
_,Int
_,Int
lu) [Tree (Int, Int, Int)]
_ <- [Tree (Int, Int, Int)]
us])

bicomps :: Tree (Vertex,Int,Int) -> Forest [Vertex]
bicomps :: Tree (Int, Int, Int) -> Forest [Int]
bicomps (Node (Int
v,Int
_,Int
_) [Tree (Int, Int, Int)]
ts)
      = [ forall a. a -> [Tree a] -> Tree a
Node (Int
vforall a. a -> [a] -> [a]
:[Int]
vs) Forest [Int]
us | (Int
_,Node [Int]
vs Forest [Int]
us) <- forall a b. (a -> b) -> [a] -> [b]
map Tree (Int, Int, Int) -> (Int, Tree [Int])
collect [Tree (Int, Int, Int)]
ts]

collect :: Tree (Vertex,Int,Int) -> (Int, Tree [Vertex])
collect :: Tree (Int, Int, Int) -> (Int, Tree [Int])
collect (Node (Int
v,Int
dv,Int
lv) [Tree (Int, Int, Int)]
ts) = (Int
lv, forall a. a -> [Tree a] -> Tree a
Node (Int
vforall a. a -> [a] -> [a]
:[Int]
vs) Forest [Int]
cs)
 where collected :: [(Int, Tree [Int])]
collected = forall a b. (a -> b) -> [a] -> [b]
map Tree (Int, Int, Int) -> (Int, Tree [Int])
collect [Tree (Int, Int, Int)]
ts
       vs :: [Int]
vs = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [Int]
ws | (Int
lw, Node [Int]
ws Forest [Int]
_) <- [(Int, Tree [Int])]
collected, Int
lwforall a. Ord a => a -> a -> Bool
<Int
dv]
       cs :: Forest [Int]
cs = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ if Int
lwforall a. Ord a => a -> a -> Bool
<Int
dv then Forest [Int]
us else [forall a. a -> [Tree a] -> Tree a
Node (Int
vforall a. a -> [a] -> [a]
:[Int]
ws) Forest [Int]
us]
                        | (Int
lw, Node [Int]
ws Forest [Int]
us) <- [(Int, Tree [Int])]
collected ]