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
// 图
function Graph(v) {
this.vertices = v;
this.edges = 0;
this.adj = [];
for (var i = 0; i < this.vertices; ++i) {
this.adj[i] = [];
this.adj[i].push("");
}
this.addEdge = addEdge;
this.showGraph = showGraph;
this.dfs = dfs;
this.bfs = bfs;
this.marked = [];
for (var i = 0; i < this.vertices; ++i) {
this.marked[i] = false;
}
}

function addEdge(v,w) {
this.adj[v].push(w);
this.adj[w].push(v);
this.edges++;
}

function showGraph() {
for (var i = 0; i < this.vertices; ++i) {
console.log(i + " -> ");
for (var j = 0; j

< this.vertices; ++j) {
if (this.adj[i][j] != undefined)
console.log(this.adj[i][j] + ' ');
}
console.log();
}
}

// 深度优先搜索
function dfs(v) {
this.marked[v] = true;
if (this.adj[v] != undefined) {
console.log("Visited vertex: " + v);
}
for (var w in this.adj[v]) {
if (!this.marked[w]) {
this.dfs(w);
}
}
}

// 广度优先搜索
function bfs(v) {
var queue = [];
this.marked[s] = true;
queue.push(s); // 添加到队尾
while (queue.length > 0) {
var v = queue.shift(); // 从队首移除
if (this.adj[v] != undefined) {
console.log("visited vertex: " + v);
}
for (var w in this.adj[v]) {
if (!this.marked[w]) {
this.marked[w] = true;
queue.push(w);
}
}
}
}

// 测试
g = new Graph(5);
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(1,3);
g.addEdge(2,4);
g.showGraph();
// test dfs
g.dfs(0);
// test bfs
g.bfs(0);