Graph Builder
A graph builder library for modeling, and traversing abstract graph structures.
This is a (incomplete) port of the guava graph library.
The main entry point for building the graph are the
GraphBuilder
for building a
MutableGraph
and
ValueGraphBuilder
for building a
MutableValueGraph
.
Graph Traversal
There is also a traverser which can perform different types of traversal of any graphs/trees (that implement SuccessorFunction, which all the Graphs created by the GraphBuilder do).
Examples
Creating a Graph
Create an undirected Mutablegraph:
const GraphBuilder = ; const graph = GraphBuilder;
Add some nodes:
graph;
Add some edges (silently adds nodes too):
graph;graph;graph;
Remove an edge:
graph;
Reading From the Graph
Everything returned is iterable.
Get connected nodes:
for const n of graph console;// prints:// chocolate// jelly
Get all the edges:
for const e of graph console;// prints:// bread => bread// peanut butter => chocolate// jelly => peanut butter
Get all edges connected to "peanut butter":
for const e of graph console;// prints:// chocolate => peanut butter// jelly => peanut butter
Get all the successors/predecessors of "peanut butter" (in an undirected graph, these are the same):
for const n of graph console;// prints:// chocolate// jellyfor const n of graph console;// prints:// chocolate// jelly
Or for a directed graph:
const graph = GraphBuilder;graph;graph;graph;for const n of graph console;// prints:// jellyfor const n of graph console;// prints:// chocolate
Traversing the Graph
The traversers return iterators.
Traverse a graph that looks like this (numbered breadth first):
1 2
| \ |
3 4--5
|
6
const GraphBuilder Traversers = ;const graph = GraphBuilder;graph;graph;graph;graph;graph;
Iterate breadth first:
const nodeIterator = Traversers; // starting from root nodes, 1 & 2console;// prints: 1, 2, 3, 4, 5, 6,
Iterate depth first preorder:
const nodeIterator = Traversers; // starting from root nodes, 1 & 2console;// prints: 1, 3, 6, 4, 5, 2
Iterate depth first postorder:
const nodeIterator = Traversers; // starting from root nodes, 1 & 2console;// prints: 6, 3, 5, 4, 1, 2
More
See the full API documentation for usage.
Full typescript
typings are also provided.