Graph()

Class representing a sparse graph implementation based on adjacency lists. A sparse graph can be used to model many different types of graphs like navigation graphs (pathfinding), dependency graphs (e.g. technology trees) or state graphs (a representation of every possible state in a game).

new Graph()

Constructs a new graph.

Author:

Members

digraph :Boolean

Whether this graph is directed or not.

Default Value:
  • false

Methods

addEdge(edge) → {Graph}

Adds an edge to the graph. If the graph is undirected, the method automatically creates the opponent edge.

Parameters:
Name Type Description
edge Edge

The edge to add.

Returns:
Graph -

A reference to this graph.

addNode(node) → {Graph}

Adds a node to the graph.

Parameters:
Name Type Description
node Node

The node to add.

Returns:
Graph -

A reference to this graph.

clear() → {Graph}

Removes all nodes and edges from this graph.

Returns:
Graph -

A reference to this graph.

fromJSON(json) → {Graph}

Restores this instance from the given JSON object.

Parameters:
Name Type Description
json Object

The JSON object.

Returns:
Graph -

A reference to this graph.

getEdge(from, to) → {Edge}

Returns an edge for the given from and to node indices. If no node is found, null is returned.

Parameters:
Name Type Description
from Number

The index of the from node.

to Number

The index of the to node.

Returns:
Edge -

The requested edge.

getEdgeCount() → {number}

Returns the edge count of the graph.

Returns:
number -

The amount of edges.

getEdgesOfNode(index, result) → {Array.<Edge>}

Gathers all edges leading from the given node index and stores them into the given array.

Parameters:
Name Type Description
index Number

The node index.

result Array.<Edge>

The result array.

Returns:
Array.<Edge> -

The result array.

getNode(index) → {Node}

Returns a node for the given node index. If no node is found, null is returned.

Parameters:
Name Type Description
index Number

The index of the node.

Returns:
Node -

The requested node.

getNodeCount() → {number}

Returns the node count of the graph.

Returns:
number -

The amount of nodes.

getNodes(result) → {Array.<Node>}

Gathers all nodes of the graph and stores them into the given array.

Parameters:
Name Type Description
result Array.<Node>

The result array.

Returns:
Array.<Node> -

The result array.

hasEdge(from, to) → {Boolean}

Return true if the graph has an edge connecting the given from and to node indices.

Parameters:
Name Type Description
from Number

The index of the from node.

to Number

The index of the to node.

Returns:
Boolean -

Whether this graph has the edge or not.

hasNode(index) → {Boolean}

Return true if the graph has the given node index.

Parameters:
Name Type Description
index Number

The node index to test.

Returns:
Boolean -

Whether this graph has the node or not.

removeEdge(edge) → {Graph}

Removes the given edge from the graph. If the graph is undirected, the method also removes the opponent edge.

Parameters:
Name Type Description
edge Edge

The edge to remove.

Returns:
Graph -

A reference to this graph.

removeNode(node) → {Graph}

Removes the given node from the graph and all edges which are connected with this node.

Parameters:
Name Type Description
node Node

The node to remove.

Returns:
Graph -

A reference to this graph.

toJSON() → {Object}

Transforms this instance into a JSON object.

Returns:
Object -

The JSON object.