A JSON-format backed graph library with advanced identification algorithms.
JSON Scheme (DirectedGraph):
Documentation
DirectedGraph
You can use the DirectedGraph
library in your project by either calling
var DirectedGraph = DirectedGraph;
or
var DirectedGraph = DG;
function DirectedGraph([struct])
Creates a directed graph based on the structure defined (a .json
object matching the specification), or creates a graph with no edges and nodes if struct
is not specified.
An example struct would look like the following (used in the remainder of the documentation)
In order to create a graph, you'll want to parse your JSON object and pass it as a parameter. For example,
//g is the JSON parsed object of graph.json located in the executing directoryvar g = JSON; //t_graph is the graph created from graph.jsonvar t_graph = g;
Or, you can create a graph without specifying a JSON file:
var t_graph = ;
The graph above contains no nodes and no edges.
DirectedGraph.prototype.edgesIn = function (node)
Returns the number of edges entering a given node.
Example:
var g = JSON;var t_graph = g; var x = t_graph;console; // #==> for the test graph, should print 'A'
DirectedGraph.prototype.edgesOut = function (node)
Returns the number of edges exiting a given node.
Example:
var g = JSON;var t_graph = g; var x = t_graph;console // #==> for the test graph, should print 'B'
DirectedGraph.prototype.getNode = function (id)
Returns a node contained in the graph with the given name (id
).
Example:
var g = JSON;var t_graph = g; var node = t_graph; // #==> assigns the variable node to the node of the graph with name 'C'
DirectedGraph.prototype.numNodes = function ()
Returns the number of nodes contained in the graph.
Example:
var g = JSON;var t_graph = g; console; // #==> prints '3' for the test graph
DirectedGraph.prorotype.getEdge = function (id)
Returns an edge contained in the graph with the given name (id
).
Example:
var g = JSON;var t_graph = g; var edge = t_graph; // #==> assigns the variable edge to the edge of the graph with name 'BC'
DirectedGraph.prototype.numEdges = function ()
Returns the number of edges contained in the graph.
Example:
var g = JSON;var t_graph = g; console // #==> prints '2' for the test graph
DirectedGraph.prototype.edges = function ()
Returns the edges array internally stored in graph-json
.
Example:
var g = JSON;var t_graph = g; var edge_array = t_graph;
DirectedGraph.prototype.isTerminal = function (node)
Returns true
if the specified node is ternminal (has no children), and false otherwise.
Example:
var g = JSON;var t_graph = g; console; // #==> prints 'true' for the test graph
DirectedGraph.prototype.dfs = function (to_find, node, graph)
Performs a depth-first-search on the given graph, searching for to_find
starting from node
.
Example:
var g = JSON;var t_graph = g; console
function hangingEdges(graph, edges)
Returns an array of the "hanging edges" - edges that either have no from
or to
node defined - in the current graph schema.
var g = JSON;var t_graph = g; var x = ;if xlength !== 0 return 'Hanging Edges Found: ' x; // # ==> uh-oh! We have hanging edges in the graph.// #==> seeing as the test graph is properly defined, no validation error will be thrown.
DirectedGraph.prototype.addNode = function (name, [dt])
Adds a node to the graph with an optional parameter containing data.
Example:
var g = JSON;var t_graph = g; t_graph; // adds node 'A' to the grapht_graph; // adds node 'B' to the graph, with data xyz
DirectedGraph.prototype.addEdge = function (name, from, to, [dt])
Adds an edge to the graph with an optional parameter containing data.
Example:
var g = JSON;var t_graph = g; t_graph; // adds edge 'A->B' to the graph from node A to node Bt_graph; // adds edge 'A->C' to the graph from node A to node C with data 10
DirectedGraph.prototype.add = function([nodes...])
Adds any number of nodes to a graph, automatically creating the nodes if they do not exist and creating edges between each adjacent node.
Example:
var g = JSON;var t_graph = g; // the following creates nodes 'a', 'b', 'c', and 'd' if they do not exist,// and then creates the edges a->b, b->c, and c->dt_graph;
DirectedGraph.prototype.tSort = function ()
Topologically sorts the graph and returns the resulting node array.
Example:
var g = JSON;var t_graph = g; console; //prints a comma-delimited topologically sorted node order of t_graph
DirectedGraph.prototype.isAcyclic = function ()
Returns true
if the graph is acyclic, false
otherwise. Uses a topological sort under the hood.
Example:
var g = JSON;var t_graph = g; ift_graph //do somethingelse //do something else
UndirectedGraph
Need to add this documentation.