alexandrepiveteau / elm-algebraic-graph / AlgebraicGraph.Graph

Graphs can be represented as an algebraic data type through four different primitives : empty, singleton, union and product. Graphs produced with these primitives have the particularity that they will never be in an inconsistent state, at the expense of possibly including some extra meta-data that is not really needed. This module has a bunch of functions to help you work with them!

Types


type Graph a

Represents a directed graph of values. So Graph (String) is a graph of strings and Graph (Int) is a graph of ints.

Construct

empty : Graph a

Create an empty graph.

singleton : a -> Graph a

Create a graph with one value.

union : Graph a -> Graph a -> Graph a

Perform the union of two graphs. Vertices and edges will be unioned.

product : Graph a -> Graph a -> Graph a

Perform the product of two graphs. Vertices will be unioned, and edges will be created from each vertex of the left graph to each vertex of the right graph.

fromList : List a -> Graph a

Create a graph with no edges from a list of vertex.

Contents

isEmpty : Graph a -> Basics.Bool

Determine if a graph is empty.

member : a -> Graph a -> Basics.Bool

Determine if a vertex is in a graph.

edges : Graph comparable -> Set ( comparable, comparable )

Expand this graph to forlm a set of all edges.

vertices : Graph comparable -> Set comparable

Expand this graph to forlm a set of all vertices.

Transform

map : (a -> b) -> Graph a -> Graph b

Map a function onto a graph, creating a graph with transformed vertices.

concatMap : (a -> Graph b) -> Graph a -> Graph b

Map a function onto a graph, and flatten the resulting graphs.

fold : (a -> b -> b) -> b -> Graph a -> b

Reduce the vertices of a graph.

fold (+) 0 (fromList [ 1, 2, 3 ]) == 6

compact : Graph comparable -> Graph comparable

Compact a graph, using the intuition that each vertex should result in a new vertex, and each inferred edge can be compacted into its own small sub-graph.

This removes duplicated edges, and does not find a minimal algebraic graph for a given set of edges andvertices.