dawg-0.1.0: DAWG

Safe HaskellNone

Data.DAWG.Graph

Contents

Description

The module provides a directed acyclic graph (DAG) implementation where all equivalent nodes (i.e. roots of DAGs equal with respect to the == function) are compressed to one node with unique identifier. It can be alternatively thought of as a minimal acyclic finite-state automata.

Synopsis

Node

data Node a Source

Two nodes (states) belong to the same equivalence class (and, consequently, they must be represented as one node in the graph) iff they are equal with respect to their values and outgoing edges.

Constructors

Node 

Fields

value :: Maybe a
 
edges :: VMap Id
 

Instances

Eq a => Eq (Node a) 
(Eq (Node a), Ord a) => Ord (Node a) 
Show a => Show (Node a) 
Binary a => Binary (Node a) 

type Id = IntSource

Node identifier.

leaf :: Node aSource

Leaf node with no children and Nothing value.

onChar :: Char -> Node a -> Maybe IdSource

Child identifier found by following the given character.

subst :: Char -> Id -> Node a -> Node aSource

Substitue the identifier of the child determined by the given character.

Graph

data Graph a Source

A set of nodes. To every node a unique identifier is assigned. Invariants:

  • freeIDs \intersection occupiedIDs = \emptySet,
  • freeIDs \sum occupiedIDs = {0, 1, ..., |freeIDs \sum occupiedIDs| - 1},

where occupiedIDs = elemSet idMap.

TODO: Is it possible to merge freeIDs with ingoMap to save some memory?

Constructors

Graph 

Fields

idMap :: !(Map (Node a) Id)

Map from nodes to IDs.

freeIDs :: !IntSet

Set of free IDs.

nodeMap :: !(IntMap (Node a))

Equivalence class represented by given ID and size of the class.

ingoMap :: !(IntMap Int)

Number of ingoing edges.

Instances

Eq a => Eq (Graph a) 
(Eq (Graph a), Ord a) => Ord (Graph a) 
Show a => Show (Graph a) 
(Binary a, Ord a) => Binary (Graph a) 

empty :: Graph aSource

Empty graph.

size :: Graph a -> IntSource

Size of the graph (number of nodes).

nodeBy :: Id -> Graph a -> Node aSource

Node with the given identifier.

nodeID :: Ord a => Node a -> Graph a -> IdSource

Retrive the node identifier.

insert :: Ord a => Node a -> Graph a -> (Id, Graph a)Source

Insert node into the graph. If the node was already a member of the graph, just increase the number of ingoing edges.

delete :: Ord a => Node a -> Graph a -> Graph aSource

Delete node from the graph. If the node was present in the graph at multiple positions, just decrease the number of ingoing edges. NOTE: The function does not delete descendant nodes which may become inaccesible.