graphwise 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +72 -1
  2. package/dist/__test__/fixtures/graphs/city-suburban-village.d.ts +3 -0
  3. package/dist/__test__/fixtures/graphs/city-suburban-village.d.ts.map +1 -0
  4. package/dist/__test__/fixtures/graphs/city-village.d.ts +3 -0
  5. package/dist/__test__/fixtures/graphs/city-village.d.ts.map +1 -0
  6. package/dist/__test__/fixtures/graphs/index.d.ts +13 -0
  7. package/dist/__test__/fixtures/graphs/index.d.ts.map +1 -0
  8. package/dist/__test__/fixtures/graphs/quality-vs-popularity.d.ts +3 -0
  9. package/dist/__test__/fixtures/graphs/quality-vs-popularity.d.ts.map +1 -0
  10. package/dist/__test__/fixtures/graphs/social-hub.d.ts +3 -0
  11. package/dist/__test__/fixtures/graphs/social-hub.d.ts.map +1 -0
  12. package/dist/__test__/fixtures/graphs/three-community.d.ts +3 -0
  13. package/dist/__test__/fixtures/graphs/three-community.d.ts.map +1 -0
  14. package/dist/__test__/fixtures/graphs/two-department.d.ts +3 -0
  15. package/dist/__test__/fixtures/graphs/two-department.d.ts.map +1 -0
  16. package/dist/__test__/fixtures/graphs/typed-entity.d.ts +3 -0
  17. package/dist/__test__/fixtures/graphs/typed-entity.d.ts.map +1 -0
  18. package/dist/__test__/fixtures/helpers.d.ts +66 -0
  19. package/dist/__test__/fixtures/helpers.d.ts.map +1 -0
  20. package/dist/__test__/fixtures/helpers.unit.test.d.ts +7 -0
  21. package/dist/__test__/fixtures/helpers.unit.test.d.ts.map +1 -0
  22. package/dist/__test__/fixtures/index.d.ts +10 -0
  23. package/dist/__test__/fixtures/index.d.ts.map +1 -0
  24. package/dist/__test__/fixtures/types.d.ts +35 -0
  25. package/dist/__test__/fixtures/types.d.ts.map +1 -0
  26. package/dist/expansion/dome.d.ts.map +1 -1
  27. package/dist/expansion/dome.integration.test.d.ts +18 -0
  28. package/dist/expansion/dome.integration.test.d.ts.map +1 -0
  29. package/dist/expansion/edge.d.ts +3 -3
  30. package/dist/expansion/edge.d.ts.map +1 -1
  31. package/dist/expansion/edge.integration.test.d.ts +11 -0
  32. package/dist/expansion/edge.integration.test.d.ts.map +1 -0
  33. package/dist/expansion/flux.d.ts +25 -0
  34. package/dist/expansion/flux.d.ts.map +1 -0
  35. package/dist/expansion/flux.integration.test.d.ts +14 -0
  36. package/dist/expansion/flux.integration.test.d.ts.map +1 -0
  37. package/dist/expansion/flux.unit.test.d.ts +2 -0
  38. package/dist/expansion/flux.unit.test.d.ts.map +1 -0
  39. package/dist/expansion/fuse.d.ts +28 -0
  40. package/dist/expansion/fuse.d.ts.map +1 -0
  41. package/dist/expansion/fuse.integration.test.d.ts +15 -0
  42. package/dist/expansion/fuse.integration.test.d.ts.map +1 -0
  43. package/dist/expansion/fuse.unit.test.d.ts +2 -0
  44. package/dist/expansion/fuse.unit.test.d.ts.map +1 -0
  45. package/dist/expansion/hae.d.ts +7 -7
  46. package/dist/expansion/hae.d.ts.map +1 -1
  47. package/dist/expansion/hae.integration.test.d.ts +11 -0
  48. package/dist/expansion/hae.integration.test.d.ts.map +1 -0
  49. package/dist/expansion/index.d.ts +6 -0
  50. package/dist/expansion/index.d.ts.map +1 -1
  51. package/dist/expansion/lace.d.ts +22 -0
  52. package/dist/expansion/lace.d.ts.map +1 -0
  53. package/dist/expansion/lace.integration.test.d.ts +14 -0
  54. package/dist/expansion/lace.integration.test.d.ts.map +1 -0
  55. package/dist/expansion/lace.unit.test.d.ts +2 -0
  56. package/dist/expansion/lace.unit.test.d.ts.map +1 -0
  57. package/dist/expansion/maze.d.ts +3 -13
  58. package/dist/expansion/maze.d.ts.map +1 -1
  59. package/dist/expansion/maze.integration.test.d.ts +11 -0
  60. package/dist/expansion/maze.integration.test.d.ts.map +1 -0
  61. package/dist/expansion/pipe.d.ts +3 -3
  62. package/dist/expansion/pipe.d.ts.map +1 -1
  63. package/dist/expansion/pipe.integration.test.d.ts +12 -0
  64. package/dist/expansion/pipe.integration.test.d.ts.map +1 -0
  65. package/dist/expansion/reach.d.ts +4 -14
  66. package/dist/expansion/reach.d.ts.map +1 -1
  67. package/dist/expansion/reach.integration.test.d.ts +9 -0
  68. package/dist/expansion/reach.integration.test.d.ts.map +1 -0
  69. package/dist/expansion/sage.d.ts +5 -13
  70. package/dist/expansion/sage.d.ts.map +1 -1
  71. package/dist/expansion/sage.integration.test.d.ts +9 -0
  72. package/dist/expansion/sage.integration.test.d.ts.map +1 -0
  73. package/dist/expansion/sift.d.ts +26 -0
  74. package/dist/expansion/sift.d.ts.map +1 -0
  75. package/dist/expansion/sift.integration.test.d.ts +13 -0
  76. package/dist/expansion/sift.integration.test.d.ts.map +1 -0
  77. package/dist/expansion/sift.unit.test.d.ts +2 -0
  78. package/dist/expansion/sift.unit.test.d.ts.map +1 -0
  79. package/dist/expansion/tide.d.ts +15 -0
  80. package/dist/expansion/tide.d.ts.map +1 -0
  81. package/dist/expansion/tide.integration.test.d.ts +14 -0
  82. package/dist/expansion/tide.integration.test.d.ts.map +1 -0
  83. package/dist/expansion/tide.unit.test.d.ts +2 -0
  84. package/dist/expansion/tide.unit.test.d.ts.map +1 -0
  85. package/dist/expansion/warp.d.ts +15 -0
  86. package/dist/expansion/warp.d.ts.map +1 -0
  87. package/dist/expansion/warp.integration.test.d.ts +13 -0
  88. package/dist/expansion/warp.integration.test.d.ts.map +1 -0
  89. package/dist/expansion/warp.unit.test.d.ts +2 -0
  90. package/dist/expansion/warp.unit.test.d.ts.map +1 -0
  91. package/dist/graph/adjacency-map.d.ts.map +1 -1
  92. package/dist/graph/index.cjs +7 -0
  93. package/dist/graph/index.cjs.map +1 -1
  94. package/dist/graph/index.js +7 -0
  95. package/dist/graph/index.js.map +1 -1
  96. package/dist/index/index.cjs +287 -42
  97. package/dist/index/index.cjs.map +1 -1
  98. package/dist/index/index.js +282 -43
  99. package/dist/index/index.js.map +1 -1
  100. package/dist/ranking/mi/etch.integration.test.d.ts +2 -0
  101. package/dist/ranking/mi/etch.integration.test.d.ts.map +1 -0
  102. package/dist/ranking/mi/notch.integration.test.d.ts +2 -0
  103. package/dist/ranking/mi/notch.integration.test.d.ts.map +1 -0
  104. package/dist/ranking/mi/scale.d.ts.map +1 -1
  105. package/dist/ranking/mi/scale.integration.test.d.ts +2 -0
  106. package/dist/ranking/mi/scale.integration.test.d.ts.map +1 -0
  107. package/dist/ranking/mi/skew.integration.test.d.ts +2 -0
  108. package/dist/ranking/mi/skew.integration.test.d.ts.map +1 -0
  109. package/dist/ranking/mi/span.integration.test.d.ts +2 -0
  110. package/dist/ranking/mi/span.integration.test.d.ts.map +1 -0
  111. package/dist/ranking/parse.integration.test.d.ts +10 -0
  112. package/dist/ranking/parse.integration.test.d.ts.map +1 -0
  113. package/dist/structures/index.cjs +12 -2
  114. package/dist/structures/index.cjs.map +1 -1
  115. package/dist/structures/index.js +12 -2
  116. package/dist/structures/index.js.map +1 -1
  117. package/dist/structures/priority-queue.d.ts +4 -2
  118. package/dist/structures/priority-queue.d.ts.map +1 -1
  119. package/dist/utils/index.cjs +76 -77
  120. package/dist/utils/index.cjs.map +1 -1
  121. package/dist/utils/index.js +76 -77
  122. package/dist/utils/index.js.map +1 -1
  123. package/package.json +1 -1
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=etch.integration.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"etch.integration.test.d.ts","sourceRoot":"","sources":["../../../src/ranking/mi/etch.integration.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=notch.integration.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notch.integration.test.d.ts","sourceRoot":"","sources":["../../../src/ranking/mi/notch.integration.test.ts"],"names":[],"mappings":""}
@@ -1 +1 @@
1
- {"version":3,"file":"scale.d.ts","sourceRoot":"","sources":["../../../src/ranking/mi/scale.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE7E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC;;GAEG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,QAAQ,EAAE,CAAC,SAAS,QAAQ,EAC3D,KAAK,EAAE,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,EAC1B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,QAAQ,GACf,MAAM,CA+BR"}
1
+ {"version":3,"file":"scale.d.ts","sourceRoot":"","sources":["../../../src/ranking/mi/scale.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE7E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC;;GAEG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,QAAQ,EAAE,CAAC,SAAS,QAAQ,EAC3D,KAAK,EAAE,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,EAC1B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,QAAQ,GACf,MAAM,CAgCR"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=scale.integration.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scale.integration.test.d.ts","sourceRoot":"","sources":["../../../src/ranking/mi/scale.integration.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=skew.integration.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skew.integration.test.d.ts","sourceRoot":"","sources":["../../../src/ranking/mi/skew.integration.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=span.integration.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"span.integration.test.d.ts","sourceRoot":"","sources":["../../../src/ranking/mi/span.integration.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * PARSE ranking integration tests.
3
+ *
4
+ * Demonstrates three structural advantages of PARSE's geometric mean approach
5
+ * over arithmetic-mean and degree-sum baselines through realistic graph scenarios.
6
+ *
7
+ * @module ranking/parse.integration.test
8
+ */
9
+ export {};
10
+ //# sourceMappingURL=parse.integration.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.integration.test.d.ts","sourceRoot":"","sources":["../../src/ranking/parse.integration.test.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG"}
@@ -6,6 +6,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
6
  */
7
7
  var PriorityQueue = class {
8
8
  heap = [];
9
+ indexMap = /* @__PURE__ */ new Map();
9
10
  /**
10
11
  * Returns the number of items in the queue.
11
12
  */
@@ -27,6 +28,7 @@ var PriorityQueue = class {
27
28
  priority
28
29
  };
29
30
  this.heap.push(entry);
31
+ this.indexMap.set(item, this.heap.length - 1);
30
32
  this.heapifyUp(this.heap.length - 1);
31
33
  }
32
34
  /**
@@ -37,10 +39,12 @@ var PriorityQueue = class {
37
39
  if (this.heap.length === 0) return;
38
40
  const root = this.heap[0];
39
41
  if (root === void 0) return;
42
+ this.indexMap.delete(root.item);
40
43
  const last = this.heap.pop();
41
44
  if (last === void 0) return root;
42
45
  if (this.heap.length > 0) {
43
46
  this.heap[0] = last;
47
+ this.indexMap.set(last.item, 0);
44
48
  this.heapifyDown(0);
45
49
  }
46
50
  return root;
@@ -66,17 +70,19 @@ var PriorityQueue = class {
66
70
  *
67
71
  * @param item - The item to find
68
72
  * @param newPriority - The new (lower) priority value
69
- * @param equals - Function to compare items for equality
73
+ * @param equals - Function to compare items for equality. If provided, uses linear search;
74
+ * otherwise uses O(1) reference-based lookup.
70
75
  */
71
76
  decreaseKey(item, newPriority, equals) {
72
77
  let foundIndex = -1;
73
- for (let i = 0; i < this.heap.length; i++) {
78
+ if (equals !== void 0) for (let i = 0; i < this.heap.length; i++) {
74
79
  const entry = this.heap[i];
75
80
  if (entry !== void 0 && equals(entry.item, item)) {
76
81
  foundIndex = i;
77
82
  break;
78
83
  }
79
84
  }
85
+ else foundIndex = this.indexMap.get(item) ?? -1;
80
86
  if (foundIndex === -1) return false;
81
87
  const entry = this.heap[foundIndex];
82
88
  if (entry === void 0) return false;
@@ -98,6 +104,8 @@ var PriorityQueue = class {
98
104
  if (currentEntry.priority >= parentEntry.priority) return;
99
105
  this.heap[current] = parentEntry;
100
106
  this.heap[parent] = currentEntry;
107
+ this.indexMap.set(parentEntry.item, current);
108
+ this.indexMap.set(currentEntry.item, parent);
101
109
  current = parent;
102
110
  }
103
111
  }
@@ -123,6 +131,8 @@ var PriorityQueue = class {
123
131
  if (finalSmallestEntry !== void 0) {
124
132
  this.heap[current] = finalSmallestEntry;
125
133
  this.heap[smallest] = currentEntry;
134
+ this.indexMap.set(finalSmallestEntry.item, current);
135
+ this.indexMap.set(currentEntry.item, smallest);
126
136
  current = smallest;
127
137
  } else return;
128
138
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":[],"sources":["../../src/structures/priority-queue.ts"],"sourcesContent":["/**\n * Priority queue entry containing an item and its priority.\n */\nexport interface PriorityEntry<T> {\n\titem: T;\n\tpriority: number;\n}\n\n/**\n * Min-heap priority queue implementation using an array-based binary heap.\n * Lower priority values have higher precedence (extracted first).\n */\nexport class PriorityQueue<T> {\n\tprivate heap: PriorityEntry<T>[] = [];\n\n\t/**\n\t * Returns the number of items in the queue.\n\t */\n\tpublic size(): number {\n\t\treturn this.heap.length;\n\t}\n\n\t/**\n\t * Returns true if the queue is empty.\n\t */\n\tpublic isEmpty(): boolean {\n\t\treturn this.heap.length === 0;\n\t}\n\n\t/**\n\t * Adds an item with the given priority to the queue.\n\t */\n\tpublic push(item: T, priority: number): void {\n\t\tconst entry: PriorityEntry<T> = { item, priority };\n\t\tthis.heap.push(entry);\n\t\tthis.heapifyUp(this.heap.length - 1);\n\t}\n\n\t/**\n\t * Removes and returns the highest precedence item (lowest priority value).\n\t * Returns undefined if the queue is empty.\n\t */\n\tpublic pop(): PriorityEntry<T> | undefined {\n\t\tif (this.heap.length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst root = this.heap[0];\n\t\tif (root === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst last = this.heap.pop();\n\t\tif (last === undefined) {\n\t\t\treturn root;\n\t\t}\n\n\t\tif (this.heap.length > 0) {\n\t\t\tthis.heap[0] = last;\n\t\t\tthis.heapifyDown(0);\n\t\t}\n\n\t\treturn root;\n\t}\n\n\t/**\n\t * Returns the highest precedence item without removing it.\n\t * Returns undefined if the queue is empty.\n\t */\n\tpublic peek(): PriorityEntry<T> | undefined {\n\t\treturn this.heap[0];\n\t}\n\n\t/**\n\t * Rebuilds the heap from the current array state.\n\t * Useful when priorities have been modified externally (e.g., phase transitions).\n\t */\n\tpublic rebuild(): void {\n\t\t// Floyd's algorithm: heapify down from last non-leaf to root\n\t\tconst start = Math.floor(this.heap.length / 2) - 1;\n\t\tfor (let i = start; i >= 0; i--) {\n\t\t\tthis.heapifyDown(i);\n\t\t}\n\t}\n\n\t/**\n\t * Decreases the priority of an existing item in the queue.\n\t * Returns true if the item was found and updated, false otherwise.\n\t *\n\t * @param item - The item to find\n\t * @param newPriority - The new (lower) priority value\n\t * @param equals - Function to compare items for equality\n\t */\n\tpublic decreaseKey(\n\t\titem: T,\n\t\tnewPriority: number,\n\t\tequals: (a: T, b: T) => boolean,\n\t): boolean {\n\t\tlet foundIndex = -1;\n\n\t\tfor (let i = 0; i < this.heap.length; i++) {\n\t\t\tconst entry = this.heap[i];\n\t\t\tif (entry !== undefined && equals(entry.item, item)) {\n\t\t\t\tfoundIndex = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (foundIndex === -1) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst entry = this.heap[foundIndex];\n\t\tif (entry === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Only update if new priority is lower (higher precedence)\n\t\tif (newPriority >= entry.priority) {\n\t\t\treturn false;\n\t\t}\n\n\t\tentry.priority = newPriority;\n\t\tthis.heapifyUp(foundIndex);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Restores heap property by moving element up from given index.\n\t */\n\tprivate heapifyUp(index: number): void {\n\t\tlet current = index;\n\n\t\twhile (current > 0) {\n\t\t\tconst parent = Math.floor((current - 1) / 2);\n\t\t\tconst currentEntry = this.heap[current];\n\t\t\tconst parentEntry = this.heap[parent];\n\n\t\t\tif (currentEntry === undefined || parentEntry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (currentEntry.priority >= parentEntry.priority) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Swap current and parent\n\t\t\tthis.heap[current] = parentEntry;\n\t\t\tthis.heap[parent] = currentEntry;\n\t\t\tcurrent = parent;\n\t\t}\n\t}\n\n\t/**\n\t * Restores heap property by moving element down from given index.\n\t */\n\tprivate heapifyDown(index: number): void {\n\t\tlet current = index;\n\t\tconst length = this.heap.length;\n\n\t\twhile (current < length) {\n\t\t\tconst left = 2 * current + 1;\n\t\t\tconst right = 2 * current + 2;\n\t\t\tlet smallest = current;\n\n\t\t\tconst currentEntry = this.heap[current];\n\t\t\tif (currentEntry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Compare with left child\n\t\t\tconst leftEntry = this.heap[left];\n\t\t\tif (\n\t\t\t\tleft < length &&\n\t\t\t\tleftEntry !== undefined &&\n\t\t\t\tleftEntry.priority < currentEntry.priority\n\t\t\t) {\n\t\t\t\tsmallest = left;\n\t\t\t}\n\n\t\t\t// Compare with right child (need to re-fetch smallest after potential update)\n\t\t\tconst rightEntry = this.heap[right];\n\t\t\tconst currentSmallestEntry = this.heap[smallest];\n\t\t\tif (\n\t\t\t\tright < length &&\n\t\t\t\trightEntry !== undefined &&\n\t\t\t\tcurrentSmallestEntry !== undefined &&\n\t\t\t\trightEntry.priority < currentSmallestEntry.priority\n\t\t\t) {\n\t\t\t\tsmallest = right;\n\t\t\t}\n\n\t\t\tif (smallest === current) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Get the final smallest entry (after determining final smallest index)\n\t\t\tconst finalSmallestEntry = this.heap[smallest];\n\t\t\tif (finalSmallestEntry !== undefined) {\n\t\t\t\tthis.heap[current] = finalSmallestEntry;\n\t\t\t\tthis.heap[smallest] = currentEntry;\n\t\t\t\tcurrent = smallest;\n\t\t\t} else {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;AAYA,IAAa,gBAAb,MAA8B;CAC7B,OAAmC,EAAE;;;;CAKrC,OAAsB;AACrB,SAAO,KAAK,KAAK;;;;;CAMlB,UAA0B;AACzB,SAAO,KAAK,KAAK,WAAW;;;;;CAM7B,KAAY,MAAS,UAAwB;EAC5C,MAAM,QAA0B;GAAE;GAAM;GAAU;AAClD,OAAK,KAAK,KAAK,MAAM;AACrB,OAAK,UAAU,KAAK,KAAK,SAAS,EAAE;;;;;;CAOrC,MAA2C;AAC1C,MAAI,KAAK,KAAK,WAAW,EACxB;EAGD,MAAM,OAAO,KAAK,KAAK;AACvB,MAAI,SAAS,KAAA,EACZ;EAGD,MAAM,OAAO,KAAK,KAAK,KAAK;AAC5B,MAAI,SAAS,KAAA,EACZ,QAAO;AAGR,MAAI,KAAK,KAAK,SAAS,GAAG;AACzB,QAAK,KAAK,KAAK;AACf,QAAK,YAAY,EAAE;;AAGpB,SAAO;;;;;;CAOR,OAA4C;AAC3C,SAAO,KAAK,KAAK;;;;;;CAOlB,UAAuB;EAEtB,MAAM,Q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AAK;GAC7B,MAAM,uBAAuB,KAAK,KAAK;AACvC,OACC,QAAQ,UACR,eAAe,KAAA,KACf,yBAAyB,KAAA,KACzB,WAAW,WAAW,qBAAqB,SAE3C,YAAW;AAGZ,OAAI,aAAa,QAChB;GAID,MAAM,qBAAqB,KAAK,KAAK;AACrC,OAAI,uBAAuB,KAAA,GAAW;AACrC,SAAK,KAAK,WAAW;AACrB,SAAK,KAAK,YAAY;AACtB,cAAU;SAEV"}
1
+ {"version":3,"file":"index.cjs","names":[],"sources":["../../src/structures/priority-queue.ts"],"sourcesContent":["/**\n * Priority queue entry containing an item and its priority.\n */\nexport interface PriorityEntry<T> {\n\titem: T;\n\tpriority: number;\n}\n\n/**\n * Min-heap priority queue implementation using an array-based binary heap.\n * Lower priority values have higher precedence (extracted first).\n */\nexport class PriorityQueue<T> {\n\tprivate heap: PriorityEntry<T>[] = [];\n\tprivate indexMap = new Map<T, number>();\n\n\t/**\n\t * Returns the number of items in the queue.\n\t */\n\tpublic size(): number {\n\t\treturn this.heap.length;\n\t}\n\n\t/**\n\t * Returns true if the queue is empty.\n\t */\n\tpublic isEmpty(): boolean {\n\t\treturn this.heap.length === 0;\n\t}\n\n\t/**\n\t * Adds an item with the given priority to the queue.\n\t */\n\tpublic push(item: T, priority: number): void {\n\t\tconst entry: PriorityEntry<T> = { item, priority };\n\t\tthis.heap.push(entry);\n\t\tthis.indexMap.set(item, this.heap.length - 1);\n\t\tthis.heapifyUp(this.heap.length - 1);\n\t}\n\n\t/**\n\t * Removes and returns the highest precedence item (lowest priority value).\n\t * Returns undefined if the queue is empty.\n\t */\n\tpublic pop(): PriorityEntry<T> | undefined {\n\t\tif (this.heap.length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst root = this.heap[0];\n\t\tif (root === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.indexMap.delete(root.item);\n\n\t\tconst last = this.heap.pop();\n\t\tif (last === undefined) {\n\t\t\treturn root;\n\t\t}\n\n\t\tif (this.heap.length > 0) {\n\t\t\tthis.heap[0] = last;\n\t\t\tthis.indexMap.set(last.item, 0);\n\t\t\tthis.heapifyDown(0);\n\t\t}\n\n\t\treturn root;\n\t}\n\n\t/**\n\t * Returns the highest precedence item without removing it.\n\t * Returns undefined if the queue is empty.\n\t */\n\tpublic peek(): PriorityEntry<T> | undefined {\n\t\treturn this.heap[0];\n\t}\n\n\t/**\n\t * Rebuilds the heap from the current array state.\n\t * Useful when priorities have been modified externally (e.g., phase transitions).\n\t */\n\tpublic rebuild(): void {\n\t\t// Floyd's algorithm: heapify down from last non-leaf to root\n\t\tconst start = Math.floor(this.heap.length / 2) - 1;\n\t\tfor (let i = start; i >= 0; i--) {\n\t\t\tthis.heapifyDown(i);\n\t\t}\n\t}\n\n\t/**\n\t * Decreases the priority of an existing item in the queue.\n\t * Returns true if the item was found and updated, false otherwise.\n\t *\n\t * @param item - The item to find\n\t * @param newPriority - The new (lower) priority value\n\t * @param equals - Function to compare items for equality. If provided, uses linear search;\n\t * otherwise uses O(1) reference-based lookup.\n\t */\n\tpublic decreaseKey(\n\t\titem: T,\n\t\tnewPriority: number,\n\t\tequals?: (a: T, b: T) => boolean,\n\t): boolean {\n\t\tlet foundIndex = -1;\n\n\t\t// If equals function provided, use linear search for custom equality\n\t\tif (equals !== undefined) {\n\t\t\tfor (let i = 0; i < this.heap.length; i++) {\n\t\t\t\tconst entry = this.heap[i];\n\t\t\t\tif (entry !== undefined && equals(entry.item, item)) {\n\t\t\t\t\tfoundIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\t// Use O(1) reference-based lookup via indexMap\n\t\t\tfoundIndex = this.indexMap.get(item) ?? -1;\n\t\t}\n\n\t\tif (foundIndex === -1) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst entry = this.heap[foundIndex];\n\t\tif (entry === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Only update if new priority is lower (higher precedence)\n\t\tif (newPriority >= entry.priority) {\n\t\t\treturn false;\n\t\t}\n\n\t\tentry.priority = newPriority;\n\t\tthis.heapifyUp(foundIndex);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Restores heap property by moving element up from given index.\n\t */\n\tprivate heapifyUp(index: number): void {\n\t\tlet current = index;\n\n\t\twhile (current > 0) {\n\t\t\tconst parent = Math.floor((current - 1) / 2);\n\t\t\tconst currentEntry = this.heap[current];\n\t\t\tconst parentEntry = this.heap[parent];\n\n\t\t\tif (currentEntry === undefined || parentEntry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (currentEntry.priority >= parentEntry.priority) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Swap current and parent\n\t\t\tthis.heap[current] = parentEntry;\n\t\t\tthis.heap[parent] = currentEntry;\n\t\t\t// Update indexMap after swap\n\t\t\tthis.indexMap.set(parentEntry.item, current);\n\t\t\tthis.indexMap.set(currentEntry.item, parent);\n\t\t\tcurrent = parent;\n\t\t}\n\t}\n\n\t/**\n\t * Restores heap property by moving element down from given index.\n\t */\n\tprivate heapifyDown(index: number): void {\n\t\tlet current = index;\n\t\tconst length = this.heap.length;\n\n\t\twhile (current < length) {\n\t\t\tconst left = 2 * current + 1;\n\t\t\tconst right = 2 * current + 2;\n\t\t\tlet smallest = current;\n\n\t\t\tconst currentEntry = this.heap[current];\n\t\t\tif (currentEntry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Compare with left child\n\t\t\tconst leftEntry = this.heap[left];\n\t\t\tif (\n\t\t\t\tleft < length &&\n\t\t\t\tleftEntry !== undefined &&\n\t\t\t\tleftEntry.priority < currentEntry.priority\n\t\t\t) {\n\t\t\t\tsmallest = left;\n\t\t\t}\n\n\t\t\t// Compare with right child (need to re-fetch smallest after potential update)\n\t\t\tconst rightEntry = this.heap[right];\n\t\t\tconst currentSmallestEntry = this.heap[smallest];\n\t\t\tif (\n\t\t\t\tright < length &&\n\t\t\t\trightEntry !== undefined &&\n\t\t\t\tcurrentSmallestEntry !== undefined &&\n\t\t\t\trightEntry.priority < currentSmallestEntry.priority\n\t\t\t) {\n\t\t\t\tsmallest = right;\n\t\t\t}\n\n\t\t\tif (smallest === current) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Get the final smallest entry (after determining final smallest index)\n\t\t\tconst finalSmallestEntry = this.heap[smallest];\n\t\t\tif (finalSmallestEntry !== undefined) {\n\t\t\t\tthis.heap[current] = finalSmallestEntry;\n\t\t\t\tthis.heap[smallest] = currentEntry;\n\t\t\t\t// Update indexMap after swap\n\t\t\t\tthis.indexMap.set(finalSmallestEntry.item, current);\n\t\t\t\tthis.indexMap.set(currentEntry.item, smallest);\n\t\t\t\tcurrent = smallest;\n\t\t\t} else {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;AAYA,IAAa,gBAAb,MAA8B;CAC7B,OAAmC,EAAE;CACrC,2BAAmB,IAAI,KAAgB;;;;CAKvC,OAAsB;AACrB,SAAO,KAAK,KAAK;;;;;CAMlB,UAA0B;AACzB,SAAO,KAAK,KAAK,WAAW;;;;;CAM7B,KAAY,MAAS,UAAwB;EAC5C,MAAM,QAA0B;GAAE;GAAM;GAAU;AAClD,OAAK,KAAK,KAAK,MAAM;AACrB,OAAK,SAAS,IAAI,MAAM,KAAK,KAAK,SAAS,EAAE;AAC7C,OAAK,UAAU,KAAK,KAAK,SAAS,EAAE;;;;;;CAOrC,MAA2C;AAC1C,MAAI,KAAK,KAAK,WAAW,EACxB;EAGD,MAAM,OAAO,KAAK,KAAK;AACvB,MAAI,SAAS,KAAA,EACZ;AAGD,OAAK,SAAS,OAAO,KAAK,KAAK;EAE/B,MAAM,OAAO,KAAK,KAAK,KAAK;AAC5B,MAAI,SAAS,KAAA,EACZ,QAAO;AAGR,MAAI,KAAK,KAAK,SAAS,GAAG;AACzB,QAAK,KAAK,KAAK;AACf,QAAK,SAAS,IAAI,KAAK,MAAM,EAA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AAQ;GACxB,MAAM,OAAO,IAAI,UAAU;GAC3B,MAAM,QAAQ,IAAI,UAAU;GAC5B,IAAI,WAAW;GAEf,MAAM,eAAe,KAAK,KAAK;AAC/B,OAAI,iBAAiB,KAAA,EACpB;GAID,MAAM,YAAY,KAAK,KAAK;AAC5B,OACC,OAAO,UACP,cAAc,KAAA,KACd,UAAU,WAAW,aAAa,SAElC,YAAW;GAIZ,MAAM,aAAa,KAAK,KAAK;GAC7B,MAAM,uBAAuB,KAAK,KAAK;AACvC,OACC,QAAQ,UACR,eAAe,KAAA,KACf,yBAAyB,KAAA,KACzB,WAAW,WAAW,qBAAqB,SAE3C,YAAW;AAGZ,OAAI,aAAa,QAChB;GAID,MAAM,qBAAqB,KAAK,KAAK;AACrC,OAAI,uBAAuB,KAAA,GAAW;AACrC,SAAK,KAAK,WAAW;AACrB,SAAK,KAAK,YAAY;AAEtB,SAAK,SAAS,IAAI,mBAAmB,MAAM,QAAQ;AACnD,SAAK,SAAS,IAAI,aAAa,MAAM,SAAS;AAC9C,cAAU;SAEV"}
@@ -5,6 +5,7 @@
5
5
  */
6
6
  var PriorityQueue = class {
7
7
  heap = [];
8
+ indexMap = /* @__PURE__ */ new Map();
8
9
  /**
9
10
  * Returns the number of items in the queue.
10
11
  */
@@ -26,6 +27,7 @@ var PriorityQueue = class {
26
27
  priority
27
28
  };
28
29
  this.heap.push(entry);
30
+ this.indexMap.set(item, this.heap.length - 1);
29
31
  this.heapifyUp(this.heap.length - 1);
30
32
  }
31
33
  /**
@@ -36,10 +38,12 @@ var PriorityQueue = class {
36
38
  if (this.heap.length === 0) return;
37
39
  const root = this.heap[0];
38
40
  if (root === void 0) return;
41
+ this.indexMap.delete(root.item);
39
42
  const last = this.heap.pop();
40
43
  if (last === void 0) return root;
41
44
  if (this.heap.length > 0) {
42
45
  this.heap[0] = last;
46
+ this.indexMap.set(last.item, 0);
43
47
  this.heapifyDown(0);
44
48
  }
45
49
  return root;
@@ -65,17 +69,19 @@ var PriorityQueue = class {
65
69
  *
66
70
  * @param item - The item to find
67
71
  * @param newPriority - The new (lower) priority value
68
- * @param equals - Function to compare items for equality
72
+ * @param equals - Function to compare items for equality. If provided, uses linear search;
73
+ * otherwise uses O(1) reference-based lookup.
69
74
  */
70
75
  decreaseKey(item, newPriority, equals) {
71
76
  let foundIndex = -1;
72
- for (let i = 0; i < this.heap.length; i++) {
77
+ if (equals !== void 0) for (let i = 0; i < this.heap.length; i++) {
73
78
  const entry = this.heap[i];
74
79
  if (entry !== void 0 && equals(entry.item, item)) {
75
80
  foundIndex = i;
76
81
  break;
77
82
  }
78
83
  }
84
+ else foundIndex = this.indexMap.get(item) ?? -1;
79
85
  if (foundIndex === -1) return false;
80
86
  const entry = this.heap[foundIndex];
81
87
  if (entry === void 0) return false;
@@ -97,6 +103,8 @@ var PriorityQueue = class {
97
103
  if (currentEntry.priority >= parentEntry.priority) return;
98
104
  this.heap[current] = parentEntry;
99
105
  this.heap[parent] = currentEntry;
106
+ this.indexMap.set(parentEntry.item, current);
107
+ this.indexMap.set(currentEntry.item, parent);
100
108
  current = parent;
101
109
  }
102
110
  }
@@ -122,6 +130,8 @@ var PriorityQueue = class {
122
130
  if (finalSmallestEntry !== void 0) {
123
131
  this.heap[current] = finalSmallestEntry;
124
132
  this.heap[smallest] = currentEntry;
133
+ this.indexMap.set(finalSmallestEntry.item, current);
134
+ this.indexMap.set(currentEntry.item, smallest);
125
135
  current = smallest;
126
136
  } else return;
127
137
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/structures/priority-queue.ts"],"sourcesContent":["/**\n * Priority queue entry containing an item and its priority.\n */\nexport interface PriorityEntry<T> {\n\titem: T;\n\tpriority: number;\n}\n\n/**\n * Min-heap priority queue implementation using an array-based binary heap.\n * Lower priority values have higher precedence (extracted first).\n */\nexport class PriorityQueue<T> {\n\tprivate heap: PriorityEntry<T>[] = [];\n\n\t/**\n\t * Returns the number of items in the queue.\n\t */\n\tpublic size(): number {\n\t\treturn this.heap.length;\n\t}\n\n\t/**\n\t * Returns true if the queue is empty.\n\t */\n\tpublic isEmpty(): boolean {\n\t\treturn this.heap.length === 0;\n\t}\n\n\t/**\n\t * Adds an item with the given priority to the queue.\n\t */\n\tpublic push(item: T, priority: number): void {\n\t\tconst entry: PriorityEntry<T> = { item, priority };\n\t\tthis.heap.push(entry);\n\t\tthis.heapifyUp(this.heap.length - 1);\n\t}\n\n\t/**\n\t * Removes and returns the highest precedence item (lowest priority value).\n\t * Returns undefined if the queue is empty.\n\t */\n\tpublic pop(): PriorityEntry<T> | undefined {\n\t\tif (this.heap.length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst root = this.heap[0];\n\t\tif (root === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst last = this.heap.pop();\n\t\tif (last === undefined) {\n\t\t\treturn root;\n\t\t}\n\n\t\tif (this.heap.length > 0) {\n\t\t\tthis.heap[0] = last;\n\t\t\tthis.heapifyDown(0);\n\t\t}\n\n\t\treturn root;\n\t}\n\n\t/**\n\t * Returns the highest precedence item without removing it.\n\t * Returns undefined if the queue is empty.\n\t */\n\tpublic peek(): PriorityEntry<T> | undefined {\n\t\treturn this.heap[0];\n\t}\n\n\t/**\n\t * Rebuilds the heap from the current array state.\n\t * Useful when priorities have been modified externally (e.g., phase transitions).\n\t */\n\tpublic rebuild(): void {\n\t\t// Floyd's algorithm: heapify down from last non-leaf to root\n\t\tconst start = Math.floor(this.heap.length / 2) - 1;\n\t\tfor (let i = start; i >= 0; i--) {\n\t\t\tthis.heapifyDown(i);\n\t\t}\n\t}\n\n\t/**\n\t * Decreases the priority of an existing item in the queue.\n\t * Returns true if the item was found and updated, false otherwise.\n\t *\n\t * @param item - The item to find\n\t * @param newPriority - The new (lower) priority value\n\t * @param equals - Function to compare items for equality\n\t */\n\tpublic decreaseKey(\n\t\titem: T,\n\t\tnewPriority: number,\n\t\tequals: (a: T, b: T) => boolean,\n\t): boolean {\n\t\tlet foundIndex = -1;\n\n\t\tfor (let i = 0; i < this.heap.length; i++) {\n\t\t\tconst entry = this.heap[i];\n\t\t\tif (entry !== undefined && equals(entry.item, item)) {\n\t\t\t\tfoundIndex = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (foundIndex === -1) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst entry = this.heap[foundIndex];\n\t\tif (entry === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Only update if new priority is lower (higher precedence)\n\t\tif (newPriority >= entry.priority) {\n\t\t\treturn false;\n\t\t}\n\n\t\tentry.priority = newPriority;\n\t\tthis.heapifyUp(foundIndex);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Restores heap property by moving element up from given index.\n\t */\n\tprivate heapifyUp(index: number): void {\n\t\tlet current = index;\n\n\t\twhile (current > 0) {\n\t\t\tconst parent = Math.floor((current - 1) / 2);\n\t\t\tconst currentEntry = this.heap[current];\n\t\t\tconst parentEntry = this.heap[parent];\n\n\t\t\tif (currentEntry === undefined || parentEntry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (currentEntry.priority >= parentEntry.priority) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Swap current and parent\n\t\t\tthis.heap[current] = parentEntry;\n\t\t\tthis.heap[parent] = currentEntry;\n\t\t\tcurrent = parent;\n\t\t}\n\t}\n\n\t/**\n\t * Restores heap property by moving element down from given index.\n\t */\n\tprivate heapifyDown(index: number): void {\n\t\tlet current = index;\n\t\tconst length = this.heap.length;\n\n\t\twhile (current < length) {\n\t\t\tconst left = 2 * current + 1;\n\t\t\tconst right = 2 * current + 2;\n\t\t\tlet smallest = current;\n\n\t\t\tconst currentEntry = this.heap[current];\n\t\t\tif (currentEntry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Compare with left child\n\t\t\tconst leftEntry = this.heap[left];\n\t\t\tif (\n\t\t\t\tleft < length &&\n\t\t\t\tleftEntry !== undefined &&\n\t\t\t\tleftEntry.priority < currentEntry.priority\n\t\t\t) {\n\t\t\t\tsmallest = left;\n\t\t\t}\n\n\t\t\t// Compare with right child (need to re-fetch smallest after potential update)\n\t\t\tconst rightEntry = this.heap[right];\n\t\t\tconst currentSmallestEntry = this.heap[smallest];\n\t\t\tif (\n\t\t\t\tright < length &&\n\t\t\t\trightEntry !== undefined &&\n\t\t\t\tcurrentSmallestEntry !== undefined &&\n\t\t\t\trightEntry.priority < currentSmallestEntry.priority\n\t\t\t) {\n\t\t\t\tsmallest = right;\n\t\t\t}\n\n\t\t\tif (smallest === current) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Get the final smallest entry (after determining final smallest index)\n\t\t\tconst finalSmallestEntry = this.heap[smallest];\n\t\t\tif (finalSmallestEntry !== undefined) {\n\t\t\t\tthis.heap[current] = finalSmallestEntry;\n\t\t\t\tthis.heap[smallest] = currentEntry;\n\t\t\t\tcurrent = smallest;\n\t\t\t} else {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;AAYA,IAAa,gBAAb,MAA8B;CAC7B,OAAmC,EAAE;;;;CAKrC,OAAsB;AACrB,SAAO,KAAK,KAAK;;;;;CAMlB,UAA0B;AACzB,SAAO,KAAK,KAAK,WAAW;;;;;CAM7B,KAAY,MAAS,UAAwB;EAC5C,MAAM,QAA0B;GAAE;GAAM;GAAU;AAClD,OAAK,KAAK,KAAK,MAAM;AACrB,OAAK,UAAU,KAAK,KAAK,SAAS,EAAE;;;;;;CAOrC,MAA2C;AAC1C,MAAI,KAAK,KAAK,WAAW,EACxB;EAGD,MAAM,OAAO,KAAK,KAAK;AACvB,MAAI,SAAS,KAAA,EACZ;EAGD,MAAM,OAAO,KAAK,KAAK,KAAK;AAC5B,MAAI,SAAS,KAAA,EACZ,QAAO;AAGR,MAAI,KAAK,KAAK,SAAS,GAAG;AACzB,QAAK,KAAK,KAAK;AACf,QAAK,YAAY,EAAE;;AAGpB,SAAO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O,UACP,cAAc,KAAA,KACd,UAAU,WAAW,aAAa,SAElC,YAAW;GAIZ,MAAM,aAAa,KAAK,KAAK;GAC7B,MAAM,uBAAuB,KAAK,KAAK;AACvC,OACC,QAAQ,UACR,eAAe,KAAA,KACf,yBAAyB,KAAA,KACzB,WAAW,WAAW,qBAAqB,SAE3C,YAAW;AAGZ,OAAI,aAAa,QAChB;GAID,MAAM,qBAAqB,KAAK,KAAK;AACrC,OAAI,uBAAuB,KAAA,GAAW;AACrC,SAAK,KAAK,WAAW;AACrB,SAAK,KAAK,YAAY;AACtB,cAAU;SAEV"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/structures/priority-queue.ts"],"sourcesContent":["/**\n * Priority queue entry containing an item and its priority.\n */\nexport interface PriorityEntry<T> {\n\titem: T;\n\tpriority: number;\n}\n\n/**\n * Min-heap priority queue implementation using an array-based binary heap.\n * Lower priority values have higher precedence (extracted first).\n */\nexport class PriorityQueue<T> {\n\tprivate heap: PriorityEntry<T>[] = [];\n\tprivate indexMap = new Map<T, number>();\n\n\t/**\n\t * Returns the number of items in the queue.\n\t */\n\tpublic size(): number {\n\t\treturn this.heap.length;\n\t}\n\n\t/**\n\t * Returns true if the queue is empty.\n\t */\n\tpublic isEmpty(): boolean {\n\t\treturn this.heap.length === 0;\n\t}\n\n\t/**\n\t * Adds an item with the given priority to the queue.\n\t */\n\tpublic push(item: T, priority: number): void {\n\t\tconst entry: PriorityEntry<T> = { item, priority };\n\t\tthis.heap.push(entry);\n\t\tthis.indexMap.set(item, this.heap.length - 1);\n\t\tthis.heapifyUp(this.heap.length - 1);\n\t}\n\n\t/**\n\t * Removes and returns the highest precedence item (lowest priority value).\n\t * Returns undefined if the queue is empty.\n\t */\n\tpublic pop(): PriorityEntry<T> | undefined {\n\t\tif (this.heap.length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst root = this.heap[0];\n\t\tif (root === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.indexMap.delete(root.item);\n\n\t\tconst last = this.heap.pop();\n\t\tif (last === undefined) {\n\t\t\treturn root;\n\t\t}\n\n\t\tif (this.heap.length > 0) {\n\t\t\tthis.heap[0] = last;\n\t\t\tthis.indexMap.set(last.item, 0);\n\t\t\tthis.heapifyDown(0);\n\t\t}\n\n\t\treturn root;\n\t}\n\n\t/**\n\t * Returns the highest precedence item without removing it.\n\t * Returns undefined if the queue is empty.\n\t */\n\tpublic peek(): PriorityEntry<T> | undefined {\n\t\treturn this.heap[0];\n\t}\n\n\t/**\n\t * Rebuilds the heap from the current array state.\n\t * Useful when priorities have been modified externally (e.g., phase transitions).\n\t */\n\tpublic rebuild(): void {\n\t\t// Floyd's algorithm: heapify down from last non-leaf to root\n\t\tconst start = Math.floor(this.heap.length / 2) - 1;\n\t\tfor (let i = start; i >= 0; i--) {\n\t\t\tthis.heapifyDown(i);\n\t\t}\n\t}\n\n\t/**\n\t * Decreases the priority of an existing item in the queue.\n\t * Returns true if the item was found and updated, false otherwise.\n\t *\n\t * @param item - The item to find\n\t * @param newPriority - The new (lower) priority value\n\t * @param equals - Function to compare items for equality. If provided, uses linear search;\n\t * otherwise uses O(1) reference-based lookup.\n\t */\n\tpublic decreaseKey(\n\t\titem: T,\n\t\tnewPriority: number,\n\t\tequals?: (a: T, b: T) => boolean,\n\t): boolean {\n\t\tlet foundIndex = -1;\n\n\t\t// If equals function provided, use linear search for custom equality\n\t\tif (equals !== undefined) {\n\t\t\tfor (let i = 0; i < this.heap.length; i++) {\n\t\t\t\tconst entry = this.heap[i];\n\t\t\t\tif (entry !== undefined && equals(entry.item, item)) {\n\t\t\t\t\tfoundIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\t// Use O(1) reference-based lookup via indexMap\n\t\t\tfoundIndex = this.indexMap.get(item) ?? -1;\n\t\t}\n\n\t\tif (foundIndex === -1) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst entry = this.heap[foundIndex];\n\t\tif (entry === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Only update if new priority is lower (higher precedence)\n\t\tif (newPriority >= entry.priority) {\n\t\t\treturn false;\n\t\t}\n\n\t\tentry.priority = newPriority;\n\t\tthis.heapifyUp(foundIndex);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Restores heap property by moving element up from given index.\n\t */\n\tprivate heapifyUp(index: number): void {\n\t\tlet current = index;\n\n\t\twhile (current > 0) {\n\t\t\tconst parent = Math.floor((current - 1) / 2);\n\t\t\tconst currentEntry = this.heap[current];\n\t\t\tconst parentEntry = this.heap[parent];\n\n\t\t\tif (currentEntry === undefined || parentEntry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (currentEntry.priority >= parentEntry.priority) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Swap current and parent\n\t\t\tthis.heap[current] = parentEntry;\n\t\t\tthis.heap[parent] = currentEntry;\n\t\t\t// Update indexMap after swap\n\t\t\tthis.indexMap.set(parentEntry.item, current);\n\t\t\tthis.indexMap.set(currentEntry.item, parent);\n\t\t\tcurrent = parent;\n\t\t}\n\t}\n\n\t/**\n\t * Restores heap property by moving element down from given index.\n\t */\n\tprivate heapifyDown(index: number): void {\n\t\tlet current = index;\n\t\tconst length = this.heap.length;\n\n\t\twhile (current < length) {\n\t\t\tconst left = 2 * current + 1;\n\t\t\tconst right = 2 * current + 2;\n\t\t\tlet smallest = current;\n\n\t\t\tconst currentEntry = this.heap[current];\n\t\t\tif (currentEntry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Compare with left child\n\t\t\tconst leftEntry = this.heap[left];\n\t\t\tif (\n\t\t\t\tleft < length &&\n\t\t\t\tleftEntry !== undefined &&\n\t\t\t\tleftEntry.priority < currentEntry.priority\n\t\t\t) {\n\t\t\t\tsmallest = left;\n\t\t\t}\n\n\t\t\t// Compare with right child (need to re-fetch smallest after potential update)\n\t\t\tconst rightEntry = this.heap[right];\n\t\t\tconst currentSmallestEntry = this.heap[smallest];\n\t\t\tif (\n\t\t\t\tright < length &&\n\t\t\t\trightEntry !== undefined &&\n\t\t\t\tcurrentSmallestEntry !== undefined &&\n\t\t\t\trightEntry.priority < currentSmallestEntry.priority\n\t\t\t) {\n\t\t\t\tsmallest = right;\n\t\t\t}\n\n\t\t\tif (smallest === current) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Get the final smallest entry (after determining final smallest index)\n\t\t\tconst finalSmallestEntry = this.heap[smallest];\n\t\t\tif (finalSmallestEntry !== undefined) {\n\t\t\t\tthis.heap[current] = finalSmallestEntry;\n\t\t\t\tthis.heap[smallest] = currentEntry;\n\t\t\t\t// Update indexMap after swap\n\t\t\t\tthis.indexMap.set(finalSmallestEntry.item, current);\n\t\t\t\tthis.indexMap.set(currentEntry.item, smallest);\n\t\t\t\tcurrent = smallest;\n\t\t\t} else {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;AAYA,IAAa,gBAAb,MAA8B;CAC7B,OAAmC,EAAE;CACrC,2BAAmB,IAAI,KAAgB;;;;CAKvC,OAAsB;AACrB,SAAO,KAAK,KAAK;;;;;CAMlB,UAA0B;AACzB,SAAO,KAAK,KAAK,WAAW;;;;;CAM7B,KAAY,MAAS,UAAwB;EAC5C,MAAM,QAA0B;GAAE;GAAM;GAAU;AAClD,OAAK,KAAK,KAAK,MAAM;AACrB,OAAK,SAAS,IAAI,MAAM,KAAK,KAAK,SAAS,EAAE;AAC7C,OAAK,UAAU,KAAK,KAAK,SAAS,EAAE;;;;;;CAOrC,MAA2C;AAC1C,MAAI,KAAK,KAAK,WAAW,EACxB;EAGD,MAAM,OAAO,KAAK,KAAK;AACvB,MAAI,SAAS,KAAA,EACZ;AAGD,OAAK,SAAS,OAAO,KAAK,KAAK;EAE/B,MAAM,OAAO,KAAK,KAAK,KAAK;AAC5B,MAAI,SAAS,KAAA,EACZ,QAAO;AAGR,MAAI,KAAK,KAA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B,OAAqB;EACxC,IAAI,UAAU;EACd,MAAM,SAAS,KAAK,KAAK;AAEzB,SAAO,UAAU,QAAQ;GACxB,MAAM,OAAO,IAAI,UAAU;GAC3B,MAAM,QAAQ,IAAI,UAAU;GAC5B,IAAI,WAAW;GAEf,MAAM,eAAe,KAAK,KAAK;AAC/B,OAAI,iBAAiB,KAAA,EACpB;GAID,MAAM,YAAY,KAAK,KAAK;AAC5B,OACC,OAAO,UACP,cAAc,KAAA,KACd,UAAU,WAAW,aAAa,SAElC,YAAW;GAIZ,MAAM,aAAa,KAAK,KAAK;GAC7B,MAAM,uBAAuB,KAAK,KAAK;AACvC,OACC,QAAQ,UACR,eAAe,KAAA,KACf,yBAAyB,KAAA,KACzB,WAAW,WAAW,qBAAqB,SAE3C,YAAW;AAGZ,OAAI,aAAa,QAChB;GAID,MAAM,qBAAqB,KAAK,KAAK;AACrC,OAAI,uBAAuB,KAAA,GAAW;AACrC,SAAK,KAAK,WAAW;AACrB,SAAK,KAAK,YAAY;AAEtB,SAAK,SAAS,IAAI,mBAAmB,MAAM,QAAQ;AACnD,SAAK,SAAS,IAAI,aAAa,MAAM,SAAS;AAC9C,cAAU;SAEV"}
@@ -11,6 +11,7 @@ export interface PriorityEntry<T> {
11
11
  */
12
12
  export declare class PriorityQueue<T> {
13
13
  private heap;
14
+ private indexMap;
14
15
  /**
15
16
  * Returns the number of items in the queue.
16
17
  */
@@ -44,9 +45,10 @@ export declare class PriorityQueue<T> {
44
45
  *
45
46
  * @param item - The item to find
46
47
  * @param newPriority - The new (lower) priority value
47
- * @param equals - Function to compare items for equality
48
+ * @param equals - Function to compare items for equality. If provided, uses linear search;
49
+ * otherwise uses O(1) reference-based lookup.
48
50
  */
49
- decreaseKey(item: T, newPriority: number, equals: (a: T, b: T) => boolean): boolean;
51
+ decreaseKey(item: T, newPriority: number, equals?: (a: T, b: T) => boolean): boolean;
50
52
  /**
51
53
  * Restores heap property by moving element up from given index.
52
54
  */
@@ -1 +1 @@
1
- {"version":3,"file":"priority-queue.d.ts","sourceRoot":"","sources":["../../src/structures/priority-queue.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC;IAC/B,IAAI,EAAE,CAAC,CAAC;IACR,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,qBAAa,aAAa,CAAC,CAAC;IAC3B,OAAO,CAAC,IAAI,CAA0B;IAEtC;;OAEG;IACI,IAAI,IAAI,MAAM;IAIrB;;OAEG;IACI,OAAO,IAAI,OAAO;IAIzB;;OAEG;IACI,IAAI,CAAC,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAM5C;;;OAGG;IACI,GAAG,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS;IAuB1C;;;OAGG;IACI,IAAI,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS;IAI3C;;;OAGG;IACI,OAAO,IAAI,IAAI;IAQtB;;;;;;;OAOG;IACI,WAAW,CACjB,IAAI,EAAE,CAAC,EACP,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,GAC7B,OAAO;IA8BV;;OAEG;IACH,OAAO,CAAC,SAAS;IAuBjB;;OAEG;IACH,OAAO,CAAC,WAAW;CAmDnB"}
1
+ {"version":3,"file":"priority-queue.d.ts","sourceRoot":"","sources":["../../src/structures/priority-queue.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC;IAC/B,IAAI,EAAE,CAAC,CAAC;IACR,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,qBAAa,aAAa,CAAC,CAAC;IAC3B,OAAO,CAAC,IAAI,CAA0B;IACtC,OAAO,CAAC,QAAQ,CAAwB;IAExC;;OAEG;IACI,IAAI,IAAI,MAAM;IAIrB;;OAEG;IACI,OAAO,IAAI,OAAO;IAIzB;;OAEG;IACI,IAAI,CAAC,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAO5C;;;OAGG;IACI,GAAG,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS;IA0B1C;;;OAGG;IACI,IAAI,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS;IAI3C;;;OAGG;IACI,OAAO,IAAI,IAAI;IAQtB;;;;;;;;OAQG;IACI,WAAW,CACjB,IAAI,EAAE,CAAC,EACP,WAAW,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,GAC9B,OAAO;IAoCV;;OAEG;IACH,OAAO,CAAC,SAAS;IA0BjB;;OAEG;IACH,OAAO,CAAC,WAAW;CAsDnB"}
@@ -1,82 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_kmeans = require("../kmeans-BIgSyGKu.cjs");
3
- //#region src/utils/clustering-coefficient.ts
4
- /**
5
- * Compute the local clustering coefficient for a single node.
6
- *
7
- * The clustering coefficient is defined as:
8
- * CC(v) = (triangles through v) / (possible triangles)
9
- * CC(v) = 2 * |{(u,w) : u,w in N(v), (u,w) in E}| / (deg(v) * (deg(v) - 1))
10
- *
11
- * For nodes with degree < 2, the clustering coefficient is 0.
12
- *
13
- * @param graph - The graph to compute on
14
- * @param nodeId - The node to compute clustering coefficient for
15
- * @returns The clustering coefficient in [0, 1], or 0 if undefined
16
- */
17
- function localClusteringCoefficient(graph, nodeId) {
18
- const neighbours = [...graph.neighbours(nodeId, "both")];
19
- const degree = neighbours.length;
20
- if (degree < 2) return 0;
21
- let triangleCount = 0;
22
- for (let i = 0; i < neighbours.length; i++) {
23
- const u = neighbours[i];
24
- if (u === void 0) continue;
25
- for (let j = i + 1; j < neighbours.length; j++) {
26
- const w = neighbours[j];
27
- if (w === void 0) continue;
28
- if (graph.getEdge(u, w) !== void 0 || graph.getEdge(w, u) !== void 0) triangleCount++;
29
- }
30
- }
31
- const possibleTriangles = degree * (degree - 1) / 2;
32
- return triangleCount / possibleTriangles;
33
- }
34
- /**
35
- * Compute approximate local clustering coefficient using sampling.
36
- *
37
- * For nodes with many neighbours, this samples neighbour pairs rather than
38
- * checking all pairs. Useful for large graphs where exact computation is expensive.
39
- *
40
- * @param graph - The graph to compute on
41
- * @param nodeId - The node to compute clustering coefficient for
42
- * @param sampleSize - Maximum number of neighbour pairs to check (default: 100)
43
- * @returns The approximate clustering coefficient in [0, 1]
44
- */
45
- function approximateClusteringCoefficient(graph, nodeId, sampleSize = 100) {
46
- const neighbours = [...graph.neighbours(nodeId, "both")];
47
- const degree = neighbours.length;
48
- if (degree < 2) return 0;
49
- const possibleTriangles = degree * (degree - 1) / 2;
50
- if (possibleTriangles <= sampleSize) return localClusteringCoefficient(graph, nodeId);
51
- let triangleCount = 0;
52
- let sampled = 0;
53
- for (let i = 0; i < neighbours.length && sampled < sampleSize; i++) {
54
- const u = neighbours[i];
55
- if (u === void 0) continue;
56
- for (let j = i + 1; j < neighbours.length && sampled < sampleSize; j++) {
57
- const w = neighbours[j];
58
- if (w === void 0) continue;
59
- sampled++;
60
- if (graph.getEdge(u, w) !== void 0 || graph.getEdge(w, u) !== void 0) triangleCount++;
61
- }
62
- }
63
- return triangleCount / sampled * (possibleTriangles / possibleTriangles);
64
- }
65
- /**
66
- * Compute clustering coefficients for multiple nodes efficiently.
67
- *
68
- * Reuses neighbour sets to avoid repeated iteration.
69
- *
70
- * @param graph - The graph to compute on
71
- * @param nodeIds - The nodes to compute clustering coefficients for
72
- * @returns Map from nodeId to clustering coefficient
73
- */
74
- function batchClusteringCoefficients(graph, nodeIds) {
75
- const results = /* @__PURE__ */ new Map();
76
- for (const nodeId of nodeIds) results.set(nodeId, localClusteringCoefficient(graph, nodeId));
77
- return results;
78
- }
79
- //#endregion
80
3
  //#region src/utils/entropy.ts
81
4
  /**
82
5
  * Entropy computation utilities for graph analysis.
@@ -161,6 +84,82 @@ function localTypeEntropy(neighbourTypes) {
161
84
  return normalisedEntropy(probabilities);
162
85
  }
163
86
  //#endregion
87
+ //#region src/utils/clustering-coefficient.ts
88
+ /**
89
+ * Compute the local clustering coefficient for a single node.
90
+ *
91
+ * The clustering coefficient is defined as:
92
+ * CC(v) = (triangles through v) / (possible triangles)
93
+ * CC(v) = 2 * |{(u,w) : u,w in N(v), (u,w) in E}| / (deg(v) * (deg(v) - 1))
94
+ *
95
+ * For nodes with degree < 2, the clustering coefficient is 0.
96
+ *
97
+ * @param graph - The graph to compute on
98
+ * @param nodeId - The node to compute clustering coefficient for
99
+ * @returns The clustering coefficient in [0, 1], or 0 if undefined
100
+ */
101
+ function localClusteringCoefficient(graph, nodeId) {
102
+ const neighbours = [...graph.neighbours(nodeId, "both")];
103
+ const degree = neighbours.length;
104
+ if (degree < 2) return 0;
105
+ let triangleCount = 0;
106
+ for (let i = 0; i < neighbours.length; i++) {
107
+ const u = neighbours[i];
108
+ if (u === void 0) continue;
109
+ for (let j = i + 1; j < neighbours.length; j++) {
110
+ const w = neighbours[j];
111
+ if (w === void 0) continue;
112
+ if (graph.getEdge(u, w) !== void 0 || graph.getEdge(w, u) !== void 0) triangleCount++;
113
+ }
114
+ }
115
+ const possibleTriangles = degree * (degree - 1) / 2;
116
+ return triangleCount / possibleTriangles;
117
+ }
118
+ /**
119
+ * Compute approximate local clustering coefficient using sampling.
120
+ *
121
+ * For nodes with many neighbours, this samples neighbour pairs rather than
122
+ * checking all pairs. Useful for large graphs where exact computation is expensive.
123
+ *
124
+ * @param graph - The graph to compute on
125
+ * @param nodeId - The node to compute clustering coefficient for
126
+ * @param sampleSize - Maximum number of neighbour pairs to check (default: 100)
127
+ * @returns The approximate clustering coefficient in [0, 1]
128
+ */
129
+ function approximateClusteringCoefficient(graph, nodeId, sampleSize = 100) {
130
+ const neighbours = [...graph.neighbours(nodeId, "both")];
131
+ const degree = neighbours.length;
132
+ if (degree < 2) return 0;
133
+ if (degree * (degree - 1) / 2 <= sampleSize) return localClusteringCoefficient(graph, nodeId);
134
+ let triangleCount = 0;
135
+ let sampled = 0;
136
+ for (let i = 0; i < neighbours.length && sampled < sampleSize; i++) {
137
+ const u = neighbours[i];
138
+ if (u === void 0) continue;
139
+ for (let j = i + 1; j < neighbours.length && sampled < sampleSize; j++) {
140
+ const w = neighbours[j];
141
+ if (w === void 0) continue;
142
+ sampled++;
143
+ if (graph.getEdge(u, w) !== void 0 || graph.getEdge(w, u) !== void 0) triangleCount++;
144
+ }
145
+ }
146
+ return triangleCount / sampled;
147
+ }
148
+ /**
149
+ * Compute clustering coefficients for multiple nodes efficiently.
150
+ *
151
+ * Reuses neighbour sets to avoid repeated iteration.
152
+ *
153
+ * @param graph - The graph to compute on
154
+ * @param nodeIds - The nodes to compute clustering coefficients for
155
+ * @returns Map from nodeId to clustering coefficient
156
+ */
157
+ function batchClusteringCoefficients(graph, nodeIds) {
158
+ const results = /* @__PURE__ */ new Map();
159
+ for (const nodeId of nodeIds) results.set(nodeId, localClusteringCoefficient(graph, nodeId));
160
+ return results;
161
+ }
162
+ //#endregion
164
163
  //#region src/utils/neighbours.ts
165
164
  /**
166
165
  * Collect neighbours into a Set, optionally excluding a specific node.
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":[],"sources":["../../src/utils/clustering-coefficient.ts","../../src/utils/entropy.ts","../../src/utils/neighbours.ts"],"sourcesContent":["/**\n * Clustering coefficient computation for graph nodes.\n *\n * The local clustering coefficient measures how close a node's neighbours\n * are to being a complete graph (clique). It is used in SPAN MI variant\n * and GRASP seed selection.\n *\n * @packageDocumentation\n */\n\nimport type { ReadableGraph, NodeId } from \"../graph\";\n\n/**\n * Compute the local clustering coefficient for a single node.\n *\n * The clustering coefficient is defined as:\n * CC(v) = (triangles through v) / (possible triangles)\n * CC(v) = 2 * |{(u,w) : u,w in N(v), (u,w) in E}| / (deg(v) * (deg(v) - 1))\n *\n * For nodes with degree < 2, the clustering coefficient is 0.\n *\n * @param graph - The graph to compute on\n * @param nodeId - The node to compute clustering coefficient for\n * @returns The clustering coefficient in [0, 1], or 0 if undefined\n */\nexport function localClusteringCoefficient(\n\tgraph: ReadableGraph,\n\tnodeId: NodeId,\n): number {\n\tconst neighbours = [...graph.neighbours(nodeId, \"both\")];\n\tconst degree = neighbours.length;\n\n\t// Nodes with degree < 2 have no possible triangles\n\tif (degree < 2) {\n\t\treturn 0;\n\t}\n\n\t// Count actual triangles: pairs of neighbours that are connected\n\tlet triangleCount = 0;\n\n\tfor (let i = 0; i < neighbours.length; i++) {\n\t\tconst u = neighbours[i];\n\t\tif (u === undefined) continue;\n\n\t\tfor (let j = i + 1; j < neighbours.length; j++) {\n\t\t\tconst w = neighbours[j];\n\t\t\tif (w === undefined) continue;\n\n\t\t\t// Check if u and w are connected\n\t\t\tif (\n\t\t\t\tgraph.getEdge(u, w) !== undefined ||\n\t\t\t\tgraph.getEdge(w, u) !== undefined\n\t\t\t) {\n\t\t\t\ttriangleCount++;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Possible triangles: deg * (deg - 1) / 2 pairs\n\t// We multiply by 2 because each triangle is counted once\n\tconst possibleTriangles = (degree * (degree - 1)) / 2;\n\n\treturn triangleCount / possibleTriangles;\n}\n\n/**\n * Compute approximate local clustering coefficient using sampling.\n *\n * For nodes with many neighbours, this samples neighbour pairs rather than\n * checking all pairs. Useful for large graphs where exact computation is expensive.\n *\n * @param graph - The graph to compute on\n * @param nodeId - The node to compute clustering coefficient for\n * @param sampleSize - Maximum number of neighbour pairs to check (default: 100)\n * @returns The approximate clustering coefficient in [0, 1]\n */\nexport function approximateClusteringCoefficient(\n\tgraph: ReadableGraph,\n\tnodeId: NodeId,\n\tsampleSize = 100,\n): number {\n\tconst neighbours = [...graph.neighbours(nodeId, \"both\")];\n\tconst degree = neighbours.length;\n\n\tif (degree < 2) {\n\t\treturn 0;\n\t}\n\n\tconst possibleTriangles = (degree * (degree - 1)) / 2;\n\n\t// If all pairs can be checked within sample limit, use exact computation\n\tif (possibleTriangles <= sampleSize) {\n\t\treturn localClusteringCoefficient(graph, nodeId);\n\t}\n\n\t// Sample pairs uniformly\n\tlet triangleCount = 0;\n\tlet sampled = 0;\n\n\t// Use reservoir sampling style approach for pair selection\n\tfor (let i = 0; i < neighbours.length && sampled < sampleSize; i++) {\n\t\tconst u = neighbours[i];\n\t\tif (u === undefined) continue;\n\n\t\tfor (let j = i + 1; j < neighbours.length && sampled < sampleSize; j++) {\n\t\t\tconst w = neighbours[j];\n\t\t\tif (w === undefined) continue;\n\n\t\t\t// Decide whether to include this pair based on remaining budget\n\t\t\tsampled++;\n\n\t\t\t// Check if u and w are connected\n\t\t\tif (\n\t\t\t\tgraph.getEdge(u, w) !== undefined ||\n\t\t\t\tgraph.getEdge(w, u) !== undefined\n\t\t\t) {\n\t\t\t\ttriangleCount++;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Extrapolate from sample\n\treturn (triangleCount / sampled) * (possibleTriangles / possibleTriangles);\n}\n\n/**\n * Compute clustering coefficients for multiple nodes efficiently.\n *\n * Reuses neighbour sets to avoid repeated iteration.\n *\n * @param graph - The graph to compute on\n * @param nodeIds - The nodes to compute clustering coefficients for\n * @returns Map from nodeId to clustering coefficient\n */\nexport function batchClusteringCoefficients(\n\tgraph: ReadableGraph,\n\tnodeIds: readonly NodeId[],\n): Map<NodeId, number> {\n\tconst results = new Map<NodeId, number>();\n\n\tfor (const nodeId of nodeIds) {\n\t\tresults.set(nodeId, localClusteringCoefficient(graph, nodeId));\n\t}\n\n\treturn results;\n}\n","/**\n * Entropy computation utilities for graph analysis.\n *\n * Shannon entropy measures uncertainty or randomness in a distribution.\n * Used in EDGE and HAE algorithms for heterogeneity-aware expansion.\n *\n * @packageDocumentation\n */\n\n/**\n * Compute Shannon entropy of a probability distribution.\n *\n * Shannon entropy is defined as:\n * H(X) = -Σ p(x) × log₂(p(x))\n *\n * A uniform distribution has maximum entropy.\n * A deterministic distribution (all probability on one value) has zero entropy.\n *\n * @param probabilities - Array of probabilities (should sum to 1)\n * @returns Entropy in bits (log base 2), or 0 if probabilities are invalid\n */\nexport function shannonEntropy(probabilities: readonly number[]): number {\n\tif (probabilities.length === 0) {\n\t\treturn 0;\n\t}\n\n\tlet entropy = 0;\n\tfor (const p of probabilities) {\n\t\t// Skip zero probabilities (log(0) is undefined, but 0 * log(0) = 0)\n\t\tif (p > 0) {\n\t\t\tentropy -= p * Math.log2(p);\n\t\t}\n\t}\n\n\treturn entropy;\n}\n\n/**\n * Compute normalised entropy (entropy divided by maximum possible entropy).\n *\n * Normalised entropy is in [0, 1], where:\n * - 0 means the distribution is deterministic (all mass on one value)\n * - 1 means the distribution is uniform (maximum uncertainty)\n *\n * This is useful for comparing entropy across distributions with different\n * numbers of possible values.\n *\n * @param probabilities - Array of probabilities (should sum to 1)\n * @returns Normalised entropy in [0, 1], or 0 if only one category\n */\nexport function normalisedEntropy(probabilities: readonly number[]): number {\n\tif (probabilities.length <= 1) {\n\t\treturn 0;\n\t}\n\n\tconst H = shannonEntropy(probabilities);\n\tconst Hmax = Math.log2(probabilities.length);\n\n\tif (Hmax === 0) {\n\t\treturn 0;\n\t}\n\n\treturn H / Hmax;\n}\n\n/**\n * Compute entropy from a frequency count.\n *\n * Converts counts to probabilities and then computes entropy.\n * This is a convenience function when you have raw counts rather than\n * normalised probabilities.\n *\n * @param counts - Array of frequency counts\n * @returns Entropy in bits\n */\nexport function entropyFromCounts(counts: readonly number[]): number {\n\tif (counts.length === 0) {\n\t\treturn 0;\n\t}\n\n\tconst total = counts.reduce((sum, c) => sum + c, 0);\n\tif (total === 0) {\n\t\treturn 0;\n\t}\n\n\tconst probabilities = counts.map((c) => c / total);\n\treturn shannonEntropy(probabilities);\n}\n\n/**\n * Compute local type entropy for a node's neighbours.\n *\n * This measures the diversity of types among a node's neighbours.\n * High entropy = heterogeneous neighbourhood (diverse types).\n * Low entropy = homogeneous neighbourhood (similar types).\n *\n * @param neighbourTypes - Array of type labels for neighbours\n * @returns Normalised entropy in [0, 1]\n */\nexport function localTypeEntropy(neighbourTypes: readonly string[]): number {\n\tif (neighbourTypes.length <= 1) {\n\t\treturn 0;\n\t}\n\n\t// Count occurrences of each type\n\tconst typeCounts = new Map<string, number>();\n\tfor (const t of neighbourTypes) {\n\t\ttypeCounts.set(t, (typeCounts.get(t) ?? 0) + 1);\n\t}\n\n\t// If all neighbours are the same type, entropy is 0\n\tif (typeCounts.size === 1) {\n\t\treturn 0;\n\t}\n\n\t// Convert to probability array\n\tconst probabilities: number[] = [];\n\tconst total = neighbourTypes.length;\n\tfor (const count of typeCounts.values()) {\n\t\tprobabilities.push(count / total);\n\t}\n\n\treturn normalisedEntropy(probabilities);\n}\n","/**\n * Neighbourhood computation utilities.\n *\n * Shared utilities for neighbourhood operations used by MI variants and other graph algorithms.\n * These functions eliminate duplication of neighbourhood set operations across multiple\n * implementations.\n *\n * @packageDocumentation\n */\n\nimport type { NodeId, NodeData, EdgeData, ReadableGraph } from \"../graph\";\n\n/**\n * Collect neighbours into a Set, optionally excluding a specific node.\n *\n * @param graph - The graph to traverse\n * @param nodeId - The source node\n * @param exclude - Optional node ID to exclude from result\n * @returns A ReadonlySet of neighbouring node IDs\n */\nexport function neighbourSet<N extends NodeData, E extends EdgeData>(\n\tgraph: ReadableGraph<N, E>,\n\tnodeId: NodeId,\n\texclude?: NodeId,\n): ReadonlySet<NodeId> {\n\tconst neighbours = new Set(graph.neighbours(nodeId));\n\tif (exclude !== undefined) {\n\t\tneighbours.delete(exclude);\n\t}\n\treturn neighbours;\n}\n\n/**\n * Compute intersection and union sizes of two neighbour sets without allocating the union set.\n *\n * This is more efficient than computing both separately, as it avoids creating a full union Set.\n *\n * @param a - First neighbourhood set\n * @param b - Second neighbourhood set\n * @returns Object with intersection and union sizes\n */\nexport function neighbourOverlap(\n\ta: ReadonlySet<NodeId>,\n\tb: ReadonlySet<NodeId>,\n): { intersection: number; union: number } {\n\tlet intersection = 0;\n\n\t// Count intersection by iterating through the smaller set\n\tconst [smaller, larger] = a.size < b.size ? [a, b] : [b, a];\n\n\tfor (const node of smaller) {\n\t\tif (larger.has(node)) {\n\t\t\tintersection++;\n\t\t}\n\t}\n\n\t// Union size = size(a) + size(b) - intersection\n\tconst union = a.size + b.size - intersection;\n\n\treturn { intersection, union };\n}\n\n/**\n * Return the actual intersection set of two neighbourhood sets.\n *\n * Needed by Adamic-Adar (iterates common neighbours) and ETCH (requires edge types of intersection edges).\n *\n * @param a - First neighbourhood set\n * @param b - Second neighbourhood set\n * @returns A ReadonlySet containing nodes in both a and b\n */\nexport function neighbourIntersection(\n\ta: ReadonlySet<NodeId>,\n\tb: ReadonlySet<NodeId>,\n): ReadonlySet<NodeId> {\n\tconst intersection = new Set<NodeId>();\n\n\t// Iterate through the smaller set for efficiency\n\tconst [smaller, larger] = a.size < b.size ? [a, b] : [b, a];\n\n\tfor (const node of smaller) {\n\t\tif (larger.has(node)) {\n\t\t\tintersection.add(node);\n\t\t}\n\t}\n\n\treturn intersection;\n}\n\n/**\n * Count the number of edges with a specific type in the graph.\n *\n * Used by ETCH MI variant to compute edge rarity weighting.\n *\n * @param graph - The graph to count edges in\n * @param type - The edge type to count\n * @returns The number of edges with the specified type\n */\nexport function countEdgesOfType<N extends NodeData, E extends EdgeData>(\n\tgraph: ReadableGraph<N, E>,\n\ttype: string,\n): number {\n\tlet count = 0;\n\tfor (const edge of graph.edges()) {\n\t\tif (edge.type === type) {\n\t\t\tcount++;\n\t\t}\n\t}\n\treturn count;\n}\n\n/**\n * Count the number of nodes with a specific type in the graph.\n *\n * Used by NOTCH MI variant to compute node rarity weighting.\n *\n * @param graph - The graph to count nodes in\n * @param type - The node type to count\n * @returns The number of nodes with the specified type\n */\nexport function countNodesOfType<N extends NodeData, E extends EdgeData>(\n\tgraph: ReadableGraph<N, E>,\n\ttype: string,\n): number {\n\tlet count = 0;\n\tfor (const nodeId of graph.nodeIds()) {\n\t\tconst node = graph.getNode(nodeId);\n\t\tif (node?.type === type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,QAAQ,MAAM;AAGlC,QAAO,kBAAkB,cAAc;;;;;;;;;;;;ACtGxC,SAAgB,aACf,OACA,QACA,SACsB;CACtB,MAAM,aAAa,IAAI,IAAI,MAAM,WAAW,OAAO,CAAC;AACpD,KAAI,YAAY,KAAA,EACf,YAAW,OAAO,QAAQ;AAE3B,QAAO;;;;;;;;;;;AAYR,SAAgB,iBACf,GACA,GAC0C;CAC1C,IAAI,eAAe;CAGnB,MAAM,CAAC,SAAS,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE;AAE3D,MAAK,MAAM,QAAQ,QAClB,KAAI,OAAO,IAAI,KAAK,CACnB;CAKF,MAAM,QAAQ,EAAE,OAAO,EAAE,OAAO;AAEhC,QAAO;EAAE;EAAc;EAAO;;;;;;;;;;;AAY/B,SAAgB,sBACf,GACA,GACsB;CACtB,MAAM,+BAAe,IAAI,KAAa;CAGtC,MAAM,CAAC,SAAS,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE;AAE3D,MAAK,MAAM,QAAQ,QAClB,KAAI,OAAO,IAAI,KAAK,CACnB,cAAa,IAAI,KAAK;AAIxB,QAAO;;;;;;;;;;;AAYR,SAAgB,iBACf,OACA,MACS;CACT,IAAI,QAAQ;AACZ,MAAK,MAAM,QAAQ,MAAM,OAAO,CAC/B,KAAI,KAAK,SAAS,KACjB;AAGF,QAAO;;;;;;;;;;;AAYR,SAAgB,iBACf,OACA,MACS;CACT,IAAI,QAAQ;AACZ,MAAK,MAAM,UAAU,MAAM,SAAS,CAEnC,KADa,MAAM,QAAQ,OAAO,EACxB,SAAS,KAClB;AAGF,QAAO"}
1
+ {"version":3,"file":"index.cjs","names":[],"sources":["../../src/utils/entropy.ts","../../src/utils/clustering-coefficient.ts","../../src/utils/neighbours.ts"],"sourcesContent":["/**\n * Entropy computation utilities for graph analysis.\n *\n * Shannon entropy measures uncertainty or randomness in a distribution.\n * Used in EDGE and HAE algorithms for heterogeneity-aware expansion.\n *\n * @packageDocumentation\n */\n\n/**\n * Compute Shannon entropy of a probability distribution.\n *\n * Shannon entropy is defined as:\n * H(X) = -Σ p(x) × log₂(p(x))\n *\n * A uniform distribution has maximum entropy.\n * A deterministic distribution (all probability on one value) has zero entropy.\n *\n * @param probabilities - Array of probabilities (should sum to 1)\n * @returns Entropy in bits (log base 2), or 0 if probabilities are invalid\n */\nexport function shannonEntropy(probabilities: readonly number[]): number {\n\tif (probabilities.length === 0) {\n\t\treturn 0;\n\t}\n\n\tlet entropy = 0;\n\tfor (const p of probabilities) {\n\t\t// Skip zero probabilities (log(0) is undefined, but 0 * log(0) = 0)\n\t\tif (p > 0) {\n\t\t\tentropy -= p * Math.log2(p);\n\t\t}\n\t}\n\n\treturn entropy;\n}\n\n/**\n * Compute normalised entropy (entropy divided by maximum possible entropy).\n *\n * Normalised entropy is in [0, 1], where:\n * - 0 means the distribution is deterministic (all mass on one value)\n * - 1 means the distribution is uniform (maximum uncertainty)\n *\n * This is useful for comparing entropy across distributions with different\n * numbers of possible values.\n *\n * @param probabilities - Array of probabilities (should sum to 1)\n * @returns Normalised entropy in [0, 1], or 0 if only one category\n */\nexport function normalisedEntropy(probabilities: readonly number[]): number {\n\tif (probabilities.length <= 1) {\n\t\treturn 0;\n\t}\n\n\tconst H = shannonEntropy(probabilities);\n\tconst Hmax = Math.log2(probabilities.length);\n\n\tif (Hmax === 0) {\n\t\treturn 0;\n\t}\n\n\treturn H / Hmax;\n}\n\n/**\n * Compute entropy from a frequency count.\n *\n * Converts counts to probabilities and then computes entropy.\n * This is a convenience function when you have raw counts rather than\n * normalised probabilities.\n *\n * @param counts - Array of frequency counts\n * @returns Entropy in bits\n */\nexport function entropyFromCounts(counts: readonly number[]): number {\n\tif (counts.length === 0) {\n\t\treturn 0;\n\t}\n\n\tconst total = counts.reduce((sum, c) => sum + c, 0);\n\tif (total === 0) {\n\t\treturn 0;\n\t}\n\n\tconst probabilities = counts.map((c) => c / total);\n\treturn shannonEntropy(probabilities);\n}\n\n/**\n * Compute local type entropy for a node's neighbours.\n *\n * This measures the diversity of types among a node's neighbours.\n * High entropy = heterogeneous neighbourhood (diverse types).\n * Low entropy = homogeneous neighbourhood (similar types).\n *\n * @param neighbourTypes - Array of type labels for neighbours\n * @returns Normalised entropy in [0, 1]\n */\nexport function localTypeEntropy(neighbourTypes: readonly string[]): number {\n\tif (neighbourTypes.length <= 1) {\n\t\treturn 0;\n\t}\n\n\t// Count occurrences of each type\n\tconst typeCounts = new Map<string, number>();\n\tfor (const t of neighbourTypes) {\n\t\ttypeCounts.set(t, (typeCounts.get(t) ?? 0) + 1);\n\t}\n\n\t// If all neighbours are the same type, entropy is 0\n\tif (typeCounts.size === 1) {\n\t\treturn 0;\n\t}\n\n\t// Convert to probability array\n\tconst probabilities: number[] = [];\n\tconst total = neighbourTypes.length;\n\tfor (const count of typeCounts.values()) {\n\t\tprobabilities.push(count / total);\n\t}\n\n\treturn normalisedEntropy(probabilities);\n}\n","/**\n * Clustering coefficient computation for graph nodes.\n *\n * The local clustering coefficient measures how close a node's neighbours\n * are to being a complete graph (clique). It is used in SPAN MI variant\n * and GRASP seed selection.\n *\n * @packageDocumentation\n */\n\nimport type { ReadableGraph, NodeId } from \"../graph\";\n\n/**\n * Compute the local clustering coefficient for a single node.\n *\n * The clustering coefficient is defined as:\n * CC(v) = (triangles through v) / (possible triangles)\n * CC(v) = 2 * |{(u,w) : u,w in N(v), (u,w) in E}| / (deg(v) * (deg(v) - 1))\n *\n * For nodes with degree < 2, the clustering coefficient is 0.\n *\n * @param graph - The graph to compute on\n * @param nodeId - The node to compute clustering coefficient for\n * @returns The clustering coefficient in [0, 1], or 0 if undefined\n */\nexport function localClusteringCoefficient(\n\tgraph: ReadableGraph,\n\tnodeId: NodeId,\n): number {\n\tconst neighbours = [...graph.neighbours(nodeId, \"both\")];\n\tconst degree = neighbours.length;\n\n\t// Nodes with degree < 2 have no possible triangles\n\tif (degree < 2) {\n\t\treturn 0;\n\t}\n\n\t// Count actual triangles: pairs of neighbours that are connected\n\tlet triangleCount = 0;\n\n\tfor (let i = 0; i < neighbours.length; i++) {\n\t\tconst u = neighbours[i];\n\t\tif (u === undefined) continue;\n\n\t\tfor (let j = i + 1; j < neighbours.length; j++) {\n\t\t\tconst w = neighbours[j];\n\t\t\tif (w === undefined) continue;\n\n\t\t\t// Check if u and w are connected\n\t\t\tif (\n\t\t\t\tgraph.getEdge(u, w) !== undefined ||\n\t\t\t\tgraph.getEdge(w, u) !== undefined\n\t\t\t) {\n\t\t\t\ttriangleCount++;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Possible triangles: deg * (deg - 1) / 2 pairs\n\t// We multiply by 2 because each triangle is counted once\n\tconst possibleTriangles = (degree * (degree - 1)) / 2;\n\n\treturn triangleCount / possibleTriangles;\n}\n\n/**\n * Compute approximate local clustering coefficient using sampling.\n *\n * For nodes with many neighbours, this samples neighbour pairs rather than\n * checking all pairs. Useful for large graphs where exact computation is expensive.\n *\n * @param graph - The graph to compute on\n * @param nodeId - The node to compute clustering coefficient for\n * @param sampleSize - Maximum number of neighbour pairs to check (default: 100)\n * @returns The approximate clustering coefficient in [0, 1]\n */\nexport function approximateClusteringCoefficient(\n\tgraph: ReadableGraph,\n\tnodeId: NodeId,\n\tsampleSize = 100,\n): number {\n\tconst neighbours = [...graph.neighbours(nodeId, \"both\")];\n\tconst degree = neighbours.length;\n\n\tif (degree < 2) {\n\t\treturn 0;\n\t}\n\n\tconst possibleTriangles = (degree * (degree - 1)) / 2;\n\n\t// If all pairs can be checked within sample limit, use exact computation\n\tif (possibleTriangles <= sampleSize) {\n\t\treturn localClusteringCoefficient(graph, nodeId);\n\t}\n\n\t// Sample pairs uniformly\n\tlet triangleCount = 0;\n\tlet sampled = 0;\n\n\t// Use reservoir sampling style approach for pair selection\n\tfor (let i = 0; i < neighbours.length && sampled < sampleSize; i++) {\n\t\tconst u = neighbours[i];\n\t\tif (u === undefined) continue;\n\n\t\tfor (let j = i + 1; j < neighbours.length && sampled < sampleSize; j++) {\n\t\t\tconst w = neighbours[j];\n\t\t\tif (w === undefined) continue;\n\n\t\t\t// Decide whether to include this pair based on remaining budget\n\t\t\tsampled++;\n\n\t\t\t// Check if u and w are connected\n\t\t\tif (\n\t\t\t\tgraph.getEdge(u, w) !== undefined ||\n\t\t\t\tgraph.getEdge(w, u) !== undefined\n\t\t\t) {\n\t\t\t\ttriangleCount++;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Extrapolate from sample\n\treturn triangleCount / sampled;\n}\n\n/**\n * Compute clustering coefficients for multiple nodes efficiently.\n *\n * Reuses neighbour sets to avoid repeated iteration.\n *\n * @param graph - The graph to compute on\n * @param nodeIds - The nodes to compute clustering coefficients for\n * @returns Map from nodeId to clustering coefficient\n */\nexport function batchClusteringCoefficients(\n\tgraph: ReadableGraph,\n\tnodeIds: readonly NodeId[],\n): Map<NodeId, number> {\n\tconst results = new Map<NodeId, number>();\n\n\tfor (const nodeId of nodeIds) {\n\t\tresults.set(nodeId, localClusteringCoefficient(graph, nodeId));\n\t}\n\n\treturn results;\n}\n","/**\n * Neighbourhood computation utilities.\n *\n * Shared utilities for neighbourhood operations used by MI variants and other graph algorithms.\n * These functions eliminate duplication of neighbourhood set operations across multiple\n * implementations.\n *\n * @packageDocumentation\n */\n\nimport type { NodeId, NodeData, EdgeData, ReadableGraph } from \"../graph\";\n\n/**\n * Collect neighbours into a Set, optionally excluding a specific node.\n *\n * @param graph - The graph to traverse\n * @param nodeId - The source node\n * @param exclude - Optional node ID to exclude from result\n * @returns A ReadonlySet of neighbouring node IDs\n */\nexport function neighbourSet<N extends NodeData, E extends EdgeData>(\n\tgraph: ReadableGraph<N, E>,\n\tnodeId: NodeId,\n\texclude?: NodeId,\n): ReadonlySet<NodeId> {\n\tconst neighbours = new Set(graph.neighbours(nodeId));\n\tif (exclude !== undefined) {\n\t\tneighbours.delete(exclude);\n\t}\n\treturn neighbours;\n}\n\n/**\n * Compute intersection and union sizes of two neighbour sets without allocating the union set.\n *\n * This is more efficient than computing both separately, as it avoids creating a full union Set.\n *\n * @param a - First neighbourhood set\n * @param b - Second neighbourhood set\n * @returns Object with intersection and union sizes\n */\nexport function neighbourOverlap(\n\ta: ReadonlySet<NodeId>,\n\tb: ReadonlySet<NodeId>,\n): { intersection: number; union: number } {\n\tlet intersection = 0;\n\n\t// Count intersection by iterating through the smaller set\n\tconst [smaller, larger] = a.size < b.size ? [a, b] : [b, a];\n\n\tfor (const node of smaller) {\n\t\tif (larger.has(node)) {\n\t\t\tintersection++;\n\t\t}\n\t}\n\n\t// Union size = size(a) + size(b) - intersection\n\tconst union = a.size + b.size - intersection;\n\n\treturn { intersection, union };\n}\n\n/**\n * Return the actual intersection set of two neighbourhood sets.\n *\n * Needed by Adamic-Adar (iterates common neighbours) and ETCH (requires edge types of intersection edges).\n *\n * @param a - First neighbourhood set\n * @param b - Second neighbourhood set\n * @returns A ReadonlySet containing nodes in both a and b\n */\nexport function neighbourIntersection(\n\ta: ReadonlySet<NodeId>,\n\tb: ReadonlySet<NodeId>,\n): ReadonlySet<NodeId> {\n\tconst intersection = new Set<NodeId>();\n\n\t// Iterate through the smaller set for efficiency\n\tconst [smaller, larger] = a.size < b.size ? [a, b] : [b, a];\n\n\tfor (const node of smaller) {\n\t\tif (larger.has(node)) {\n\t\t\tintersection.add(node);\n\t\t}\n\t}\n\n\treturn intersection;\n}\n\n/**\n * Count the number of edges with a specific type in the graph.\n *\n * Used by ETCH MI variant to compute edge rarity weighting.\n *\n * @param graph - The graph to count edges in\n * @param type - The edge type to count\n * @returns The number of edges with the specified type\n */\nexport function countEdgesOfType<N extends NodeData, E extends EdgeData>(\n\tgraph: ReadableGraph<N, E>,\n\ttype: string,\n): number {\n\tlet count = 0;\n\tfor (const edge of graph.edges()) {\n\t\tif (edge.type === type) {\n\t\t\tcount++;\n\t\t}\n\t}\n\treturn count;\n}\n\n/**\n * Count the number of nodes with a specific type in the graph.\n *\n * Used by NOTCH MI variant to compute node rarity weighting.\n *\n * @param graph - The graph to count nodes in\n * @param type - The node type to count\n * @returns The number of nodes with the specified type\n */\nexport function countNodesOfType<N extends NodeData, E extends EdgeData>(\n\tgraph: ReadableGraph<N, E>,\n\ttype: string,\n): number {\n\tlet count = 0;\n\tfor (const nodeId of graph.nodeIds()) {\n\t\tconst node = graph.getNode(nodeId);\n\t\tif (node?.type === type) {\n\t\t\tcount++;\n\t\t}\n\t}\n\treturn count;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,eAAe,eAA0C;AACxE,KAAI,cAAc,WAAW,EAC5B,QAAO;CAGR,IAAI,UAAU;AACd,MAAK,MAAM,KAAK,cAEf,KAAI,IAAI,EACP,YAAW,IAAI,KAAK,KAAK,EAAE;AAI7B,QAAO;;;;;;;;;;;;;;;AAgBR,SAAgB,kBAAkB,eAA0C;AAC3E,KAAI,cAAc,UAAU,EAC3B,QAAO;CAGR,MAAM,IAAI,eAAe,cAAc;CACvC,MAAM,OAAO,KAAK,KAAK,cAAc,OAAO;AAE5C,KAAI,SAAS,EACZ,QAAO;AAGR,QAAO,IAAI;;;;;;;;;;;;AAaZ,SAAgB,kBAAkB,QAAmC;AACpE,KAAI,OAAO,WAAW,EACrB,QAAO;CAGR,MAAM,QAAQ,OAAO,QAAQ,KAAK,MAAM,MAAM,GAAG,EAAE;AACnD,KAAI,UAAU,EACb,QAAO;AAIR,QAAO,eADe,OAAO,KAAK,MAAM,IAAI,MAAM,CACd;;;;;;;;;;;;AAarC,SAAgB,iBAAiB,gBAA2C;AAC3E,KAAI,eAAe,UAAU,EAC5B,QAAO;CAIR,MAAM,6BAAa,IAAI,KAAqB;AAC5C,MAAK,MAAM,KAAK,eACf,YAAW,IAAI,IAAI,WAAW,IAAI,EAAE,IAAI,KAAK,EAAE;AAIhD,KAAI,WAAW,SAAS,EACvB,QAAO;CAIR,MAAM,gBAA0B,EAAE;CAClC,MAAM,QAAQ,eAAe;AAC7B,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