Skip to content

Commit 3371243

Browse files
committed
Merge pull request mgechev#10 from AndreyGeonya/first-docs
add first docs, fix readme
2 parents 023167c + 5dda0d1 commit 3371243

File tree

10 files changed

+288
-235
lines changed

10 files changed

+288
-235
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ var jsdoc = require('gulp-jsdoc'),
33
gulp = require('gulp');
44

55
gulp.task('jsdoc', function () {
6-
gulp.src('./src/**/*.js')
6+
gulp.src(['./src/**/*.js', "readme.md"])
77
.pipe(jsdoc('../javascript-algorithms-docs'));
88
});

readme.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
## Note
1+
## About
22

3-
Note that not all algorithms are well tested so bugs are quite possible.
3+
This repository contains JavaScript implementations of different famous Computer Science algorithms.
44

5-
## About
5+
API reference with usage examples available <a href="https://mgechev.github.io/javascript-algorithms/" target="_blank">here</a>.
6+
7+
*Note: not all algorithms are well tested so bugs are quite possible.*
8+
9+
## Development
10+
11+
To install all dev dependencies use:
12+
13+
```Bash
14+
npm install
15+
```
16+
17+
To setup documentation repository:
18+
19+
* go to the parent directory of the root of `javascript-algorithms`;
20+
* clone `javascript-algorithms` to directory called `javascript-algorithms-docs` (`git clone git@github.com:mgechev/javascript-algorithms.git javascript-algorithms-docs`);
21+
* change current branch in `javascript-algorithms-docs` to `gh-pages` (`git checkout gh-pages`).
22+
23+
To generate documentation call:
24+
25+
`gulp jsdocs` inside `javascript-algorithms` folder. Content of the `javascript-algorithms-docs` will be updated and you will be able to look it in your browser.
626

7-
This repository contains different famous Computer Science algorithms implemented in JavaScript
827

9-
In order to run the tests use:
28+
To run tests use:
1029

1130
```Bash
12-
jasmine-node test/
31+
./node_modules/jasmine-node/bin/jasmine-node test/
1332
```
1433

1534
and all `*.spec.js` files will be executed.
@@ -25,4 +44,4 @@ Initiate the PR.
2544

2645
## License
2746

28-
The code in this repository is distributed under the terms of the MIT license.
47+
The code in this repository is distributed under the terms of the MIT license.

src/graphs/others/topological-sort.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
(function (exports) {
22
'use strict';
33

4-
/**
5-
* Complexity O(|E|), where E is the set
6-
* which contains all edges and |E| is their count.
7-
*/
84
var topologicalSort = (function () {
95

106
function topologicalSortHelper(node, visited, temp, graph, result) {
@@ -25,11 +21,24 @@
2521
}
2622

2723
/**
28-
* Implements the topological sort algorithm.
24+
* Topological sort algorithm of a directed acyclic graph.<br><br>
25+
* Time complexity: O(|E|) where E is a number of edges.
2926
*
3027
* @public
31-
* @param {object} graph A graph represented with list of neighbors
32-
* @return {array} The list containing all nodes in topological sorted order
28+
* @module graphs/others/topological-sort
29+
* @param {Array} graph Adjacency list, which represents the graph.
30+
* @returns {Array} Ordered vertices.
31+
*
32+
* @example
33+
* var topsort = require('path-to-algorithms/src/graphs/others/topological-sort').topologicalSort;
34+
* var graph = {
35+
* v1: ['v2', 'v5'],
36+
* v2: [],
37+
* v3: ['v1', 'v2', 'v4', 'v5'],
38+
* v4: [],
39+
* v5: []
40+
* };
41+
* var vertices = topsort(graph); // ['v3', 'v4', 'v1', 'v5', 'v2']
3342
*/
3443
return function (graph) {
3544
var result = [],
@@ -46,5 +55,4 @@
4655

4756
exports.topologicalSort = topologicalSort;
4857

49-
}(typeof exports === 'undefined' ? window : exports));
50-
58+
}(typeof exports === 'undefined' ? window : exports));

src/graphs/searching/bfs.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414

1515
/**
1616
* Breath-First graph searching algorithm.
17-
* Returns the shortest path between startNode and targetNode.
18-
* Time complexity O(|V|*|V|).
17+
* Returns the shortest path between startNode and targetNode.<br><br>
18+
* Time complexity: O(|V|^2).
1919
*
20-
* @module graphs/searching
2120
* @public
22-
* @param {array} graph The adjust matrix, which represents the graph
23-
* @param {number} startNode The start node
24-
* @param {number} targetNode The target, which should be reached
21+
* @module graphs/searching/bfs
2522
* @param {Array} graph Adjacency matrix, which represents the graph.
26-
* @returns {array} The shortest path from startNode to targetNode
2723
* @param {Number} startNode Start node.
2824
* @param {Number} targetNode Target, which should be reached.
2925
* @returns {Array} Shortest path from startNode to targetNode.
@@ -65,4 +61,4 @@
6561

6662
exports.bfs = bfs;
6763

68-
}((typeof window === 'undefined') ? module.exports : window));
64+
}((typeof window === 'undefined') ? module.exports : window));

src/graphs/searching/dfs.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
/**
2-
* Depth-First Search graph searching algorithm.
3-
* Finds out whether there's a path between
4-
* two nodes - start node and a target.
5-
*/
6-
71
(function (exports) {
82
'use strict';
93

104
var dfs = (function () {
115

12-
/**
13-
* Implements an iterative DFS.
14-
* Complexity O(|V|^2), since it uses adjust matrix.
15-
*/
166
function hasPath(graph, current, goal) {
177
var stack = [],
188
visited = [],
@@ -35,14 +25,26 @@
3525
}
3626

3727
/**
38-
* Returns whether there's a path between two nodes
39-
* in a graph represented with adjust matrix.
28+
* Depth-First graph searching algorithm.
29+
* Returns whether there's a path between two nodes in a graph.<br><br>
30+
* Time complexity: O(|V|^2).
4031
*
32+
* @module graphs/searching/dfs
4133
* @public
42-
* @param {array} graph Adjust matrix representation of a graph
43-
* @param {number} start A start node
44-
* @param {number} goal The target node
45-
* @return {boolean} Returns whether there's a path between start and goal
34+
* @param {Array} graph Adjacency matrix, which represents the graph.
35+
* @param {Number} start Start node.
36+
* @param {Number} goal Target node.
37+
* @return {Boolean} Returns true if path between two nodes exists.
38+
*
39+
* @example
40+
* var dfs = require('../src/graphs/searching/dfs').dfs;
41+
* var graph = [[1, 1, 0, 0, 1, 0],
42+
* [1, 0, 1, 0, 1, 0],
43+
* [0, 1, 0, 1, 0, 0],
44+
* [0, 0, 1, 0, 1, 1],
45+
* [1, 1, 0, 1, 0, 0],
46+
* [0, 0, 0, 1, 0, 0]];
47+
* var pathExists = dfs(graph, 1, 5); // true
4648
*/
4749
return function (graph, start, goal) {
4850
return hasPath(graph, start, goal);
@@ -51,4 +53,4 @@
5153

5254
exports.dfs = dfs;
5355

54-
}(typeof exports === 'undefined' ? window : exports));
56+
}(typeof exports === 'undefined' ? window : exports));

src/graphs/shortest-path/bellman-ford.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,42 @@
77
this.weight = weight;
88
}
99

10-
// Complexity O(|V||E|)
10+
/**
11+
* Bellman–Ford algorithm computes shortest paths from a single source
12+
* vertex to all of the other vertices in a weighted digraph (negative weights allowed).<br><br>
13+
* Time complexity: O(|V||E|) where V and E are the number of vertices and edges respectively.
14+
*
15+
* @public
16+
* @module graphs/shortest-path/bellman-ford
17+
* @param {Array} vertexes Vertices of the graph.
18+
* @param {Array} edges Edges of the graph.
19+
* @param {Number} source Start vertex.
20+
* @returns {Object} Object with two arrays (parents and distances) with shortest-path information.
21+
*
22+
* @example
23+
* require('path-to-algorithms/src/graphs/shortest-path/bellman-ford');
24+
*
25+
* var glob = (typeof window === 'undefined') ? global : window;
26+
* var Edge = glob.Edge;
27+
* var bellmanFord = glob.bellmanFord;
28+
* var edges = [];
29+
* var vertexes = [0, 1, 2, 3, 4];
30+
*
31+
* edges.push(new Edge(0, 1, -1));
32+
* edges.push(new Edge(0, 2, 4));
33+
* edges.push(new Edge(1, 2, 3));
34+
* edges.push(new Edge(1, 3, 2));
35+
* edges.push(new Edge(3, 1, 1));
36+
* edges.push(new Edge(4, 3, -3));
37+
* edges.push(new Edge(1, 4, 2));
38+
* edges.push(new Edge(3, 2, 5));
39+
*
40+
* // {
41+
* // parents: { '0': null, '1': 0, '2': 1, '3': 4, '4': 1 },
42+
* // distances: { '0': 0, '1': -1, '2': 2, '3': -2, '4': 1 }
43+
* // }
44+
* var pathInfo = bellmanFord(vertexes, edges, 0);
45+
*/
1146
function bellmanFord(vertexes, edges, source) {
1247
var distances = {}, parents = {}, c;
1348
for (var i = 0; i < vertexes.length; i += 1) {
@@ -38,20 +73,4 @@
3873
global.Edge = Edge;
3974
global.bellmanFord = bellmanFord;
4075

41-
}((typeof window === 'undefined') ? global : window));
42-
43-
// var glob = (typeof window === 'undefined') ? global : window;
44-
// var Edge = glob.Edge;
45-
// var bellmanFord = glob.bellmanFord;
46-
// var edges = [];
47-
// var vertexes = [0, 1, 2, 3, 4];
48-
// edges.push(new Edge(0, 1, -1));
49-
// edges.push(new Edge(0, 2, 4));
50-
// edges.push(new Edge(1, 2, 3));
51-
// edges.push(new Edge(1, 3, 2));
52-
// edges.push(new Edge(3, 1, 1));
53-
// edges.push(new Edge(4, 3, -3));
54-
// edges.push(new Edge(1, 4, 2));
55-
// edges.push(new Edge(3, 2, 5));
56-
//
57-
// console.log(bellmanFord(vertexes, edges, 0));
76+
}((typeof window === 'undefined') ? global : window));

0 commit comments

Comments
 (0)