max-priority-queue-typed 2.4.5 → 2.5.1
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.
- package/README.md +63 -0
- package/dist/cjs/index.cjs +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/max-priority-queue-typed.js +691 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -77,65 +77,6 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
77
77
|
* console.log(neighborsA[0].key); // 'B';
|
|
78
78
|
* console.log(neighborsA[1].key); // 'C';
|
|
79
79
|
* @example
|
|
80
|
-
* // UndirectedGraph deleteEdge and vertex operations
|
|
81
|
-
* const graph = new UndirectedGraph<string>();
|
|
82
|
-
*
|
|
83
|
-
* // Build a simple undirected graph
|
|
84
|
-
* graph.addVertex('X');
|
|
85
|
-
* graph.addVertex('Y');
|
|
86
|
-
* graph.addVertex('Z');
|
|
87
|
-
* graph.addEdge('X', 'Y', 1);
|
|
88
|
-
* graph.addEdge('Y', 'Z', 2);
|
|
89
|
-
* graph.addEdge('X', 'Z', 3);
|
|
90
|
-
*
|
|
91
|
-
* // Delete an edge
|
|
92
|
-
* graph.deleteEdge('X', 'Y');
|
|
93
|
-
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
94
|
-
*
|
|
95
|
-
* // Bidirectional deletion confirmed
|
|
96
|
-
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
97
|
-
*
|
|
98
|
-
* // Other edges should remain
|
|
99
|
-
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
100
|
-
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
101
|
-
*
|
|
102
|
-
* // Delete a vertex
|
|
103
|
-
* graph.deleteVertex('Y');
|
|
104
|
-
* console.log(graph.hasVertex('Y')); // false;
|
|
105
|
-
* console.log(graph.size); // 2;
|
|
106
|
-
* @example
|
|
107
|
-
* // UndirectedGraph connectivity and neighbors
|
|
108
|
-
* const graph = new UndirectedGraph<string>();
|
|
109
|
-
*
|
|
110
|
-
* // Build a friendship network
|
|
111
|
-
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
112
|
-
* for (const person of people) {
|
|
113
|
-
* graph.addVertex(person);
|
|
114
|
-
* }
|
|
115
|
-
*
|
|
116
|
-
* // Add friendships (undirected edges)
|
|
117
|
-
* graph.addEdge('Alice', 'Bob', 1);
|
|
118
|
-
* graph.addEdge('Alice', 'Charlie', 1);
|
|
119
|
-
* graph.addEdge('Bob', 'Diana', 1);
|
|
120
|
-
* graph.addEdge('Charlie', 'Eve', 1);
|
|
121
|
-
* graph.addEdge('Diana', 'Eve', 1);
|
|
122
|
-
*
|
|
123
|
-
* // Get friends of each person
|
|
124
|
-
* const aliceFriends = graph.getNeighbors('Alice');
|
|
125
|
-
* console.log(aliceFriends[0].key); // 'Bob';
|
|
126
|
-
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
127
|
-
* console.log(aliceFriends.length); // 2;
|
|
128
|
-
*
|
|
129
|
-
* const dianaFriends = graph.getNeighbors('Diana');
|
|
130
|
-
* console.log(dianaFriends[0].key); // 'Bob';
|
|
131
|
-
* console.log(dianaFriends[1].key); // 'Eve';
|
|
132
|
-
* console.log(dianaFriends.length); // 2;
|
|
133
|
-
*
|
|
134
|
-
* // Verify bidirectional friendship
|
|
135
|
-
* const bobFriends = graph.getNeighbors('Bob');
|
|
136
|
-
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
137
|
-
* console.log(bobFriends[1].key); // 'Diana';
|
|
138
|
-
* @example
|
|
139
80
|
* // UndirectedGraph for social network connectivity analysis
|
|
140
81
|
* interface Person {
|
|
141
82
|
* id: number;
|
|
@@ -285,6 +226,42 @@ export class UndirectedGraph<
|
|
|
285
226
|
* @param v2 - The other vertex or key.
|
|
286
227
|
* @returns Edge instance or `undefined`.
|
|
287
228
|
* @remarks Time O(1) avg, Space O(1)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
* @example
|
|
259
|
+
* // Get edge between vertices
|
|
260
|
+
* const g = new UndirectedGraph();
|
|
261
|
+
* g.addVertex('A');
|
|
262
|
+
* g.addVertex('B');
|
|
263
|
+
* g.addEdge('A', 'B', 7);
|
|
264
|
+
* console.log(g.getEdge('A', 'B')?.weight); // 7;
|
|
288
265
|
*/
|
|
289
266
|
getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined {
|
|
290
267
|
let edgeMap: EO[] | undefined = [];
|
|
@@ -334,6 +311,65 @@ export class UndirectedGraph<
|
|
|
334
311
|
* @param otherSideVertexKey - Required second endpoint when deleting by pair.
|
|
335
312
|
* @returns Removed edge or `undefined`.
|
|
336
313
|
* @remarks Time O(1) avg, Space O(1)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
* @example
|
|
347
|
+
* // UndirectedGraph deleteEdge and vertex operations
|
|
348
|
+
* const graph = new UndirectedGraph<string>();
|
|
349
|
+
*
|
|
350
|
+
* // Build a simple undirected graph
|
|
351
|
+
* graph.addVertex('X');
|
|
352
|
+
* graph.addVertex('Y');
|
|
353
|
+
* graph.addVertex('Z');
|
|
354
|
+
* graph.addEdge('X', 'Y', 1);
|
|
355
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
356
|
+
* graph.addEdge('X', 'Z', 3);
|
|
357
|
+
*
|
|
358
|
+
* // Delete an edge
|
|
359
|
+
* graph.deleteEdge('X', 'Y');
|
|
360
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
361
|
+
*
|
|
362
|
+
* // Bidirectional deletion confirmed
|
|
363
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
364
|
+
*
|
|
365
|
+
* // Other edges should remain
|
|
366
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
367
|
+
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
368
|
+
*
|
|
369
|
+
* // Delete a vertex
|
|
370
|
+
* graph.deleteVertex('Y');
|
|
371
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
372
|
+
* console.log(graph.size); // 2;
|
|
337
373
|
*/
|
|
338
374
|
deleteEdge(edgeOrOneSideVertexKey: EO | VertexKey, otherSideVertexKey?: VertexKey): EO | undefined {
|
|
339
375
|
let oneSide: VO | undefined, otherSide: VO | undefined;
|
|
@@ -361,6 +397,43 @@ export class UndirectedGraph<
|
|
|
361
397
|
* @param vertexOrKey - Vertex or key.
|
|
362
398
|
* @returns `true` if removed; otherwise `false`.
|
|
363
399
|
* @remarks Time O(deg), Space O(1)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
* @example
|
|
430
|
+
* // Remove vertex and edges
|
|
431
|
+
* const g = new UndirectedGraph();
|
|
432
|
+
* g.addVertex('A');
|
|
433
|
+
* g.addVertex('B');
|
|
434
|
+
* g.addEdge('A', 'B');
|
|
435
|
+
* g.deleteVertex('A');
|
|
436
|
+
* console.log(g.hasVertex('A')); // false;
|
|
364
437
|
*/
|
|
365
438
|
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
366
439
|
let vertexKey: VertexKey;
|
|
@@ -431,6 +504,42 @@ export class UndirectedGraph<
|
|
|
431
504
|
* Unique set of undirected edges across endpoints.
|
|
432
505
|
* @returns Array of edges.
|
|
433
506
|
* @remarks Time O(E), Space O(E)
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
* @example
|
|
537
|
+
* // Get all edges
|
|
538
|
+
* const g = new UndirectedGraph();
|
|
539
|
+
* g.addVertex('A');
|
|
540
|
+
* g.addVertex('B');
|
|
541
|
+
* g.addEdge('A', 'B');
|
|
542
|
+
* console.log(g.edgeSet().length); // 1;
|
|
434
543
|
*/
|
|
435
544
|
edgeSet(): EO[] {
|
|
436
545
|
const edgeSet: Set<EO> = new Set();
|
|
@@ -442,6 +551,73 @@ export class UndirectedGraph<
|
|
|
442
551
|
return [...edgeSet];
|
|
443
552
|
}
|
|
444
553
|
|
|
554
|
+
/**
|
|
555
|
+
* UndirectedGraph connectivity and neighbors
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
* @example
|
|
589
|
+
* // UndirectedGraph connectivity and neighbors
|
|
590
|
+
* const graph = new UndirectedGraph<string>();
|
|
591
|
+
*
|
|
592
|
+
* // Build a friendship network
|
|
593
|
+
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
594
|
+
* for (const person of people) {
|
|
595
|
+
* graph.addVertex(person);
|
|
596
|
+
* }
|
|
597
|
+
*
|
|
598
|
+
* // Add friendships (undirected edges)
|
|
599
|
+
* graph.addEdge('Alice', 'Bob', 1);
|
|
600
|
+
* graph.addEdge('Alice', 'Charlie', 1);
|
|
601
|
+
* graph.addEdge('Bob', 'Diana', 1);
|
|
602
|
+
* graph.addEdge('Charlie', 'Eve', 1);
|
|
603
|
+
* graph.addEdge('Diana', 'Eve', 1);
|
|
604
|
+
*
|
|
605
|
+
* // Get friends of each person
|
|
606
|
+
* const aliceFriends = graph.getNeighbors('Alice');
|
|
607
|
+
* console.log(aliceFriends[0].key); // 'Bob';
|
|
608
|
+
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
609
|
+
* console.log(aliceFriends.length); // 2;
|
|
610
|
+
*
|
|
611
|
+
* const dianaFriends = graph.getNeighbors('Diana');
|
|
612
|
+
* console.log(dianaFriends[0].key); // 'Bob';
|
|
613
|
+
* console.log(dianaFriends[1].key); // 'Eve';
|
|
614
|
+
* console.log(dianaFriends.length); // 2;
|
|
615
|
+
*
|
|
616
|
+
* // Verify bidirectional friendship
|
|
617
|
+
* const bobFriends = graph.getNeighbors('Bob');
|
|
618
|
+
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
619
|
+
* console.log(bobFriends[1].key); // 'Diana';
|
|
620
|
+
*/
|
|
445
621
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
|
|
446
622
|
const neighbors: VO[] = [];
|
|
447
623
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -506,6 +682,45 @@ export class UndirectedGraph<
|
|
|
506
682
|
* Tarjan-based bridge and articulation point detection.
|
|
507
683
|
* @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
|
|
508
684
|
* @remarks Time O(V + E), Space O(V + E)
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
* @example
|
|
715
|
+
* // Find articulation points and bridges
|
|
716
|
+
* const g = new UndirectedGraph();
|
|
717
|
+
* g.addVertex('A');
|
|
718
|
+
* g.addVertex('B');
|
|
719
|
+
* g.addVertex('C');
|
|
720
|
+
* g.addEdge('A', 'B');
|
|
721
|
+
* g.addEdge('B', 'C');
|
|
722
|
+
* const result = g.tarjan();
|
|
723
|
+
* console.log(result); // defined;
|
|
509
724
|
*/
|
|
510
725
|
tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; bridges: EO[]; cutVertices: VO[] } {
|
|
511
726
|
const dfnMap = new Map<VO, number>();
|
|
@@ -637,6 +852,46 @@ export class UndirectedGraph<
|
|
|
637
852
|
* Uses DFS with parent tracking.
|
|
638
853
|
* @returns `true` if a cycle exists, `false` otherwise.
|
|
639
854
|
* @remarks Time O(V + E), Space O(V)
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
* @example
|
|
885
|
+
* // Detect cycle
|
|
886
|
+
* const g = new UndirectedGraph();
|
|
887
|
+
* g.addVertex('A');
|
|
888
|
+
* g.addVertex('B');
|
|
889
|
+
* g.addVertex('C');
|
|
890
|
+
* g.addEdge('A', 'B');
|
|
891
|
+
* g.addEdge('B', 'C');
|
|
892
|
+
* console.log(g.hasCycle()); // false;
|
|
893
|
+
* g.addEdge('C', 'A');
|
|
894
|
+
* console.log(g.hasCycle()); // true;
|
|
640
895
|
*/
|
|
641
896
|
hasCycle(): boolean {
|
|
642
897
|
const visited = new Set<VO>();
|
|
@@ -665,6 +920,45 @@ export class UndirectedGraph<
|
|
|
665
920
|
* Get bridges discovered by `tarjan()`.
|
|
666
921
|
* @returns Array of edges that are bridges.
|
|
667
922
|
* @remarks Time O(B), Space O(1)
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
* @example
|
|
953
|
+
* // Find bridge edges
|
|
954
|
+
* const g = new UndirectedGraph();
|
|
955
|
+
* g.addVertex('A');
|
|
956
|
+
* g.addVertex('B');
|
|
957
|
+
* g.addVertex('C');
|
|
958
|
+
* g.addEdge('A', 'B');
|
|
959
|
+
* g.addEdge('B', 'C');
|
|
960
|
+
* const bridges = g.getBridges();
|
|
961
|
+
* console.log(bridges.length); // 2;
|
|
668
962
|
*/
|
|
669
963
|
getBridges() {
|
|
670
964
|
return this.tarjan().bridges;
|
|
@@ -674,6 +968,46 @@ export class UndirectedGraph<
|
|
|
674
968
|
* Get articulation points discovered by `tarjan()`.
|
|
675
969
|
* @returns Array of cut vertices.
|
|
676
970
|
* @remarks Time O(C), Space O(1)
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
* @example
|
|
1001
|
+
* // Find articulation points
|
|
1002
|
+
* const g = new UndirectedGraph();
|
|
1003
|
+
* g.addVertex('A');
|
|
1004
|
+
* g.addVertex('B');
|
|
1005
|
+
* g.addVertex('C');
|
|
1006
|
+
* g.addEdge('A', 'B');
|
|
1007
|
+
* g.addEdge('B', 'C');
|
|
1008
|
+
* const cuts = g.getCutVertices();
|
|
1009
|
+
* console.log(cuts.length); // 1;
|
|
1010
|
+
* console.log(cuts[0].key); // 'B';
|
|
677
1011
|
*/
|
|
678
1012
|
getCutVertices() {
|
|
679
1013
|
return this.tarjan().cutVertices;
|