forked from ohxxx/algorithm-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreadth-first-search.js
More file actions
136 lines (108 loc) · 3.02 KB
/
Copy pathbreadth-first-search.js
File metadata and controls
136 lines (108 loc) · 3.02 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
const Queue = require('../queue/queue')
const Colors = {
WHITE: 0,
GREY: 1,
BLACK: 2
}
const initializeColor = vertices => {
const color = {}
for (let i = 0; i < vertices.length; i++) {
color[vertices[i]] = Colors.WHITE
}
return color
};
const breadthFirstSearch = (graph, startVertex, callback) => {
const vertices = graph.getVertices()
const adjList = graph.getAdjList()
const color = initializeColor(vertices)
const queue = new Queue()
queue.enqueue(startVertex)
while (!queue.isEmpty()) {
const u = queue.dequeue()
const neighbors = adjList.get(u)
color[u] = Colors.GREY
for (let i = 0; i < neighbors.length; i++) {
const w = neighbors[i]
if (color[w] === Colors.WHITE) {
color[w] = Colors.GREY
queue.enqueue(w)
}
}
color[u] = Colors.BLACK
if (callback) callback(u);
}
}
const BFS = (graph, startVertex) => {
const vertices = graph.getVertices()
const adjList = graph.getAdjList()
const color = initializeColor(vertices)
const queue = new Queue()
const distances = {}
const predecessors = {}
queue.enqueue(startVertex)
for (let i = 0; i < vertices.length; i++) {
distances[vertices[i]] = 0
predecessors[vertices[i]] = null
}
while (!queue.isEmpty()) {
const u = queue.dequeue()
const neighbors = adjList.get(u)
color[u] = Colors.GREY
for (let i = 0; i < neighbors.length; i++) {
const w = neighbors[i]
if (color[w] === Colors.WHITE) {
color[w] = Colors.GREY
distances[w] = distances[u] + 1
predecessors[w] = u
queue.enqueue(w)
}
}
color[u] = Colors.BLACK
}
return {
distances,
predecessors
};
};
const Graph = require('./graph')
const Stack = require('../stack/stack')
const graph = new Graph();
const vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
for (let i = 0; i < vertices.length; i++) {
graph.addVertex(vertices[i])
}
graph.addEdge('A', 'B')
graph.addEdge('A', 'C')
graph.addEdge('A', 'D')
graph.addEdge('C', 'D')
graph.addEdge('C', 'G')
graph.addEdge('D', 'G')
graph.addEdge('D', 'H')
graph.addEdge('B', 'E')
graph.addEdge('B', 'F')
graph.addEdge('E', 'I')
console.log('********* toString ***********');
console.log(graph.toString());
console.log(' ');
console.log('********* breadthFirstSearch ***********');
const printVertex = (value) => console.log('Visited vertex: ' + value)
breadthFirstSearch(graph, vertices[0], printVertex)
console.log(' ');
console.log('********* BFS ***********');
const shortestPathA = BFS(graph, vertices[0]);
console.log('最短路径A的距离', shortestPathA.distances);
console.log('最短路径A的前溯点', shortestPathA.predecessors);
const fromVertex = vertices[0];
for (let i = 1; i < vertices.length; i++) {
const toVertex = vertices[i]
const path = new Stack()
for (let v = toVertex; v !== fromVertex; v = shortestPathA.predecessors[v]) {
path.push(v)
}
path.push(fromVertex)
let s = path.pop()
while (!path.isEmpty()) {
s += ' - ' + path.pop()
}
console.log(s)
}