Skip to content

Commit 6cfc039

Browse files
committed
Merge branch 'tayllan-master'
2 parents fad0f53 + 1797907 commit 6cfc039

File tree

5 files changed

+356
-1
lines changed

5 files changed

+356
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright (C) 2014 Tayllan Búrigo
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"], to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
'use strict';
23+
24+
var time = 0,
25+
visitedNodes = {},
26+
finishingTimes = {};
27+
28+
/**
29+
* Depth First Search for all the vertices in the graph
30+
* Worst Case Complexity: O(V * E)
31+
*
32+
* @param {Object}
33+
* @return {Object} representing the order in which the
34+
* vertices are visited
35+
*/
36+
var dfsAdjacencyListStart = function (graph) {
37+
graph.vertices.forEach(function (v) {
38+
if (visitedNodes[v] !== true) {
39+
dfsAdjacencyList(graph, v);
40+
}
41+
});
42+
43+
return finishingTimes;
44+
};
45+
46+
/**
47+
* Depth First Search for the vertices reachable from 'startNode'
48+
* Worst Case Complexity: O(V * E)
49+
*
50+
* @param {Object}
51+
* @param {Object}
52+
* @return {Object}
53+
*/
54+
var dfsAdjacencyList = function (graph, startNode) {
55+
visitedNodes[startNode] = true;
56+
57+
graph.neighbors(startNode).forEach(function (v) {
58+
if (visitedNodes[v] !== true) {
59+
dfsAdjacencyList(graph, v);
60+
}
61+
});
62+
63+
finishingTimes[startNode] = time++;
64+
65+
return finishingTimes;
66+
};
67+
68+
var depthFirstSearch = {
69+
dfsAdjacencyList: dfsAdjacencyList,
70+
dfsAdjacencyListStart: dfsAdjacencyListStart
71+
};
72+
73+
module.exports = depthFirstSearch;

algorithms/sorting/radix_sort.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* Copyright (C) 2014 Tayllan Búrigo
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
'use strict';
23+
24+
/**
25+
* Sorts an array of objects according to their 'key' property
26+
* Every object inside the array MUST have the 'key' property with
27+
* a integer value.
28+
*
29+
* Asymptotic Complexity: O(array.length * d), where 'd' represents
30+
* the amount of digits in the larger key of the array
31+
*
32+
* @param Array
33+
* @return Array
34+
*/
35+
var radixSort = function (array) {
36+
var max = maximumKey(array);
37+
var digitsMax = amountOfDigits(max);
38+
39+
for (var i = 0; i < digitsMax; i++) {
40+
array = auxiliaryCountingSort(array, i);
41+
}
42+
43+
return array;
44+
};
45+
46+
/**
47+
* Auxiliary sorting method for RadixSort
48+
* Sorts an array of objects according to only one digit of
49+
* their 'key' property. The digit to be sorted is determined
50+
* by the 'mod' variable
51+
* Every object inside the array MUST have the 'key' property with
52+
* a integer value.
53+
*
54+
* Execution Time: (2 * array.length + 10)
55+
* Asymptotic Complexity: O(array.length)
56+
*
57+
* @param Array
58+
* @return Array
59+
*/
60+
var auxiliaryCountingSort = function (array, mod) {
61+
var length = array.length;
62+
var bucket = [];
63+
64+
for (var i = 0; i < 10; i++) {
65+
bucket[i] = [];
66+
}
67+
68+
for (i = 0; i < length; i++) {
69+
var digit = parseInt((array[i].key / Math.pow(10, mod)).toFixed(mod)) % 10;
70+
bucket[digit].push(array[i]);
71+
}
72+
73+
var pointer = 0;
74+
75+
for (i = 0; i < 10; i++) {
76+
var localLength = bucket[i].length;
77+
78+
for (var j = 0; j < localLength; j++) {
79+
array[pointer++] = bucket[i][j];
80+
}
81+
}
82+
83+
return array;
84+
};
85+
86+
/**
87+
* Finds the maximum key from an array of objects
88+
*
89+
* Asymptotic Complexity: O(array.length)
90+
*
91+
* @param Array
92+
* @return Integer if array non-empty
93+
* Undefined otherwise
94+
*/
95+
var maximumKey = function (array) {
96+
var length = array.length;
97+
98+
if (length > 0) {
99+
var max = array[0].key;
100+
101+
for (var i = 1; i < length; i++) {
102+
if (array[i].key > max) {
103+
max = array[i].key;
104+
}
105+
}
106+
107+
return max;
108+
}
109+
else {
110+
return undefined;
111+
}
112+
};
113+
114+
/**
115+
* Returns the amount of digits contained in a number
116+
*
117+
* Asymptotic Complexity: O(d), where 'd' represents the
118+
* amount of digits in the number
119+
*
120+
* @param Number
121+
* @return Number
122+
*/
123+
var amountOfDigits = function (number) {
124+
if (number === 0) {
125+
return 1;
126+
}
127+
else {
128+
var counter = 0;
129+
130+
// For positive numbers
131+
while (parseInt(number) > 0) {
132+
number /= 10;
133+
++counter;
134+
}
135+
136+
// For negative numbers
137+
while (parseInt(number) < 0) {
138+
number /= 10;
139+
++counter;
140+
}
141+
142+
return counter;
143+
}
144+
};
145+
146+
module.exports = radixSort;

main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var lib = {
2828
SPFA: require('./algorithms/graph/SPFA'),
2929
bellmanFord: require('./algorithms/graph/bellman_ford'),
3030
eulerPath: require('./algorithms/graph/euler_path'),
31+
depthFirstSearch: require('./algorithms/graph/depth_first_search')
3132
},
3233
Math: {
3334
fibonacci: require('./algorithms/math/fibonacci'),
@@ -47,7 +48,8 @@ var lib = {
4748
heapSort: require('./algorithms/sorting/heap_sort'),
4849
mergeSort: require('./algorithms/sorting/merge_sort'),
4950
quicksort: require('./algorithms/sorting/quicksort'),
50-
selectionSort: require('./algorithms/sorting/selection_sort')
51+
selectionSort: require('./algorithms/sorting/selection_sort'),
52+
radixSort: require('./algorithms/sorting/radix_sort')
5153
},
5254
String: {
5355
editDistance: require('./algorithms/string/edit_distance'),
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (C) 2014 Tayllan Búrigo
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"], to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
'use strict';
23+
24+
var depthFirstSearch = require('../../../algorithms/graph/depth_first_search'),
25+
Graph = require('../../../data_structures/graph'),
26+
assert = require('assert'),
27+
graph = new Graph(true);
28+
29+
graph.addEdge('one', 'three');
30+
graph.addEdge('one', 'four');
31+
graph.addEdge('four', 'two');
32+
graph.addEdge('two', 'one');
33+
graph.addEdge('three', 'one');
34+
graph.addEdge('five', 'six');
35+
36+
describe('Depth First Search Algorithm', function () {
37+
it('should visit only the nodes reachable from the node {one} (inclusive)',
38+
function () {
39+
var finishingTimes = depthFirstSearch.dfsAdjacencyList(graph, 'one');
40+
41+
assert.equal(finishingTimes.five, undefined);
42+
assert.equal(finishingTimes.six, undefined);
43+
44+
assert.equal(finishingTimes.one, 3);
45+
assert.equal(finishingTimes.two, 1);
46+
assert.equal(finishingTimes.three, 0);
47+
assert.equal(finishingTimes.four, 2);
48+
}
49+
);
50+
51+
it('should visit all the nodes in the graph',
52+
function () {
53+
var finishingTimes = depthFirstSearch.dfsAdjacencyListStart(graph);
54+
55+
assert.equal(finishingTimes.one, 3);
56+
assert.equal(finishingTimes.two, 1);
57+
assert.equal(finishingTimes.three, 0);
58+
assert.equal(finishingTimes.four, 2);
59+
assert.equal(finishingTimes.five, 5);
60+
assert.equal(finishingTimes.six, 4);
61+
}
62+
);
63+
});
64+

test/algorithms/sorting/radix_sort.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright (C) 2014 Tayllan Búrigo
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"], to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
'use strict';
23+
24+
var radixSort = require('../../../algorithms/sorting/radix_sort'),
25+
assert = require('assert');
26+
27+
var firstObject = {
28+
someProperty: 'The',
29+
key: 88541234132
30+
};
31+
32+
var secondObject = {
33+
someProperty: 'winter',
34+
key: 90071992540992
35+
};
36+
37+
var thirdObject = {
38+
someProperty: 'is',
39+
key: 0
40+
};
41+
42+
var fourthObject = {
43+
someProperty: 'coming',
44+
key: 65234567,
45+
anotherProperty: '!'
46+
};
47+
48+
var array = [
49+
thirdObject,
50+
fourthObject,
51+
firstObject,
52+
secondObject,
53+
secondObject,
54+
firstObject,
55+
firstObject,
56+
fourthObject
57+
];
58+
59+
describe('Radix Sort', function () {
60+
it('should sort the given array', function () {
61+
array = radixSort(array);
62+
63+
// Asserts that the array is truly sorted
64+
assert.deepEqual(array.indexOf(thirdObject), 0);
65+
assert.deepEqual(array.indexOf(fourthObject), 1);
66+
assert.deepEqual(array.indexOf(firstObject), 3);
67+
assert.deepEqual(array.indexOf(secondObject), 6);
68+
assert.deepEqual(array.indexOf({key: 99}), -1);
69+
});
70+
});

0 commit comments

Comments
 (0)