csDfs.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.csDfs = csDfs;
  6. var _csMarked = require("./csMarked.js");
  7. var _csMark = require("./csMark.js");
  8. var _csUnflip = require("./csUnflip.js");
  9. /**
  10. * Depth-first search computes the nonzero pattern xi of the directed graph G (Matrix) starting
  11. * at nodes in B (see csReach()).
  12. *
  13. * @param {Number} j The starting node for the DFS algorithm
  14. * @param {Matrix} g The G matrix to search, ptr array modified, then restored
  15. * @param {Number} top Start index in stack xi[top..n-1]
  16. * @param {Number} k The kth column in B
  17. * @param {Array} xi The nonzero pattern xi[top] .. xi[n - 1], an array of size = 2 * n
  18. * The first n entries is the nonzero pattern, the last n entries is the stack
  19. * @param {Array} pinv The inverse row permutation vector, must be null for L * x = b
  20. *
  21. * @return {Number} New value of top
  22. *
  23. * Reference: http://faculty.cse.tamu.edu/davis/publications.html
  24. */
  25. function csDfs(j, g, top, xi, pinv) {
  26. // g arrays
  27. var index = g._index;
  28. var ptr = g._ptr;
  29. var size = g._size;
  30. // columns
  31. var n = size[1];
  32. // vars
  33. var i, p, p2;
  34. // initialize head
  35. var head = 0;
  36. // initialize the recursion stack
  37. xi[0] = j;
  38. // loop
  39. while (head >= 0) {
  40. // get j from the top of the recursion stack
  41. j = xi[head];
  42. // apply permutation vector
  43. var jnew = pinv ? pinv[j] : j;
  44. // check node j is marked
  45. if (!(0, _csMarked.csMarked)(ptr, j)) {
  46. // mark node j as visited
  47. (0, _csMark.csMark)(ptr, j);
  48. // update stack (last n entries in xi)
  49. xi[n + head] = jnew < 0 ? 0 : (0, _csUnflip.csUnflip)(ptr[jnew]);
  50. }
  51. // node j done if no unvisited neighbors
  52. var done = 1;
  53. // examine all neighbors of j, stack (last n entries in xi)
  54. for (p = xi[n + head], p2 = jnew < 0 ? 0 : (0, _csUnflip.csUnflip)(ptr[jnew + 1]); p < p2; p++) {
  55. // consider neighbor node i
  56. i = index[p];
  57. // check we have visited node i, skip it
  58. if ((0, _csMarked.csMarked)(ptr, i)) {
  59. continue;
  60. }
  61. // pause depth-first search of node j, update stack (last n entries in xi)
  62. xi[n + head] = p;
  63. // start dfs at node i
  64. xi[++head] = i;
  65. // node j is not done
  66. done = 0;
  67. // break, to start dfs(i)
  68. break;
  69. }
  70. // check depth-first search at node j is done
  71. if (done) {
  72. // remove j from the recursion stack
  73. head--;
  74. // and place in the output stack
  75. xi[--top] = j;
  76. }
  77. }
  78. return top;
  79. }