-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
157 lines (136 loc) · 3.05 KB
/
graph.js
File metadata and controls
157 lines (136 loc) · 3.05 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
const Queue = require('./queue');
class Graph {
constructor() {
this.vertices = [];
this.adjList = new Map();
}
/**
* 给图添加一个节点
* @param v 要添加的节点
*/
addVertex(v) {
this.vertices.push(v);
this.adjList.set(v, new Set());
}
/**
* 给图添加一条边
* @param v 边的其中一个节点
* @param w 边的其中另一个节点
*/
addEdge(v, w) {
this.adjList.get(v).add(w);
this.adjList.get(w).add(v);
}
//
/**
* 删除一条边
* @param v 边的其中一个节点
* @param w 边的其中另一个节点
*/
removeEdge(v, w) {
this.adjList.get(v).delete(w);
this.adjList.get(w).delete(v);
}
/**
* 广度优先遍历
* @param v 要遍历的开始节点
* @param cb 每一个节点要执行的回调函数
*/
bfs(v, cb) {
const read = {};
Object.values(this.vertices).forEach(key => {
read[key] = 'white';
}
);
const queue = new Queue();
queue.enqueue(v);
read[v] = 'black';
while (!queue.isEmpty()) {
let u = queue.dequeue();
let neighbors = this.adjList.get(u);
for (let v of neighbors) {
if (read[v] === 'white') {
queue.enqueue(v);
read[v] = 'black';
}
}
if (cb) cb(u);
}
}
/**
* 深度优先遍历
* @param v 要遍历的开始节点
* @param cb 每一个节点要执行的回调函数
*/
dfs(v, cb) {
const read = {};
Object.values(this.vertices).forEach(key => {
read[key] = 'white';
}
);
const dfsVisit = (u, read, cb) => {
if (cb) cb(u);
const neighbors = this.adjList.get(u);
read[u] = 'black';
for (let v of neighbors) {
if (read[v] === 'white') {
dfsVisit(v, read, cb);
}
}
};
dfsVisit(v, read, cb);
}
/**
* 展示所有的图节点和边的关系(用领接表的样式)
* @returns {string}
*/
toString() {
let str = '';
const vertices = this.vertices;
for (let i = 0; i < this.vertices.length; i++) {
str += vertices[i] + ' -> ';
let neighbors = Array.from(this.adjList.get(vertices[i]));
for (let j = 0; j < neighbors.length; j++) {
str += neighbors[j] + ' ';
}
str += '\n';
}
return str;
}
}
// e.g;
// const graph = new Graph();
//
// graph.addVertex('a');
// graph.addVertex('b');
// graph.addVertex('c');
// graph.addVertex('d');
// graph.addVertex('e');
// graph.addVertex('f');
// graph.addVertex('g');
//
// graph.addEdge('a', 'b');
// graph.addEdge('a', 'c');
// graph.addEdge('a', 'e');
// graph.addEdge('a', 'd');
// graph.addEdge('b', 'd');
// graph.addEdge('c', 'd');
// graph.addEdge('c', 'e');
// graph.addEdge('c', 'f');
// graph.addEdge('c', 'g');
// console.log(graph);
// console.log(graph.toString());
// graph.removeEdge('a', 'b');
//
// console.log(graph);
// console.log(graph.toString());
//
//
//
// function log(v) {
// console.log(v + ' -> ')
// }
// 测试bfs算法
// graph.bfs('a', log)
// 测试dfs算法
// graph.dfs('a', log)