Skip to content

Commit 9607ab5

Browse files
committed
chapter 11: [Graphs]
1 parent d916fb1 commit 9607ab5

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

examples/PacktDataStructuresAlgorithms.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/chapter11/02-BFS.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ console.log(graph.toString());
2727

2828
console.log('********* bfs with callback ***********');
2929

30-
const printNode = (value) => console.log('Visited vertex: ' + value);
30+
const printVertex = (value) => console.log('Visited vertex: ' + value);
3131

32-
breadthFirstSearch(graph, myVertices[0], printNode);
32+
breadthFirstSearch(graph, myVertices[0], printVertex);
3333

3434
console.log('********* sorthest path - BFS ***********');
35-
var shortestPathA = BFS(graph, myVertices[0]);
35+
const shortestPathA = BFS(graph, myVertices[0]);
3636
console.log(shortestPathA.distances);
3737
console.log(shortestPathA.predecessors);
3838

3939
//from A to all other vertices
40-
var fromVertex = myVertices[0];
40+
const fromVertex = myVertices[0];
4141

42-
for (i = 1; i < myVertices.length; i++) {
43-
var toVertex = myVertices[i],
44-
path = new Stack();
45-
for (var v = toVertex; v !== fromVertex; v = shortestPathA.predecessors[v]) {
42+
for (let i = 1; i < myVertices.length; i++) {
43+
const toVertex = myVertices[i];
44+
const path = new Stack();
45+
for (let v = toVertex; v !== fromVertex; v = shortestPathA.predecessors[v]) {
4646
path.push(v);
4747
}
4848
path.push(fromVertex);
49-
var s = path.pop();
49+
let s = path.pop();
5050
while (!path.isEmpty()) {
5151
s += ' - ' + path.pop();
5252
}

examples/chapter11/03-DFS.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { Graph } = PacktDataStructuresAlgorithms;
22
const { depthFirstSearch } = PacktDataStructuresAlgorithms;
33
const { DFS } = PacktDataStructuresAlgorithms;
44

5-
let graph = new Graph();
5+
let graph = new Graph(true);
66

77
let myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
88

@@ -26,13 +26,13 @@ console.log(graph.toString());
2626

2727
console.log('********* dfs with callback ***********');
2828

29-
const printNode = value => console.log('Visited vertex: ' + value);
29+
const printVertex = value => console.log('Visited vertex: ' + value);
3030

31-
depthFirstSearch(graph, printNode);
31+
depthFirstSearch(graph, printVertex);
3232

3333
console.log('********* topological sort - DFS ***********');
3434

35-
graph = new Graph();
35+
graph = new Graph(true); // directed graph
3636

3737
myVertices = ['A', 'B', 'C', 'D', 'E', 'F'];
3838
for (i = 0; i < myVertices.length; i++) {

src/js/algorithms/graph/depth-first-search.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const initializeColor = vertices => {
1515
};
1616

1717
const depthFirstSearchVisit = (u, color, adjList, callback) => {
18-
color[u] = 'grey';
18+
color[u] = Colors.GREY;
1919
if (callback) {
2020
callback(u);
2121
}

src/js/algorithms/graph/prim.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const prim = graph => {
2626
const u = minKey(graph, key, visited);
2727
visited[u] = true;
2828
for (let v = 0; v < length; v++) {
29-
if (graph[u][v] && visited[v] === false && graph[u][v] < key[v]) {
29+
if (graph[u][v] && !visited[v] && graph[u][v] < key[v]) {
3030
parent[v] = u;
3131
key[v] = graph[u][v];
3232
}

src/js/data-structures/graph.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ export default class Graph {
2121
}
2222
this.adjList.get(a).push(b);
2323
if (!this.isDirected) {
24-
// this.adjList.get(b).push(a);
24+
this.adjList.get(b).push(a);
2525
}
26-
// adjList.get(w).push(v); //commented to run the improved DFS with topological sorting
2726
}
2827
getVertices() {
2928
return this.vertices;

0 commit comments

Comments
 (0)