当前位置: 代码迷 >> 综合 >> Graph
  详细解决方案

Graph

热度:94   发布时间:2024-01-27 04:17:05.0
 function Graph() {// 属性this.vertexes = [] // 存储顶点this.adjList = new Dictionay() // 存储边// 添加方法Graph.prototype.addVertex = function (v) {this.vertexes.push(v)this.adjList.set(v, [])}Graph.prototype.addEdge = function (v, w) {this.adjList.get(v).push(w)this.adjList.get(w).push(v)}Graph.prototype.toString = function () {var resultStr = ""for (var i = 0; i < this.vertexes.length; i++) {resultStr += this.vertexes[i] + "->"var adj = this.adjList.get(this.vertexes[i])for (var j = 0; j < adj.length; j++) {resultStr += adj[j] + " "}resultStr += "\n"}return resultStr}// 初始化颜色Graph.prototype.initializeColor = function () {var colors = []for (var i = 0; i < this.vertexes.length; i++) {colors[this.vertexes[i]] = "white"}return colors}// 广度优先算法Graph.prototype.bfs = function (v, handler) {// 1.初始化颜色var color = this.initializeColor()// 2.创建队列var queue = new Queue()// 3.将传入的顶点放入队列中queue.enqueue(v)// 4.从队列中依次取出和放入数据while (!queue.isEmpty()) {// 4.1.从队列中取出数据var qv = queue.dequeue()// 4.2.获取qv相邻的所有顶点var qAdj = this.adjList.get(qv)// 4.3.将qv的颜色设置成灰色color[qv] = "gray"// 4.4.将qAdj的所有顶点依次压入队列中for (var i = 0; i < qAdj.length; i++) {var a = qAdj[i]if (color[a] === "white") {color[a] = "gray"queue.enqueue(a)}}// 4.5.因为qv已经探测完毕, 将qv设置成黑色color[qv] = "black"// 4.6.处理qvif (handler) {handler(qv)}}}// 深度优先搜索Graph.prototype.dfs = function (handler) {// 1.初始化颜色var color = this.initializeColor()// 2.遍历所有的顶点, 开始访问for (var i = 0; i < this.vertexes.length; i++) {if (color[this.vertexes[i]] === "white") {this.dfsVisit(this.vertexes[i], color, handler)}}}// dfs的递归调用方法Graph.prototype.dfsVisit = function (u, color, handler) {// 1.将u的颜色设置为灰色color[u] = "gray"// 2.处理u顶点if (handler) {handler(u)}// 3.u的所有邻接顶点的访问var uAdj = this.adjList.get(u)for (var i = 0; i < uAdj.length; i++) {var w = uAdj[i]if (color[w] === "white") {this.dfsVisit(w, color, handler)}}// 4.将u设置为黑色color[u] = "black"}}

 

  相关解决方案