Computation over graphs. Unless otherwise specified a graph is a simple JS object whose properties are interpreted as nodes that refer to arrays whose elements describe edges.

var testGraph = {
  "a": ["b", "c"],
  "b": ["c", "d", "e", "f"],
  "d": ["c", "f"],
  "e": ["a", "f"],
  "f": []
}

Methods

(inner) clone(graph) → {Object.<string, Array.<string>>}

Returns a copy of graph map.

Parameters:
NameTypeDescription
graphObject.<string, Array.<string>>

The map to copy.

Returns:

The copy of the graph.

Type: 
Object.<string, Array.<string>>

(inner) hull(g, id, ignoredKeyList, maxDepthopt) → {Array.<string>}

Takes a graph in object format and a start id and then traverses the graph and gathers all nodes that can be reached from that start id. Returns a list of those nodes. Optionally use ignore list to filter out certain nodes that shouldn't be considered and maxDepth to stop early. By default a maxDepth of 20 is used.

Parameters:
NameTypeAttributesDescription
gObject.<string, Array.<string>>

The graph to traverse.

idstring

The id of the node to start the hull computation from.

ignoredKeyListArray.<string>

The ids of the nodes to skip from the traversal.

maxDepthnumber<optional>

The maximum recursion depth of the traversal.

Returns:

The list of ids of all the nodes inside the hull.

Type: 
Array.<string>
Example
var testGraph = {
"a": ["b", "c"],
"b": ["c", "d", "e", "f"],
"d": ["c", "f"],
"e": ["a", "f"],
"f": []
}
hull(testGraph, "d") // => ["c", "f"]
hull(testGraph, "e") // => ['a', 'f', 'b', 'c', 'd', 'e']
hull(testGraph, "e", ["b"]) // => ["a", "f", "c"]

(inner) invert(g) → {Object.<string, Array.<string>>}

Inverts the references of graph object g.

Parameters:
NameTypeDescription
gObject.<string, Array.<string>>

The graph to invert.

Returns:

The inverted graph.

Type: 
Object.<string, Array.<string>>
Example
invert({a: ["b"], b: ["a", "c"]})
  // => {a: ["b"], b: ["a"], c: ["b"]}

(inner) random(nKeysopt) → {Object.<string, Array.<string>>}

Generates a graph based on a given number of ids that should appear.

Parameters:
NameTypeAttributesDefaultDescription
nKeysnumber<optional>
10

The number of ids inside the generated graph.

Returns:

A graph with the provided number of ids initialized at random.

Type: 
Object.<string, Array.<string>>

(inner) reduce(doFunc, graph, rootNode, carryOver, context) → {*}

Starts with rootNode and visits all (in)directly related nodes, calling doFunc at each node. The result of doFunc is passed as first argument to the next iterator call. For the first call the value carryOver is used.

Parameters:
NameTypeDescription
doFuncfunction

The function applied to each visited node.

graphObject.<string, Array.<string>>

The graph to traverse.

rootNodestring

The id of the node to start the traversal from.

carryOver*

The value to feed into the first call of doFunc.

contextObject

The object to bind this to when calling the doFunc.

Returns:

The final result of the reducer. Example: var depGraph = {a: ["b", "c"],b: ["c"]} reduce((_, ea, i) => console.log("%s %s", ea, i), depGraph, "a")

Type: 
*

(inner) sortByReference(depGraph, startNode) → {Array.<Array.<string>>}

Sorts graph into an array of arrays. Each "bucket" contains the graph nodes that have no other incoming nodes than those already visited. This means, we start with the leaf nodes and then walk our way up. This is useful for computing how to traverse a dependency graph: You get a sorted list of dependencies that also allows circular references.

Parameters:
NameTypeDescription
depGraphObject.<string, Array.<string>>

The graph to sort into buckets.

startNodestring

The id of the node at the top of the dependency graph.

Returns:

The sorted sets of nodes.

Type: 
Array.<Array.<string>>
Example
var depGraph = {a: ["b", "c"], b: ["c"], c: ["b"]};
sortByReference(depGraph, "a");
// => [["c"], ["b"], ["a"]]

(inner) subgraphReachableBy(graphMap, startId, ignore, maxDepthopt) → {Object.<string, Array.<string>>}

Like hull but returns subgraph map of graphMap.

Parameters:
NameTypeAttributesDescription
graphMapObject.<string, Array.<string>>

The graph to compute the subgraph for.

startIdstring

The id of the node to start the subgraph computation from.

ignoreArray.<string>

The ids of the nodes to skip from the traversal.

maxDepthnumber<optional>

The maximum recursion depth of the traversal.

See
  • hull
Returns:

The subgraph reachable from id.

Type: 
Object.<string, Array.<string>>
Example
subgraphReachableBy(testGraph, "e", [], 2);
// => {e: [ 'a', 'f' ], a: [ 'b', 'c' ], f: []}

(inner) without(graph, ids) → {Object.<string, Array.<string>>}

Returns a copy of graph map with the given ids removed.

Parameters:
NameTypeDescription
graphObject.<string, Array.<string>>

The map to copy.

idsArray.<string>

The list of ids to exclude in the copy.

Returns:

The (filtered) copy of the graph.

Type: 
Object.<string, Array.<string>>