forked from jacomyal/sigma.js
-
Notifications
You must be signed in to change notification settings - Fork 2
Graph API
Alexis Jacomy edited this page Dec 3, 2013
·
6 revisions
This class implements sigma's graph model. It is also usable as a standalone to deal with graph data in your applications.
-
nodes() : array
-
nodes( string ) : object
-
nodes( array ) : array
- This methods is used to retrieve nodes from the graph. If no argument is given, then an array containing references to every nodes will be returned. The method can also be called with the ID of an node to only retrieve the related node, or an array of IDs to obtain an array of the specified nodes.
-
edges() : array
-
edges( string ) : object
-
edges( array ) : array
- This methods is used to retrieve edges from the graph. If no argument is given, then an array containing references to every edges will be returned. The method can also be called with the ID of an edge to only retrieve the related edge, or an array of IDs to obtain an array of the specified edges.
-
addNode( object ) : graph
- This method adds a node to the graph. The node must be an object, with a string under the key
id
. Except for this, it is possible to add any other attribute, that will be preserved all along the manipulations. Also, the node will be cloned when added to the graph, to prevent itsid
to be writable in the future. - The method returns the graph instance.
- This method adds a node to the graph. The node must be an object, with a string under the key
-
addEdge( object ) : graph
- This method adds an edge to the graph. The edge must be an object, with a string under the key
id
, and strings under the keyssource
andtarget
that are existing node IDs. Except for this, it is possible to add any other attribute, that will be preserved all along the manipulations. The edge will be cloned when added to the graph, to prevent itsid
,source
andtarget
to be writable in the future. - The method returns the graph instance.
- This method adds an edge to the graph. The edge must be an object, with a string under the key
-
dropNode( string ) : graph
- This method takes an existing node
id
as argument and drops the related node from the graph. It also removes each edge that is bound to it, through thedropEdge
method. An error is thrown if the node does not exist. - The method returns the graph instance.
- This method takes an existing node
-
dropEdge( string ) : graph
- This method takes an existing edge
id
as argument and drops the related edge from the graph. An error is thrown if the edge does not exist. - The method returns the graph instance.
- This method takes an existing edge
-
degree( string, ?string ) : number
-
degree( array, ?string ) : array
- This methods is used to retrieve the degree of one or several nodes. If the ID of a node is given, then its degree will be returned. If an array of existing node IDs is given, then the array of the corresponding degrees will be returned instead.
- It is also possible to specify as second argument the type of degree to retrieve:
- By default the disconnected degree will be used (the number of edges that are connected to the node).
- If
"in"
is given as second argument, then the incoming degrees will be returned (the number of edges that "come from" the node) - If
"out"
is given as second argument, then the outcoming degrees will be returned (the number of edges that "go to" the node)
-
read( object ) : graph
- This method reads an object and adds the nodes and edges, through the proper methods
addNode
andaddEdge
. It then returns the graph. - Here is an example:
- This method reads an object and adds the nodes and edges, through the proper methods
var myGraph = new graph();
myGraph.read({
nodes: [
{ id: 'n0' },
{ id: 'n1' }
],
edges: [
{
id: 'e0',
source: 'n0',
target: 'n1'
}
]
});
console.log(
myGraph.nodes().length,
myGraph.edges().length
); // outputs 2 1
-
clear() : graph
- This method empties the nodes and edges arrays, as well as the different indexes, and then returns the graph. It is faster than removing by hand
dropNode
anddropEdge
, since it basically just "reset" the graph.
- This method empties the nodes and edges arrays, as well as the different indexes, and then returns the graph. It is faster than removing by hand
-
kill( object ) : void
- This method kills the graph. It basically empties each index and methods attached to the graph, and destroys the references.
For people who might want to implement graph algorithms (clustering, graph-specific metrics, etc...), sigma.classes.graph
provides some static methods
- addMethod(string, function)
- attach(string, string, function)
- addIndex(string, object)