1 |
|
2 | //
|
3 | // # Create nodes
|
4 | //
|
5 |
|
6 | var iper = require('iper')
|
7 | , should = require('should');
|
8 |
|
9 | var IperGraph = iper.IperGraph;
|
10 |
|
11 | //
|
12 | // Create an empty graph, and create a node
|
13 | //
|
14 |
|
15 | var graph = new IperGraph();
|
16 | graph.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 |
|
23 | var id1 = graph.createNode([]);
|
24 | var 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 |
|
31 | graph.createEdge([id1, id2]);
|
32 |
|
33 |
|
34 | // We can also get a reference to the node
|
35 | var node1 = graph.getNode(id1);
|
36 |
|
37 | /*
|
38 | // node1.data is an array, so we can push something in it
|
39 | node1.data.push('foo');
|
40 |
|
41 | // we can ask the degree of the node, that is the number of edges it has
|
42 | node1.degree.should.be.eql(1);
|
43 |
|
44 | // It is possible to set a #maxDegree when creating a node
|
45 | var id3 = graph.createNode('example', {maxDegree:2});
|
46 |
|
47 | // node3 will have at most 2 edges
|
48 | var node3 = graph.getNode(id3);
|
49 |
|
50 | // by the way, since we are working with hypergraphs,
|
51 | // edges can join more than 2 nodes
|
52 | graph.createEdge([id1, id2, id3]);
|
53 | node3.degree.should.be.eql(1);
|
54 |
|
55 | // Let' s add another edge to node3, it should be the last
|
56 | graph.createEdge([id1, id3]);
|
57 | node3.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
|
65 | node3.degree.should.be.eql(2);
|
66 |
|
67 | */
|
68 |
|