containers-0.6.2.1: Assorted concrete container types

Data.Graph

Description

Finite Graphs

The Graph type is an adjacency list representation of a finite, directed graph with vertices of type Int.

The SCC type represents a strongly-connected component of a graph.

Implementation

The implementation is based on

Synopsis

Graphs

Adjacency list representation of a graph, mapping each vertex to its list of successors.

type Bounds = (Vertex, Vertex) Source #

The bounds of an Array.

type Edge = (Vertex, Vertex) Source #

An edge from the first vertex to the second.

type Vertex = Int Source #

Abstract representation of vertices.

type Table a = Array Vertex a Source #

Table indexed by a contiguous set of vertices.

Note: This is included for backwards compatibility.

Graph Construction

graphFromEdges :: Ord key => [(node, key, [key])] -> (Graph, Vertex -> (node, key, [key]), key -> Maybe Vertex) Source #

Build a graph from a list of nodes uniquely identified by keys, with a list of keys of nodes this node should have edges to.

This function takes an adjacency list representing a graph with vertices of type key labeled by values of type node and produces a Graph-based representation of that list. The Graph result represents the shape of the graph, and the functions describe a) how to retrieve the label and adjacent vertices of a given vertex, and b) how to retrive a vertex given a key.

(graph, nodeFromVertex, vertexFromKey) = graphFromEdges edgeList
• graph :: Graph is the raw, array based adjacency list for the graph.
• nodeFromVertex :: Vertex -> (node, key, [key]) returns the node associated with the given 0-based Int vertex; see warning below.
• vertexFromKey :: key -> Maybe Vertex returns the Int vertex for the key if it exists in the graph, Nothing otherwise.

To safely use this API you must either extract the list of vertices directly from the graph or first call vertexFromKey k to check if a vertex corresponds to the key k. Once it is known that a vertex exists you can use nodeFromVertex to access the labelled node and adjacent vertices. See below for examples.

Note: The out-list may contain keys that don't correspond to nodes of the graph; they are ignored.

Warning: The nodeFromVertex function will cause a runtime exception if the given Vertex does not exist.

Examples

Expand

An empty graph.

(graph, nodeFromVertex, vertexFromKey) = graphFromEdges []
graph = array (0,-1) []

A graph where the out-list references unspecified nodes ('c'), these are ignored.

(graph, _, _) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c'])]
array (0,1) [(0,[1]),(1,[])]

A graph with 3 vertices: ("a") -> ("b") -> ("c")

(graph, nodeFromVertex, vertexFromKey) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c']), ("c", 'c', [])]
graph == array (0,2) [(0,[1]),(1,[2]),(2,[])]
nodeFromVertex 0 == ("a",'a',"b")
vertexFromKey 'a' == Just 0

Get the label for a given key.

let getNodePart (n, _, _) = n
(graph, nodeFromVertex, vertexFromKey) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c']), ("c", 'c', [])]

Construction

Arguments

 :: Ord key => [(node, key, [key])] The graph: a list of nodes uniquely identified by keys, with a list of keys of nodes this node has edges to. The out-list may contain keys that don't correspond to nodes of the graph; such edges are ignored. -> [SCC node]

The strongly connected components of a directed graph, reverse topologically sorted.

Examples

Expand
stronglyConnComp [("a",0,[1]),("b",1,[2,3]),("c",2,[1]),("d",3,[3])]
== [CyclicSCC ["d"],CyclicSCC ["b","c"],AcyclicSCC "a"]

Arguments

 :: Ord key => [(node, key, [key])] The graph: a list of nodes uniquely identified by keys, with a list of keys of nodes this node has edges to. The out-list may contain keys that don't correspond to nodes of the graph; such edges are ignored. -> [SCC (node, key, [key])] Reverse topologically sorted

The strongly connected components of a directed graph, reverse topologically sorted. The function is the same as stronglyConnComp, except that all the information about each node retained. This interface is used when you expect to apply SCC to (some of) the result of SCC, so you don't want to lose the dependency information.

Examples

Expand
stronglyConnCompR [("a",0,[1]),("b",1,[2,3]),("c",2,[1]),("d",3,[3])]
== [CyclicSCC [("d",3,[3])],CyclicSCC [("b",1,[2,3]),("c",2,[1])],AcyclicSCC ("a",0,[1])]

Conversion

flattenSCC :: SCC vertex -> [vertex] Source #

The vertices of a strongly connected component.

flattenSCCs :: [SCC a] -> [a] Source #

The vertices of a list of strongly connected components.

Trees

type Forest a = [Tree a] Source #

data Tree a Source #

Non-empty, possibly infinite, multi-way trees; also known as rose trees.

Constructors

 Node a (Forest a)
Instances