Class: GenericGraph

(protected) GenericGraph()

A generic graph that does not allow parallel edges, self loops, or multiple edges. Behind the scenes, this graph is represented as an adjacency list using immutable.js Map. To use objects as vertices, the object should have a valid implementation of the equals() and hashCode() methods. Objects should also include a valueOf() method that returns a primitive value.

Constructor

(protected) new GenericGraph()

Constructs a new empty graph.
Source:
Example
class Foo {
  private readonly a: number;
  private readonly b: number;

  constructor(a: number, b: number) {
    this.a = a;
    this.b = b;
  }

  equals(other: any) {
    if (!(other instanceof Foo)) return false;

    return this.a == other.a;
  }

  hashCode() {
    return this.a;
  }

  valueOf() {
    return this.a + this.b;
  }
}
const g = new GenericGraph<Foo>();

Classes

GenericGraph

Members

adj

Returns an immutable copy of the adjacency list.
Source:

Methods

addPath(path)

Adds all the edges in the path to the graph.
Parameters:
Name Type Description
path Array.<V> the path to add.
Source:

fromEdgeList(edgeList)

Adds all the edges in the edge list to the graph
Parameters:
Name Type Description
edgeList Array.<Array.<V>> the list of edges to add.
Source:

hasVertex(v) → {boolean}

Determines if the graph contains the vertex v.
Parameters:
Name Type Description
v V the vertex to check.\
Source:
Returns:
true if the graph contains the vertex v, false otherwise.
Type
boolean

numberOfNodes() → {number}

Returns the number of vertices in the graph.
Source:
Returns:
the number of vertices in the graph.
Type
number

removeVertex(v)

Removes a vertex from the graph.
Parameters:
Name Type Description
v V the vertex to remove.
Source:

toString() → {string}

Returns a string representation of the graph. Just calls the toString() method of the underlying adjacency list.
Source:
Returns:
a string representation of the graph.
Type
string

validateEdge(v, w)

Validates that the edge (v, w) exists in the graph.
Parameters:
Name Type Description
v V
w V
Source:
Throws:
EdgeDoesNotExistError if the edge does not exist.

validateVertex(v)

Validates that the vertex v exists in the graph.
Parameters:
Name Type Description
v V
Source:
Throws:
VertexDoesNotExistError if the vertex does not exist.

vertices() → {Array.<V>}

Returns an array of all the vertices in the graph.
Source:
Returns:
an array of all the vertices in the graph.
Type
Array.<V>