Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Third edition #83

Merged
merged 2 commits into from
May 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -30,6 +30,8 @@
"no-restricted-globals": 0,
"no-multi-assign": 0,
"prefer-destructuring": ["error", {"object": true, "array": false}],
"padded-blocks": 0
"padded-blocks": 0,
"no-sparse-arrays": 0,
"array-bracket-spacing": 0
}
}
2 changes: 1 addition & 1 deletion examples/chapter12/04-Dijkstra.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ const graph = [

console.log("********* Dijkstra's Algorithm - Shortest Path ***********");

var dist = dijkstra(graph, 0);
const dist = dijkstra(graph, 0);

for (i = 0; i < dist.length; i++){
console.log(i + '\t\t' + dist[i]);
11 changes: 6 additions & 5 deletions src/ts/algorithms/graph/breadth-first-search.ts
Original file line number Diff line number Diff line change
@@ -15,16 +15,17 @@ const initializeColor = (vertices: (string | number)[]) => {
return color;
};

export const breadthFirstSearch = (graph: Graph, callback: Function) => {
export const breadthFirstSearch = (graph: Graph, startVertex: any, callback: Function) => {
const vertices = graph.getVertices();
const adjList = graph.getAdjList();
const color = initializeColor(vertices);
const queue = new Queue();
queue.enqueue(vertices);

queue.enqueue(startVertex);

while (!queue.isEmpty()) {
const u = queue.dequeue(),
neighbors = adjList.get(u);
const u = queue.dequeue();
const neighbors = adjList.get(u);
color[u] = Colors.GREY;
for (let i = 0; i < neighbors.length; i++) {
const w = neighbors[i];
@@ -40,7 +41,7 @@ export const breadthFirstSearch = (graph: Graph, callback: Function) => {
}
};

export const bfs = (graph: Graph, startVertex: number) => {
export const bfs = (graph: Graph, startVertex: any) => {
const vertices = graph.getVertices();
const adjList = graph.getAdjList();
const color = initializeColor(vertices);
27 changes: 13 additions & 14 deletions src/ts/algorithms/graph/depth-first-search.ts
Original file line number Diff line number Diff line change
@@ -54,12 +54,12 @@ const DFSVisit = (
d: any,
f: any,
p: any,
time: number,
time: any,
adjList: any
) => {
// console.log('discovered ' + u);
color[u] = Colors.GREY;
d[u] = ++time;
d[u] = ++time.count;
const neighbors = adjList.get(u);
for (let i = 0; i < neighbors.length; i++) {
const w = neighbors[i];
@@ -69,35 +69,34 @@ const DFSVisit = (
}
}
color[u] = Colors.BLACK;
f[u] = ++time;
f[u] = ++time.count;
// console.log('explored ' + u);
};

export const DFS = (graph: Graph) => {

const vertices = graph.getVertices();
const adjList = graph.getAdjList();
const color = initializeColor(vertices);
const d: any = {};
const f: any = {};
const p: any = {};
const time = 0;
const time = { count: 0 };

for (let i = 0; i < vertices.length; i++) {
f[vertices[i]] = 0;
d[vertices[i]] = 0;
p[vertices[i]] = null;
f[vertices[i]] = 0;
d[vertices[i]] = 0;
p[vertices[i]] = null;
}

for (let i = 0; i < vertices.length; i++) {
if (color[vertices[i]] === Colors.WHITE) {
DFSVisit(vertices[i], color, d, f, p, time, adjList);
}
if (color[vertices[i]] === Colors.WHITE) {
DFSVisit(vertices[i], color, d, f, p, time, adjList);
}
}

return {
discovery: d,
finished: f,
predecessors: p
discovery: d,
finished: f,
predecessors: p
};
};
5 changes: 1 addition & 4 deletions src/ts/data-structures/graph.ts
Original file line number Diff line number Diff line change
@@ -20,13 +20,10 @@ export default class Graph {
if (!this.adjList.get(b)) {
this.addVertex(b);
}

this.adjList.get(a).push(b);

if (!this.isDirected) {
if (this.isDirected !== true) {
this.adjList.get(b).push(a);
}
// adjList.get(w).push(v); //commented to run the improved DFS with topological sorting
}

getVertices() {
8 changes: 0 additions & 8 deletions src/ts/data-structures/models/red-black-node.ts
Original file line number Diff line number Diff line change
@@ -19,12 +19,4 @@ export class RedBlackNode<K> extends Node<K> {
isRed() {
return this.color === Colors.RED;
}

flipColor() {
if (this.color === Colors.RED) {
this.color = Colors.BLACK;
} else {
this.color = Colors.RED;
}
}
}
4 changes: 2 additions & 2 deletions src/ts/data-structures/red-black-tree.ts
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ export default class RedBlackTree<T> extends BinarySearchTree<T> {
const uncle = grandParent.right;

// case 1: uncle of node is also red - only recoloring
if (uncle && uncle.color === Colors.RED) {
if (uncle && uncle.isRed()) {
grandParent.color = Colors.RED;
parent.color = Colors.BLACK;
uncle.color = Colors.BLACK;
@@ -137,7 +137,7 @@ export default class RedBlackTree<T> extends BinarySearchTree<T> {
const uncle = grandParent.left;

// case 1: uncle is read - only recoloring
if (uncle && uncle.color === Colors.RED) {
if (uncle && uncle.isRed()) {
grandParent.color = Colors.RED;
parent.color = Colors.BLACK;
uncle.color = Colors.BLACK;
2 changes: 2 additions & 0 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as _util from './util';
export const util = _util;

export { default as Stack } from './data-structures/stack';

export { default as LinkedList } from './data-structures/linked-list';
export { default as DoublyLinkedList } from './data-structures/doubly-linked-list';
export { default as CircularLinkedList } from './data-structures/circular-linked-list';
65 changes: 65 additions & 0 deletions test/js/algorithms/graph/breadth-first-search.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'mocha';
import { expect } from 'chai';
import { BFS, breadthFirstSearch, Graph } from '../../../../src/js/index';

describe('Breadth First Search', () => {
let count;
const vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
let graph;

function assertCallback(value) {
expect(value).to.equal(vertices[count]);
count++;
}

beforeEach(() => {
count = 0;
graph = new Graph();

for (let i = 0; i < vertices.length; i++) {
graph.addVertex(vertices[i]);
}

graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('A', 'D');
graph.addEdge('C', 'D');
graph.addEdge('C', 'G');
graph.addEdge('D', 'G');
graph.addEdge('D', 'H');
graph.addEdge('B', 'E');
graph.addEdge('B', 'F');
graph.addEdge('E', 'I');
});

it('breadthFirstSearch', () => {
breadthFirstSearch(graph, vertices[0], assertCallback);
});

it('sorthest path - BFS', () => {
const shortestPathA = BFS(graph, vertices[0]);

expect(shortestPathA.distances).to.deep.equal({
A: 0,
B: 1,
C: 1,
D: 1,
E: 2,
F: 2,
G: 2,
H: 2,
I: 3
});
expect(shortestPathA.predecessors).to.deep.equal({
A: null,
B: 'A',
C: 'A',
D: 'A',
E: 'B',
F: 'B',
G: 'C',
H: 'D',
I: 'E'
});
});
});
64 changes: 64 additions & 0 deletions test/js/algorithms/graph/depth-first-search.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'mocha';
import { expect } from 'chai';
import { DFS, depthFirstSearch, Graph } from '../../../../src/js/index';

describe('Depth First Search', () => {
let count;
const vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
const dfsCallBack = ['A', 'B', 'E', 'I', 'F', 'C', 'D', 'G', 'H'];
let graph;

beforeEach(() => {
count = 0;
graph = new Graph(true);
});

function assertCallback(value) {
expect(value).to.equal(dfsCallBack[count]);
count++;
}

it('depthFirstSearch', () => {
for (let i = 0; i < vertices.length; i++) {
graph.addVertex(vertices[i]);
}

graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('A', 'D');
graph.addEdge('C', 'D');
graph.addEdge('C', 'G');
graph.addEdge('D', 'G');
graph.addEdge('D', 'H');
graph.addEdge('B', 'E');
graph.addEdge('B', 'F');
graph.addEdge('E', 'I');

depthFirstSearch(graph, assertCallback);
});

it('topological sort - DFS', () => {
const myVertices = ['A', 'B', 'C', 'D', 'E', 'F'];
for (let i = 0; i < myVertices.length; i++) {
graph.addVertex(myVertices[i]);
}
graph.addEdge('A', 'C');
graph.addEdge('A', 'D');
graph.addEdge('B', 'D');
graph.addEdge('B', 'E');
graph.addEdge('C', 'F');
graph.addEdge('F', 'E');

const result = DFS(graph);

expect(result.discovery).to.deep.equal({
A: 1, B: 11, C: 2, D: 8, E: 4, F: 3
});
expect(result.finished).to.deep.equal({
A: 10, B: 12, C: 7, D: 9, E: 5, F: 6
});
expect(result.predecessors).to.deep.equal({
A: null, B: null, C: 'A', D: 'A', E: 'F', F: 'C'
});
});
});
21 changes: 21 additions & 0 deletions test/js/algorithms/graph/dijkstra.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'mocha';
import { expect } from 'chai';
import { dijkstra } from '../../../../src/js/index';

describe('Dijkstra\'s Algorithm - Shortest Path', () => {

it('Shortest Path', () => {
const graph = [
[0, 2, 4, 0, 0, 0],
[0, 0, 2, 4, 2, 0],
[0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 2],
[0, 0, 0, 3, 0, 2],
[0, 0, 0, 0, 0, 0]
];

expect(dijkstra(graph, 0)).to.deep.equal([0, 2, 4, 6, 4, 6]);

});

});
26 changes: 26 additions & 0 deletions test/js/algorithms/graph/floyd-warshall.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'mocha';
import { expect } from 'chai';
import { floydWarshall } from '../../../../src/js/index';

describe('Floyd-Warshall Algorithm - All-Pairs Shortest Path', () => {
it('All-Pairs Shortest Path', () => {
const INF = Infinity;
const graph = [
[INF, 2, 4, INF, INF, INF],
[INF, INF, 2, 4, 2, INF],
[INF, INF, INF, INF, 3, INF],
[INF, INF, INF, INF, INF, 2],
[INF, INF, INF, 3, INF, 2],
[INF, INF, INF, INF, INF, INF]
];

expect(floydWarshall(graph)).to.deep.equal([
[0, 2, 4, 6, 4, 6],
[INF, 0, 2, 4, 2, 4],
[INF, INF, 0, 6, 3, 5],
[INF, INF, INF, 0, INF, 2],
[INF, INF, INF, 3, 0, 2],
[INF, INF, INF, INF, INF, 0]
]);
});
});
18 changes: 18 additions & 0 deletions test/js/algorithms/graph/kruskal.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'mocha';
import { expect } from 'chai';
import { kruskal } from '../../../../src/js/index';

describe('Kruskal Algorithm - Minimum Spanning Tree', () => {
it('Minimum Spanning Tree', () => {
const graph = [
[0, 2, 4, 0, 0, 0],
[2, 0, 2, 4, 2, 0],
[4, 2, 0, 0, 3, 0],
[0, 4, 0, 0, 3, 2],
[0, 2, 3, 3, 0, 2],
[0, 0, 0, 2, 2, 0]
];

expect(kruskal(graph)).to.deep.equal([ , 0, 1, 1, 1, 3]);
});
});
18 changes: 18 additions & 0 deletions test/js/algorithms/graph/prim.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'mocha';
import { expect } from 'chai';
import { prim } from '../../../../src/js/index';

describe('Prim\'s Algorithm - Minimum Spanning Tree', () => {
it('Minimum Spanning Tree', () => {
const graph = [
[0, 2, 4, 0, 0, 0],
[2, 0, 2, 4, 2, 0],
[4, 2, 0, 0, 3, 0],
[0, 4, 0, 0, 3, 2],
[0, 2, 3, 3, 0, 2],
[0, 0, 0, 2, 2, 0]
];

expect(prim(graph)).to.deep.equal([-1, 0, 1, 5, 1, 4]);
});
});
47 changes: 47 additions & 0 deletions test/ts/algorithms/graph/breadth-first-search.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'mocha';
import { expect } from 'chai';
import { BFS, breadthFirstSearch, Graph, Stack } from '../../../../src/ts/index';

describe('Breadth First Search', () => {

let count;
const vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
let graph: Graph;

beforeEach(() => {
count = 0;
graph = new Graph();

for (let i = 0; i < vertices.length; i++) {
graph.addVertex(vertices[i]);
}

graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('A', 'D');
graph.addEdge('C', 'D');
graph.addEdge('C', 'G');
graph.addEdge('D', 'G');
graph.addEdge('D', 'H');
graph.addEdge('B', 'E');
graph.addEdge('B', 'F');
graph.addEdge('E', 'I');
});

it('breadthFirstSearch', () => {
breadthFirstSearch(graph, vertices[0], assertCallback);
});

function assertCallback(value) {
expect(value).to.equal(vertices[count]);
count++;
}

it('sorthest path - BFS', () => {
const shortestPathA = BFS(graph, vertices[0]);

expect(shortestPathA.distances).to.deep.equal({A: 0, B: 1, C: 1, D: 1, E: 2,  F: 2, G: 2, H: 2, I: 3});
expect(shortestPathA.predecessors).to.deep.equal({A: null, B: 'A', C: 'A', D: 'A', E: 'B',  F: 'B', G: 'C', H: 'D', I: 'E'});
});

});
58 changes: 58 additions & 0 deletions test/ts/algorithms/graph/depth-first-search.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'mocha';
import { expect } from 'chai';
import { DFS, depthFirstSearch, Graph } from '../../../../src/ts/index';

describe('Depth First Search', () => {
let count;
const vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
const dfsCallBack = ['A', 'B', 'E', 'I', 'F', 'C', 'D', 'G', 'H'];
let graph: Graph;

beforeEach(() => {
count = 0;
graph = new Graph(true);
});

it('depthFirstSearch', () => {
for (let i = 0; i < vertices.length; i++) {
graph.addVertex(vertices[i]);
}

graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('A', 'D');
graph.addEdge('C', 'D');
graph.addEdge('C', 'G');
graph.addEdge('D', 'G');
graph.addEdge('D', 'H');
graph.addEdge('B', 'E');
graph.addEdge('B', 'F');
graph.addEdge('E', 'I');

depthFirstSearch(graph, assertCallback);
});

function assertCallback(value) {
expect(value).to.equal(dfsCallBack[count]);
count++;
}

it('topological sort - DFS', () => {
const myVertices = ['A', 'B', 'C', 'D', 'E', 'F'];
for (let i = 0; i < myVertices.length; i++) {
graph.addVertex(myVertices[i]);
}
graph.addEdge('A', 'C');
graph.addEdge('A', 'D');
graph.addEdge('B', 'D');
graph.addEdge('B', 'E');
graph.addEdge('C', 'F');
graph.addEdge('F', 'E');

const result = DFS(graph);

expect(result.discovery).to.deep.equal({A: 1, B: 11, C: 2, D: 8, E: 4, F: 3});
expect(result.finished).to.deep.equal({A: 10, B: 12, C: 7, D: 9, E: 5, F: 6});
expect(result.predecessors).to.deep.equal({A: null, B: null, C: 'A', D: 'A', E: 'F', F: 'C'});
});
});
21 changes: 21 additions & 0 deletions test/ts/algorithms/graph/dijkstra.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'mocha';
import { expect } from 'chai';
import { dijkstra } from '../../../../src/ts/index';

describe('Dijkstra\'s Algorithm - Shortest Path', () => {

it('Shortest Path', () => {
const graph = [
[0, 2, 4, 0, 0, 0],
[0, 0, 2, 4, 2, 0],
[0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 2],
[0, 0, 0, 3, 0, 2],
[0, 0, 0, 0, 0, 0]
];

expect(dijkstra(graph, 0)).to.deep.equal([0, 2, 4, 6, 4, 6]);

});

});
26 changes: 26 additions & 0 deletions test/ts/algorithms/graph/floyd-warshall.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'mocha';
import { expect } from 'chai';
import { floydWarshall } from '../../../../src/ts/index';

describe('Floyd-Warshall Algorithm - All-Pairs Shortest Path', () => {
it('All-Pairs Shortest Path', () => {
const INF = Infinity;
const graph = [
[INF, 2, 4, INF, INF, INF],
[INF, INF, 2, 4, 2, INF],
[INF, INF, INF, INF, 3, INF],
[INF, INF, INF, INF, INF, 2],
[INF, INF, INF, 3, INF, 2],
[INF, INF, INF, INF, INF, INF]
];

expect(floydWarshall(graph)).to.deep.equal([
[0, 2, 4, 6, 4, 6],
[INF, 0, 2, 4, 2, 4],
[INF, INF, 0, 6, 3, 5],
[INF, INF, INF, 0, INF, 2],
[INF, INF, INF, 3, 0, 2],
[INF, INF, INF, INF, INF, 0]
]);
});
});
18 changes: 18 additions & 0 deletions test/ts/algorithms/graph/kruskal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'mocha';
import { expect } from 'chai';
import { kruskal } from '../../../../src/ts/index';

describe('Kruskal Algorithm - Minimum Spanning Tree', () => {
it('Minimum Spanning Tree', () => {
const graph = [
[0, 2, 4, 0, 0, 0],
[2, 0, 2, 4, 2, 0],
[4, 2, 0, 0, 3, 0],
[0, 4, 0, 0, 3, 2],
[0, 2, 3, 3, 0, 2],
[0, 0, 0, 2, 2, 0]
];

expect(kruskal(graph)).to.deep.equal([ , 0, 1, 1, 1, 3]);
});
});
18 changes: 18 additions & 0 deletions test/ts/algorithms/graph/prim.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'mocha';
import { expect } from 'chai';
import { prim } from '../../../../src/ts/index';

describe('Prim\'s Algorithm - Minimum Spanning Tree', () => {
it('Minimum Spanning Tree', () => {
const graph = [
[0, 2, 4, 0, 0, 0],
[2, 0, 2, 4, 2, 0],
[4, 2, 0, 0, 3, 0],
[0, 4, 0, 0, 3, 2],
[0, 2, 3, 3, 0, 2],
[0, 0, 0, 2, 2, 0]
];

expect(prim(graph)).to.deep.equal([-1, 0, 1, 5, 1, 4]);
});
});