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
| function Graph() { let vertices = []; let adjLists = new Dictionary(); const initColor = () => { let color = []; const len = vertices.length; for (let i = 0; i < len; i++) { color[vertices[i]] = 'white'; } return color; } this.addVertex = (v) => { vertices.push(v); adjLists.set(v, []); } this.addEdge = (v, w) => { if (vertices.indexOf(v) === -1 || vertices.indexOf(w) === -1) { return false; } else { adjLists.get(v).push(w); adjLists.get(w).push(v); } } this.initVertex = (arr) => { const len = arr.length; for (let i = 0; i < len; i++) { this.addVertex(arr[i]); } } this.initEdge = (arr) => { let len = arr.length; for (let i = 0; i < len; i++) { this.addEdge(arr[i][0], arr[i][1]) } } this.getVertives = () => vertices this.getAdjLists = () => adjLists; this.toString = () => { const len = vertices.length; let str = ''; for (let i = 0; i < len; i++) { str += `${vertices[i]} --> `; let neighbors = adjLists.get(vertices[i]); for (let j = 0; j < neighbors.length; j++) { str += `${neighbors[j]} `; } str += '\n'; } return str; } this.bfs = (v, callback) => { let queue = new Queue(); let color = initColor(); let d = {}; let pred = {}; for (let i = 0; i < vertices.length; i++) { d[vertices[i]] = 0; pred[vertices[i]] = null; } queue.enqueue(v); color[v] = 'gray'; while(!queue.isEmpty()) { let u = queue.dequeue(); let neighbors = adjLists.get(u); for (let i = 0; i < neighbors.length; i++) { if (color[neighbors[i]] === 'white') { queue.enqueue(neighbors[i]); color[neighbors[i]] = 'gray'; d[neighbors[i]] = d[u] + 1; pred[neighbors[i]] = u; } } color[u] = 'black'; if (callback) { callback(u) } } return { distance: d, predecessors: pred, }; } this.getShortestPath = (v) => { const { distance, predecessors } = this.bfs(v); const len = vertices.length; const fromVertex = v; let paths = ''; for (let i = 0; i < len; i++) { let toVertex = vertices[i]; let path = new Stack(); for (let v = toVertex; v !== fromVertex; v = predecessors[v]) { path.push(v); } paths += `${fromVertex}`; while (!path.isEmpty()) { paths += ` - ${path.pop()}`; } paths += '\n'; } return paths; } this.dfs = (callback) => { let time = 0; let color = initColor(); let d = {}, f = {}, p = {}; const len = vertices.length; for (let i = 0; i < len; i++) { d[vertices[i]] = 0; f[vertices[i]] = 0; p[vertices[i]] = null; } const dfsVisit = (v, callback) => { console.log('discovered ', v); d[v] = ++time; color[v] = 'gray'; if (callback) { callback(v); } let neighbors = adjLists.get(v); for (let i = 0; i < neighbors.length; i++) { if (color[neighbors[i]] === 'white') { p[neighbors[i]] = v; dfsVisit(neighbors[i], callback); } } color[v] = 'black'; f[v] = ++time; console.log('explored ', v); } for (let i = 0; i < len; i++) { if (color[vertices[i]] === 'white') { dfsVisit(vertices[i], callback); } } return { discovery: d, finished: f, predecessors: p, }; } this.getDFSTime = () => { const { discovery, finished } = this.dfs(); let time_info = ''; const len = vertices.length; for (let i = 0; i < len; i++) { let v = vertices[i]; time_info += `${v}: ${discovery[v]}/${finished[v]}\n`; } return time_info; } }
|