Create and grow a graph
mainTo start, call createGraph() to initialize an empty graph. You can grow the graph by adding nodes or links.
Adding Nodes:
Use addNode(id, [data]) to add a single node. The id is typically a string or number.
Adding Links:
Use addLink(source, target, [data]) to connect two nodes. If the nodes specified by source and target do not exist in the graph, addLink() will automatically create them.
Removing Elements:
- Use
removeNode(id)to remove a node. - Use
removeLink(link)to remove a link. Note that this method requires the actual link object, not just the IDs. - Use
clear()to remove all nodes and links from the graph.
var createGraph = require('ngraph.graph');
var g = createGraph();
// Adding nodes
g.addNode('hello');
g.addNode('world');
// Adding links (creates nodes if they don't exist)
g.addLink('space', 'bar');
// Connecting existing nodes
g.addLink('hello', 'world');
// Removing
g.removeNode('space');
// Removing a link requires the link object
g.forEachLinkedNode('hello', function(linkedNode, link) {
g.removeLink(link);
});
// Clear everything
g.clear();