summaryrefslogtreecommitdiff
path: root/src/Data/Graph/Dynamic/EulerTour.hs
blob: adbe230aa2c26c7bbf9e9b5b603859fc916980d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
-- | This module provides dynamic connectivity for an acyclic graph (i.e. a
-- forest).
--
-- It is based on:
-- /Finding biconnected components and computing tree functions in logarithmic parallel time/
-- by /Robert E. Tarjan and Uzi Vishki/ (1984).
--
-- We use two naming conventions in this module:
--
-- * A prime suffix (@'@) indicates a simpler or less polymorphic version of a
-- function or datatype.  For example, see 'empty' and 'empty'', and
-- 'Graph' and 'Graph''.
--
-- * An underscore suffix (@_@) means that the return value is ignored.  For
-- example, see 'link' and 'link_'.
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE RecordWildCards     #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Graph.Dynamic.EulerTour
    ( -- * Type
      Forest
    , Graph
    , Graph'

      -- * Construction
    , empty
    , empty'
    , edgeless
    , edgeless'
    , fromTree
    , fromTree'

      -- * Queries
    , connected
    , edge
    , vertex
    , neighbours

      -- * Modifying
    , link
    , link_
    , cut
    , cut_
    , insert
    , insert_
    , delete
    , delete_

      -- * Advanced/internal operations
    , findRoot
    , componentSize
    , spanningForest

      -- * Debugging
    , print
    ) where

import           Control.Monad                         (filterM, foldM, forM_,
                                                        void)
import           Control.Monad.Primitive
import qualified Data.Graph.Dynamic.Internal.HashTable as HT
import qualified Data.Graph.Dynamic.Internal.Random    as Random
import qualified Data.Graph.Dynamic.Internal.Tree      as Tree
import           Data.Hashable                         (Hashable)
import qualified Data.HashMap.Strict                   as HMS
import qualified Data.HashSet                          as HS
import qualified Data.List.NonEmpty                    as NonEmpty
import           Data.Maybe
import           Data.Monoid
import           Data.Proxy                            (Proxy (..))
import qualified Data.Tree                             as DT
import           Prelude                               hiding (print)

-- | The most general type for an Euler Tour Forest.  Used by other modules.
data Forest t a s v = ETF
    { edges :: {-# UNPACK#-} !(HT.HashTable s v (HMS.HashMap v (t s (v, v) a)))
    , toMonoid :: v -> v -> a
    , treeGen :: (Tree.TreeGen t s)
    }

-- | Graph type polymorphic in the tree used to represent sequences.
type Graph t s v = Forest t () s v

-- | Simple graph type.
type Graph' s v = Graph Random.Tree s v

insertTree
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, s ~ PrimState m)
    => Forest t a s v -> v -> v -> t s (v, v) a -> m ()
insertTree (ETF ht _ _) x y t = do
    mbMap <- HT.lookup ht x
    case mbMap of
        Nothing -> HT.insert ht x $ HMS.singleton y t
        Just m  -> HT.insert ht x $ HMS.insert y t m

lookupTree
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, s ~ PrimState m)
    => Forest t a s v -> v -> v -> m (Maybe (t s (v, v) (a)))
lookupTree (ETF ht _ _) x y = do
    mbMap <- HT.lookup ht x
    case mbMap of
        Nothing -> return Nothing
        Just m  -> return $ HMS.lookup y m

deleteTree
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, s ~ PrimState m)
    => Forest t a s v -> v -> v -> m ()
deleteTree (ETF ht _ _) x y = do
    mbMap <- HT.lookup ht x
    case mbMap of
        Nothing -> return ()
        Just m0 ->
            let m1 = HMS.delete y m0 in
            if HMS.null m1 then HT.delete ht x else HT.insert ht x m1

-- | /O(1)/
--
-- Create the empty tree.
empty
    :: forall t m v a. (Tree.Tree t, PrimMonad m)
    => (v -> v -> a) -> m (Forest t a (PrimState m) v)
empty f = do
  ht <- HT.new
  tg <- Tree.newTreeGen (Proxy :: Proxy t)
  return $ ETF ht f tg

-- | Simple version of 'empty'.
empty'
    :: PrimMonad m => m (Graph' (PrimState m) v)
empty' = empty (\_ _ -> ())

-- | /O(v*log(v))/
--
-- Create a graph with the given vertices but no edges.
edgeless
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => (v -> v -> a) -> [v] -> m (Forest t a (PrimState m) v)
edgeless toMonoid vs = do
    etf <- empty toMonoid
    forM_ vs $ \v -> do
        node <- Tree.singleton (treeGen etf) (v, v) (toMonoid v v)
        insertTree etf v v node
    return etf

-- | Simple version of 'edgeless'.
edgeless'
    :: (Eq v, Hashable v, PrimMonad m)
    => [v] -> m (Graph' (PrimState m) v)
edgeless' = edgeless (\_ _ -> ())

-- | Create a graph from a 'DT.Tree'.  Note that the values in nodes must be
-- unique.
fromTree
    :: forall v m t a. (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => (v -> v -> a) -> DT.Tree v -> m (Forest t a (PrimState m) v)
fromTree toMonoid tree = do
    etf <- empty toMonoid
    _ <- go etf tree
    return etf
  where
    go etf (DT.Node l children) = do
      node0 <- Tree.singleton (treeGen etf) (l, l) (toMonoid l l)
      insertTree etf l l node0
      foldM (go' etf l) node0 children

    go' etf parent node0 tr@(DT.Node l _) = do
      lnode     <- go etf tr
      parentToL <- Tree.singleton (treeGen etf) (parent, l) (toMonoid parent l)
      lToParent <- Tree.singleton (treeGen etf) (l, parent) (toMonoid l parent)

      node1 <- Tree.concat $ node0 NonEmpty.:| [parentToL, lnode, lToParent]
      insertTree etf l parent lToParent
      insertTree etf parent l parentToL
      return node1

-- | Simple version of 'fromTree'.
fromTree'
    :: (Eq v, Hashable v, PrimMonad m)
    => DT.Tree v -> m (Graph' (PrimState m) v)
fromTree' = fromTree (\_ _ -> ())

findRoot
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, s ~ PrimState m, Monoid a)
    => Forest t a s v -> v -> m (Maybe (t s (v, v) a))
findRoot etf v = do
    mbTree <- lookupTree etf v v
    case mbTree of
        Nothing -> return Nothing
        Just t  -> Just <$> Tree.root t

-- | /O(log(v))/
--
-- Remove an edge in between two vertices.  If there is no edge in between
-- these vertices, do nothing.  Return whether or not an edge was actually
-- removed.
cut
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> v -> m Bool
cut etf a b = do
  mbAb <- lookupTree etf a b
  mbBa <- lookupTree etf b a
  case (mbAb, mbBa) of
    _ | a == b -> return False -- Can't cut self-loops
    (Just ab, Just ba) -> do
      (part1, part2) <- Tree.split ab

      baIsInPart1 <- case part1 of
        Just p -> Tree.connected p ba
        _      -> return False

      (mbL, _, mbR) <- if baIsInPart1 then do
        (part3, part4) <- Tree.split ba
        return (part3, part4, part2)
      else do
        (part3, part4) <- Tree.split ba
        return (part1, part3, part4)

      _ <- sequenceA $ Tree.append <$> mbL <*> mbR
      deleteTree etf a b
      deleteTree etf b a
      return True

    (Nothing, _) -> return False -- No edge to cut
    (_, Nothing) -> return False -- No edge to cut

-- | Version of 'cut' which ignores the result.
cut_
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> v -> m ()
cut_ etf a b = void (cut etf a b)

-- | reroot the represented tree by shifting the euler tour.  Returns the new
-- root.
reroot
    :: (Tree.Tree t, PrimMonad m, s ~ PrimState m, Monoid v)
    => t s a v -> m (t s a v)
reroot t = do
    (mbPre, mbPost) <- Tree.split t
    t1 <- maybe (return t) (t `Tree.cons`) mbPost
    maybe (return t1) (t1 `Tree.append`) mbPre

-- | /O(log(v))/
--
-- Check if this edge exists in the graph.
edge
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m)
    => Forest t a (PrimState m) v -> v -> v -> m Bool
edge etf a b = isJust <$> lookupTree etf a b

-- | /O(log(v))/
--
-- Check if this vertex exists in the graph.
vertex
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m)
    => Forest t a (PrimState m) v -> v -> m Bool
vertex etf a = isJust <$> lookupTree etf a a

-- | /O(log(v))/
--
-- Check if a path exists in between two vertices.
connected
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> v -> m Bool
connected etf a b = do
  mbALoop <- lookupTree etf a a
  mbBLoop <- lookupTree etf b b
  case (mbALoop, mbBLoop) of
    (Just aLoop, Just bLoop) -> Tree.connected aLoop bLoop
    _                        -> return False

-- | /O(log(v))/
--
-- Insert an edge in between two vertices.  If the vertices are already
-- connected, we don't do anything, since this is an acyclic graph.  Returns
-- whether or not an edge was actually inserted.
link
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> v -> m Bool
link etf@ETF{..} a b = do
  mbALoop <- lookupTree etf a a
  mbBLoop <- lookupTree etf b b
  case (mbALoop, mbBLoop) of
    (Just aLoop, Just bLoop) -> Tree.connected aLoop bLoop >>= \case
        True -> return False
        False -> do

          bLoop1            <- reroot bLoop
          abNode            <- Tree.singleton treeGen (a, b) (toMonoid a b)
          baNode            <- Tree.singleton treeGen (b, a) (toMonoid b a)
          bLoop2            <- abNode `Tree.cons` bLoop1
          bLoop3            <- bLoop2 `Tree.snoc` baNode
          (mbPreA, mbPostA) <- Tree.split aLoop

          _ <- Tree.concat $
            aLoop NonEmpty.:| catMaybes
            [ Just bLoop3
            , mbPostA
            , mbPreA
            ]

          insertTree etf a b abNode
          insertTree etf b a baNode
          return True

    _ -> return False

-- | Version of 'link' which ignores the result.
link_
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> v -> m ()
link_ etf a b = void (link etf a b)

-- | /O(log(v))/
--
-- Insert a new vertex.  Do nothing if it is already there.  Returns whether
-- or not a vertex was inserted in the graph.
insert
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> m Bool
insert etf@ETF{..} v = do
    mbTree <- lookupTree etf v v
    case mbTree of
        Just  _ -> return False
        Nothing -> do
            node <- Tree.singleton treeGen (v, v) (toMonoid v v)
            insertTree etf v v node
            return True

-- | Version of 'insert' which ignores the result.
insert_
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> m ()
insert_ etf v = void (insert etf v)

-- | /O(log(v) + n/ where /n/ is the number of neighbours
--
-- Get all neighbours of the given vertex.
neighbours
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> m [v]
neighbours etf x = fromMaybe [] <$> maybeNeighbours etf x

maybeNeighbours
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> m (Maybe [v])
maybeNeighbours (ETF ht _ _) x = do
    mbMap <- HT.lookup ht x
    case mbMap of
        Nothing -> return Nothing
        Just m  -> return $ Just $ filter (/= x) $ map fst $ HMS.toList m

-- | /O(n*log(v))/ where /n/ is the number of neighbours
--
-- Remove a vertex from the graph, if it exists.  If it is connected to any
-- other vertices, those edges are cut first.  Returns whether or not a vertex
-- was removed from the graph.
delete
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> m Bool
delete etf x = do
    mbNbs <- maybeNeighbours etf x
    case mbNbs of
        Nothing  -> return False
        Just nbs -> do
            forM_ nbs $ \y -> cut etf x y
            deleteTree etf x x
            return True

-- | Version of 'delete' which ignores the result.
delete_
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, Monoid a)
    => Forest t a (PrimState m) v -> v -> m ()
delete_ etf x = void (delete etf x)

print :: (Show a, Monoid b, Tree.TestTree t) => Forest t b RealWorld a -> IO ()
print (ETF ht _ _) = do
    maps <- map snd <$> HT.toList ht
    let trees = concatMap (map snd . HMS.toList) maps
    comps <- components trees
    forM_ comps $ \comp -> do
        root <- Tree.root comp
        Tree.print root
        putStrLn ""
  where
    components [] = return []
    components (t : ts) = do
        ts' <- filterM (fmap not . Tree.connected t) ts
        (t :) <$> components ts'

componentSize
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m, s ~ PrimState m)
    => Forest t (Sum Int) s v -> v -> m Int
componentSize etf v = do
  mbTree <- lookupTree etf v v
  case mbTree of
    Nothing -> return 0
    Just tree -> do
      root <- Tree.root tree
      getSum <$> Tree.aggregate root

-- | Obtain the current spanning forest.
spanningForest
    :: (Eq v, Hashable v, Tree.Tree t, Monoid a, PrimMonad m)
    => Forest t a (PrimState m) v -> m (DT.Forest v)
spanningForest (ETF ht _ _) = do
    maps <- map snd <$> HT.toList ht
    let trees = concatMap (map snd . HMS.toList) maps
    go HS.empty [] trees
  where
    go _visited acc [] = return acc
    go visited acc (t : ts) = do
        root  <- Tree.readRoot t
        label <- Tree.label root
        if HS.member label visited then
            go visited acc ts
        else do
            st <- spanningTree root
            go (HS.insert label visited) (st : acc) ts

spanningTree
    :: (Eq v, Hashable v, PrimMonad m, Monoid e, Tree.Tree t)
    => t (PrimState m) (v, v) e -> m (DT.Tree v)
spanningTree tree = do
    list <- Tree.toList tree
    case list of
        ((r, _) : _) -> return $ DT.Node r (fst $ go Nothing [] list)
        _            -> fail
            "Data.Graph.Dynamic..EulerTour.spanningTree: empty list"
  where
    go _mbParent acc []      = (acc, [])
    go mbParent acc ((a, b) : edges)
        | a == b             = go mbParent acc edges  -- Ignore self-loops.
        | Just b == mbParent = (acc, edges)  -- Like a closing bracket.
        | otherwise          =
            -- Parse child.
            let (child, rest) = go (Just a) [] edges in
            go mbParent (DT.Node b child : acc) rest