UNPKG

1.58 kBJavaScriptView Raw
1
2//
3// # Create nodes
4//
5
6var iper = require('iper')
7 , should = require('should');
8
9var IperGraph = iper.IperGraph;
10
11//
12// Create an empty graph, and create a node
13//
14
15var graph = new IperGraph();
16graph.createNode();
17
18//
19// Every node will be given a unique identifier, that is returned by #createNode() as a convenience.
20// Lets create two nodes and store ids to use them later.
21//
22
23var id1 = graph.createNode([]);
24var id2 = graph.createNode([]);
25
26//
27// Now we can use id1 and id2 to refer to the nodes,
28// for example we can create an edge joining the nodes.
29//
30
31graph.createEdge([id1, id2]);
32
33
34// We can also get a reference to the node
35var node1 = graph.getNode(id1);
36
37/*
38// node1.data is an array, so we can push something in it
39node1.data.push('foo');
40
41// we can ask the degree of the node, that is the number of edges it has
42node1.degree.should.be.eql(1);
43
44// It is possible to set a #maxDegree when creating a node
45var id3 = graph.createNode('example', {maxDegree:2});
46
47// node3 will have at most 2 edges
48var node3 = graph.getNode(id3);
49
50// by the way, since we are working with hypergraphs,
51// edges can join more than 2 nodes
52graph.createEdge([id1, id2, id3]);
53node3.degree.should.be.eql(1);
54
55// Let' s add another edge to node3, it should be the last
56graph.createEdge([id1, id3]);
57node3.degree.should.be.eql(2);
58
59// adding another edge should not be possible
60(function () {
61 graph.createEdge([id2, id3]);
62}).should.throwError();
63
64// node3 degree is unchanged
65node3.degree.should.be.eql(2);
66
67*/
68