csEtree.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.csEtree = csEtree;
  6. /**
  7. * Computes the elimination tree of Matrix A (using triu(A)) or the
  8. * elimination tree of A'A without forming A'A.
  9. *
  10. * @param {Matrix} a The A Matrix
  11. * @param {boolean} ata A value of true the function computes the etree of A'A
  12. *
  13. * Reference: http://faculty.cse.tamu.edu/davis/publications.html
  14. */
  15. function csEtree(a, ata) {
  16. // check inputs
  17. if (!a) {
  18. return null;
  19. }
  20. // a arrays
  21. var aindex = a._index;
  22. var aptr = a._ptr;
  23. var asize = a._size;
  24. // rows & columns
  25. var m = asize[0];
  26. var n = asize[1];
  27. // allocate result
  28. var parent = []; // (n)
  29. // allocate workspace
  30. var w = []; // (n + (ata ? m : 0))
  31. var ancestor = 0; // first n entries in w
  32. var prev = n; // last m entries (ata = true)
  33. var i, inext;
  34. // check we are calculating A'A
  35. if (ata) {
  36. // initialize workspace
  37. for (i = 0; i < m; i++) {
  38. w[prev + i] = -1;
  39. }
  40. }
  41. // loop columns
  42. for (var k = 0; k < n; k++) {
  43. // node k has no parent yet
  44. parent[k] = -1;
  45. // nor does k have an ancestor
  46. w[ancestor + k] = -1;
  47. // values in column k
  48. for (var p0 = aptr[k], p1 = aptr[k + 1], p = p0; p < p1; p++) {
  49. // row
  50. var r = aindex[p];
  51. // node
  52. i = ata ? w[prev + r] : r;
  53. // traverse from i to k
  54. for (; i !== -1 && i < k; i = inext) {
  55. // inext = ancestor of i
  56. inext = w[ancestor + i];
  57. // path compression
  58. w[ancestor + i] = k;
  59. // check no anc., parent is k
  60. if (inext === -1) {
  61. parent[i] = k;
  62. }
  63. }
  64. if (ata) {
  65. w[prev + r] = k;
  66. }
  67. }
  68. }
  69. return parent;
  70. }