ac-library-hs-1.1.0.0: Data structures and algorithms
Safe HaskellSafe-Inferred
LanguageGHC2021

AtCoder.Extra.Graph

Description

Re-export of the Csr module and generic graph search functions.

Since: 1.1.0.0

Synopsis

Re-export of CSR

The Csr data type and all the functions such as build or adj are re-exported.

CSR helpers

swapDupe :: Unbox (Int, Int, w) => Vector (Int, Int, w) -> Vector (Int, Int, w) Source #

\(O(n)\) Converts non-directed edges into directional edges. This is a convenient function for making an input to build.

Example

Expand

swapDupe duplicates each edge reversing the direction:

>>> import AtCoder.Extra.Graph qualified as Gr
>>> import Data.Vector.Unboxed qualified as VU
>>> Gr.swapDupe $ VU.fromList [(0, 1, ()), (1, 2, ())]
[(0,1,()),(1,0,()),(1,2,()),(2,1,())]

Create a non-directed graph:

>>> let gr = Gr.build 3 . Gr.swapDupe $ VU.fromList [(0, 1, ()), (1, 2, ())]
>>> gr `Gr.adj` 0
[1]
>>> gr `Gr.adj` 1
[0,2]
>>> gr `Gr.adj` 2
[1]

Since: 1.1.0.0

swapDupe' :: Unbox (Int, Int) => Vector (Int, Int) -> Vector (Int, Int) Source #

\(O(n)\) Converts non-directed edges into directional edges. This is a convenient function for making an input to build'.

Example

Expand

swapDupe' duplicates each edge reversing the direction:

>>> import AtCoder.Extra.Graph qualified as Gr
>>> import Data.Vector.Unboxed qualified as VU
>>> Gr.swapDupe' $ VU.fromList [(0, 1), (1, 2)]
[(0,1),(1,0),(1,2),(2,1)]

Create a non-directed graph:

>>> let gr = Gr.build' 3 . Gr.swapDupe' $ VU.fromList [(0, 1), (1, 2)]
>>> gr `Gr.adj` 0
[1]
>>> gr `Gr.adj` 1
[0,2]
>>> gr `Gr.adj` 2
[1]

Since: 1.1.0.0

scc :: Csr w -> Vector (Vector Int) Source #

\(O(n + m)\) Returns the strongly connected components.

Example

Expand
>>> import AtCoder.Extra.Graph qualified as Gr
>>> import Data.Vector.Unboxed qualified as VU
>>> -- 0 == 1 -> 2    3
>>> let gr = Gr.build' 4 $ VU.fromList [(0, 1), (1, 0), (1, 2)]
>>> Gr.scc gr
[[3],[0,1],[2]]

Since: 1.1.0.0

Graph search

topSort :: Int -> (Int -> Vector Int) -> Vector Int Source #

\(O(n \log n + m)\) Returns the lexicographically smallest topological ordering of the given graph.

Constraints

  • The graph must be a DAG.

Example

Expand
>>> import AtCoder.Extra.Graph qualified as Gr
>>> import Data.Vector.Unboxed qualified as VU
>>> let n = 5
>>> let gr = Gr.build' n $ VU.fromList [(1, 2), (4, 0), (0, 3)]
>>> Gr.topSort n (gr `Gr.adj`)
[1,2,4,0,3]

Since: 1.1.0.0