graph.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * DevExtreme (viz/sankey/graph.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var WHITE = "white";
  11. var GRAY = "gray";
  12. var BLACK = "black";
  13. var routines = {
  14. maxOfArray: function(arr, callback) {
  15. var m = 0;
  16. var callback_function = function(v) {
  17. return v
  18. };
  19. if (callback) {
  20. callback_function = callback
  21. }
  22. for (var i = 0; i < arr.length; i++) {
  23. if (callback_function(arr[i]) > m) {
  24. m = callback_function(arr[i])
  25. }
  26. }
  27. return m
  28. }
  29. };
  30. var getVertices = function(links) {
  31. var vert = [];
  32. links.forEach(function(link) {
  33. if (vert.indexOf(link[0]) === -1) {
  34. vert.push(link[0])
  35. }
  36. if (vert.indexOf(link[1]) === -1) {
  37. vert.push(link[1])
  38. }
  39. });
  40. return vert
  41. };
  42. var getAdjacentVertices = function(links, vertex) {
  43. var avert = [];
  44. links.forEach(function(link) {
  45. if (link[0] === vertex && avert.indexOf(link[1]) === -1) {
  46. avert.push(link[1])
  47. }
  48. });
  49. return avert
  50. };
  51. var getReverseAdjacentVertices = function(links, vertex) {
  52. var avert = [];
  53. links.forEach(function(link) {
  54. if (link[1] === vertex && avert.indexOf(link[0]) === -1) {
  55. avert.push(link[0])
  56. }
  57. });
  58. return avert
  59. };
  60. var struct = {
  61. _hasCycle: false,
  62. _sortedList: [],
  63. hasCycle: function(links) {
  64. var _this = this;
  65. this._hasCycle = false;
  66. this._sortedList = [];
  67. var vertices = {};
  68. var allVertices = getVertices(links);
  69. allVertices.forEach(function(vertex) {
  70. vertices[vertex] = {
  71. color: WHITE
  72. }
  73. });
  74. allVertices.forEach(function(vertex) {
  75. if (vertices[vertex].color === WHITE) {
  76. _this._depthFirstSearch(links, vertices, vertex)
  77. }
  78. });
  79. this._sortedList.reverse();
  80. return this._hasCycle
  81. },
  82. _depthFirstSearch: function(links, vertices, vertex) {
  83. vertices[vertex].color = GRAY;
  84. var averts = getAdjacentVertices(links, vertex);
  85. for (var a = 0; a < averts.length; a++) {
  86. if (vertices[averts[a]].color === WHITE) {
  87. this._depthFirstSearch(links, vertices, averts[a])
  88. } else {
  89. if (vertices[averts[a]].color === GRAY) {
  90. this._hasCycle = true
  91. }
  92. }
  93. }
  94. this._sortedList.push({
  95. name: vertex,
  96. lp: null,
  97. incoming: getReverseAdjacentVertices(links, vertex),
  98. outgoing: getAdjacentVertices(links, vertex)
  99. });
  100. vertices[vertex].color = BLACK
  101. },
  102. computeLongestPaths: function(links) {
  103. var sortedVertices = this._sortedList;
  104. sortedVertices.forEach(function(vertex) {
  105. var averts = getReverseAdjacentVertices(links, vertex.name);
  106. if (0 === averts.length) {
  107. vertex.lp = 0
  108. } else {
  109. var maxLP = [];
  110. averts.forEach(function(adjacentVertex) {
  111. maxLP.push(sortedVertices.filter(function(sv) {
  112. return sv.name === adjacentVertex
  113. })[0].lp)
  114. });
  115. vertex.lp = routines.maxOfArray(maxLP) + 1
  116. }
  117. });
  118. return this._sortedList
  119. }
  120. };
  121. module.exports = {
  122. struct: struct,
  123. routines: routines,
  124. getVertices: getVertices,
  125. getAdjacentVertices: getAdjacentVertices,
  126. getReverseAdjacentVertices: getReverseAdjacentVertices
  127. };