undirected-graph-typed 2.2.2 → 2.2.3
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 +169 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/umd/undirected-graph-typed.js.map +1 -1
- package/dist/umd/undirected-graph-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +96 -2
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +322 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
package/README.md
CHANGED
|
@@ -35,6 +35,175 @@ yarn add undirected-graph-typed
|
|
|
35
35
|
|
|
36
36
|
[//]: # (No deletion!!! Start of Example Replace Section)
|
|
37
37
|
|
|
38
|
+
### basic UndirectedGraph vertex and edge creation
|
|
39
|
+
```typescript
|
|
40
|
+
// Create a simple undirected graph
|
|
41
|
+
const graph = new UndirectedGraph<string>();
|
|
42
|
+
|
|
43
|
+
// Add vertices
|
|
44
|
+
graph.addVertex('A');
|
|
45
|
+
graph.addVertex('B');
|
|
46
|
+
graph.addVertex('C');
|
|
47
|
+
graph.addVertex('D');
|
|
48
|
+
|
|
49
|
+
// Verify vertices exist
|
|
50
|
+
console.log(graph.hasVertex('A')); // true;
|
|
51
|
+
console.log(graph.hasVertex('B')); // true;
|
|
52
|
+
console.log(graph.hasVertex('E')); // false;
|
|
53
|
+
|
|
54
|
+
// Check vertex count
|
|
55
|
+
console.log(graph.size); // 4;
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### UndirectedGraph edge operations (bidirectional)
|
|
59
|
+
```typescript
|
|
60
|
+
const graph = new UndirectedGraph<string>();
|
|
61
|
+
|
|
62
|
+
// Add vertices
|
|
63
|
+
graph.addVertex('A');
|
|
64
|
+
graph.addVertex('B');
|
|
65
|
+
graph.addVertex('C');
|
|
66
|
+
|
|
67
|
+
// Add undirected edges (both directions automatically)
|
|
68
|
+
graph.addEdge('A', 'B', 1);
|
|
69
|
+
graph.addEdge('B', 'C', 2);
|
|
70
|
+
graph.addEdge('A', 'C', 3);
|
|
71
|
+
|
|
72
|
+
// Verify edges exist in both directions
|
|
73
|
+
console.log(graph.hasEdge('A', 'B')); // true;
|
|
74
|
+
console.log(graph.hasEdge('B', 'A')); // true; // Bidirectional!
|
|
75
|
+
|
|
76
|
+
console.log(graph.hasEdge('C', 'B')); // true;
|
|
77
|
+
console.log(graph.hasEdge('B', 'C')); // true; // Bidirectional!
|
|
78
|
+
|
|
79
|
+
// Get neighbors of A
|
|
80
|
+
const neighborsA = graph.getNeighbors('A');
|
|
81
|
+
console.log(neighborsA[0].key); // 'B';
|
|
82
|
+
console.log(neighborsA[1].key); // 'C';
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### UndirectedGraph deleteEdge and vertex operations
|
|
86
|
+
```typescript
|
|
87
|
+
const graph = new UndirectedGraph<string>();
|
|
88
|
+
|
|
89
|
+
// Build a simple undirected graph
|
|
90
|
+
graph.addVertex('X');
|
|
91
|
+
graph.addVertex('Y');
|
|
92
|
+
graph.addVertex('Z');
|
|
93
|
+
graph.addEdge('X', 'Y', 1);
|
|
94
|
+
graph.addEdge('Y', 'Z', 2);
|
|
95
|
+
graph.addEdge('X', 'Z', 3);
|
|
96
|
+
|
|
97
|
+
// Delete an edge
|
|
98
|
+
graph.deleteEdge('X', 'Y');
|
|
99
|
+
console.log(graph.hasEdge('X', 'Y')); // false;
|
|
100
|
+
|
|
101
|
+
// Bidirectional deletion confirmed
|
|
102
|
+
console.log(graph.hasEdge('Y', 'X')); // false;
|
|
103
|
+
|
|
104
|
+
// Other edges should remain
|
|
105
|
+
console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
106
|
+
console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
107
|
+
|
|
108
|
+
// Delete a vertex
|
|
109
|
+
graph.deleteVertex('Y');
|
|
110
|
+
console.log(graph.hasVertex('Y')); // false;
|
|
111
|
+
console.log(graph.size); // 2;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### UndirectedGraph connectivity and neighbors
|
|
115
|
+
```typescript
|
|
116
|
+
const graph = new UndirectedGraph<string>();
|
|
117
|
+
|
|
118
|
+
// Build a friendship network
|
|
119
|
+
const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
120
|
+
for (const person of people) {
|
|
121
|
+
graph.addVertex(person);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Add friendships (undirected edges)
|
|
125
|
+
graph.addEdge('Alice', 'Bob', 1);
|
|
126
|
+
graph.addEdge('Alice', 'Charlie', 1);
|
|
127
|
+
graph.addEdge('Bob', 'Diana', 1);
|
|
128
|
+
graph.addEdge('Charlie', 'Eve', 1);
|
|
129
|
+
graph.addEdge('Diana', 'Eve', 1);
|
|
130
|
+
|
|
131
|
+
// Get friends of each person
|
|
132
|
+
const aliceFriends = graph.getNeighbors('Alice');
|
|
133
|
+
console.log(aliceFriends[0].key); // 'Bob';
|
|
134
|
+
console.log(aliceFriends[1].key); // 'Charlie';
|
|
135
|
+
console.log(aliceFriends.length); // 2;
|
|
136
|
+
|
|
137
|
+
const dianaFriends = graph.getNeighbors('Diana');
|
|
138
|
+
console.log(dianaFriends[0].key); // 'Bob';
|
|
139
|
+
console.log(dianaFriends[1].key); // 'Eve';
|
|
140
|
+
console.log(dianaFriends.length); // 2;
|
|
141
|
+
|
|
142
|
+
// Verify bidirectional friendship
|
|
143
|
+
const bobFriends = graph.getNeighbors('Bob');
|
|
144
|
+
console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
145
|
+
console.log(bobFriends[1].key); // 'Diana';
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### UndirectedGraph for social network connectivity analysis
|
|
149
|
+
```typescript
|
|
150
|
+
interface Person {
|
|
151
|
+
id: number;
|
|
152
|
+
name: string;
|
|
153
|
+
location: string;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// UndirectedGraph is perfect for modeling symmetric relationships
|
|
157
|
+
// (friendships, collaborations, partnerships)
|
|
158
|
+
const socialNetwork = new UndirectedGraph<number, Person>();
|
|
159
|
+
|
|
160
|
+
// Add people as vertices
|
|
161
|
+
const people: [number, Person][] = [
|
|
162
|
+
[1, { id: 1, name: 'Alice', location: 'New York' }],
|
|
163
|
+
[2, { id: 2, name: 'Bob', location: 'San Francisco' }],
|
|
164
|
+
[3, { id: 3, name: 'Charlie', location: 'Boston' }],
|
|
165
|
+
[4, { id: 4, name: 'Diana', location: 'New York' }],
|
|
166
|
+
[5, { id: 5, name: 'Eve', location: 'Seattle' }]
|
|
167
|
+
];
|
|
168
|
+
|
|
169
|
+
for (const [id] of people) {
|
|
170
|
+
socialNetwork.addVertex(id);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Add friendships (automatically bidirectional)
|
|
174
|
+
socialNetwork.addEdge(1, 2, 1); // Alice <-> Bob
|
|
175
|
+
socialNetwork.addEdge(1, 3, 1); // Alice <-> Charlie
|
|
176
|
+
socialNetwork.addEdge(2, 4, 1); // Bob <-> Diana
|
|
177
|
+
socialNetwork.addEdge(3, 5, 1); // Charlie <-> Eve
|
|
178
|
+
socialNetwork.addEdge(4, 5, 1); // Diana <-> Eve
|
|
179
|
+
|
|
180
|
+
console.log(socialNetwork.size); // 5;
|
|
181
|
+
|
|
182
|
+
// Find direct connections for Alice
|
|
183
|
+
const aliceConnections = socialNetwork.getNeighbors(1);
|
|
184
|
+
console.log(aliceConnections[0].key); // 2;
|
|
185
|
+
console.log(aliceConnections[1].key); // 3;
|
|
186
|
+
console.log(aliceConnections.length); // 2;
|
|
187
|
+
|
|
188
|
+
// Verify bidirectional connections
|
|
189
|
+
console.log(socialNetwork.hasEdge(1, 2)); // true;
|
|
190
|
+
console.log(socialNetwork.hasEdge(2, 1)); // true; // Friendship works both ways!
|
|
191
|
+
|
|
192
|
+
// Remove a person from network
|
|
193
|
+
socialNetwork.deleteVertex(2); // Bob leaves
|
|
194
|
+
console.log(socialNetwork.hasVertex(2)); // false;
|
|
195
|
+
console.log(socialNetwork.size); // 4;
|
|
196
|
+
|
|
197
|
+
// Alice loses Bob as a friend
|
|
198
|
+
const updatedAliceConnections = socialNetwork.getNeighbors(1);
|
|
199
|
+
console.log(updatedAliceConnections[0].key); // 3;
|
|
200
|
+
console.log(updatedAliceConnections[1]); // undefined;
|
|
201
|
+
|
|
202
|
+
// Diana loses Bob as a friend
|
|
203
|
+
const dianaConnections = socialNetwork.getNeighbors(4);
|
|
204
|
+
console.log(dianaConnections[0].key); // 5;
|
|
205
|
+
console.log(dianaConnections[1]); // undefined;
|
|
206
|
+
```
|
|
38
207
|
|
|
39
208
|
[//]: # (No deletion!!! End of Example Replace Section)
|
|
40
209
|
|