A simple functional graph library. Keys used to identify nodes can be any comparable
, and both nodes and edges 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)
.
A directed graph. (Graph Int String Float)
is a graph that uses Int
s for identifying its nodes, and lets you store a String
on each node and a Float
on each edge.
getData : comparable -> Graph comparable data edgeData -> Maybe data
Get the data associated with a specific node.
getEdgeData : comparable -> comparable -> Graph comparable data edgeData -> Maybe edgeData
Get the data associated with a specific edge.
If you want to pass the edge as a 2-tuple instead, you can use getEdgeData
from the Pair module.
member : comparable -> Graph comparable data edgeData -> Basics.Bool
Determine if a node identified by a key is in the graph.
memberEdge : ( comparable, comparable ) -> Graph comparable data edgeData -> Basics.Bool
Determine if an edge identified by a pair of keys is in the graph.
incoming : comparable -> Graph comparable data edgeData -> Set comparable
Get the set of incoming edges to a node.
outgoing : comparable -> Graph comparable data edgeData -> Set comparable
Get the set of outgoing edges from a node.
isEmpty : Graph comparable data edgeData -> Basics.Bool
Determine if the graph is empty.
size : Graph comparable data edgeData -> Basics.Int
Determine the number of nodes in the graph.
keys : Graph comparable data edgeData -> List comparable
Get the keys for all nodes in the graph.
nodes : Graph comparable data edgeData -> List ( comparable, Maybe data )
Get the (key, data) pair for each node in the graph.
edges : Graph comparable data edgeData -> List ( comparable, comparable )
Get the (from, to) pair for each edge in the graph.
edgesWithData : Graph comparable data edgeData -> List ( comparable, comparable, Maybe edgeData )
Get the (from, to, Maybe edgeData) pair for each edge in the graph.
isAcyclic : Graph comparable data edgeData -> Basics.Bool
Determine if a graph contains any loops or cycles.
empty : Graph comparable data edgeData
Create an empty graph.
insert : comparable -> Graph comparable data edgeData -> Graph comparable data edgeData
Insert a node. Does not overwrite metadata if node already exists.
insertData : comparable -> data -> Graph comparable data edgeData -> Graph comparable data edgeData
Update metadata for a node. Creates the node if it does not already exist.
insertEdge : comparable -> comparable -> Graph comparable data edgeData -> Graph comparable data edgeData
Insert an edge between two nodes. Creates any nodes that do not already exist.
If you want to pass the edge as a 2-tuple instead, you can use insertEdge
from the Pair module.
insertEdgeData : comparable -> comparable -> edgeData -> Graph comparable data edgeData -> Graph comparable data edgeData
Insert an edge with some metadata between two nodes. Creates any nodes that do not already exist.
If you want to pass the edge as a 2-tuple instead, you can use insertEdgeData
from the Pair module.
remove : comparable -> Graph comparable data edgeData -> Graph comparable data edgeData
Remove a node by its key. No-op if node doesn't exist.
removeData : comparable -> Graph comparable data edgeData -> Graph comparable data edgeData
Remove the metadata associated with a specific node.
removeEdge : comparable -> comparable -> Graph comparable data edgeData -> Graph comparable data edgeData
Remove an edge identified by its source and target keys. No-op if source, target or edge doesn't exist.
If you want to pass the edge as a 2-tuple instead, you can use removeEdge
from the Pair module.
removeEdgeData : comparable -> comparable -> Graph comparable data edgeData -> Graph comparable data edgeData
Remove the metadata associated with a specific edge.
If you want to pass the edge as a 2-tuple instead, you can use removeEdgeData
from the Pair module.
update : comparable -> (Maybe data -> Maybe data) -> Graph comparable data edgeData -> Graph comparable data edgeData
Update the metadata associated with a specific node.
updateEdge : comparable -> comparable -> (Maybe edgeData -> Maybe edgeData) -> Graph comparable data edgeData -> Graph comparable data edgeData
Update the metadata associated with a specific edge.
If you want to pass the edge as a 2-tuple instead, you can use updateEdge
from the Pair module.
map : (comparable -> Maybe data1 -> Maybe data2) -> Graph comparable data1 edgeData -> Graph comparable data2 edgeData
Apply a function to the data associated with each node in a graph.
mapEdge : (comparable -> comparable -> Maybe edgeData1 -> Maybe edgeData2) -> Graph comparable data edgeData1 -> Graph comparable data edgeData2
Apply a function to the data associated with each edge in a graph.
If you want to pass the edge as a 2-tuple instead, you can use mapEdge
from the Pair module.
foldl : (comparable -> Maybe data -> a -> a) -> a -> Graph comparable data edgeData -> 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 edgeData -> a
Fold over the node keys and data in a graph, in order from highest key to lowest key.
filter : (comparable -> Maybe data -> Basics.Bool) -> Graph comparable data edgeData -> Graph comparable data edgeData
Create a copy of the graph, only keeping nodes where the predicate function returned True.
partition : (comparable -> Maybe data -> Basics.Bool) -> Graph comparable data edgeData -> ( Graph comparable data edgeData, Graph comparable data edgeData )
Partition a graph into two parts. The left one, with the nodes where the predicate function returned True, and right one, where it returned False.
union : Graph comparable data edgeData -> Graph comparable data edgeData -> Graph comparable data edgeData
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 node or edge, the metadata in the left graph will be used.
intersect : Graph comparable data edgeData -> Graph comparable data edgeData -> Graph comparable data edgeData
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 both graphs have metadata for the same node or edge, the metadata in the left graph will be used.
postOrder : Graph comparable data edgeData -> List comparable
Get a list of all keys in postorder.
topologicalSort : Graph comparable data edgeData -> Maybe (List comparable)
Get a topological sorting of the graph, if the graph doesn't contain any loops or cycles.
These are commented out in the release build, since they rely on Debug.toString
.
graphviz
generates a graphviz file from a Graph.valid
checks if there's any internal inconsistency in the graph.