Skip to content

Commit 354cf53

Browse files
committed
chapter 11: [Graphs]
1 parent 1829d34 commit 354cf53

34 files changed

+1100
-13
lines changed

.eslintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
"comma-dangle": 0,
2727
"no-underscore-dangle": 0,
2828
"no-param-reassign": 0,
29-
"no-return-assign": 0
29+
"no-return-assign": 0,
30+
"no-restricted-globals": 0,
31+
"no-multi-assign": 0,
32+
"prefer-destructuring": ["error", {"object": true, "array": false}]
3033
}
3134
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Work in Progress.
2020
* 08: [Recursion](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter08)
2121
* 09: [Trees](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter09)
2222
* 10: [Heap](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter10)
23+
* 11: [Graphs](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter11)
2324

2425
### Third Edition Updates
2526

examples/PacktDataStructuresAlgorithms.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="01-UsingGraphs.js"></script>
10+
</body>
11+
</html>

examples/chapter11/01-UsingGraphs.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { Graph } = PacktDataStructuresAlgorithms;
2+
3+
const graph = new Graph();
4+
5+
const myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
6+
7+
for (let i = 0; i < myVertices.length; i++) {
8+
graph.addVertex(myVertices[i]);
9+
}
10+
graph.addEdge('A', 'B');
11+
graph.addEdge('A', 'C');
12+
graph.addEdge('A', 'D');
13+
graph.addEdge('C', 'D');
14+
graph.addEdge('C', 'G');
15+
graph.addEdge('D', 'G');
16+
graph.addEdge('D', 'H');
17+
graph.addEdge('B', 'E');
18+
graph.addEdge('B', 'F');
19+
graph.addEdge('E', 'I');
20+
21+
console.log('********* printing graph ***********');
22+
23+
console.log(graph.toString());

examples/chapter11/02-BFS.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="02-BFS.js"></script>
10+
</body>
11+
</html>

examples/chapter11/02-BFS.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { Graph } = PacktDataStructuresAlgorithms;
2+
const { Stack } = PacktDataStructuresAlgorithms;
3+
const { BFS } = PacktDataStructuresAlgorithms;
4+
const { breadthFirstSearch } = PacktDataStructuresAlgorithms;
5+
6+
const graph = new Graph();
7+
8+
const myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
9+
10+
for (let i = 0; i < myVertices.length; i++) {
11+
graph.addVertex(myVertices[i]);
12+
}
13+
graph.addEdge('A', 'B');
14+
graph.addEdge('A', 'C');
15+
graph.addEdge('A', 'D');
16+
graph.addEdge('C', 'D');
17+
graph.addEdge('C', 'G');
18+
graph.addEdge('D', 'G');
19+
graph.addEdge('D', 'H');
20+
graph.addEdge('B', 'E');
21+
graph.addEdge('B', 'F');
22+
graph.addEdge('E', 'I');
23+
24+
console.log('********* printing graph ***********');
25+
26+
console.log(graph.toString());
27+
28+
console.log('********* bfs with callback ***********');
29+
30+
const printNode = (value) => console.log('Visited vertex: ' + value);
31+
32+
breadthFirstSearch(graph, myVertices[0], printNode);
33+
34+
console.log('********* sorthest path - BFS ***********');
35+
var shortestPathA = BFS(graph, myVertices[0]);
36+
console.log(shortestPathA.distances);
37+
console.log(shortestPathA.predecessors);
38+
39+
//from A to all other vertices
40+
var fromVertex = myVertices[0];
41+
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]) {
46+
path.push(v);
47+
}
48+
path.push(fromVertex);
49+
var s = path.pop();
50+
while (!path.isEmpty()) {
51+
s += ' - ' + path.pop();
52+
}
53+
console.log(s);
54+
}

examples/chapter11/03-DFS.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="03-DFS.js"></script>
10+
</body>
11+
</html>

examples/chapter11/03-DFS.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const { Graph } = PacktDataStructuresAlgorithms;
2+
const { depthFirstSearch } = PacktDataStructuresAlgorithms;
3+
const { DFS } = PacktDataStructuresAlgorithms;
4+
5+
let graph = new Graph();
6+
7+
let myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
8+
9+
for (let i = 0; i < myVertices.length; i++) {
10+
graph.addVertex(myVertices[i]);
11+
}
12+
graph.addEdge('A', 'B');
13+
graph.addEdge('A', 'C');
14+
graph.addEdge('A', 'D');
15+
graph.addEdge('C', 'D');
16+
graph.addEdge('C', 'G');
17+
graph.addEdge('D', 'G');
18+
graph.addEdge('D', 'H');
19+
graph.addEdge('B', 'E');
20+
graph.addEdge('B', 'F');
21+
graph.addEdge('E', 'I');
22+
23+
console.log('********* printing graph ***********');
24+
25+
console.log(graph.toString());
26+
27+
console.log('********* dfs with callback ***********');
28+
29+
const printNode = value => console.log('Visited vertex: ' + value);
30+
31+
depthFirstSearch(graph, printNode);
32+
33+
console.log('********* topological sort - DFS ***********');
34+
35+
graph = new Graph();
36+
37+
myVertices = ['A', 'B', 'C', 'D', 'E', 'F'];
38+
for (i = 0; i < myVertices.length; i++) {
39+
graph.addVertex(myVertices[i]);
40+
}
41+
graph.addEdge('A', 'C');
42+
graph.addEdge('A', 'D');
43+
graph.addEdge('B', 'D');
44+
graph.addEdge('B', 'E');
45+
graph.addEdge('C', 'F');
46+
graph.addEdge('F', 'E');
47+
48+
const result = DFS(graph);
49+
console.log(result.discovery);
50+
console.log(result.finished);
51+
console.log(result.predecessors);
52+
53+
const fTimes = result.finished;
54+
s = '';
55+
for (let count = 0; count < myVertices.length; count++) {
56+
let max = 0;
57+
let maxName = null;
58+
for (i = 0; i < myVertices.length; i++) {
59+
if (fTimes[myVertices[i]] > max) {
60+
max = fTimes[myVertices[i]];
61+
maxName = myVertices[i];
62+
}
63+
}
64+
s += ' - ' + maxName;
65+
delete fTimes[maxName];
66+
}
67+
console.log(s);

examples/chapter11/04-Dijkstra.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="04-Dijkstra.js"></script>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)