Graph

Graph

Undirected, weighted graph representation

Constructor

new Graph(numVerticies)

Example
const graph = new Collections.Graph(97);
// FOR ALL EXAMPLES BELOW. ASSUME graph IS CLEARED BEFORE EACH EXAMPLE
Parameters:
Name Type Description
numVerticies number Number of expected verticies for the graph

Methods

addEdge(vertex1, vertex2, weightopt) → {undefined}

Connects two verticies to create an undirected edge
Example
graph.addVertex("A");
graph.addVertex("B");
graph.addEdge("A", "B", 4); // adds edge between "A" & "B" of weight 4
Parameters:
Name Type Attributes Default Description
vertex1 * The first vertex
vertex2 * The second vertex
weight number <optional>
0 Optional cost of edge between @param vertex1, vertex2
Returns:
Type
undefined

addVertex(vertex) → {undefined}

Adds a vertex to the graph
Example
graph.addVertex("A");
graph.addVertex("B");
// two verticies with id "A" and "B" are added to graph
Parameters:
Name Type Description
vertex * The vertex to place into graph
Returns:
Type
undefined

BFS(startingVertex) → {Array}

Performs Breadth First Search
Parameters:
Name Type Description
startingVertex * The vertex to start Search from
Returns:
An Array containing verticies in order visited through BFS
Type
Array

DFS(startingVertex) → {Array}

Performs Depth First Search
Parameters:
Name Type Description
startingVertex * The vertex to start Search from
Returns:
An Array containing verticies in order visited through DFS
Type
Array

isConnected() → {boolean}

Reports whether the graph is connected
Returns:
True if connected and false otherwise
Type
boolean