Class: Graph

algo.core. Graph

new Graph()

Generic graph class. Supports vertices and edge insertion, deletion. Multiple edge connections between vertices, directed edges, self connected vertices.
Source:

Methods

addEdge(source, target, properties) → {algo.core.GraphEdge}

add a new edge to the graph
Parameters:
Name Type Description
source
target
properties
Source:
Returns:
Type
algo.core.GraphEdge

addRandomEdge()

a useful helper function for testing and simple visualization, adds a random edge between two random vertices that are not already connected in either direction
Source:

addVertex(properties) → {algo.core.GraphVertex}

add a vertex to the graph and invoke the optional callback
Parameters:
Name Type Description
properties
Source:
Returns:
Type
algo.core.GraphVertex

findVertices(property, value)

find all vertices with a given property value.
Parameters:
Name Type Description
property
value
Source:

getAdjacentVertices(vertex) → {Array}

return a set of vertices that are connected to the given vertex or vertex list by an in or out edge NOTE: The list does not include the starting vertex or vertices.
Parameters:
Name Type Description
vertex algo.core.GraphVertex | Array
Source:
Returns:
- array of algo.core.GraphVertex
Type
Array

getComponents()

return a list of list of vertices where each sublist defines a discrete/connected component of the graph. The components are sorted according to size with the largest components first.
Source:

getEdges(v1, v2)

return all directed edges from v1 -> v2
Parameters:
Name Type Description
v1
v2
Source:

getInVertices(vertex) → {Array}

return a set of vertices that have edges pointing at the given vertex. Since multi-edges are supported we ensure a set is returned
Parameters:
Name Type Description
vertex algo.core.GraphVertex
Source:
Returns:
- array of algo.core.GraphVertex
Type
Array

getOutVertices(vertex) → {Array}

return a set of vertices that are connected to the given vertex by an out bound edge
Parameters:
Name Type Description
vertex algo.core.GraphVertex
Source:
Returns:
- array of algo.core.GraphVertex
Type
Array

getVertexEdges(vertex) → {Array}

return all edges that are either in or out of the vertex
Parameters:
Name Type Description
vertex
Source:
Returns:
all the edges attached to this vertex regardless of direction
Type
Array

hasEdge(source, target)

return true if there is an edge between source, target
Parameters:
Name Type Description
source
target
Source:
Returns:
boolean

removeEdge(edge)

remove an edge from the graph. Removed references to that edge from each vertex
Parameters:
Name Type Description
edge
Source:

removeEdgelessVertices()

helper for making pretty graphs. Remove any vertex that has no edge
Source:

removeVertex(vertex) → {Array}

remove a vertex from the graph
Parameters:
Name Type Description
vertex algo.core.GraphVertex
Source:
Returns:
any edges that were removed in addition to the vertex that was removed
Type
Array