Graph.IDirectedGraph
A directed, weighted graph data structure.
Properties
edgeCount
The number of edges in the graph.
console.log(`the graph has ${graph.edgeCount} edges`);
edges
An iterable of all edges in the graph.
for (const edge of graph.edges) {
console.log(`edge: ${edge.src.id} -> ${edge.dst.id}`);
}
vertexCount
The number of vertices in the graph.
console.log(`the graph has ${graph.vertexCount} vertices`);
vertices
An iterable of all vertices in the graph.
for (const vertex of graph.vertices) {
console.log(`vertex: ${vertex.id}`);
}
Methods
addVertex
Add a vertex or set of vertices to the graph.
// for vertices with undefined "data" (no data associated with each vertex)
const undefGraph = mpSdk.Graph.createDirectedGraph<undefined>();
graph.addVertex(
{ id: 'a' },
{ id: 'b' },
);
// for vertices with any other data
const graph = mpSdk.Graph.createDirectedGraph<number>();
graph.addVertex(
{ id: 'a', data: 1 },
{ id: 'b', data: 2 },
);
| Parameter | Type |
|---|---|
| vertexData | T extends undefined | void ? VertexIdDescriptor[] : Array<VertexDescriptor<T>> A variable number of [`VertexDescriptor`](/reference/graph#type-VertexDescriptor)s to use to create nodes in the graph. |
clear
Remove all vertices and edges from the graph.
graph.clear();
// graph.vertexCount === 0
// graph.edgeCount === 0
commit
Trigger any attached observers if there were changes to this graph. If there are no changes to the graph, this is a no-op and no callbacks will be triggered.
graph.commit();
dispose
Remove all vertices and edges from the graph.
Also call an optional onDispose provided at construction.
graph.dispose();
// graph.vertexCount === 0
// graph.edgeCount === 0
edge
Get the edge that connects one vertex to another.
const edge = graph.edge(a, b);
if (edge) {
console.log(`Found an edge from "${edge.src.id}" to "${edge.dst.id}" with weight ${edge.weight}`);
}
| Parameter | Type |
|---|---|
| src | Graph.Vertex<T> The source vertex. |
| dst | Graph.Vertex<T> The destination vertex. |
Returns: Graph.Edge<T> | undefined
If an edge from src to dst exists, returns the Edge. Otherwise, undefined.
filterVertices
Filter the vertices of this graph to ones that meet the condition specified.
// find all vertices that are islands (have no edges in nor out)
graph.filterVertices(verex => (vertex.edgesOutCount + vertex.edgesInCount) > 10);
| Parameter | Type |
|---|---|
| predicate | (vertex: Graph.Vertex<T>) => boolean The callback to run against each vertex to determine whether to include it in the returned array. |
| thisArg? | any The "this" argument to use in predicate |
Returns: Array<Graph.Vertex<T>>
findVertex
Find a vertex of this graph.
// find a highly connected vertex with more than 10 edges
graph.findVertex(vertex => (vertex.edgesOutCount + vertex.edgesInCount) > 10);
| Parameter | Type |
|---|---|
| predicate | (vertex: Graph.Vertex<T>) => boolean The callback to run against each vertex until one is found (by returning true.) |
| thisArg? | any The "this" argument to use in predicate |
Returns: Graph.Vertex<T> | undefined
hasEdge
Test if an edge from one vertex to another is present in the graph.
if (graph.hasEdge(a, b)) {
console.log('an edge from "a" to "b" was found');
}
| Parameter | Type |
|---|---|
| src | Graph.Vertex<T> The source vertex. |
| dst | Graph.Vertex<T> The destination vertex. |
Returns: boolean
If an edge from src to dst exists, returns true. Otherwise, false.
hasVertex
Test if a vertex associated with id is present in the graph.
if (graph.hasVertex('a')) {
console.log('a vertex with id "a" was found');
}
| Parameter | Type |
|---|---|
| id | string The vertex's id. |
Returns: boolean
If a vertex with id exists, returns true. Otherwise, false.
onEdgesChanged
Subscribe to edge changes.
After this graph's edges have been updated using setEdge or removeEdge,
the observer attached will have its onChanged function called when commit is called.
graph.onEdgesChanged({
onChanged() {
console.log(`this graph's edges have changed`);
}
});
| Parameter | Type |
|---|---|
| observer | IObserver<Graph.IDirectedGraph<T>> an observer notified when the graph's edges change |
Returns: ISubscription
onVerticesChanged
Subscribe to vertex changes.
After this graph's vertices have been updated using addVertex or removeVertex,
the observer attached will have its onChanged function called when commit is called.
graph.onVerticesChanged({
onChanged() {
console.log(`this graph's vertices have changed`);
}
});
| Parameter | Type |
|---|---|
| observer | IObserver<Graph.IDirectedGraph<T>> an observer notified when the graph's vertices change |
Returns: ISubscription
removeEdge
Remove an edge or a set of edges from the graph.
graph.removeEdge(e1, e2);
| Parameter | Type |
|---|---|
| edges | Array<Graph.Edge<T>> A variable number of edges to remove. |
removeVertex
Remove a vertex or a set of vertices from the graph.
graph.removeVertex(a, b);
| Parameter | Type |
|---|---|
| vertices | Array<Graph.Vertex<T>> A variable number of vertices to remove. |
setEdge
Set the weight of an edge or set of edges between pairs of vertices representing the connections from src to dst.
If an error is thrown, the graph is left untouched, no edges are added.
graph.setEdge(
{ src: a, dst: b, weight: 5 },
{ src: b, dst: c, weight: 10 },
);
// update edge ab
graph.setEdge(
{ src: a, dst: b, weight: 10 },
);
| Parameter | Type |
|---|---|
| edgeDescs | Array<EdgeDescriptor<T>> One or more [`EdgeDescriptor`](/reference/graph#type-EdgeDescriptor)s describing the edges to set. |
vertex
Get the vertex associated with id.
const a = graph.vertex('a');
if (a) {
console.log(`Found a vertex with id "${a.id}"`);
}
| Parameter | Type |
|---|---|
| id | string The vertex's id. |
Returns: Graph.Vertex<T> | undefined
If a vertex with id exists, returns the Vertex. Otherwise, undefined.
watch
Watch a collection and update the graph as the collection changes.
Note: If you need a graph of enabled sweeps, use Sweep.createGraph instead of the code snippet below
// create a graph of enabled sweeps
const graph = mpSdk.createGraph();
const sub = graph.watch({
collection: mpSdk.Sweep.collection,
isNeighborOf(sweepSrc, sweepDst) {
return sweepSrc.data.neighbors.includes(sweepDst.id);
},
neighborsOf(sweepVertex) {
return sweepVertex.data.neighbors.values();
},
weightBetween(sweepSrc, sweepDst) {
const dx = sweepDst.data.position.x - sweepSrc.data.position.x;
const dy = sweepDst.data.position.y - sweepSrc.data.position.y;
const dz = sweepDst.data.position.z - sweepSrc.data.position.z;
return Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2);
},
shouldAdd(sweep) {
return sweep.enabled;
},
});
// some time later when the graph no longer needs updating
sub.cancel();
| Parameter | Type |
|---|---|
| collectionAdaptor | Graph.ICollectionAdaptor<T> the adaptor that maps an observable collection to graph vertices and edges |
Returns: ISubscription
ISubscription