Read

#.hasNode

Returns whether the given node is found in the graph.

Example

graph.addNode('Timothy');

graph.hasNode('Timothy');
>>> true

graph.hasNode('Jack');
>>> false

Arguments

  • node any: node to find.

#.hasEdge

Returns whether the given edge is found in the graph or whether an edge links the given source & target.

Example

graph.addNode('Timothy');
graph.addNode('Clarice');
const edge = graph.addEdge('Clarice', 'Timothy');

// Using the edge's key:
graph.hasEdge(edge);
>>> true

// Using the edge's source & target:
graph.hasEdge('Clarice', 'Timothy');
>>> true

graph.hasEdge('Clarice', 'John');
>>> false

Arguments

  1. Using the key:

    • edge any: edge to find.
  2. Using the source & target:

    • source any: source of the edge to find.
    • target any: target of the edge to find.

Variants

#.hasDirectedEdge, #.hasUndirectedEdge

#.edge

Returns the key of the edge between given source & target or undefined if such an edge does not exist.

Note that this method will throw if either source or target is not found in the graph.

In addition, this method won't work on a multi graph and will throw because the graph cannot know which edge to return since there might be multiple edges between source & target.

Example

graph.addNodesFrom(['Timothy', 'Clarice', 'Olivia']);
graph.addEdgeWithKey('C->T' 'Clarice', 'Timothy');

graph.edge('Clarice', 'Timothy');
>>> 'C->T'

graph.edge('Clarice', 'Olivia');
>>> undefined

Arguments

  • source any: source of the edge to find.
  • target any: target of the edge to find.

Variants

#.directedEdge, #.undirectedEdge

#.degree

Returns the degree of the given node.

Will throw if the node is not found in the graph.

Example

graph.addNodeFrom(['Timothy', 'Jack', 'Clarice', 'Martha']);
graph.addEdge('Timothy', 'Jack');
graph.addEdge('Timothy', 'Clarice');
graph.addEdge('Martha', 'Timothy');

graph.degree('Timothy');
>>> 3
graph.inDegree('Timothy');
>>> 1
graph.outDegree('Timothy');
>>> 2

Arguments

  • node any: target node.
  • selfLoops [boolean] true: whether to keep the self loops when computing the degree.

Variants

  • #.inDegree
  • #.outDegree
  • #.directedDegree (#.inDegree + #.outDegree)
  • #.undirectedDegree
  • #.degree (#.directedDegree + #.undirectedDegree)

#.source

Returns the source of the given edge.

Example

graph.addNode('Timothy');
graph.addNode('Clarice');
const edge = graph.addEdge('Clarice', 'Timothy');

graph.source(edge);
>>> 'Clarice'

Arguments

  • edge any: target edge.

#.target

Returns the target of the given edge.

Example

graph.addNode('Timothy');
graph.addNode('Clarice');
const edge = graph.addEdge('Clarice', 'Timothy');

graph.target(edge);
>>> 'Timothy'

Arguments

  • edge any: target edge.

#.opposite

Given a node & an edge, returns the node at the other end of the edge.

Example

graph.addNode('Timothy');
graph.addNode('Clarice');
const edge = graph.addEdge('Clarice', 'Timothy');

graph.opposite('Timothy', edge);
>>> 'Clarice'

Arguments

  • node any: target node.
  • edge any: target edge.

#.extremities

Returns both extremities of the given edge.

Example

graph.addNode('Timothy');
graph.addNode('Clarice');
const edge = graph.addEdge('Clarice', 'Timothy');

graph.extremities(edge);
>>> ['Timothy', 'Clarice']

Arguments

  • edge any: target edge.

#.directed

Returns whether the given edge is directed.

Example

graph.addNode('Timothy');
graph.addNode('Clarice');
const edge = graph.addEdge('Clarice', 'Timothy');
const undirectedEdge = graph.addUndirectedEdge('Clarice', 'Timothy');

graph.directed(edge);
>>> true
graph.directed(undirectedEdge);
>>> false

Arguments

  • edge any: target edge.

Variants

#.undirected

#.selfLoop

Returns whether the given edge is a self-loop.

Example

graph.addNode('Timothy');
const edge = graph.addEdge('Timothy', 'Timothy');

graph.selfLoop(edge);
>>> true

Arguments

  • edge any: target edge.