-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph.js
More file actions
194 lines (169 loc) · 4.31 KB
/
Graph.js
File metadata and controls
194 lines (169 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//a vertex
function Vertex(label) {
this.label = label;
}
/**
* A graph implemented using adjacency list
* @param v is the number of vertices
* @constructor
*/
function Graph(v) {
this.vertices = v;
this.edges = 0;
this.adj = [];
this.vertexList = []; //store the vertex details, like course name, for Topological Sort
for (var i = 0; i < this.vertices; ++i) {
this.adj[i] = [];
}
this.addEdge = addEdge;
this.showGraph = showGraph;
this.dfs = dfs;
this.bfs = bfs;
this.pathTo = pathTo;
this.hasPathTo = hasPathTo;
this.topSortHelper = topSortHelper;
this.topSort = topSort;
//this.toString = toString;
//array to store visited nodes
this.marked = [];
//initialize it false values equal to # of vertices
for (var i = 0; i < this.vertices; i++) {
this.marked[i] = false;
}
this.edgeTo = new Array();
}
function addEdge(v, w) {
//find the adj list for vertex v and insert w
this.adj[v].push(w);
//find the adj list for vertex w and insert v
this.adj[w].push(v);
//increment the number of edges by 1
this.edges++
}
function showGraph() {
var visited = [];
var str = '';
for (var i = 0; i < this.vertices; ++i) {
str += this.vertexList[i] + " -> ";
visited.push(this.vertexList[i]);
for (var j = 0; j < this.vertices; ++j) {
if (this.adj[i][j] != undefined) {
if (visited.indexOf(this.vertexList[j]) < 0) {
str += this.vertexList[j] + ' ';
}
}
}
console.log(str);
visited.pop();
}
}
/**
* Depth First Search Algorithm
* v is where to start the search from
* */
function dfs(v) {
//mark the vertex as visited
this.marked[v] = true;
//print visited vertices not required for dfs to work
if (this.adj[v] != undefined) {
console.log("Visited vertex " + v);
}
for (var i = 0; i < this.adj[v].length; i++) {
console.log(this.adj[v]);
var element = this.adj[v][i];
if (!this.marked[element]) {
this.dfs(element);
}
}
}
function bfs(s) {
var queue = [];
this.marked[s] = true;
queue.push(s); // add to back of queue
while (queue.length > 0) {
var v = queue.shift(); // remove from front of queue
if (v != undefined) {
console.log("Visited vertex: " + v);
}
for (var i = 0; i < this.adj[v].length; i++) {
var element = this.adj[v][i];
if (!this.marked[element]) {
this.edgeTo[element] = v;
this.marked[element] = true;
queue.push(element);
}
}
}
}
function hasPathTo(v) {
return this.marked[v];
}
function pathTo(v) {
var source = 0;
if (!this.hasPathTo(v)) {
return undefined;
}
var path = [];
for (var i = v; i != source; i = this.edgeTo[i]) {
path.push(i);
}
path.push(source);
return path;
}
function topSort() {
var stack = [];
var visited = [];
//initializing visited array to false
for (var i = 0; i < this.vertices; i++) {
visited[i] = false;
}
for (var i = 0; i < this.vertices; i++) {
if (visited[i] == false) {
this.topSortHelper(i, visited, stack);
}
}
for (var i = 0; i < stack.length; i++) {
if (stack[i] != undefined && stack[i] != false) {
console.log(this.vertexList[stack[i]]);
}
}
}
function topSortHelper(v, visited, stack) {
visited[v] = true;
for (var i = 0; i < this.adj[v]; i++) {
var element = this.adj[v][i];
if (!visited[element]) {
this.topSortHelper(visited[element], visited, stack);
}
}
stack.push(v);
}
/*
g = new Graph(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(2, 4);
g.bfs(0);
var vertex = 3;
var paths = g.pathTo(vertex);
var str = '';
while (paths.length > 0) {
if (paths.length > 1) {
str += (paths.pop() + '-');
} else {
str += paths.pop();
}
}
console.log(str);*/
g = new Graph(6);
g.addEdge(1, 2);
g.addEdge(2, 5);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(0, 1);
g.vertexList = ["CS1", "CS2", "Data Structures",
"Assembly Language", "Operating Systems",
"Algorithms"];
g.showGraph();
g.topSort();