drathier/elm-graph/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Graph

A simple functional graph library. Keys used to identify nodes can be any comparable and nodes can have any kind of metadata associated with them.

All operations that look at a single node are at most O(log n). Operations that look at all elements in the graph are at most O(n log n).

Types

type alias Graph =
Graph

A directed graph. Graph Int String is a graph that uses Ints for identifying its nodes, and lets you store a String on each node.

Build

empty : Graph comparable data

Create an empty graph.

insertNode : comparable -> Graph comparable data -> Graph comparable data

Insert a node. Does not overwrite metadata if node already exists.

insertNodeData : comparable -> data -> Graph comparable data -> Graph comparable data

Update metadata for a node. Creates the node if it does not already exist.

( comparable, comparable )
-> Graph comparable data
-> Graph comparable data

Insert an edge between two nodes. Creates any nodes that do not already exist.

removeNode : comparable -> Graph comparable data -> Graph comparable data

Remove a node by its key. No-op if node doesn't exist.

( comparable, comparable )
-> Graph comparable data
-> Graph comparable data

Remove an edge identified by its source and target keys. No-op if source, target or edge doesn't exist.

Query

getData : comparable -> Graph comparable data -> Maybe data

Get the data associated with a specific node.

member : comparable -> Graph comparable data -> Bool

Determine if a node identified by a key is in the graph.

memberEdge : ( comparable, comparable ) -> Graph comparable data -> Bool

Determine if an edge identified by a pair of keys is in the graph.

incoming : comparable -> Graph comparable data -> Set comparable

Get the set of incoming edges to a node.

outgoing : comparable -> Graph comparable data -> Set comparable

Get the set of outgoing edges from a node.

size : Graph comparable data -> Int

Determine the number of nodes in the graph.

keys : Graph comparable data -> List comparable

Get the keys for all nodes in the graph.

nodes : Graph comparable data -> List ( comparable, Maybe data )

Get the (key, data) pair for each node in the graph.

edges : Graph comparable data -> List ( comparable, comparable )

Get the (from, to) pair for each edge in the graph.

isAcyclic : Graph comparable data -> Bool

Determine if a graph contains any loops or cycles.

reachable : comparable -> Graph comparable data -> Set comparable

Get the set of reachable nodes from a key, following outgoing edges any number of steps.

relativeOrder : comparable -> comparable -> Graph comparable data -> RelativeOrdering

Returns the relative ordering of two keys in a directed acyclic graph. If there is a path from a to b over outgoing edges, a is Before b. If there is no path between them, they compare Concurrent.

Transform

map :
(comparable -> Maybe data1 -> Maybe data2)
-> Graph comparable data1
-> Graph comparable data2

Apply a function to the data associated with each node in a graph.

foldl : (comparable -> Maybe data -> a -> a) -> a -> Graph comparable data -> a

Fold over the node keys and data in a graph, in order from lowest key to highest key.

foldr : (comparable -> Maybe data -> a -> a) -> a -> Graph comparable data -> a

Fold over the node keys and data in a graph, in order from highest key to lowest key.

Set operations

(comparable -> Maybe data -> Bool)
-> Graph comparable data
-> ( Graph comparable data, Graph comparable data )

Partition a graph into two parts, one with the nodes where the predicate function returned True, and one where it returned False.

Graph comparable data
-> Graph comparable data
-> Graph comparable data

Join two graphs together. If an edge appears between two nodes in either of the graphs, it will be in the resulting graph. If a node identified by a specific key appears in any of the graphs, it will be in the resulting graph. If both graphs have metadata for the same key, the metadata in the left graph will be used.

Graph comparable data
-> Graph comparable data
-> Graph comparable data

Create a graph based on the intersection of two graphs. If both graphs have the same node, edge or associated metadata, it will be in the resulting graph. If one graph has metadata and the other only has the node, the node will be in the resulting graph, but the metadata will not.

Algorithms and Traversal

postOrder : Graph comparable data -> List comparable

Get a list of all keys according to a postorder traversal of a spanning tree of the graph.

topologicalSort : Graph comparable data -> Maybe (List comparable)

Get a topological sorting of the graph, if the graph doesn't contain any loops or cycles.

Feature flags

enableDagReachability : Graph comparable data -> Maybe (Graph comparable data)

Enable the dynamic reachability optimization for directed acyclic graphs. This allows O(log n) queries for the relative ordering of two elements in a partially ordered set, and O(log n) queries for the set of nodes that are reachable from a specific node.

The downside is that modifying the graph now takes O(log n * (nodes before this node)) time on average. Modifying the beginning of the graph is thus quite fast, but inserting an edge at the end takes O(n log n) time. The other downside is that this optimization only works on directed acyclic graphs.

disableDagReachability : Graph comparable data -> Graph comparable data

Disable the dynamic reachability optimization for directed acyclic graphs.