summaryrefslogtreecommitdiff
path: root/src/Data/Graph/Dynamic/Levels.hs
blob: cce43660b6fe9fe733fb406e0332dc9fe1d169c9 (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
-- | This module implements full dynamic grah connectivity.
--
-- It is based on:
-- /Poly-logarithmic deterministic fully-dynamic algorithms for connectivity, minimum spanning tree, 2-edge, and biconnectivity/
-- by /Jacob Holm, Kristian de Lichtenberg and Mikkel Thorup/ (1998).
--
-- 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 BangPatterns        #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE MultiWayIf          #-}
{-# LANGUAGE RecordWildCards     #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Graph.Dynamic.Levels
    ( -- * Type
      Graph
    , Graph'

      -- * Construction
    , empty
    , empty'
    , edgeless
    , edgeless'
    , complete
    , complete'

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

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

      -- * Advanced/internal
    , spanningForest
    ) where

import           Control.Monad
import           Control.Monad.Primitive
import           Data.Bits
import           Data.Hashable                      (Hashable)
import qualified Data.HashMap.Strict                as HMS
import qualified Data.HashSet                       as HS
import qualified Data.List                          as L
import           Data.Maybe                         (fromMaybe)
import           Data.Monoid
import           Data.Primitive.MutVar
import qualified Data.Tree                          as DT
import qualified Data.Vector.Mutable                as VM

import qualified Data.Graph.Dynamic.EulerTour       as ET
import qualified Data.Graph.Dynamic.Internal.Random as Random
import           Data.Graph.Dynamic.Internal.Tree   (Tree)
import qualified Data.Graph.Dynamic.Internal.Tree   as Tree

type EdgeSet v = HMS.HashMap v (HS.HashSet v)

linkEdgeSet :: (Eq v, Hashable v) => v -> v -> EdgeSet v -> EdgeSet v
linkEdgeSet x y =
    HMS.insertWith HS.union x (HS.singleton y) .
    HMS.insertWith HS.union y (HS.singleton x)

cutEdgeSet :: (Eq v, Hashable v) => v -> v -> EdgeSet v -> EdgeSet v
cutEdgeSet x y = HMS.adjust (HS.delete x) y . HMS.adjust (HS.delete y) x

memberEdgeSet :: (Eq v, Hashable v) => v -> v -> EdgeSet v -> Bool
memberEdgeSet x y = maybe False (y `HS.member`) . HMS.lookup x

data L t s v = L
  { numVerts :: !Int
  , allEdges :: !(EdgeSet v)
  , unLevels :: !(VM.MVector s (ET.Forest t (Sum Int) s v, EdgeSet v))
  }

newtype Graph t s v = Graph (MutVar s (L t s v))

type Graph' s v = Graph Random.Tree s v

logBase2 :: Int -> Int
logBase2 x = finiteBitSize x - 1 - countLeadingZeros x

-- | /O(1)/
--
-- Create an empty graph.
empty :: (Eq v, Hashable v, Tree t, PrimMonad m) => m (Graph t (PrimState m) v)
empty = edgeless []

-- | Simple version of 'empty'.
empty' :: (Eq v, Hashable v, PrimMonad m) => m (Graph' (PrimState m) v)
empty' = empty

-- | Create a graph with the given vertices but no edges.
edgeless
  :: (Eq v, Hashable v, Tree t, PrimMonad m)
  => [v] -> m (Graph t (PrimState m) v)
edgeless xs = do
  unLevels <- VM.new 0
  let allEdges = HMS.empty
      numVerts = 0
  g <- Graph <$> newMutVar L {..}
  mapM_ (insert g) xs
  return g

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

-- | Create the complete graph with the given vertices.
complete
    :: (Eq v, Hashable v, Tree t, PrimMonad m)
    => [v] -> m (Graph t (PrimState m) v)
complete vertices = do
    g <- edgeless vertices
    forM_ (pairs vertices) $ \(x, y) -> link g x y
    return g
  where
    pairs :: [a] -> [(a, a)]
    pairs []       = []
    pairs (x : xs) =
        [(x, y) | y <- xs] ++ pairs xs

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

-- | /O(log(v))/
--
-- Insert an edge in between two vertices.  If the vertices already have
-- an edge between them don't do anything.  Returns whether or not an edge was
-- actually inserted.
link
    :: (Eq v, Hashable v, Tree t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> v -> m Bool
link (Graph levels) a b = do
  L {..} <- readMutVar levels
  let !newAllEdges = linkEdgeSet a b allEdges
  if memberEdgeSet a b allEdges || a == b || VM.null unLevels
    then return False
    else do
      (thisEtf, thisNonTreeEdges) <- VM.read unLevels 0
      isTreeEdge <- ET.link thisEtf a b
      let !thisNonTreeEdges'
            | isTreeEdge = thisNonTreeEdges
            | otherwise  = linkEdgeSet a b thisNonTreeEdges

      VM.write unLevels 0 (thisEtf, thisNonTreeEdges')
      writeMutVar levels $ L
          {allEdges = newAllEdges, unLevels = unLevels, numVerts = numVerts}
      return True

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

-- | /O(log(v))/
--
-- Check if a path exists in between two vertices.
connected
    :: (Eq v, Hashable v, Tree t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> v -> m Bool
connected _ a b | a == b = return True
connected (Graph levels) a b = do
  L {..} <- readMutVar levels
  if VM.null unLevels
    then return False
    else do
      (etf, _) <- VM.read unLevels 0
      ET.connected etf a b

-- | Check if this edge exists in the graph.
edge
    :: (Eq v, Hashable v, Tree t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> v -> m Bool
edge (Graph levels) a b = do
    L {..} <- readMutVar levels
    return $ memberEdgeSet a b allEdges

-- | Check if this vertex exists in the graph.
vertex
    :: (Eq v, Hashable v, Tree.Tree t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> m Bool
vertex (Graph levels) a = do
    L {..} <- readMutVar levels
    return $ a `HMS.member` allEdges

-- | Ammortized /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
    :: forall t m v. (Eq v, Hashable v, Tree t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> v -> m Bool
cut (Graph levels) a b = do
  L {..} <- readMutVar levels
  let newAllEdges = cutEdgeSet a b allEdges
  if VM.length unLevels == 0 || a == b
    then return False
    else do
      cut' <- go unLevels (VM.length unLevels-1)
      writeMutVar levels L {allEdges = newAllEdges, ..}
      return cut'
  where
    go :: VM.MVector (PrimState m) (ET.Forest t (Sum Int) (PrimState m) v, EdgeSet v) -> Int -> m Bool
    go unLevels idx = do
      -- traceShowM ("go", idx)
      (etf, nonTreeEdges0) <- VM.read unLevels idx
      cutResult <- ET.cut etf a b
      case cutResult of
        False -> do
          let !nonTreeEdges1 = cutEdgeSet a b nonTreeEdges0
          VM.write unLevels idx (etf, nonTreeEdges1)
          if idx > 0 then go unLevels (idx - 1) else return False
        True -> do
          aSize <- ET.componentSize etf a
          bSize <- ET.componentSize etf b
          let (smaller, _bigger) = if aSize <= bSize then (a, b) else (b, a)
          Just sRoot <- ET.findRoot etf smaller

          -- These are all edges, and vertices within the smaller tree.
          sTreeEdges <- Tree.toList sRoot
          let !sVertices = HS.fromList $ map fst $
                    filter (uncurry (==)) sTreeEdges

          -- We need to consider all edges incident to the smaller tree.
          let sIncidentEdges =
                [ (x, y)
                | x <- HS.toList sVertices
                , y <- maybe [] HS.toList (HMS.lookup x nonTreeEdges0)
                ]

          -- Find a replacement and punish all edges we visit.
          let findRep punish [] = (punish, Nothing)
              findRep punish ((x, y) : candidates)
                | y `HS.member` sVertices =
                    findRep ((x, y) : punish) candidates
                | otherwise =
                    (punish, Just (x, y))

          -- Perform the search
          let (punished, replacementEdge) = findRep [] sIncidentEdges

          -- Increase the levels of the tree edges and the punished edges.
          nonTreeEdges1 <- if
              | idx + 1 >= VM.length unLevels -> return nonTreeEdges0
              | otherwise -> do
                    (incEtf, incNonTreeEdges0) <- VM.read unLevels (idx + 1)

                    let moveTreeEdge (x, y) =
                            ET.link_ incEtf x y

                    let moveNonTreeEdge !(ntes, !incNTes) (x, y) =
                            (cutEdgeSet x y ntes, linkEdgeSet x y incNTes)

                    mapM_ moveTreeEdge sTreeEdges
                    let !(!nonTreeEdges1, !incNonTreeEdges1) = L.foldl'
                            moveNonTreeEdge (nonTreeEdges0, incNonTreeEdges0) punished

                    VM.write unLevels (idx + 1) (incEtf, incNonTreeEdges1)
                    return nonTreeEdges1

          case replacementEdge of
            Nothing  -> do
              VM.write unLevels idx (etf, nonTreeEdges1)
              if idx > 0 then go unLevels (idx - 1) else return True
            Just rep@(c, d) -> do
              let !nonTreeEdges2 = cutEdgeSet c d nonTreeEdges1
              VM.write unLevels idx (etf, nonTreeEdges2)
              ET.link_ etf c d
              propagateReplacement unLevels (idx - 1) rep
              return True

    propagateReplacement unLevels idx (c, d) = when (idx >= 0) $ do
      (etf, _) <- VM.read unLevels idx
      ET.cut_ etf a b
      ET.link_ etf c d
      -- TODO: mess with edges??
      propagateReplacement unLevels (idx - 1) (c, d)

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

-- | 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 t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> m Bool
insert (Graph g) x = do
  l@L {..} <- readMutVar g
  if HMS.member x allEdges then
      return False
  else do
    let newAllEdges = HMS.insert x HS.empty allEdges
    let numVertices = numVerts + 1
    unLevels' <- do
      let oldNumLevels = VM.length unLevels
      newUnLevels <- VM.take (logBase2 numVertices + 1) <$>
        VM.grow unLevels (max 0 $ logBase2 numVertices - oldNumLevels + 1)
      forM_ [oldNumLevels .. logBase2 numVertices] $ \levelIdx -> do
        df <- ET.edgeless (\v1 v2 -> if v1 == v2 then Sum 1 else Sum 0) $ map fst $ HMS.toList allEdges
        VM.write newUnLevels levelIdx (df, HMS.empty)
      return newUnLevels
    let updateLevel i
            | i >= VM.length unLevels' = return ()
            | otherwise               = do
                (forest, nt) <- VM.read unLevels' i
                ET.insert_ forest x
                VM.write unLevels' i (forest, nt)
                updateLevel (i + 1)

    updateLevel 0
    writeMutVar g $ l {allEdges = newAllEdges, unLevels = unLevels', numVerts = numVertices}
    return True

-- | Version of 'insert' which ignores the result.
insert_
    :: (Eq v, Hashable v, Tree t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> m ()
insert_ g x = void (insert g x)

-- | 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 t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> m Bool
delete g@(Graph levels) x = do
  l0 <- readMutVar levels
  case HMS.lookup x (allEdges l0) of
    Nothing -> return False
    Just nbs -> do
      forM_ nbs $ \y -> cut g x y

      l1 <- readMutVar levels
      let newAllEdges = HMS.delete x (allEdges l1)
          updateLevel i
              | i >= VM.length (unLevels l1) = return ()
              | otherwise                    = do
                  (forest, nt) <- VM.read (unLevels l1) i
                  ET.delete_ forest x
                  VM.write (unLevels l1) i (forest, HMS.delete x nt)
                  updateLevel (i + 1)

      updateLevel 0
      writeMutVar levels $ l1 {allEdges = newAllEdges, numVerts = numVerts l0 - 1}
      return True

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

-- | Get all neighbours of the given vertex.
neighbours
    :: (Eq v, Hashable v, Tree t, PrimMonad m)
    => Graph t (PrimState m) v -> v -> m (HS.HashSet v)
neighbours (Graph levels) x = do
    l0 <- readMutVar levels
    return $ fromMaybe HS.empty (HMS.lookup x (allEdges l0))

-- | Obtain the current spanning forest.
spanningForest
    :: (Eq v, Hashable v, Tree t, PrimMonad m)
    => Graph t (PrimState m) v -> m (DT.Forest v)
spanningForest (Graph levels) = do
  L {..} <- readMutVar levels
  if VM.null unLevels
    then return []
    else do
      (etf, _) <- VM.read unLevels 0
      ET.spanningForest etf