Skip to main content

interface GraphBase.Builder<N,Tp>

undocumented

Implemented by: ArrowGraphBase.Builder<N,Tp>, EdgeGraphBase.Builder<N,Tp>, Graph.Builder<N>

Type parameters

NameConstraintsDefaultDescription
Nundocumented
TpGraphBase.TypesGraphBase.Typesundocumented

Properties

connectionSize

Returns the amount of connections in the graph.

Definition

readonly connectionSize: number;

example
ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
.connectionsSize
// => 2

context

Returns the context associated to this collection instance.

Definition

readonly context: WithGraphValues<Tp, N, unknown>['context'];

isEmpty

Returns true if there are no entries in the builder.

Definition

readonly isEmpty: boolean;

example
ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
.isEmpty
// => false

nodeSize

Returns the amount of nodes in the graph.

Definition

readonly nodeSize: number;

example
ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
.nodeSize
// => 3

Methods

addGraphElement

Adds the given element graph element to the builder, where a graph element is either a one-element tuple containing a node, or a two-element tuple containing two nodes indicating a connection.

Definition

addGraphElement(element: GraphElement<N>): boolean;

Parameters

NameTypeDescription
elementGraphElement<N>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.addGraphElement([1]) // => false
b.addGraphElement([4]) // => true
b.addGraphElement([2, 3]) // => false
b.addGraphElement([4, 1]) // => true

addGraphElements

Adds the graph elements in the given elements StreamSource to the graph.

Definition

addGraphElements(elements: StreamSource<GraphElement<N>>): boolean;

Parameters

NameTypeDescription
elementsStreamSource<GraphElement<N>>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.addGraphElements([[4], [5]]) // => true
b.addGraphElements([[3, 1], [1]]) // => true
b.addGraphElements([[1, 2], [1]]) // => false

addNode

Adds the given node to the graph.

Definition

addNode(node: N): boolean;

Parameters

NameTypeDescription
nodeN
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.addNode(6) // => true
b.addNode(1) // => false

addNodes

Adds the given nodes to the builder.

Definition

addNodes(nodes: StreamSource<N>): boolean;

Parameters

NameTypeDescription
nodesStreamSource<N>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.addNodes([3, 4, 5]) // => true
b.addNodes([1, 2]) // => false

build

Returns an immutable Graph containing the links in this Builder instance.

Definition

build(): WithGraphValues<Tp, N, unknown>['normal'];

example
const b = ArrowGraphHashed.builder<number, number>()
b.connect(1, 2)
b.addNode(3)
const g = b.build()
console.log(g.toArray())
// => [[1, 2], [3]]

connect

Adds a connection between node1 and node2 to the graph.

Definition

connect(node1: N, node2: N): boolean;

Parameters

NameTypeDescription
node1Nthe first connection node
node2N
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.connect(3, 1) // => true
b.connect(1, 2) // => false

connectAll

Adds the connections in given connections StreamSource to the graph.

Definition

connectAll(connections: StreamSource<WithGraphValues<Tp, N, unknown>['link']>): boolean;

Parameters

NameTypeDescription
connectionsStreamSource<WithGraphValues<Tp, N, unknown>['link']>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.connectAll([[1, 2], [3, 1]]) // => true
b.connectAll([[1, 2]]) // => false

connectIfNodesExist

Returns true if the graph has changed

Definition

connectIfNodesExist(node1: N, node2: N): boolean;

Parameters

NameTypeDescription
node1N
node2N
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.connectIfNodesExist(3, 1) // => true
b.connectIfNodesExist(3, 4) // => false

disconnect

Removes the connection between given node1 and node2 if the connection was present.

Definition

disconnect<UN = N>(node1: RelatedTo<N, UN>, node2: RelatedTo<N, UN>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
node1RelatedTo<N, UN>the first connection node
node2RelatedTo<N, UN>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.disconnect(1, 2) // => true
b.disconnect(3, 4) // => false

disconnectAll

Removes all connections from the given connections StreamSource from the graph.

Definition

disconnectAll<UN = N>(connections: StreamSource<Link<RelatedTo<N, UN>>>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
connectionsStreamSource<Link<RelatedTo<N, UN>>>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.disconnectAll([[1, 2], [3, 4]]) // => true
b.disconnectAll([[3, 4], [5, 6]]) // => false

forEach

Performs given function f for each entry of the collection, using given state as initial traversal state.

Definition

forEach(f: (entry: [N] | WithGraphValues<Tp, N, unknown>['link'], index: number, halt: () => void) => void, options?: {
      state?: TraverseState;
    }): void;

Parameters

NameTypeDescription
f(entry: [N] | WithGraphValues<Tp, N, unknown>['link'], index: number, halt: () => void) => voidthe function to perform for each entry, receiving:
- entry: the next graph element
- index: the index of the element
- halt: a function that, if called, ensures that no new elements are passed
options{
      state?: TraverseState;
    }
object containing the following
- state: (optional) the traverse state
example
const b = ArrowGraphHashed.of([1], [2, 3], [4]).toBuilder();
b.forEach((entry, i, halt) => {
console.log([entry]);
if (i >= 1) halt();
})
// => logs [1] [2, 3]
note

O(N)

hasConnection

Returns true if the graph has a connection between given nodes node1 and node2.

Definition

hasConnection<UN = N>(node1: RelatedTo<N, UN>, node2: RelatedTo<N, UN>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
node1RelatedTo<N, UN>the first connection node
node2RelatedTo<N, UN>the second connection node
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.hasConnection(1, 2) // => true
b.hasConnection(6, 7) // => false

hasNode

Returns true if the graph contains the given node.

Definition

hasNode<UN = N>(node: RelatedTo<N, UN>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
nodeRelatedTo<N, UN>the node to search
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.hasNode(1) // => true
b.hasNode(6) // => false

removeNode

Removes the given node, and any of its connections, from the graph.

Definition

removeNode<UN = N>(node: RelatedTo<N, UN>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
nodeRelatedTo<N, UN>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.removeNode(1) // => true
b.removeNode(6) // => false

removeNodes

Removes the given nodes, and any of their connections, from the graph.

Definition

removeNodes<UN = N>(nodes: StreamSource<RelatedTo<N, UN>>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
nodesStreamSource<RelatedTo<N, UN>>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.removeNodes([1, 6, 7]) // => true
b.removeNodes([6, 7]) // => false