-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.js
152 lines (114 loc) · 3.26 KB
/
graph.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
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
class Graph {
constructor() {
this.adjacencyList = {};
}
addVertex(vertex) {
if(!this.adjacencyList[ vertex ]) {
this.adjacencyList[ vertex ] = [];
}
}
addEdge(v1, v2) {
this.adjacencyList[ v1 ].push(v2);
this.adjacencyList[ v2 ].push(v1);
}
removeEdge(v1, v2) {
this.adjacencyList[ v1 ] = this.adjacencyList[ v1 ].filter(
v => v !== v2
);
this.adjacencyList[ v2 ] = this.adjacencyList[ v2 ].filter(
v => v !== v1
);
}
removeVertex(vertex) {
while(this.adjacencyList[ vertex ].length) {
const adjacentVertex = this.adjacencyList[ vertex ].pop();
this.removeEdge(vertex, adjacentVertex);
}
delete this.adjacencyList[ vertex ];
}
recursiveDepthFirstSearch(start) {
const result = [];
const visited = {};
const adjacencyList = this.adjacencyList;
function dfs(vertex) {
if(!vertex) return null;
visited[ vertex ] = true;
result.push(vertex);
adjacencyList[ vertex ].forEach(neighbour => {
if(!visited[ neighbour ]) {
return dfs(neighbour);
}
});
}
dfs(start);
return result;
}
iterativeDepthFirstSearch(start) {
const stack = [ start ];
const result = [];
const visited = {};
let currentVertex;
visited[ start ] = true;
while(stack.length) {
currentVertex = stack.pop();
result.push(currentVertex);
this.adjacencyList[ currentVertex ].forEach(neighbour => {
if(!visited[ neighbour ]) {
visited[ neighbour ] = true;
stack.push(neighbour);
}
});
}
return result;
}
breadthFirstSearch(start) {
const queue = [ start ];
const result = [];
const visited = {};
let currentVertex;
visited[ start ] = true;
while(queue.length) {
currentVertex = queue.shift();
result.push(currentVertex);
this.adjacencyList[ currentVertex ].forEach(neighbour => {
if(!visited[ neighbour ]) {
visited[ neighbour ] = true;
queue.push(neighbour);
}
});
}
return result;
}
}
const g = new Graph();
// graph.addVertex("Aspen");
// graph.addVertex("Dallas");
// graph.addVertex("Tokyo");
// graph.addEdge("Aspen", "Dallas");
// graph.addEdge("Tokyo", "Aspen");
// graph.removeEdge("Aspen", "Dallas");
// graph.removeVertex("Dallas")
// console.log(graph.adjacencyList);
g.addVertex("A");
g.addVertex("B");
g.addVertex("C");
g.addVertex("D");
g.addVertex("E");
g.addVertex("F");
g.addEdge("A", "B");
g.addEdge("A", "C");
g.addEdge("B", "D");
g.addEdge("C", "E");
g.addEdge("D", "E");
g.addEdge("D", "F");
g.addEdge("E", "F");
// A
// / \
// B C
// | |
// D --- E
// \ /
// F
// console.log(g.recursiveDepthFirstSearch("A"));
// console.log(g.iterativeDepthFirstSearch("A"));
console.log(g.breadthFirstSearch("A"));