Cabal-2.0.0.2: A framework for packaging Haskell software

Copyright(c) Edward Z. Yang 2016
LicenseBSD3
Maintainercabal-dev@haskell.org
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

Distribution.Compat.Graph

Contents

Description

A data type representing directed graphs, backed by Data.Graph. It is strict in the node type.

This is an alternative interface to Data.Graph. In this interface, nodes (identified by the IsNode type class) are associated with a key and record the keys of their neighbors. This interface is more convenient than Graph, which requires vertices to be explicitly handled by integer indexes.

The current implementation has somewhat peculiar performance characteristics. The asymptotics of all map-like operations mirror their counterparts in Data.Map. However, to perform a graph operation, we first must build the Data.Graph representation, an operation that takes O(V + E log V). However, this operation can be amortized across all queries on that particular graph.

Some nodes may be broken, i.e., refer to neighbors which are not stored in the graph. In our graph algorithms, we transparently ignore such edges; however, you can easily query for the broken vertices of a graph using broken (and should, e.g., to ensure that a closure of a graph is well-formed.) It's possible to take a closed subset of a broken graph and get a well-formed graph.

Synopsis

Graph type

data Graph a Source #

A graph of nodes a. The nodes are expected to have instance of class IsNode.

Instances

Foldable Graph Source # 

Methods

fold :: Monoid m => Graph m -> m #

foldMap :: Monoid m => (a -> m) -> Graph a -> m #

foldr :: (a -> b -> b) -> b -> Graph a -> b #

foldr' :: (a -> b -> b) -> b -> Graph a -> b #

foldl :: (b -> a -> b) -> b -> Graph a -> b #

foldl' :: (b -> a -> b) -> b -> Graph a -> b #

foldr1 :: (a -> a -> a) -> Graph a -> a #

foldl1 :: (a -> a -> a) -> Graph a -> a #

toList :: Graph a -> [a] #

null :: Graph a -> Bool #

length :: Graph a -> Int #

elem :: Eq a => a -> Graph a -> Bool #

maximum :: Ord a => Graph a -> a #

minimum :: Ord a => Graph a -> a #

sum :: Num a => Graph a -> a #

product :: Num a => Graph a -> a #

(Eq (Key a), Eq a) => Eq (Graph a) Source # 

Methods

(==) :: Graph a -> Graph a -> Bool #

(/=) :: Graph a -> Graph a -> Bool #

(IsNode a, Read a, Show (Key a)) => Read (Graph a) Source # 
Show a => Show (Graph a) Source # 

Methods

showsPrec :: Int -> Graph a -> ShowS #

show :: Graph a -> String #

showList :: [Graph a] -> ShowS #

(IsNode a, Binary a, Show (Key a)) => Binary (Graph a) Source # 

Methods

put :: Graph a -> Put #

get :: Get (Graph a) #

putList :: [Graph a] -> Put #

(NFData a, NFData (Key a)) => NFData (Graph a) Source # 

Methods

rnf :: Graph a -> () #

class Ord (Key a) => IsNode a where Source #

The IsNode class is used for datatypes which represent directed graph nodes. A node of type a is associated with some unique key of type Key a; given a node we can determine its key (nodeKey) and the keys of its neighbors (nodeNeighbors).

Minimal complete definition

nodeKey, nodeNeighbors

Associated Types

type Key a :: * Source #

Methods

nodeKey :: a -> Key a Source #

nodeNeighbors :: a -> [Key a] Source #

Query

null :: Graph a -> Bool Source #

O(1). Is the graph empty?

size :: Graph a -> Int Source #

O(1). The number of nodes in the graph.

member :: IsNode a => Key a -> Graph a -> Bool Source #

O(log V). Check if the key is in the graph.

lookup :: IsNode a => Key a -> Graph a -> Maybe a Source #

O(log V). Lookup the node at a key in the graph.

Construction

empty :: IsNode a => Graph a Source #

O(1). The empty graph.

insert :: IsNode a => a -> Graph a -> Graph a Source #

O(log V). Insert a node into a graph.

deleteKey :: IsNode a => Key a -> Graph a -> Graph a Source #

O(log V). Delete the node at a key from the graph.

deleteLookup :: IsNode a => Key a -> Graph a -> (Maybe a, Graph a) Source #

O(log V). Lookup and delete. This function returns the deleted value if it existed.

Combine

unionLeft :: IsNode a => Graph a -> Graph a -> Graph a Source #

O(V + V'). Left-biased union, preferring entries from the first map when conflicts occur.

unionRight :: IsNode a => Graph a -> Graph a -> Graph a Source #

O(V + V'). Right-biased union, preferring entries from the second map when conflicts occur. nodeKey x = nodeKey (f x).

Graph algorithms

stronglyConnComp :: Graph a -> [SCC a] Source #

Ω(V + E). Compute the strongly connected components of a graph. Requires amortized construction of graph.

data SCC vertex :: * -> * #

Strongly connected component.

Constructors

AcyclicSCC vertex

A single vertex that is not in any cycle.

CyclicSCC [vertex]

A maximal set of mutually reachable vertices.

Instances

Functor SCC 

Methods

fmap :: (a -> b) -> SCC a -> SCC b #

(<$) :: a -> SCC b -> SCC a #

NFData a => NFData (SCC a) 

Methods

rnf :: SCC a -> () #

cycles :: Graph a -> [[a]] Source #

Ω(V + E). Compute the cycles of a graph. Requires amortized construction of graph.

broken :: Graph a -> [(a, [Key a])] Source #

O(1). Return a list of nodes paired with their broken neighbors (i.e., neighbor keys which are not in the graph). Requires amortized construction of graph.

neighbors :: Graph a -> Key a -> Maybe [a] Source #

Lookup the immediate neighbors from a key in the graph. Requires amortized construction of graph.

revNeighbors :: Graph a -> Key a -> Maybe [a] Source #

Lookup the immediate reverse neighbors from a key in the graph. Requires amortized construction of graph.

closure :: Graph a -> [Key a] -> Maybe [a] Source #

Compute the subgraph which is the closure of some set of keys. Returns Nothing if one (or more) keys are not present in the graph. Requires amortized construction of graph.

revClosure :: Graph a -> [Key a] -> Maybe [a] Source #

Compute the reverse closure of a graph from some set of keys. Returns Nothing if one (or more) keys are not present in the graph. Requires amortized construction of graph.

topSort :: Graph a -> [a] Source #

Topologically sort the nodes of a graph. Requires amortized construction of graph.

revTopSort :: Graph a -> [a] Source #

Reverse topologically sort the nodes of a graph. Requires amortized construction of graph.

Conversions

Maps

toMap :: Graph a -> Map (Key a) a Source #

O(1). Convert a graph into a map from keys to nodes. The resulting map m is guaranteed to have the property that all ((k,n) -> k == nodeKey n) (toList m).

Lists

fromDistinctList :: (IsNode a, Show (Key a)) => [a] -> Graph a Source #

O(V log V). Convert a list of nodes (with distinct keys) into a graph.

toList :: Graph a -> [a] Source #

O(V). Convert a graph into a list of nodes.

keys :: Graph a -> [Key a] Source #

O(V). Convert a graph into a list of keys.

Sets

keysSet :: Graph a -> Set (Key a) Source #

O(V). Convert a graph into a set of keys.

Graphs

toGraph :: Graph a -> (Graph, Vertex -> a, Key a -> Maybe Vertex) Source #

O(1). Convert a graph into a Graph. Requires amortized construction of graph.

Node type

data Node k a Source #

A simple, trivial data type which admits an IsNode instance.

Constructors

N a k [k] 

Instances

Functor (Node k) Source # 

Methods

fmap :: (a -> b) -> Node k a -> Node k b #

(<$) :: a -> Node k b -> Node k a #

(Eq k, Eq a) => Eq (Node k a) Source # 

Methods

(==) :: Node k a -> Node k a -> Bool #

(/=) :: Node k a -> Node k a -> Bool #

(Show k, Show a) => Show (Node k a) Source # 

Methods

showsPrec :: Int -> Node k a -> ShowS #

show :: Node k a -> String #

showList :: [Node k a] -> ShowS #

Ord k => IsNode (Node k a) Source # 

Associated Types

type Key (Node k a) :: * Source #

Methods

nodeKey :: Node k a -> Key (Node k a) Source #

nodeNeighbors :: Node k a -> [Key (Node k a)] Source #

type Key (Node k a) Source # 
type Key (Node k a) = k

nodeValue :: Node k a -> a Source #

Get the value from a Node.