1+ /**
2+ * Creates graph structure
3+ *
4+ * @returns {graph } Returns graph object
5+ */
16primitives . common . graph = function ( ) {
27 var _edges = { } ,
38 MAXIMUMTOTALWEIGHT = 1 ,
49 MINIMUMWEIGHT = 2 ;
510
11+ /**
12+ * Adds edge to the graph
13+ * @param {string } from The id of the start node
14+ * @param {string } to The id of the end node
15+ * @param {object } edge The edge contextual object
16+ */
617 function addEdge ( from , to , edge ) {
718 if ( ( _edges [ from ] == null || _edges [ from ] [ to ] == null ) && edge != null ) {
819
@@ -18,6 +29,13 @@ primitives.common.graph = function () {
1829 }
1930 }
2031
32+ /**
33+ * Returns edge context object
34+ *
35+ * @param {string } from The edge's from node id
36+ * @param {string } to The edge's to node id
37+ * @returns {object } The edge's context object
38+ */
2139 function edge ( from , to ) {
2240 var result = null ;
2341 if ( _edges [ from ] != null && _edges [ from ] [ to ] ) {
@@ -26,11 +44,32 @@ primitives.common.graph = function () {
2644 return result ;
2745 }
2846
47+ /**
48+ * Returns true if node exists in the graph
49+ *
50+ * @param {string } from The node id
51+ * @returns {boolean } Returns true if node exists
52+ */
2953 function hasNode ( from ) {
3054 return _edges . hasOwnProperty ( from ) ;
3155 }
3256
33- function loopNodeEdges ( thisArg , itemid , onEdge ) { // onEdge = function(to, edge) {}
57+ /**
58+ * Callback for itterating edges of the graph's node
59+ *
60+ * @callback onEdgeCallback
61+ * @param {string } to The neighbouring node id
62+ * @param {Object } edge The edge's context object
63+ */
64+
65+ /**
66+ * Loop edges of the node
67+ *
68+ * @param {object } thisArg The callback function invocation context
69+ * @param {string } itemid The node id
70+ * @param {onEdgeCallback } onEdge A callback function to call for every edge of the node
71+ */
72+ function loopNodeEdges ( thisArg , itemid , onEdge ) {
3473 var neighbours , neighbourKey ;
3574 if ( onEdge != null ) {
3675 neighbours = _edges [ itemid ] ;
@@ -44,7 +83,22 @@ primitives.common.graph = function () {
4483 }
4584 }
4685
47- function loopNodes ( thisArg , startNode , onItem ) { // onItem = function(itemid) {}
86+ /**
87+ * Callback function for itterating graphs nodes
88+ *
89+ * @callback onNodeCallback
90+ * @param {string } to The next neighbouring node id
91+ */
92+
93+ /**
94+ * Loop nodes of the graph
95+ *
96+ * @param {object } thisArg The callback function invocation context
97+ * @param {string } [itemid=undefined] The optional start node id. If start node is undefined,
98+ * function loops graphs node starting from first available node
99+ * @param {onNodeCallback } onItem A callback function to be called for every neighbouring node
100+ */
101+ function loopNodes ( thisArg , startNode , onItem ) {
48102 var processed = { } ;
49103 if ( startNode == null ) {
50104 for ( startNode in _edges ) {
@@ -59,8 +113,7 @@ primitives.common.graph = function () {
59113 }
60114 }
61115
62- function _loopNodes ( thisArg , startNode , processed , onItem ) { // onItem = function(itemid) {}
63- /* Graph */
116+ function _loopNodes ( thisArg , startNode , processed , onItem ) {
64117 var margin = [ ] ,
65118 marginKey ,
66119 newMargin ,
@@ -92,17 +145,23 @@ primitives.common.graph = function () {
92145 }
93146 }
94147
95- /*
96- Function: primitives.common.graph.getSpanningTree
97- Get maximum spanning tree. Graph may have disconnected sub graphs, so start node is nessasary.
98-
99- Parameters:
100- startNode - The node to start searching for maximum spanning tree. Graph is not nessasary connected
101- getWeightFunc - Call back function to get weight of edge. function(edge)
102-
103- Returns:
104- primitives.common.tree structure
105- */
148+ /**
149+ * Callback for finding edge weight
150+ *
151+ * @callback getGraphEdgeWeightCallback
152+ * @param {object } edge The edge context object
153+ * @param {string } fromItem The edge's start node id
154+ * @param {string } toItem The edge's end node id
155+ * @return {number } Returns weight of the edge
156+ */
157+
158+ /**
159+ * Get maximum spanning tree. Graph may have disconnected sub graphs, so start node is nessasary.
160+ *
161+ * @param {string } startNode The node to start searching for maximum spanning tree. Graph is not nessasary connected
162+ * @param {getGraphEdgeWeightCallback } getWeightFunc Callback function to get weight of an edge.
163+ * @returns {tree } Returns tree structure containing maximum spanning tree of the graph
164+ */
106165 function getSpanningTree ( startNode , getWeightFunc ) {
107166 var result = primitives . common . tree ( ) ,
108167 margin = primitives . common . FibonacciHeap ( true ) ,
@@ -170,31 +229,27 @@ primitives.common.graph = function () {
170229 return result ;
171230 }
172231
173- /*
174- Function: primitives.common.graph.getTotalWeightGrowthSequence
175- Get graph growth sequence. The sequence of graph traversing order.
176-
177- Parameters:
178- thisArg - call back functions context
179- onEdgeWeight - Call back function to weight edge of graph. function(edge)
180- onItem - Call back function on next item found
181- */
232+ /**
233+ * Get graph growth sequence. The sequence of graph traversing order.
234+ *
235+ * @param {object } thisArg The callback function invocation context
236+ * @param {getGraphEdgeWeightCallback } getWeightFunc Callback function to get weight of an edge.
237+ * @param {onNodeCallback } onItem A callback function to be called for every node of the growth sequence
238+ */
182239 function getTotalWeightGrowthSequence ( thisArg , onEdgeWeight , onItem ) {
183240 var startNode = _findStartNode ( thisArg , onEdgeWeight ) ;
184241
185242 _getGrowthSequence ( thisArg , startNode , onEdgeWeight , onItem , MAXIMUMTOTALWEIGHT ) ;
186243 }
187244
188- /*
189- Function: primitives.common.graph.getMinimumWeightGrowthSequence
190- Get graph growth sequence. The sequence of graph traversing order.
191-
192- Parameters:
193- thisArg - call back functions context
194- startNode - The node to start searching for grows sequence.
195- onEdgeWeight - Call back function to weight edge of graph. function(edge)
196- onItem - Call back function on next item found
197- */
245+ /**
246+ * Get minimum weight graph growth sequence. The sequence of the traversing order of the graph nodes.
247+ *
248+ * @param {object } thisArg The callback function invocation context
249+ * @param {string } [startNode=undefined] The optional start node id
250+ * @param {getGraphEdgeWeightCallback } onEdgeWeight Callback function to get weight of an edge.
251+ * @param {onNodeCallback } onItem A callback function to be called for every node of the growth sequence
252+ */
198253 function getMinimumWeightGrowthSequence ( thisArg , startNode , onEdgeWeight , onItem ) {
199254 _getGrowthSequence ( thisArg , startNode , onEdgeWeight , onItem , MINIMUMWEIGHT ) ;
200255 }
@@ -300,19 +355,26 @@ primitives.common.graph = function () {
300355 }
301356 }
302357
303- /*
304- Function: primitives.common.graph.getShortestPath
305- Get shortest path between two nodes in graph. Start and end nodes supposed to have connection path. All connections have the same weight.
306-
307- Parameters:
308- startNode - The node to start.
309- endNode - The end node.
310- getWeightFunc - Call back function to weight edge of graph. function(edge, fromItem, toItem)
311-
312- Returns:
313- Array containing nodes names of connection path.
314- */
315- function getShortestPath ( thisArg , startNode , endNodes , getWeightFunc , onPathFound ) { // getWeightFunc = function(edge, fromItem, toItem), onPathFound = function(path, to)
358+ /**
359+ * Callback for returning optimal connection path for every end node.
360+ *
361+ * @callback onPathFoundCallback
362+ * @param {string[] } path An array of connection path node ids.
363+ * @param {string } to The end node id, the connection path is found for.
364+ */
365+
366+ /**
367+ * Get shortest path between two nodes in graph. The start and the end nodes are supposed to have connection path.
368+ *
369+ * @param {object } thisArg The callback function invocation context
370+ * @param {string } startNode The start node id
371+ * @param {string[] } endNodes The array of end node ids.
372+ * @param {getGraphEdgeWeightCallback } getWeightFunc Callback function to get weight of an edge.
373+ * @param {onNodeCallback } onItem A callback function to be called for every node of the growth sequence
374+ * @param {onPathFoundCallback } onPathFound A callback function to be called for every end node
375+ * with the optimal connection path
376+ */
377+ function getShortestPath ( thisArg , startNode , endNodes , getWeightFunc , onPathFound ) {
316378 var margin = primitives . common . FibonacciHeap ( false ) ,
317379 distance = { } ,
318380 breadcramps = { } ,
0 commit comments