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

+4-1
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

+1
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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+11
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

+23
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

+11
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

+54
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

+11
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

+67
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

+11
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>

examples/chapter11/04-Dijkstra.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { dijkstra } = PacktDataStructuresAlgorithms;
2+
3+
const graph = [
4+
[0, 2, 4, 0, 0, 0],
5+
[0, 0, 2, 4, 2, 0],
6+
[0, 0, 0, 0, 3, 0],
7+
[0, 0, 0, 0, 0, 2],
8+
[0, 0, 0, 3, 0, 2],
9+
[0, 0, 0, 0, 0, 0]
10+
];
11+
12+
console.log("********* Dijkstra's Algorithm - Shortest Path ***********");
13+
14+
var dist = dijkstra(graph, 0);
15+
16+
for (i = 0; i < dist.length; i++){
17+
console.log(i + '\t\t' + dist[i]);
18+
}
+11
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="05-Floyd-Warshall.js"></script>
10+
</body>
11+
</html>
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { floydWarshall } = PacktDataStructuresAlgorithms;
2+
3+
const INF = Infinity;
4+
const graph = [
5+
[INF, 2, 4, INF, INF, INF],
6+
[INF, INF, 2, 4, 2, INF],
7+
[INF, INF, INF, INF, 3, INF],
8+
[INF, INF, INF, INF, INF, 2],
9+
[INF, INF, INF, 3, INF, 2],
10+
[INF, INF, INF, INF, INF, INF]
11+
];
12+
13+
console.log('********* Floyd-Warshall Algorithm - All-Pairs Shortest Path ***********');
14+
15+
dist = floydWarshall(graph);
16+
17+
let s = '';
18+
for (let i = 0; i < dist.length; ++i) {
19+
s = '';
20+
for (var j = 0; j < dist.length; ++j) {
21+
if (dist[i][j] === INF) s += 'INF ';
22+
else s += dist[i][j] + ' ';
23+
}
24+
console.log(s);
25+
}

examples/chapter11/06-Prim.html

+11
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="06-Prim.js"></script>
10+
</body>
11+
</html>

examples/chapter11/06-Prim.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { prim } = PacktDataStructuresAlgorithms;
2+
3+
const graph = [
4+
[0, 2, 4, 0, 0, 0],
5+
[2, 0, 2, 4, 2, 0],
6+
[4, 2, 0, 0, 3, 0],
7+
[0, 4, 0, 0, 3, 2],
8+
[0, 2, 3, 3, 0, 2],
9+
[0, 0, 0, 2, 2, 0]
10+
];
11+
12+
console.log("********* Prim's Algorithm - Minimum Spanning Tree ***********");
13+
14+
const parent = prim(graph);
15+
16+
console.log('Edge Weight');
17+
for (let i = 1; i < graph.length; i++) {
18+
console.log(parent[i] + ' - ' + i + ' ' + graph[i][parent[i]]);
19+
}

examples/chapter11/07-Kruskal.html

+11
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="07-Kruskal.js"></script>
10+
</body>
11+
</html>

examples/chapter11/07-Kruskal.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { kruskal } = PacktDataStructuresAlgorithms;
2+
3+
const graph = [
4+
[0, 2, 4, 0, 0, 0],
5+
[2, 0, 2, 4, 2, 0],
6+
[4, 2, 0, 0, 3, 0],
7+
[0, 4, 0, 0, 3, 2],
8+
[0, 2, 3, 3, 0, 2],
9+
[0, 0, 0, 2, 2, 0]
10+
];
11+
12+
console.log('********* Kruskal Algorithm - Minimum Spanning Tree ***********');
13+
14+
const parent = kruskal(graph);
15+
16+
console.log('Edge Weight');
17+
for (i = 1; i < graph.length; i++) {
18+
console.log(parent[i] + ' - ' + i + ' ' + graph[i][parent[i]]);
19+
}

examples/index.html

+15-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
<a href="#scroll-tab-6" class="mdl-layout__tab">06</a>
4747
<a href="#scroll-tab-7" class="mdl-layout__tab">07</a>
4848
<a href="#scroll-tab-8" class="mdl-layout__tab">08</a>
49+
<a href="#scroll-tab-9" class="mdl-layout__tab">09</a>
50+
<a href="#scroll-tab-10" class="mdl-layout__tab">10</a>
51+
<a href="#scroll-tab-11" class="mdl-layout__tab">11</a>
4952
</div>
5053
</header>
5154
<main class="mdl-layout__content">
@@ -187,9 +190,19 @@
187190
</div>
188191
</div>
189192
</section>
190-
<section class="mdl-layout__tab-panel" id="scroll-tab-9">
193+
<section class="mdl-layout__tab-panel" id="scroll-tab-11">
191194
<div class="page-content">
192-
Soon.
195+
<div class="page-content mdl-layout--fixed-drawer">
196+
<div class="mdl-layout__drawer is-visible">
197+
<nav class="mdl-navigation">
198+
<a class="mdl-navigation__link" href="chapter11/01-UsingGraphs.html">01-UsingGraphs</a>
199+
<a class="mdl-navigation__link" href="chapter11/02-BFS.html">02-BFS</a>
200+
<a class="mdl-navigation__link" href="chapter11/03-DFS.html">03-DFS</a>
201+
<a class="mdl-navigation__link" href="chapter11/04-Dijkstra.html">04-Dijkstra</a>
202+
<a class="mdl-navigation__link" href="chapter11/05-Floyd-Warshall.html">05-Floyd-Warshall</a>
203+
</nav>
204+
</div>
205+
</div>
193206
</div>
194207
</section>
195208
</main>

0 commit comments

Comments
 (0)