undirected-graph-typed 1.49.2 → 1.49.4
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/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/data-structures/graph/abstract-graph.d.ts +8 -17
- package/dist/data-structures/graph/abstract-graph.js +43 -29
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +6 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +43 -43
- package/dist/data-structures/linked-list/doubly-linked-list.js +49 -49
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/data-structures/queue/queue.d.ts +33 -33
- package/dist/data-structures/queue/queue.js +40 -40
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/graph/abstract-graph.ts +56 -27
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +53 -53
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/queue.ts +45 -45
|
@@ -33,15 +33,13 @@ export declare class SkipList<K, V> {
|
|
|
33
33
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
34
34
|
*/
|
|
35
35
|
/**
|
|
36
|
-
* Time Complexity: O(
|
|
36
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
37
37
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
38
38
|
*
|
|
39
|
-
*
|
|
40
|
-
* @
|
|
41
|
-
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
|
|
42
|
-
* List.
|
|
39
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
40
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
43
41
|
*/
|
|
44
|
-
|
|
42
|
+
get first(): V | undefined;
|
|
45
43
|
/**
|
|
46
44
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
47
45
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -50,12 +48,10 @@ export declare class SkipList<K, V> {
|
|
|
50
48
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
51
49
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
52
50
|
*
|
|
53
|
-
*
|
|
54
|
-
* @
|
|
55
|
-
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
|
|
56
|
-
* otherwise it returns `undefined`.
|
|
51
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
52
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
57
53
|
*/
|
|
58
|
-
get(
|
|
54
|
+
get last(): V | undefined;
|
|
59
55
|
/**
|
|
60
56
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
61
57
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -63,8 +59,13 @@ export declare class SkipList<K, V> {
|
|
|
63
59
|
/**
|
|
64
60
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
65
61
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
62
|
+
*
|
|
63
|
+
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
64
|
+
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
65
|
+
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
|
|
66
|
+
* List.
|
|
66
67
|
*/
|
|
67
|
-
|
|
68
|
+
add(key: K, value: V): void;
|
|
68
69
|
/**
|
|
69
70
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
70
71
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -73,24 +74,21 @@ export declare class SkipList<K, V> {
|
|
|
73
74
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
74
75
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
75
76
|
*
|
|
76
|
-
* The `
|
|
77
|
-
* @param {K} key - The key parameter
|
|
78
|
-
* @returns The `
|
|
79
|
-
*
|
|
77
|
+
* The function `get` retrieves the value associated with a given key from a skip list data structure.
|
|
78
|
+
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
|
|
79
|
+
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
|
|
80
|
+
* otherwise it returns `undefined`.
|
|
80
81
|
*/
|
|
81
|
-
|
|
82
|
+
get(key: K): V | undefined;
|
|
82
83
|
/**
|
|
83
84
|
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
84
85
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
85
86
|
*/
|
|
86
87
|
/**
|
|
87
|
-
* Time Complexity: O(
|
|
88
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
88
89
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
89
|
-
*
|
|
90
|
-
* Get the value of the first element (the smallest element) in the Skip List.
|
|
91
|
-
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
92
90
|
*/
|
|
93
|
-
|
|
91
|
+
has(key: K): boolean;
|
|
94
92
|
/**
|
|
95
93
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
96
94
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -99,10 +97,12 @@ export declare class SkipList<K, V> {
|
|
|
99
97
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
100
98
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
101
99
|
*
|
|
102
|
-
*
|
|
103
|
-
* @
|
|
100
|
+
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
101
|
+
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
102
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
103
|
+
* skip list, and `false` if the key was not found in the skip list.
|
|
104
104
|
*/
|
|
105
|
-
|
|
105
|
+
delete(key: K): boolean;
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
108
108
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -42,6 +42,41 @@ class SkipList {
|
|
|
42
42
|
get probability() {
|
|
43
43
|
return this._probability;
|
|
44
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
47
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
48
|
+
*/
|
|
49
|
+
/**
|
|
50
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
51
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
52
|
+
*
|
|
53
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
54
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
55
|
+
*/
|
|
56
|
+
get first() {
|
|
57
|
+
const firstNode = this.head.forward[0];
|
|
58
|
+
return firstNode ? firstNode.value : undefined;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
62
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
63
|
+
*/
|
|
64
|
+
/**
|
|
65
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
66
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
67
|
+
*
|
|
68
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
69
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
70
|
+
*/
|
|
71
|
+
get last() {
|
|
72
|
+
let current = this.head;
|
|
73
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
74
|
+
while (current.forward[i]) {
|
|
75
|
+
current = current.forward[i];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return current.value;
|
|
79
|
+
}
|
|
45
80
|
/**
|
|
46
81
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
47
82
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -100,7 +135,7 @@ class SkipList {
|
|
|
100
135
|
return undefined;
|
|
101
136
|
}
|
|
102
137
|
/**
|
|
103
|
-
* Time Complexity: O(
|
|
138
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
104
139
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
105
140
|
*/
|
|
106
141
|
/**
|
|
@@ -147,41 +182,6 @@ class SkipList {
|
|
|
147
182
|
}
|
|
148
183
|
return false;
|
|
149
184
|
}
|
|
150
|
-
/**
|
|
151
|
-
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
152
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
153
|
-
*/
|
|
154
|
-
/**
|
|
155
|
-
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
156
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
157
|
-
*
|
|
158
|
-
* Get the value of the first element (the smallest element) in the Skip List.
|
|
159
|
-
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
160
|
-
*/
|
|
161
|
-
get first() {
|
|
162
|
-
const firstNode = this.head.forward[0];
|
|
163
|
-
return firstNode ? firstNode.value : undefined;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
167
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
168
|
-
*/
|
|
169
|
-
/**
|
|
170
|
-
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
171
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
172
|
-
*
|
|
173
|
-
* Get the value of the last element (the largest element) in the Skip List.
|
|
174
|
-
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
175
|
-
*/
|
|
176
|
-
get last() {
|
|
177
|
-
let current = this.head;
|
|
178
|
-
for (let i = this.level - 1; i >= 0; i--) {
|
|
179
|
-
while (current.forward[i]) {
|
|
180
|
-
current = current.forward[i];
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return current.value;
|
|
184
|
-
}
|
|
185
185
|
/**
|
|
186
186
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
187
187
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -32,6 +32,32 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
32
32
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
33
33
|
*/
|
|
34
34
|
get size(): number;
|
|
35
|
+
/**
|
|
36
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
37
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
38
|
+
*
|
|
39
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
40
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
41
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
42
|
+
*/
|
|
43
|
+
get first(): E | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
46
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
47
|
+
*/
|
|
48
|
+
/**
|
|
49
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
50
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
51
|
+
*
|
|
52
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
53
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
54
|
+
* array is empty, it returns `undefined`.
|
|
55
|
+
*/
|
|
56
|
+
get last(): E | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
59
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
60
|
+
*/
|
|
35
61
|
/**
|
|
36
62
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
37
63
|
* @public
|
|
@@ -42,7 +68,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
42
68
|
*/
|
|
43
69
|
static fromArray<E>(elements: E[]): Queue<E>;
|
|
44
70
|
/**
|
|
45
|
-
* Time Complexity: O(1) - constant time as it
|
|
71
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
46
72
|
* Space Complexity: O(1) - no additional space is used.
|
|
47
73
|
*/
|
|
48
74
|
/**
|
|
@@ -55,7 +81,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
55
81
|
*/
|
|
56
82
|
push(element: E): boolean;
|
|
57
83
|
/**
|
|
58
|
-
* Time Complexity: O(
|
|
84
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
59
85
|
* Space Complexity: O(1) - no additional space is used.
|
|
60
86
|
*/
|
|
61
87
|
/**
|
|
@@ -67,19 +93,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
67
93
|
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
68
94
|
*/
|
|
69
95
|
shift(): E | undefined;
|
|
70
|
-
/**
|
|
71
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
72
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
73
|
-
*/
|
|
74
|
-
/**
|
|
75
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
76
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
77
|
-
*
|
|
78
|
-
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
79
|
-
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
80
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
81
|
-
*/
|
|
82
|
-
get first(): E | undefined;
|
|
83
96
|
/**
|
|
84
97
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
85
98
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -93,19 +106,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
93
106
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
94
107
|
*/
|
|
95
108
|
peek(): E | undefined;
|
|
96
|
-
/**
|
|
97
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
98
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
99
|
-
*/
|
|
100
|
-
/**
|
|
101
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
102
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
103
|
-
*
|
|
104
|
-
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
105
|
-
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
106
|
-
* array is empty, it returns `undefined`.
|
|
107
|
-
*/
|
|
108
|
-
get last(): E | undefined;
|
|
109
109
|
/**
|
|
110
110
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
111
111
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -247,6 +247,11 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
247
247
|
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
248
248
|
*/
|
|
249
249
|
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
250
|
+
/**
|
|
251
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
252
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
253
|
+
*/
|
|
254
|
+
get first(): E | undefined;
|
|
250
255
|
/**
|
|
251
256
|
* The enqueue function adds a value to the end of an array.
|
|
252
257
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -257,11 +262,6 @@ export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
257
262
|
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
258
263
|
*/
|
|
259
264
|
dequeue(): E | undefined;
|
|
260
|
-
/**
|
|
261
|
-
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
262
|
-
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
263
|
-
*/
|
|
264
|
-
get first(): E | undefined;
|
|
265
265
|
/**
|
|
266
266
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
267
267
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
@@ -37,6 +37,36 @@ class Queue extends base_1.IterableElementBase {
|
|
|
37
37
|
get size() {
|
|
38
38
|
return this.nodes.length - this.offset;
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
42
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
43
|
+
*
|
|
44
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
45
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
46
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
47
|
+
*/
|
|
48
|
+
get first() {
|
|
49
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
53
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
54
|
+
*/
|
|
55
|
+
/**
|
|
56
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
57
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
58
|
+
*
|
|
59
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
60
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
61
|
+
* array is empty, it returns `undefined`.
|
|
62
|
+
*/
|
|
63
|
+
get last() {
|
|
64
|
+
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
68
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
69
|
+
*/
|
|
40
70
|
/**
|
|
41
71
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
42
72
|
* @public
|
|
@@ -49,7 +79,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
49
79
|
return new Queue(elements);
|
|
50
80
|
}
|
|
51
81
|
/**
|
|
52
|
-
* Time Complexity: O(1) - constant time as it
|
|
82
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
53
83
|
* Space Complexity: O(1) - no additional space is used.
|
|
54
84
|
*/
|
|
55
85
|
/**
|
|
@@ -65,7 +95,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
65
95
|
return true;
|
|
66
96
|
}
|
|
67
97
|
/**
|
|
68
|
-
* Time Complexity: O(
|
|
98
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
69
99
|
* Space Complexity: O(1) - no additional space is used.
|
|
70
100
|
*/
|
|
71
101
|
/**
|
|
@@ -89,21 +119,6 @@ class Queue extends base_1.IterableElementBase {
|
|
|
89
119
|
this._offset = 0;
|
|
90
120
|
return first;
|
|
91
121
|
}
|
|
92
|
-
/**
|
|
93
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
94
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
95
|
-
*/
|
|
96
|
-
/**
|
|
97
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
98
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
99
|
-
*
|
|
100
|
-
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
101
|
-
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
102
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
103
|
-
*/
|
|
104
|
-
get first() {
|
|
105
|
-
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
106
|
-
}
|
|
107
122
|
/**
|
|
108
123
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
109
124
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -119,21 +134,6 @@ class Queue extends base_1.IterableElementBase {
|
|
|
119
134
|
peek() {
|
|
120
135
|
return this.first;
|
|
121
136
|
}
|
|
122
|
-
/**
|
|
123
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
124
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
125
|
-
*/
|
|
126
|
-
/**
|
|
127
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
128
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
129
|
-
*
|
|
130
|
-
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
131
|
-
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
132
|
-
* array is empty, it returns `undefined`.
|
|
133
|
-
*/
|
|
134
|
-
get last() {
|
|
135
|
-
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
136
|
-
}
|
|
137
137
|
/**
|
|
138
138
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
139
139
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -315,6 +315,14 @@ exports.Queue = Queue;
|
|
|
315
315
|
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
316
316
|
*/
|
|
317
317
|
class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
318
|
+
/**
|
|
319
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
320
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
321
|
+
*/
|
|
322
|
+
get first() {
|
|
323
|
+
var _a;
|
|
324
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
325
|
+
}
|
|
318
326
|
/**
|
|
319
327
|
* The enqueue function adds a value to the end of an array.
|
|
320
328
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -329,14 +337,6 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
|
329
337
|
dequeue() {
|
|
330
338
|
return this.shift();
|
|
331
339
|
}
|
|
332
|
-
/**
|
|
333
|
-
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
334
|
-
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
335
|
-
*/
|
|
336
|
-
get first() {
|
|
337
|
-
var _a;
|
|
338
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
339
|
-
}
|
|
340
340
|
/**
|
|
341
341
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
342
342
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "undirected-graph-typed",
|
|
3
|
-
"version": "1.49.
|
|
3
|
+
"version": "1.49.4",
|
|
4
4
|
"description": "Undirected Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -145,6 +145,6 @@
|
|
|
145
145
|
"typescript": "^4.9.5"
|
|
146
146
|
},
|
|
147
147
|
"dependencies": {
|
|
148
|
-
"data-structure-typed": "^1.49.
|
|
148
|
+
"data-structure-typed": "^1.49.4"
|
|
149
149
|
}
|
|
150
150
|
}
|
|
@@ -1957,7 +1957,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1957
1957
|
}
|
|
1958
1958
|
}
|
|
1959
1959
|
|
|
1960
|
-
protected _defaultOneParamCallback = (node: N) => node.key;
|
|
1960
|
+
protected _defaultOneParamCallback = (node: N | null | undefined) => node ? node.key : undefined;
|
|
1961
1961
|
|
|
1962
1962
|
/**
|
|
1963
1963
|
* Swap the data of two nodes in the binary tree.
|
|
@@ -156,10 +156,10 @@ export abstract class AbstractGraph<
|
|
|
156
156
|
|
|
157
157
|
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
|
|
158
158
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
159
|
-
return this.
|
|
159
|
+
return this._addVertex(keyOrVertex);
|
|
160
160
|
} else {
|
|
161
161
|
const newVertex = this.createVertex(keyOrVertex, value);
|
|
162
|
-
return this.
|
|
162
|
+
return this._addVertex(newVertex);
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
|
|
@@ -173,19 +173,7 @@ export abstract class AbstractGraph<
|
|
|
173
173
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
174
174
|
*/
|
|
175
175
|
|
|
176
|
-
|
|
177
|
-
* Time Complexity: O(1) - Constant time for Map operations.
|
|
178
|
-
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
179
|
-
*
|
|
180
|
-
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
181
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
182
|
-
* (`VertexKey`).
|
|
183
|
-
* @returns The method is returning a boolean value.
|
|
184
|
-
*/
|
|
185
|
-
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
186
|
-
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
187
|
-
return this._vertexMap.delete(vertexKey);
|
|
188
|
-
}
|
|
176
|
+
abstract deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
189
177
|
|
|
190
178
|
/**
|
|
191
179
|
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
|
|
@@ -242,14 +230,14 @@ export abstract class AbstractGraph<
|
|
|
242
230
|
|
|
243
231
|
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
|
|
244
232
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
245
|
-
return this.
|
|
233
|
+
return this._addEdge(srcOrEdge);
|
|
246
234
|
} else {
|
|
247
235
|
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
248
236
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
|
249
237
|
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
|
250
238
|
if (dest instanceof AbstractVertex) dest = dest.key;
|
|
251
239
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
252
|
-
return this.
|
|
240
|
+
return this._addEdge(newEdge);
|
|
253
241
|
} else {
|
|
254
242
|
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
255
243
|
}
|
|
@@ -1147,14 +1135,6 @@ export abstract class AbstractGraph<
|
|
|
1147
1135
|
return this.tarjan(false, false, false, false).lowMap;
|
|
1148
1136
|
}
|
|
1149
1137
|
|
|
1150
|
-
/**
|
|
1151
|
-
* The function `getCycles` returns a map of cycles found using the Tarjan algorithm.
|
|
1152
|
-
* @returns The function `getCycles()` is returning a `Map<number, VO[]>`.
|
|
1153
|
-
*/
|
|
1154
|
-
getCycles(): Map<number, VO[]> {
|
|
1155
|
-
return this.tarjan(false, false, false, true).cycles;
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
1138
|
/**
|
|
1159
1139
|
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
1160
1140
|
* @returns an array of VO objects, specifically the cut vertexes.
|
|
@@ -1180,6 +1160,55 @@ export abstract class AbstractGraph<
|
|
|
1180
1160
|
return this.tarjan(false, true, false, false).bridges;
|
|
1181
1161
|
}
|
|
1182
1162
|
|
|
1163
|
+
/**
|
|
1164
|
+
* O(V+E+C)
|
|
1165
|
+
* O(V+C)
|
|
1166
|
+
*/
|
|
1167
|
+
getCycles(isInclude2Cycle: boolean = false): VertexKey[][] {
|
|
1168
|
+
const cycles: VertexKey[][] = [];
|
|
1169
|
+
const visited: Set<VO> = new Set();
|
|
1170
|
+
|
|
1171
|
+
const dfs = (vertex: VO, currentPath: VertexKey[], visited: Set<VO>) => {
|
|
1172
|
+
if (visited.has(vertex)) {
|
|
1173
|
+
if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
|
|
1174
|
+
cycles.push([...currentPath]);
|
|
1175
|
+
}
|
|
1176
|
+
return;
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
visited.add(vertex);
|
|
1180
|
+
currentPath.push(vertex.key);
|
|
1181
|
+
|
|
1182
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
1183
|
+
neighbor && dfs(neighbor, currentPath, visited);
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
visited.delete(vertex);
|
|
1187
|
+
currentPath.pop();
|
|
1188
|
+
};
|
|
1189
|
+
|
|
1190
|
+
for (const vertex of this.vertexMap.values()) {
|
|
1191
|
+
dfs(vertex, [], visited);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
// Use a set to eliminate duplicate cycles
|
|
1195
|
+
const uniqueCycles = new Map<string, VertexKey[]>();
|
|
1196
|
+
|
|
1197
|
+
for (const cycle of cycles) {
|
|
1198
|
+
const sorted = [...cycle].sort().toString()
|
|
1199
|
+
|
|
1200
|
+
if (uniqueCycles.has(sorted)) continue
|
|
1201
|
+
else {
|
|
1202
|
+
uniqueCycles.set(sorted, cycle)
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// Convert the unique cycles back to an array
|
|
1207
|
+
return [...uniqueCycles].map(cycleString =>
|
|
1208
|
+
cycleString[1]
|
|
1209
|
+
);
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1183
1212
|
/**
|
|
1184
1213
|
* Time Complexity: O(n)
|
|
1185
1214
|
* Space Complexity: O(n)
|
|
@@ -1247,9 +1276,9 @@ export abstract class AbstractGraph<
|
|
|
1247
1276
|
}
|
|
1248
1277
|
}
|
|
1249
1278
|
|
|
1250
|
-
protected abstract
|
|
1279
|
+
protected abstract _addEdge(edge: EO): boolean;
|
|
1251
1280
|
|
|
1252
|
-
protected
|
|
1281
|
+
protected _addVertex(newVertex: VO): boolean {
|
|
1253
1282
|
if (this.hasVertex(newVertex)) {
|
|
1254
1283
|
return false;
|
|
1255
1284
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
@@ -240,7 +240,7 @@ export class DirectedGraph<
|
|
|
240
240
|
* (`VertexKey`).
|
|
241
241
|
* @returns The method is returning a boolean value.
|
|
242
242
|
*/
|
|
243
|
-
|
|
243
|
+
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
244
244
|
let vertexKey: VertexKey;
|
|
245
245
|
let vertex: VO | undefined;
|
|
246
246
|
if (this.isVertexKey(vertexOrKey)) {
|
|
@@ -252,8 +252,12 @@ export class DirectedGraph<
|
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
if (vertex) {
|
|
255
|
-
this.
|
|
256
|
-
|
|
255
|
+
const neighbors = this.getNeighbors(vertex);
|
|
256
|
+
for (const neighbor of neighbors) {
|
|
257
|
+
this._inEdgeMap.delete(neighbor);
|
|
258
|
+
}
|
|
259
|
+
this._outEdgeMap.delete(vertex);
|
|
260
|
+
this._inEdgeMap.delete(vertex);
|
|
257
261
|
}
|
|
258
262
|
|
|
259
263
|
return this._vertexMap.delete(vertexKey);
|
|
@@ -596,6 +600,7 @@ export class DirectedGraph<
|
|
|
596
600
|
}
|
|
597
601
|
}
|
|
598
602
|
|
|
603
|
+
|
|
599
604
|
/**
|
|
600
605
|
* Time Complexity: O(1)
|
|
601
606
|
* Space Complexity: O(1)
|
|
@@ -605,13 +610,13 @@ export class DirectedGraph<
|
|
|
605
610
|
* Time Complexity: O(1)
|
|
606
611
|
* Space Complexity: O(1)
|
|
607
612
|
*
|
|
608
|
-
* The function `
|
|
613
|
+
* The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
|
|
609
614
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
610
615
|
* needs to be added to the graph.
|
|
611
616
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
612
617
|
* source or destination vertex does not exist in the graph.
|
|
613
618
|
*/
|
|
614
|
-
protected
|
|
619
|
+
protected _addEdge(edge: EO): boolean {
|
|
615
620
|
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
616
621
|
return false;
|
|
617
622
|
}
|
|
@@ -212,7 +212,7 @@ export class UndirectedGraph<
|
|
|
212
212
|
* (`VertexKey`).
|
|
213
213
|
* @returns The method is returning a boolean value.
|
|
214
214
|
*/
|
|
215
|
-
|
|
215
|
+
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
216
216
|
let vertexKey: VertexKey;
|
|
217
217
|
let vertex: VO | undefined;
|
|
218
218
|
if (this.isVertexKey(vertexOrKey)) {
|
|
@@ -381,7 +381,7 @@ export class UndirectedGraph<
|
|
|
381
381
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
382
382
|
* @returns a boolean value.
|
|
383
383
|
*/
|
|
384
|
-
protected
|
|
384
|
+
protected _addEdge(edge: EO): boolean {
|
|
385
385
|
for (const end of edge.vertexMap) {
|
|
386
386
|
const endVertex = this._getVertex(end);
|
|
387
387
|
if (endVertex === undefined) return false;
|