T
is the node type of the graph. Nodes can be anything in all the included examples they are simple objects.
Create an edge between two nodes in the graph.
Throws a [[NodeDoesNotExistsError
]] if no either of the nodes you are attempting to connect do not exist.
The first node to connect (in DirectedGraph
s and DirectedAcyclicGraph
s this is the from
node.)
The second node to connect (in DirectedGraph
s and DirectedAcyclicGraph
s this is the to
node)
Returns a specific node given the node identity returned from the insert
function
This simply returns all the nodes stored in the graph
An optional function that indicates the sort order of the returned array
Add a node to the graph if it doesn't already exist. If it does, throw a NodeAlreadyExistsError
.
The node to be added
A string
that is the identity of the newly inserted node. This is created by applying the nodeIdentity
.
This replaces an existing node in the graph with an updated version.
Throws a [[NodeDoesNotExistsError
]] if no node with the same identity already exists.
__Caveat_:_ The default identity function means that this will never work since if the node changes it will have a different hash
.
The new node that is replacing the old one.
The node to insert or update
The identity string of the node inserted or updated.
Generated using TypeDoc
Graph
A
Graph
is is a simple undirected graph. On it's own it isn't too useful but it forms the basic functionality for theDirectedGraph
andDirectedAcyclicGraph
.Creating a Graph
You can create a graph to contain any type of node, for example:
type NodeType = { a: Number, b: string } const graph = new Graph<NodeType>() // Add a node of the defined type const node: string = graph.insert({ a: 10, b: 'string' }) // Get the node back const nodeValue: NodeType | undefined = graph.getNode(node);
Defining a custom node identity
When you create a graph you likely want to create include a custom
nodeIdentity
function. This function tells the graph how to uniquely identify nodes in a graph, default is to simply use an hash which means that functionality likereplace
will not work.type NodeType = { count: number, name: string } const graph = new Graph<NodeType>((n) => n.name) // Add a node graph.insert({ count: 5, name: 'node1' }) // This will throw an error even though `count` is different because they share a name. graph.insert({ count: 20, name: 'node1' })
Adding an edge
Graphs without edges aren't very useful. Inserting edges is done using the node identity string returned by the node identity function.
const node1: string = graph.insert({ count: 5, name: 'node1' }) const node2: string = graph.insert({ count: 20, name: 'node2' }) graph.addEdge(node1, node2) // This will throw an error since there is no node with the later name. graph.addEdge(node1, 'not a real node')
In an undirected graph the order in which you input the node names doesn't matter, but in directed graphs the "from node" comes first and the "to node" will come second.
Replacing a node
If a node already exists you can update it using
replace
.nodeIdentity(newNode)
must be equal tonodeIdentity(oldNode)
.const node1: string = graph.insert({ count: 5, name: 'node1' }) const node2: string = graph.insert({ count: 20, name: 'node2' }) // This will work because the name has not changed. graph.replace({ count: 15, name: 'node1' }) // This will not work because the name has changed. graph.replace({ count: 20, name: 'node3' })
replace
will throw aNodeDoesntExistError
exception if you are trying to replace a node that is missing from the graph.Upsert
Often you will want to create a node node if it doesn't exist and update it does. This can be achieved using
upsert
.const node1: string = graph.insert({ count: 5, name: 'node1' }) // Both of these will work, the first updating node1 and the second creating a node. const node2: string = graph.upsert({ count: 15, name: 'node1' }) const node3: string = graph.upsert({ count: 25, name: 'node3' })
upsert
always return the node identity string of the inserted or updated node. At presented there is no way to tell if the node was created or updated.