Class: Graph

Graph()

Graph Class

Constructor

new Graph()

Source:

Members

edges

Returns number of edges in the graph
Source:

size

Returns number of nodes in the graph
Source:

Methods

addEdge(nodeA, nodeB, weightopt)

Adds a new edge between two nodes
Parameters:
Name Type Attributes Default Description
nodeA number | string
nodeB number | string
weight any <optional>
1 Optional, default 1 if not provided
Source:

addNode(id, valopt)

Adds a new node to the graph
Parameters:
Name Type Attributes Description
id number | string
val any <optional>
Source:

getNode(id) → {any}

Returns value of node
Parameters:
Name Type Description
id number | string
Source:
Returns:
- Value
Type
any

getPath(nodeA, nodeB) → {Array.<Array>}

Returns array of the shortest path between two nodes Based on the shortest number of intermediate nodes
Parameters:
Name Type Description
nodeA number | string
nodeB number | string
Source:
Returns:
- [{node, weight}]
Type
Array.<Array>

getPathWeighted(nodeA, nodeB) → {Array.<Array>}

Returns array of the shortest weighted path between two nodes (Dijkstra algorithm) Based on the shortest number of intermediate nodes
Parameters:
Name Type Description
nodeA number | string
nodeB number | string
Source:
Returns:
- [{nodes: [], distance: number}]
Type
Array.<Array>

getWeight(nodeA, nodeB) → {number}

Returns the sum of weights between two nodes, including intermediate ones. Based on the shortest number of intermediate nodes
Parameters:
Name Type Description
nodeA number | string
nodeB number | string
Source:
Returns:
Type
number

nodesConnected(nodeA, nodeB) → {boolean}

Returns true/false if edge exists between two nodes
Parameters:
Name Type Description
nodeA number | string
nodeB number | string
Source:
Returns:
- True/False if connection exists between nodes
Type
boolean

removeEdge(nodeA, nodeB) → {boolean}

Removes an existing edge between two nodes
Parameters:
Name Type Description
nodeA number | string
nodeB number | string
Source:
Returns:
Type
boolean

removeNode(id) → {boolean}

Remove the node, and its edges across all the nodes
Parameters:
Name Type Description
id number | string
Source:
Returns:
Type
boolean

updateEdge(nodeA, nodeB, weightopt) → {boolean}

Updates the weight of an existing edge between two nodes
Parameters:
Name Type Attributes Default Description
nodeA number | string
nodeB number | string
weight weight <optional>
1 Optional, default 1 if not provided
Source:
Returns:
Type
boolean

updateNode(id, valopt) → {boolean}

Updates value for an existing node
Parameters:
Name Type Attributes Description
id number | string
val any <optional>
Source:
Returns:
Type
boolean