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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
| class ValuePair { constructor(key, value) { this.key = key; this.value = value; }
toString() { return `[#${this.key}: ${this.value}]`; } }
function defaultToString(item) { if (item === null) { return 'NULL'; } if (item === undefined) { return 'UNDEFINED'; } if (typeof item === 'string' || item instanceof String) { return `${item}`; } return item.toString(); }
class Dictionary { constructor(toStrFn = defaultToString) { this.toStrFn = toStrFn; this.table = {}; } hasKey(key) { return this.table[this.toStrFn(key)] != null; } set(key, value) { if (key != null && value != null) { const tableKey = this.toStrFn(key); this.table[tableKey] = new ValuePair(key,value); return true; } return false; } remove(key) { if (this.hasKey(key)) { delete this.table[this.toStrFn(key)]; return true; } return false; } get(key) { const valuePair = this.table[this.toStrFn(key)]; return valuePair == null ? undefined : valuePair.value; } keyValues() { return Object.values(this.table); } values() { return this.keyValues().map(valuePair => valuePair.value); }
keys() { return this.keyValues().map(valuePair => valuePair.key); } forEach(callbackFn) { const valuePairs = this.keyValues(); for (let i = 0; i < valuePairs.length; i++) { const result = callbackFn(valuePairs[i].key, valuePairs[i].value); if (result === false) { break; } } }
isEmpty() { return this.size() === 0; }
size() { return Object.keys(this.table).length; }
clear() { this.table = {}; }
toString() { if (this.isEmpty()) { return ''; } const valuePairs = this.keyValues(); let objString = `${valuePairs[0].toString()}`; for (let i = 1; i < valuePairs.length; i++) { objString = `${objString},${valuePairs[i].toString()}`; } return objString; }
}
class Graph { constructor (isDirected = false) { this.isDirected = isDirected; this.vertices = []; this.adjList = new Dictionary(); } addVertex(v) { if (!this.vertices.includes(v)) { this.vertices.push(v); this.adjList.set(v, []); } } addEdge(a, b ) { if (!this.adjList.get(a)) { this.addVertex(a); } if (!this.adjList.get(b)) { this.adjList(b); } this.adjList.get(a).push(b); if (this.isDirected !== true) { this.adjList.get(b).push(a); } } getVertices() { return this.vertices; } getAdjList() { return this.adjList; } toString() { let s = ''; for (let i = 0; i < this.vertices.length; i++) { s += `${this.vertices[i]} -> `; const neighbors = this.adjList.get(this.vertices[i]); for (let j = 0; j < neighbors.length; j++) { s +=`${neighbors[j]} `; } s += '\n'; } return s; }
}
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 depthFirstSearchVisit = (u, color, adjList, callback) =>{ color[u] = Colors.GREY; if (callback) { callback(u); } const neighbors = adjList.get(u); for (let i = 0; i < neighbors.length; i++) { const w = neighbors[i]; if (color[w] === Colors.WHITE) { depthFirstSearchVisit(w, color, adjList, callback); } } color[u] = Colors.BLACK; };
const depthFirstSearch = (graph, callback) =>{ const vertices = graph.getVertices(); const adjList = graph.getAdjList(); const color = initializeColor(vertices); for (let i = 0; i < vertices.length; i++) { if (color[vertices[i]] === Colors.WHITE) { depthFirstSearchVisit(vertices[i], color, adjList, callback); } }
};
const DFSVisit = (u, color, d, f, p, time, adjList) => { color[u] = Colors.GREY; d[u] = ++time.count; const neighbors = adjList.get(u); for (let i =0; i <neighbors.length; i++) { const w = neighbors[i]; if (color[w] === Colors.WHITE) { p[w] = u; DFSVisit(w, color, d, f, p, time, adjList); } } color[u] = Colors.BLACK; f[u] = ++time.count; };
const DFS = graph =>{ const vertices = graph.getVertices(); const adjList = graph.getAdjList(); const color = initializeColor(vertices); const d = {}; const f = {}; const p = {}; const time = {count: 0}; for (let i = 0; i < vertices.length; i++) { f[vertices[i]] = 0; d[vertices[i]] = 0; p[vertices[i]] = null; } for (let i = 0; i < vertices.length; i++) { if (color[vertices[i]] === Colors.WHITE) { DFSVisit(vertices[i], color, d, f, p, time, adjList); } } return { discovery: d, finished: f, predecessors:p };
};
let graph = new Graph(true);
let myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
for (let i = 0; i < myVertices.length; i++) { graph.addVertex(myVertices[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('********* printing graph ***********');
console.log(graph.toString());
console.log('********* dfs with callback ***********');
const printVertex = value => console.log('Visited vertex: ' + value);
depthFirstSearch(graph, printVertex); console.log('********* DFS ***********');
const result = DFS(graph); console.log('discovery', result.discovery); console.log('finished', result.finished); console.log('predecessors', result.predecessors);
console.log('********* topological sort - DFS ***********');
graph = new Graph(true);
myVertices = ['A', 'B', 'C', 'D', 'E', 'F']; for (i = 0; i < myVertices.length; i++) { graph.addVertex(myVertices[i]); } graph.addEdge('A', 'C'); graph.addEdge('A', 'D'); graph.addEdge('B', 'D'); graph.addEdge('B', 'E'); graph.addEdge('C', 'F'); graph.addEdge('F', 'E');
const result2 = DFS(graph); console.log('discovery', result.discovery); console.log('finished', result.finished); console.log('predecessors', result.predecessors);
const fTimes = result2.finished; s = ''; for (let count = 0; count < myVertices.length; count ++) { let max = 0; let maxName = null; for (i = 0; i < myVertices.length; i++) { if (fTimes[myVertices[i]] > max ) { max = fTimes[myVertices[i]]; maxName = myVertices[i]; } } s += ' - ' + maxName; delete fTimes[maxName]; } console.log(s);
DFS
|