Skip to content

Commit fa3a077

Browse files
committed
added chapter 09
1 parent d0f7bbf commit fa3a077

File tree

4 files changed

+283
-0
lines changed

4 files changed

+283
-0
lines changed

chapter08.zip

-2.54 KB
Binary file not shown.

chapter09/01-Graph.js

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
function Graph() {
2+
3+
var vertices = []; //list
4+
5+
var adjList = new Dictionary();
6+
7+
this.addVertex = function(v){
8+
vertices.push(v);
9+
adjList.set(v, []); //initialize adjacency list with array as well;
10+
};
11+
12+
this.addEdge = function(v, w){
13+
adjList.get(v).push(w);
14+
//adjList.get(w).push(v); //commented to run the improved DFS with topological sorting
15+
};
16+
17+
this.toString = function(){
18+
var s = '';
19+
for (var i=0; i<vertices.length; i++){
20+
s += vertices[i] + ' -> ';
21+
var neighbors = adjList.get(vertices[i]);
22+
for (var j=0; j<neighbors.length; j++){
23+
s += neighbors[j] + ' ';
24+
}
25+
s += '\n';
26+
}
27+
return s;
28+
};
29+
30+
var initializeColor = function(){
31+
var color = [];
32+
for (var i=0; i<vertices.length; i++){
33+
color[vertices[i]] = 'white';
34+
}
35+
return color;
36+
};
37+
38+
this.bfs = function(v, callback){
39+
40+
var color = initializeColor(),
41+
queue = new Queue();
42+
queue.enqueue(v);
43+
44+
while (!queue.isEmpty()){
45+
var u = queue.dequeue(),
46+
neighbors = adjList.get(u);
47+
color[u] = 'grey';
48+
for (var i=0; i<neighbors.length; i++){
49+
var w = neighbors[i];
50+
if (color[w] === 'white'){
51+
color[w] = 'grey';
52+
queue.enqueue(w);
53+
}
54+
}
55+
color[u] = 'black';
56+
if (callback) {
57+
callback(u);
58+
}
59+
}
60+
};
61+
62+
this.dfs = function(callback){
63+
64+
var color = initializeColor();
65+
66+
for (var i=0; i<vertices.length; i++){
67+
if (color[vertices[i]] === 'white'){
68+
dfsVisit(vertices[i], color, callback);
69+
}
70+
}
71+
};
72+
73+
var dfsVisit = function(u, color, callback){
74+
75+
color[u] = 'grey';
76+
if (callback) {
77+
callback(u);
78+
}
79+
console.log('Discovered ' + u);
80+
var neighbors = adjList.get(u);
81+
for (var i=0; i<neighbors.length; i++){
82+
var w = neighbors[i];
83+
if (color[w] === 'white'){
84+
dfsVisit(w, color, callback);
85+
}
86+
}
87+
color[u] = 'black';
88+
console.log('explored ' + u);
89+
};
90+
91+
92+
this.BFS = function(v){
93+
94+
var color = initializeColor(),
95+
queue = new Queue(),
96+
d = [],
97+
pred = [];
98+
queue.enqueue(v);
99+
100+
for (var i=0; i<vertices.length; i++){
101+
d[vertices[i]] = 0;
102+
pred[vertices[i]] = null;
103+
}
104+
105+
while (!queue.isEmpty()){
106+
var u = queue.dequeue(),
107+
neighbors = adjList.get(u);
108+
color[u] = 'grey';
109+
for (i=0; i<neighbors.length; i++){
110+
var w = neighbors[i];
111+
if (color[w] === 'white'){
112+
color[w] = 'grey';
113+
d[w] = d[u] + 1;
114+
pred[w] = u;
115+
queue.enqueue(w);
116+
}
117+
}
118+
color[u] = 'black';
119+
}
120+
121+
return {
122+
distances: d,
123+
predecessors: pred
124+
};
125+
};
126+
127+
var time = 0;
128+
this.DFS = function(){
129+
130+
var color = initializeColor(),
131+
d = [],
132+
f = [],
133+
p = [];
134+
time = 0;
135+
136+
for (var i=0; i<vertices.length; i++){
137+
f[vertices[i]] = 0;
138+
d[vertices[i]] = 0;
139+
p[vertices[i]] = null;
140+
}
141+
142+
for (i=0; i<vertices.length; i++){
143+
if (color[vertices[i]] === 'white'){
144+
DFSVisit(vertices[i], color, d, f, p);
145+
}
146+
}
147+
148+
return {
149+
discovery: d,
150+
finished: f,
151+
predecessors: p
152+
};
153+
};
154+
155+
var DFSVisit = function(u, color, d, f, p){
156+
157+
console.log('discovered ' + u);
158+
color[u] = 'grey';
159+
d[u] = ++time;
160+
var neighbors = adjList.get(u);
161+
for (var i=0; i<neighbors.length; i++){
162+
var w = neighbors[i];
163+
if (color[w] === 'white'){
164+
p[w] = u;
165+
DFSVisit(w,color, d, f, p);
166+
}
167+
}
168+
color[u] = 'black';
169+
f[u] = ++time;
170+
console.log('explored ' + u);
171+
};
172+
}

chapter09/02-UsingGraphs.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Graph.js"></script>
9+
<script type="text/javascript" src="../chapter04/01-Queue.js"></script>
10+
<script type="text/javascript" src="../chapter03/01-Stack.js"></script>
11+
<script type="text/javascript" src="../chapter07/01-Dictionaries.js"></script>
12+
<script type="text/javascript" src="02-UsingGraphs.js"></script>
13+
</body>
14+
</html>

chapter09/02-UsingGraphs.js

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
var graph = new Graph();
2+
3+
var myVertices = ['A','B','C','D','E','F','G','H','I'];
4+
5+
for (var i=0; i<myVertices.length; i++){
6+
graph.addVertex(myVertices[i]);
7+
}
8+
graph.addEdge('A', 'B');
9+
graph.addEdge('A', 'C');
10+
graph.addEdge('A', 'D');
11+
graph.addEdge('C', 'D');
12+
graph.addEdge('C', 'G');
13+
graph.addEdge('D', 'G');
14+
graph.addEdge('D', 'H');
15+
graph.addEdge('B', 'E');
16+
graph.addEdge('B', 'F');
17+
graph.addEdge('E', 'I');
18+
19+
console.log('********* printing graph ***********');
20+
21+
console.log(graph.toString());
22+
23+
console.log('********* bfs ***********');
24+
25+
function printNode(value){
26+
console.log('Visited vertex: ' + value);
27+
}
28+
29+
graph.bfs(myVertices[0], printNode);
30+
31+
console.log('********* dfs ***********');
32+
33+
graph.dfs();
34+
35+
console.log('********* sorthest path - BFS ***********');
36+
var shortestPathA = graph.BFS(myVertices[0]);
37+
console.log(shortestPathA.distances);
38+
console.log(shortestPathA.predecessors);
39+
40+
//from A to all other vertices
41+
var fromVertex = myVertices[0];
42+
43+
for (i=1; i<myVertices.length; i++){
44+
var toVertex = myVertices[i],
45+
path = new Stack();
46+
for (var v=toVertex; v!== fromVertex; v=shortestPathA.predecessors[v]) {
47+
path.push(v);
48+
}
49+
path.push(fromVertex);
50+
var s = path.pop();
51+
while (!path.isEmpty()){
52+
s += ' - ' + path.pop();
53+
}
54+
console.log(s);
55+
}
56+
57+
console.log('********* topological sort - DFS ***********');
58+
59+
//var result = graph.DFS();
60+
//console.log(result.discovery);
61+
//console.log(result.finished);
62+
//console.log(result.predecessors);
63+
64+
graph = new Graph();
65+
66+
myVertices = ['A','B','C','D','E','F'];
67+
for (i=0; i<myVertices.length; i++){
68+
graph.addVertex(myVertices[i]);
69+
}
70+
graph.addEdge('A', 'C');
71+
graph.addEdge('A', 'D');
72+
graph.addEdge('B', 'D');
73+
graph.addEdge('B', 'E');
74+
graph.addEdge('C', 'F');
75+
graph.addEdge('F', 'E');
76+
77+
78+
var result = graph.DFS();
79+
console.log(result.discovery);
80+
console.log(result.finished);
81+
console.log(result.predecessors);
82+
83+
var fTimes = result.finished;
84+
s = '';
85+
for (var count=0; count<myVertices.length; count++){
86+
var max = 0;
87+
var maxName = null;
88+
for (i=0; i<myVertices.length; i++){
89+
if (fTimes[myVertices[i]] > max){
90+
max = fTimes[myVertices[i]];
91+
maxName = myVertices[i];
92+
}
93+
}
94+
s += ' - ' + maxName;
95+
delete fTimes[maxName];
96+
}
97+
console.log(s);

0 commit comments

Comments
 (0)