资讯专栏INFORMATION COLUMN

BFS,DFS 算法原理及js实现

刘德刚 / 1991人阅读

1. 说明

本文所有的算法严格按照《算法导论》,本文将详细的对BFSDFS进行分析,并提供算法的 js 实现,同时会对创建链表的方式进行优化

2. 图的表示

图的表示分为对顶点集 V 的表示和对边集 E 的表示,这里的重点是如何表示边,边的表示分为邻接矩阵邻接链表这两种表示方法,邻接矩阵适合表示边稠密的图,其消耗空间为|V|*|V|,如果是无向图,则可以用上三角矩阵或者下三角矩阵来表示,是空间消耗变为|V|*|V|/2邻接链表适合表示边稀疏的图,其消耗的空间为 O(|V|+|E|),用邻接链表表示图很紧凑,没有空间浪费,用《算法导论》中的原话就是,邻接链表表示图,鲁棒性很高。本文涉及的图,全部用邻接链表表示。

2.1. 本文的算法都是对该图的操作

2.2. 对上图进行邻接链表的转化

从上图可以看到我们将图的分为两部分,顶点和边,我们分别对这两部分进行表示,我们用数组去存放顶点,用链表去描述边。A-E 做为节点的标识。数字表示顶点在数组中的位置。由这幅图可以看到从节点 A 发出的边有两条,分别是 ,和

3. BFS 广度优先搜索

广度优先搜索的思想是,对于图G和给定的节点s,广度优先搜索需要一个辅助的先进先出的队列 Q

s加入到Q

sQ总移出,用临时变量接受s,如果s没有被访问过,从s出发,发现s的所有邻接节点并放入Q

访问s

Q队列的第一个元素移除队列作为新的s执行2-4过程直到队列Q为空

3.1 表示顶点的数据结构

</>复制代码

  1. function Vertex() {
  2. if (!(this instanceof Vertex))
  3. return new Vertex();
  4. this.color = this.WHITE; //初始为 白色
  5. this.pi = null; //初始为 无前驱
  6. this.d = this.INFINITY; //初始为 无穷大
  7. this.edges = null; //由顶点发出的所有边
  8. this.value = null; //节点的值 默认为空
  9. }
  10. Vertex.prototype = {
  11. constructor: Vertex,
  12. WHITE: "white", //白色
  13. GRAY: "gray", //灰色
  14. BLACK: "black", //黑色
  15. INFINITY: null, //d 为 null 时表示无穷大
  16. }

为了跟踪算法的进展,我们对图进行搜索的时候会对图中的顶点进行涂色,图初始化是顶点全部为白色,当第一次发现某个节点时,我们将他涂为灰色,当对某个节点访问完成后,我们将它涂为黑色。在这里我们看到每个节点都有 五个 属性,color表示节点的颜色,pi 表示前驱结点,d 表示广度优先搜索中从源节点到当前节点的距离,edges 表示从当前节点发出的所有边,value 表示节点存放的数据

3.2 表示边的数据结构

</>复制代码

  1. function Edge() {
  2. if (!(this instanceof Edge))
  3. return new Edge();
  4. this.index = null; //边所依附的节点的位置
  5. this.sibling = null;
  6. }

可以看到,边包含两个两个属性,index,和siblingindex表示这条边连接的节点在顶点数组中的位置,sibling只想下一个连接兄弟节点的边。

3.3 表示图的数据结构

</>复制代码

  1. function Graph() {
  2. if (!(this instanceof Graph))
  3. return new Graph();
  4. this.graph = []; //存放顶点的数组
  5. }
  6. Graph.prototype = {
  7. constructor: Graph,
  8. addNode: function (node) {
  9. this.graph.push(node);
  10. },
  11. getNode: function (index) {
  12. return this.graph[index];
  13. }
  14. }

3.4 构建图

</>复制代码

  1. //创建 顶点
  2. var vA = Vertex();
  3. var vB = Vertex();
  4. var vC = Vertex();
  5. var vD = Vertex();
  6. var vE = Vertex();
  7. var vF = Vertex();
  8. vA.value = "A";
  9. vB.value = "B";
  10. vC.value = "C";
  11. vD.value = "D";
  12. vE.value = "E";
  13. vF.value = "F";
  14. //构建由 A 节点发出的边集
  15. var eA1 = Edge();
  16. var eA2 = Edge();
  17. eA1.index = 1;
  18. eA2.index = 3;
  19. eA1.sibling = eA2;
  20. vA.edges = eA1;
  21. //构建有 B 节点发出的边集
  22. var eB1 = Edge();
  23. var eB2 = Edge();
  24. var eB3 = Edge();
  25. eB1.index = 0;
  26. eB2.index = 4;
  27. eB3.index = 2;
  28. eB1.sibling = eB2;
  29. eB2.sibling = eB3;
  30. vB.edges = eB1;
  31. //构建由 C 节点发出的边
  32. var eC1 = Edge();
  33. var eC2 = Edge();
  34. var eC3 = Edge();
  35. eC1.index = 1;
  36. eC2.index = 4;
  37. eC3.index = 5;
  38. eC1.sibling = eC2;
  39. eC2.sibling = eC3;
  40. vC.edges = eC1;
  41. //构建由 D 节点发出的边
  42. var eD1 = Edge();
  43. eD1.index = 0;
  44. vD.edges = eD1;
  45. //构建由 E 节点发出的边
  46. var eE1 = Edge();
  47. var eE2 = Edge();
  48. var eE3 = Edge();
  49. eE1.index = 1;
  50. eE2.index = 2;
  51. eE3.index = 5;
  52. eE1.sibling = eE2;
  53. eE2.sibling = eE3;
  54. vE.edges = eE1;
  55. //构建由 F 节点发出的边
  56. var eF1 = Edge();
  57. var eF2 = Edge();
  58. eF1.index = 2;
  59. eF2.index = 4;
  60. eF1.sibling = eF2;
  61. vF.edges = eF1;
  62. //构建图
  63. var g = Graph();
  64. g.addNode(vA);
  65. g.addNode(vB);
  66. g.addNode(vC);
  67. g.addNode(vD);
  68. g.addNode(vE);
  69. g.addNode(vF);

3.5 BFS算法

</>复制代码

  1. //广度优先搜索
  2. function BFS(g, s) {
  3. let queue = []; //辅助队列 Q
  4. s.color = s.GRAY; //首次发现s涂为灰色
  5. s.d = 0; //距离为0
  6. queue.push(s); //将s放入队列 Q
  7. while (queue.length > 0) { //当队列Q中有顶点时执行搜索
  8. let u = queue.shift(); //将Q中的第一个元素移出
  9. if (u.edges == null) continue; //如果从当前顶点没有发出边
  10. let sibling = u.edges; //获取表示邻接边的链表的头节点
  11. while (sibling != null) { //当链表不为空
  12. let index = sibling.index; //当前边所连接的顶点在队列中的位置
  13. let n = g.getNode(index); //获取顶点
  14. if (n.color == n.WHITE) { //如果没有被访问过
  15. n.color = n.GRAY; //涂为灰色
  16. n.d = u.d + 1; //距离加1
  17. n.pi = u; //设置前驱节点
  18. queue.push(n); //将 n 放入队列 Q
  19. }
  20. sibling = sibling.sibling; //下一条边
  21. }
  22. u.color = u.BLACK; //当前顶点访问结束 涂为黑色
  23. }
  24. }

3.6 完整代码可粘贴到浏览器控制台运行

</>复制代码

  1. //数据结构 邻接链表-顶点
  2. function Vertex() {
  3. if (!(this instanceof Vertex))
  4. return new Vertex();
  5. this.color = this.WHITE; //初始为 白色
  6. this.pi = null; //初始为 无前驱
  7. this.d = this.INFINITY; //初始为 无穷大
  8. this.edges = null; //由顶点发出的所有边
  9. this.value = null; //节点的值 默认为空
  10. }
  11. Vertex.prototype = {
  12. constructor: Vertex,
  13. WHITE: "white", //白色
  14. GRAY: "gray", //灰色
  15. BLACK: "black", //黑色
  16. INFINITY: null, //d 为 null 时表示无穷大
  17. }
  18. //数据结构 邻接链表-边
  19. function Edge() {
  20. if (!(this instanceof Edge))
  21. return new Edge();
  22. this.index = null; //边所依附的节点的位置
  23. this.sibling = null;
  24. }
  25. //数据结构 图-G
  26. function Graph() {
  27. if (!(this instanceof Graph))
  28. return new Graph();
  29. this.graph = [];
  30. }
  31. Graph.prototype = {
  32. constructor: Graph,
  33. //这里加进来的已经具备了边的关系
  34. addNode: function (node) {
  35. this.graph.push(node);
  36. },
  37. getNode: function (index) {
  38. return this.graph[index];
  39. }
  40. }
  41. //广度优先搜索
  42. function BFS(g, s) {
  43. let queue = [];
  44. s.color = s.GRAY;
  45. s.d = 0;
  46. queue.push(s);
  47. while (queue.length > 0) {
  48. let u = queue.shift();
  49. if (u.edges == null) continue;
  50. let sibling = u.edges;
  51. while (sibling != null) {
  52. let index = sibling.index;
  53. let n = g.getNode(index);
  54. if (n.color == n.WHITE) {
  55. n.color = n.GRAY;
  56. n.d = u.d + 1;
  57. n.pi = u;
  58. queue.push(n);
  59. }
  60. sibling = sibling.sibling;
  61. }
  62. u.color = u.BLACK;
  63. console.log(u);
  64. }
  65. }
  66. //创建 顶点
  67. var vA = Vertex();
  68. var vB = Vertex();
  69. var vC = Vertex();
  70. var vD = Vertex();
  71. var vE = Vertex();
  72. var vF = Vertex();
  73. vA.value = "A";
  74. vB.value = "B";
  75. vC.value = "C";
  76. vD.value = "D";
  77. vE.value = "E";
  78. vF.value = "F";
  79. //构建由 A 节点发出的边集
  80. var eA1 = Edge();
  81. var eA2 = Edge();
  82. eA1.index = 1;
  83. eA2.index = 3;
  84. eA1.sibling = eA2;
  85. vA.edges = eA1;
  86. //构建有 B 节点发出的边集
  87. var eB1 = Edge();
  88. var eB2 = Edge();
  89. var eB3 = Edge();
  90. eB1.index = 0;
  91. eB2.index = 4;
  92. eB3.index = 2;
  93. eB1.sibling = eB2;
  94. eB2.sibling = eB3;
  95. vB.edges = eB1;
  96. //构建由 C 节点发出的边
  97. var eC1 = Edge();
  98. var eC2 = Edge();
  99. var eC3 = Edge();
  100. eC1.index = 1;
  101. eC2.index = 4;
  102. eC3.index = 5;
  103. eC1.sibling = eC2;
  104. eC2.sibling = eC3;
  105. vC.edges = eC1;
  106. //构建由 D 节点发出的边
  107. var eD1 = Edge();
  108. eD1.index = 0;
  109. vD.edges = eD1;
  110. //构建由 E 节点发出的边
  111. var eE1 = Edge();
  112. var eE2 = Edge();
  113. var eE3 = Edge();
  114. eE1.index = 1;
  115. eE2.index = 2;
  116. eE3.index = 5;
  117. eE1.sibling = eE2;
  118. eE2.sibling = eE3;
  119. vE.edges = eE1;
  120. //构建由 F 节点发出的边
  121. var eF1 = Edge();
  122. var eF2 = Edge();
  123. eF1.index = 2;
  124. eF2.index = 4;
  125. eF1.sibling = eF2;
  126. vF.edges = eF1;
  127. //构建图
  128. var g = Graph();
  129. g.addNode(vA);
  130. g.addNode(vB);
  131. g.addNode(vC);
  132. g.addNode(vD);
  133. g.addNode(vE);
  134. g.addNode(vF);
  135. BFS(g, vB);

顶点的访问顺序为 B->A->E->C->D->F

4. DFS 深度优先搜索

特点
深度优先搜索一般默认的源点有多个,搜索时的前驱子图会构成一个深度优先森林,这是依据深度优先搜索的搜索结果的使用深度优先搜索算法常常作为另一个算法的一个子程序被使用深度优先搜索在节点中增加了一个发现的时间戳,一个访问的时间戳,通常能帮助我们推断算法的行为,在d-f之间是灰色,在f之后是黑色,时间戳为12*|v|之间的整数

算法思想
只要有可能,就在图中尽量“深入”,总是对最近才发现的节点v的出发边进行探索,知道该节点的所有出发边都被发现为止。一旦v的所有发出的边都被发现,搜索则“回溯”到v的前驱节点,该过程一直持续到源节点可达的所有节点都被发现为止,如果还有未发现的节点,则深度优先搜索将从这些未被发现的节点中任选一个作为新的源节点,并重复同样的搜索过程

4.1 算法数据结构

深度优先搜索的数据结构只有在表示顶点时稍有不同,其它的都相同,这里给出表示顶点的数据结构

</>复制代码

  1. function Vertex() {
  2. if (!(this instanceof Vertex))
  3. return new Vertex();
  4. this.color = this.WHITE; //初始为 白色
  5. this.pi = null; //初始为 无前驱
  6. this.d = null; //时间戳 发现时
  7. this.f = null; //时间戳 邻接链表扫描完成时
  8. this.edges = null; //由顶点发出的所有边
  9. this.value = null; //节点的值 默认为空
  10. }
  11. Vertex.prototype = {
  12. constructor: Vertex,
  13. WHITE: "white", //白色
  14. GRAY: "gray", //灰色
  15. BLACK: "black", //黑色
  16. }

可以看到顶点数据结构中的多了一个f,同时d的含义也发生了变化df作为发现和访问完成的时间戳,取值为从12*|v|

4.2 DFS算法

</>复制代码

  1. function DFS(g) {
  2. let t = 0; //时间戳
  3. for (let v of g.vertexs) { //让每个节点都作为一次源节点
  4. if (v.color == v.WHITE) DFSVisit(g, v);
  5. }
  6. function DFSVisit(g, v) {
  7. t = t + 1; //时间戳加一
  8. v.d = t;
  9. v.color = v.GRAY;
  10. let sibling = v.edges;
  11. while (sibling != null) {
  12. let index = sibling.index;
  13. let n = g.getNode(index);
  14. if (n.color == n.WHITE) {
  15. n.pi = v;
  16. DFSVisit(g, n); //先纵向找
  17. }
  18. sibling = sibling.sibling; //利用递归的特性来回溯
  19. }
  20. v.color = v.BLACK;
  21. t = t + 1; //时间戳加一
  22. v.f = t;
  23. }
  24. }

4.3 DFS完整代码

</>复制代码

  1. function Vertex() {
  2. if (!(this instanceof Vertex))
  3. return new Vertex();
  4. this.color = this.WHITE; //初始为 白色
  5. this.pi = null; //初始为 无前驱
  6. this.d = null; //时间戳 发现时
  7. this.f = null; //时间戳 邻接链表扫描完成
  8. this.edges = null; //由顶点发出的所有边
  9. this.value = null; //节点的值 默认为空
  10. }
  11. Vertex.prototype = {
  12. constructor: Vertex,
  13. WHITE: "white", //白色
  14. GRAY: "gray", //灰色
  15. BLACK: "black", //黑色
  16. }
  17. //数据结构 图-G
  18. function Graph() {
  19. if (!(this instanceof Graph))
  20. return new Graph();
  21. this.vertexs = [];
  22. }
  23. Graph.prototype = {
  24. constructor: Graph,
  25. addNode: function (node) {
  26. this.vertexs.push(node);
  27. },
  28. getNode: function (index) {
  29. return this.vertexs[index];
  30. }
  31. }
  32. //这里 t 作为全局变量和参数时结果不一样 因为 js 对于基本类型的参数采用的是值捕获,对于对象类型的参数采用的是引用捕获
  33. function DFS(g) {
  34. let t = 0;
  35. for (let v of g.vertexs) {
  36. if (v.color == v.WHITE) DFSVisit(g, v);
  37. }
  38. function DFSVisit(g, v) {
  39. t = t + 1;
  40. v.d = t;
  41. v.color = v.GRAY;
  42. let sibling = v.edges;
  43. while (sibling != null) {
  44. let index = sibling.index;
  45. let n = g.getNode(index);
  46. if (n.color == n.WHITE) {
  47. n.pi = v;
  48. DFSVisit(g, n); //先纵向找
  49. }
  50. sibling = sibling.sibling; //利用递归的特性来回溯
  51. }
  52. v.color = v.BLACK;
  53. t = t + 1;
  54. v.f = t;
  55. console.log(v);
  56. }
  57. }
  58. //数据结构 邻接链表-边
  59. function Edge() {
  60. if (!(this instanceof Edge))
  61. return new Edge();
  62. this.index = null; //边所依附的节点的位置
  63. this.sibling = null;
  64. }
  65. //创建 顶点
  66. var vA = Vertex();
  67. var vB = Vertex();
  68. var vC = Vertex();
  69. var vD = Vertex();
  70. var vE = Vertex();
  71. var vF = Vertex();
  72. vA.value = "A";
  73. vB.value = "B";
  74. vC.value = "C";
  75. vD.value = "D";
  76. vE.value = "E";
  77. vF.value = "F";
  78. //构建由 A 节点发出的边集
  79. var eA1 = Edge();
  80. var eA2 = Edge();
  81. eA1.index = 1;
  82. eA2.index = 3;
  83. eA1.sibling = eA2;
  84. vA.edges = eA1;
  85. //构建有 B 节点发出的边集
  86. var eB1 = Edge();
  87. var eB2 = Edge();
  88. var eB3 = Edge();
  89. eB1.index = 0;
  90. eB2.index = 4;
  91. eB3.index = 2;
  92. eB1.sibling = eB2;
  93. eB2.sibling = eB3;
  94. vB.edges = eB1;
  95. //构建由 C 节点发出的边
  96. var eC1 = Edge();
  97. var eC2 = Edge();
  98. var eC3 = Edge();
  99. eC1.index = 1;
  100. eC2.index = 4;
  101. eC3.index = 5;
  102. eC1.sibling = eC2;
  103. eC2.sibling = eC3;
  104. vC.edges = eC1;
  105. //构建由 D 节点发出的边
  106. var eD1 = Edge();
  107. eD1.index = 0;
  108. vD.edges = eD1;
  109. //构建由 E 节点发出的边
  110. var eE1 = Edge();
  111. var eE2 = Edge();
  112. var eE3 = Edge();
  113. eE1.index = 1;
  114. eE2.index = 2;
  115. eE3.index = 5;
  116. eE1.sibling = eE2;
  117. eE2.sibling = eE3;
  118. vE.edges = eE1;
  119. //构建由 F 节点发出的边
  120. var eF1 = Edge();
  121. var eF2 = Edge();
  122. eF1.index = 2;
  123. eF2.index = 4;
  124. eF1.sibling = eF2;
  125. vF.edges = eF1;
  126. //构建图
  127. var g = Graph();
  128. g.addNode(vA);
  129. g.addNode(vB);
  130. g.addNode(vC);
  131. g.addNode(vD);
  132. g.addNode(vE);
  133. g.addNode(vF);
  134. DFS(g);

节点访问顺序为 F->C->E->B->D->A

5. 对构建链表的方式进行优化

我们发现构建图的操作过于繁琐,于是想简化图的构建方式,简化后如下

</>复制代码

  1. var vertexs = ["A", "B", "C", "D", "E", "F"];
  2. var edges = {
  3. A: [{ id: "B", w: 1 }, { id: "D", w: 2 }],
  4. B: [{ id: "A", w: 3 }, { id: "E", w: 3 }, { id: "C", w: 7 }],
  5. C: [{ id: "B", w: 5 }, { id: "E", w: 3 }, { id: "F", w: 4 }],
  6. D: [{ id: "A", w: 2 }],
  7. E: [{ id: "B", w: 3 }, { id: "C", w: 7 }, { id: "F", w: 3 }],
  8. F: [{ id: "C", w: 6 }, { id: "E", w: 9 }]
  9. }
  10. var g = Graph();
  11. g.initVertex(vertexs);
  12. g.initEdge(edges);

我们想用这种方式初始化一个图,w为边的权值

这里的改进只是针对图的构建,所有无论时BFS,还是DFS,表示顶点和边的数据结构都没有变,只有对表示图的数据结构 Graph进行改进

5.1 改进之后的Graph

//数据结构 图-G

</>复制代码

  1. //数据结构 图-G
  2. function Graph() {
  3. if (!(this instanceof Graph))
  4. return new Graph();
  5. this.graph = [];
  6. this.refer = new Map(); //字典 用来映射标节点的识符和数组中的位置
  7. }
  8. Graph.prototype = {
  9. constructor: Graph,
  10. //这里加进来的已经具备了边的关系
  11. addNode: function(node) {
  12. this.graph.push(node);
  13. },
  14. getNode: function(index) {
  15. return this.graph[index];
  16. },
  17. //创建图的 节点
  18. initVertex: function(vertexs) {
  19. //创建节点并初始化节点属性 value
  20. for (let value of vertexs) {
  21. let vertex = Vertex();
  22. vertex.value = value;
  23. this.graph.push(vertex);
  24. }
  25. //初始化 字典
  26. for (let i in this.graph) {
  27. this.refer.set(this.graph[i].value,i);
  28. }
  29. },
  30. //建立图中 边 的关系
  31. initEdge: (function(){
  32. //创建链表,返回链表的第一个节点
  33. function createLink(index, len, edges, refer) {
  34. if (index >= len) return null;
  35. let edgeNode = Edge();
  36. edgeNode.index = refer.get(edges[index].id); //边连接的节点 用在数组中的位置表示 参照字典
  37. edgeNode.w = edges[index].w; //边的权值
  38. edgeNode.sibling = createLink(++index, len, edges, refer); //通过递归实现 回溯
  39. return edgeNode;
  40. }
  41. return function(edges) {
  42. for (let field in edges) {
  43. let index = this.refer.get(field); //从字典表中找出节点在 graph 中的位置
  44. let vertex = this.graph[index]; //获取节点
  45. vertex.edges = createLink(0, edges[field].length, edges[field], this.refer);
  46. }
  47. }
  48. }())
  49. }

5.2 改进之后的BFS完整代码

DFS相同

</>复制代码

  1. function Vertex() {
  2. if (!(this instanceof Vertex))
  3. return new Vertex();
  4. this.color = this.WHITE; //初始为 白色
  5. this.pi = null; //初始为 无前驱
  6. this.d = this.INFINITY; //初始为 无穷大
  7. this.edges = null; //由顶点发出的所有边
  8. this.value = null; //节点的值 默认为空
  9. }
  10. Vertex.prototype = {
  11. constructor: Vertex,
  12. WHITE: "white", //白色
  13. GRAY: "gray", //灰色
  14. BLACK: "black", //黑色
  15. INFINITY: null, //d 为 null 时表示无穷大
  16. }
  17. //数据结构 邻接链表-边
  18. function Edge() {
  19. if (!(this instanceof Edge))
  20. return new Edge();
  21. this.index = null; //边所依附的节点的位置
  22. this.sibling = null;
  23. this.w = null; //保存边的权值
  24. }
  25. //数据结构 图-G
  26. function Graph() {
  27. if (!(this instanceof Graph))
  28. return new Graph();
  29. this.graph = [];
  30. this.refer = new Map(); //字典 用来映射标节点的识符和数组中的位置
  31. }
  32. Graph.prototype = {
  33. constructor: Graph,
  34. //这里加进来的已经具备了边的关系
  35. addNode: function(node) {
  36. this.graph.push(node);
  37. },
  38. getNode: function(index) {
  39. return this.graph[index];
  40. },
  41. //创建图的 节点
  42. initVertex: function(vertexs) {
  43. //创建节点并初始化节点属性 value
  44. for (let value of vertexs) {
  45. let vertex = Vertex();
  46. vertex.value = value;
  47. this.graph.push(vertex);
  48. }
  49. //初始化 字典
  50. for (let i in this.graph) {
  51. this.refer.set(this.graph[i].value,i);
  52. }
  53. },
  54. //建立图中 边 的关系
  55. initEdge: (function(){
  56. //创建链表,返回链表的第一个节点
  57. function createLink(index, len, edges, refer) {
  58. if (index >= len) return null;
  59. let edgeNode = Edge();
  60. edgeNode.index = refer.get(edges[index].id); //边连接的节点 用在数组中的位置表示 参照字典
  61. edgeNode.w = edges[index].w; //边的权值
  62. edgeNode.sibling = createLink(++index, len, edges, refer); //通过递归实现 回溯
  63. return edgeNode;
  64. }
  65. return function(edges) {
  66. for (let field in edges) {
  67. let index = this.refer.get(field); //从字典表中找出节点在 graph 中的位置
  68. let vertex = this.graph[index]; //获取节点
  69. vertex.edges = createLink(0, edges[field].length, edges[field], this.refer);
  70. }
  71. }
  72. }())
  73. }
  74. //广度优先搜索
  75. function BFS(g, s) {
  76. let queue = [];
  77. s.color = s.GRAY;
  78. s.d = 0;
  79. queue.push(s);
  80. while (queue.length > 0) {
  81. let u = queue.shift();
  82. if (u.edges == null) continue;
  83. let sibling = u.edges;
  84. while (sibling != null) {
  85. let index = sibling.index;
  86. let n = g.getNode(index);
  87. if (n.color == n.WHITE) {
  88. n.color = n.GRAY;
  89. n.d = u.d + 1;
  90. n.pi = u;
  91. queue.push(n);
  92. }
  93. sibling = sibling.sibling;
  94. }
  95. u.color = u.BLACK;
  96. console.log(u)
  97. }
  98. }
  99. var vertexs = ["A", "B", "C", "D", "E", "F"];
  100. var edges = {
  101. A: [{ id: "B", w: 1 }, { id: "D", w: 2 }],
  102. B: [{ id: "A", w: 3 }, { id: "E", w: 3 }, { id: "C", w: 7 }],
  103. C: [{ id: "B", w: 5 }, { id: "E", w: 3 }, { id: "F", w: 4 }],
  104. D: [{ id: "A", w: 2 }],
  105. E: [{ id: "B", w: 3 }, { id: "C", w: 7 }, { id: "F", w: 3 }],
  106. F: [{ id: "C", w: 6 }, { id: "E", w: 9 }]
  107. }
  108. //构建图
  109. var g = Graph();
  110. g.initVertex(vertexs);
  111. g.initEdge(edges);
  112. //调用BFS
  113. BFS(g, g.graph[1]);
6. 总结

着重体会

1 如何用邻接链表表示图的边

2 如何用递归的特性实现回溯

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/92176.html

相关文章

  • JS算法之深度优先遍历(DFS)和广度优先遍历(BFS)

    摘要:算法之深度优先遍历和广度优先遍历背景在开发页面的时候,我们有时候会遇到这种需求在页面某个节点中遍历,找到目标节点,我们正常做法是利用选择器,或者,但在本文,我们从算法的角度去查找节点,同时理解一下深度优先遍历和广度优先遍历的原理。 JS算法之深度优先遍历(DFS)和广度优先遍历(BFS) 背景 在开发页面的时候,我们有时候会遇到这种需求:在页面某个dom节点中遍历,找到目标dom节点,...

    roadtogeek 评论0 收藏0
  • js 中二叉树的深度遍历与广度遍历(递归实现与非递归实现)

    摘要:树中结点的最大层次称为树的深度或高度。二叉树有深度遍历和广度遍历,深度遍历有前序中序和后序三种遍历方法。二叉树的前序遍历可以用来显示目录结构等中序遍历可以实现表达式树,在编译器底层很有用后序遍历可以用来实现计算目录内的文件及其信息等。 树的简介 栈、队列、链表等数据结构,都是顺序数据结构。而树是非顺序数据结构。树型结构是一类非常重要的非线性结构。直观地,树型结构是以分支关系定义的层次结...

    Yuanf 评论0 收藏0
  • js版本的BFS&DFS

    摘要:队列栈队列是先进先出,后进后出,常用的操作是取第一个元素尾部加入一个元素。栈是后进先出,就像一个垃圾桶,后入的垃圾先被倒出来。遍历中间过程,每一个节点入栈的时候是灰色的,出栈的时候是黑色的。 0. 前言 广度优先搜索(BFS)和深度优先搜索(DFS),大家可能在oj上见过,各种求路径、最短路径、最优方法、组合等等。于是,我们不妨动手试一下js版本怎么玩。 1.队列、栈 队列是先进先出,...

    刘福 评论0 收藏0
  • 拓扑排序原理分析js实现

    摘要:但是一个偏序关系,如果我们默认,先出现的排在前面,那么我们就能比较,的关系了。对于算法的每个节点,我们都有一个发现时间,一个访问时间,当运行完成时,对于图中的任意一条边,都有所以拓扑排序的次序与顶点的完成时间恰好相反。 1. 偏序和全序的概念 1.1. 偏序 设R是集合A上的一个二元关系,若R满足下列三个性质则称R为A上的偏序关系自反性:对任意x∈A,有∈R反对称性:对任意的x,y∈A...

    QiShare 评论0 收藏0
  • PHP面试:常见查找算法一篇说透

    摘要:我们现在来看二分搜索算法的两种变形插值搜索和指数搜索。插值搜索是对二分搜索算法的改进,插值搜索可以基于搜索的值选择到达不同的位置。 预警 在本篇文章中,将为各位老铁介绍不同的搜索算法以及它们的复杂度。因为力求通俗易懂,所以篇幅可能较长,大伙可以先Mark下来,每天抽时间看一点理解一点。本文配套的Github Repo,欢迎各位老铁star,会一直更新的。 开篇 和排序类似,搜索或者叫做...

    付永刚 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<