-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphBFS.js
122 lines (101 loc) · 2.93 KB
/
graphBFS.js
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
function Graph() {
this.adjacencyList = {};
}
// previously-implemented functions
Graph.prototype.numEdges = function() {
let total = 0;
Object.values(this.adjacencyList).forEach(list => {
total += list.length;
});
// note that we've double-counted up til now since we've looked at
// the adjacencyList for every node.
return total / 2;
};
Graph.prototype.addVertex = function(vertex) {
this.adjacencyList[vertex] = [];
};
Graph.prototype.addEdge = function(vertex1, vertex2) {
this.adjacencyList[vertex1].push(vertex2);
this.adjacencyList[vertex2].push(vertex1);
};
Graph.prototype.removeVertex = function(vertex) {
while (this.adjacencyList[vertex].length) {
const adjacentVertex = this.adjacencyList[vertex].pop();
this.removeEdge(adjacentVertex, vertex);
}
delete this.adjacencyList[vertex];
};
Graph.prototype.removeEdge = function(vertex1, vertex2) {
this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter(
v => v !== vertex2
);
this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter(
v => v !== vertex1
);
};
// Start a queue with the value
// Add the value to a seen/visited tracker
// While there is something in the queue...
// Remove the first item from the queue
// Add its value to the answer you're returning
// Add all its neighbors to the queue and mark seen/visited IF they haven't been seen/visited yet
// Iteratively
Graph.prototype.breadthFirstSearch = function(value) {
let q = [value];
let visited = new Set(q);
let ans = [];
while (q.length) {
let vertex = q.shift();
ans.push(vertex);
let adjacencyList = this.adjacencyList[vertex];
for (let v of adjacencyList) {
if (!visited.has(v)) {
visited.add(v);
q.push(v);
}
}
}
return ans;
};
// Recursively
// Graph.prototype.breadthFirstSearch = function(start, q=[start], seen = new Set(q), result = []) {
// let vertex = q.shift();
// if (!vertex) return result;
// result.push(vertex);
// let adjacencyList = this.adjacencyList[vertex];
// for (let v of adjacencyList) {
// if(!(seen.has(v))) {
// seen.add(v);
// q.push(v);
// }
// }
// return this.breadthFirstSearch(start, q, seen, result);
// }
// Testing Setup
var graph = new Graph();
graph.addVertex("S");
graph.addVertex("P");
graph.addVertex("U");
graph.addVertex("X");
graph.addVertex("Q");
graph.addVertex("Y");
graph.addVertex("V");
graph.addVertex("R");
graph.addVertex("W");
graph.addVertex("T");
graph.addEdge("S", "P");
graph.addEdge("S", "U");
graph.addEdge("P", "X");
graph.addEdge("U", "X");
graph.addEdge("P", "Q");
graph.addEdge("U", "V");
graph.addEdge("X", "Q");
graph.addEdge("X", "Y");
graph.addEdge("X", "V");
graph.addEdge("Q", "R");
graph.addEdge("Y", "R");
graph.addEdge("Y", "W");
graph.addEdge("V", "W");
graph.addEdge("R", "T");
graph.addEdge("W", "T");
console.log(graph.breadthFirstSearch("S")); // ["S", "P", "U", "X", "Q", "V", "Y", "R", "W", "T"]