@thi.ng/adjacency 2.4.1 → 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/CHANGELOG.md +13 -1
- package/README.md +79 -0
- package/binary.js +1 -1
- package/package.json +13 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2023-10-
|
|
3
|
+
- **Last updated**: 2023-10-23T07:37:37Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,18 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [2.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/adjacency@2.5.0) (2023-10-19)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- explicit version bump for @firfi's recent additions ([#400](https://github.com/thi-ng/umbrella/issues/400)) ([0d00025](https://github.com/thi-ng/umbrella/commit/0d00025))
|
|
17
|
+
- see: [2fd123d741586fe29a8cc63b7aa30f3ea9d35ab2](https://github.com/thi-ng/umbrella/commit/2fd123d741586fe29a8cc63b7aa30f3ea9d35ab2)
|
|
18
|
+
- update readme with API examples
|
|
19
|
+
|
|
20
|
+
#### 🩹 Bug fixes
|
|
21
|
+
|
|
22
|
+
- fix AdjacencyBitMatrix.numVertices() ([bd034ab](https://github.com/thi-ng/umbrella/commit/bd034ab))
|
|
23
|
+
|
|
12
24
|
## [2.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/adjacency@2.4.0) (2023-10-18)
|
|
13
25
|
|
|
14
26
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ This project is part of the
|
|
|
19
19
|
- [Dependencies](#dependencies)
|
|
20
20
|
- [Usage examples](#usage-examples)
|
|
21
21
|
- [API](#api)
|
|
22
|
+
- [Basic usage](#basic-usage)
|
|
22
23
|
- [Authors](#authors)
|
|
23
24
|
- [License](#license)
|
|
24
25
|
|
|
@@ -102,6 +103,84 @@ A selection:
|
|
|
102
103
|
|
|
103
104
|
TODO
|
|
104
105
|
|
|
106
|
+
### Basic usage
|
|
107
|
+
|
|
108
|
+
```ts tangle:export/readme.ts
|
|
109
|
+
import { defAdjBitMatrix, type Edge } from "@thi.ng/adjacency";
|
|
110
|
+
|
|
111
|
+
// relationships
|
|
112
|
+
const rels = {
|
|
113
|
+
a: ["b", "c"],
|
|
114
|
+
b: ["d"],
|
|
115
|
+
c: ["d", "e"],
|
|
116
|
+
e: ["a", "d", "b"],
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// form set of unique node IDs
|
|
120
|
+
const nodeIDs = [
|
|
121
|
+
...new Set(Object.entries(rels).flatMap(([id, rels]) => [id, ...rels])),
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
// the current adjacency matrix impls only support numeric node IDs
|
|
125
|
+
// therefore, we first map node names to numeric IDs
|
|
126
|
+
const index = new Map(nodeIDs.map((id, i) => [id, i]));
|
|
127
|
+
|
|
128
|
+
// transform relationships into sequence of edges (aka `[from,to]` tuples)
|
|
129
|
+
const edges = Object.entries(rels).flatMap(([id, rels]) =>
|
|
130
|
+
rels.map((x) => <Edge>[index.get(id), index.get(x)])
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
// build adjacency matrix, treat as undirected graph
|
|
134
|
+
// edges can also be added/removed later
|
|
135
|
+
const graph = defAdjBitMatrix(nodeIDs.length, edges, true);
|
|
136
|
+
|
|
137
|
+
// graph queries
|
|
138
|
+
console.log("edges:", graph.numEdges(), "verts:", graph.numVertices());
|
|
139
|
+
// edges: 8 verts: 5
|
|
140
|
+
|
|
141
|
+
// check if vertex/node is present in graph
|
|
142
|
+
// (this is implementation specific and for the bitmatrix backed version here
|
|
143
|
+
// only true if the vertex has at least 1 edge...)
|
|
144
|
+
console.log(graph.hasVertex(index.get("d")!));
|
|
145
|
+
// true
|
|
146
|
+
|
|
147
|
+
// are `a` and `d` connected?
|
|
148
|
+
console.log(graph.hasEdge(index.get("a")!, index.get("d")!));
|
|
149
|
+
// false
|
|
150
|
+
|
|
151
|
+
// number of connected nodes for `a`
|
|
152
|
+
// (in directed graphs, there's also possibility to distinguish between in/out/inout)
|
|
153
|
+
console.log(graph.degree(index.get("a")!));
|
|
154
|
+
// 3
|
|
155
|
+
|
|
156
|
+
// neighbors of `a` (with reverse lookup of node names)
|
|
157
|
+
console.log(graph.neighbors(index.get("a")!).map((x) => nodeIDs[x]));
|
|
158
|
+
// [ 'b', 'c', 'e' ]
|
|
159
|
+
|
|
160
|
+
// serialize to GraphViz DOT format (see result visualization below)
|
|
161
|
+
console.log(graph.toDot(nodeIDs));
|
|
162
|
+
// graph g {
|
|
163
|
+
// "d"--"e";
|
|
164
|
+
// "c"--"d";
|
|
165
|
+
// "c"--"e";
|
|
166
|
+
// "b"--"d";
|
|
167
|
+
// "b"--"e";
|
|
168
|
+
// "a"--"b";
|
|
169
|
+
// "a"--"c";
|
|
170
|
+
// "a"--"e";
|
|
171
|
+
// }
|
|
172
|
+
|
|
173
|
+
// resize to new capacity & add add/remove vertices/edges
|
|
174
|
+
graph.resize(10);
|
|
175
|
+
|
|
176
|
+
graph.addEdge(4, 5);
|
|
177
|
+
graph.removeEdge(0, 1);
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
GraphViz visualization of the above example graph:
|
|
181
|
+
|
|
182
|
+

|
|
183
|
+
|
|
105
184
|
## Authors
|
|
106
185
|
|
|
107
186
|
- [Karsten Schmidt](https://thi.ng) (Main author)
|
package/binary.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/adjacency",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "Sparse & bitwise adjacency matrices, lists and selected traversal algorithms for directed & undirected graphs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -37,20 +37,20 @@
|
|
|
37
37
|
"test": "testament test"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@thi.ng/api": "^8.9.
|
|
41
|
-
"@thi.ng/arrays": "^2.6.
|
|
42
|
-
"@thi.ng/bitfield": "^2.3.
|
|
43
|
-
"@thi.ng/dcons": "^3.2.
|
|
44
|
-
"@thi.ng/errors": "^2.3.
|
|
45
|
-
"@thi.ng/sparse": "^0.3.
|
|
40
|
+
"@thi.ng/api": "^8.9.6",
|
|
41
|
+
"@thi.ng/arrays": "^2.6.4",
|
|
42
|
+
"@thi.ng/bitfield": "^2.3.1",
|
|
43
|
+
"@thi.ng/dcons": "^3.2.65",
|
|
44
|
+
"@thi.ng/errors": "^2.3.6",
|
|
45
|
+
"@thi.ng/sparse": "^0.3.70"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@microsoft/api-extractor": "^7.
|
|
49
|
-
"@thi.ng/testament": "^0.3.
|
|
50
|
-
"@thi.ng/vectors": "^7.7.
|
|
51
|
-
"rimraf": "^5.0.
|
|
48
|
+
"@microsoft/api-extractor": "^7.38.0",
|
|
49
|
+
"@thi.ng/testament": "^0.3.24",
|
|
50
|
+
"@thi.ng/vectors": "^7.7.20",
|
|
51
|
+
"rimraf": "^5.0.5",
|
|
52
52
|
"tools": "^0.0.1",
|
|
53
|
-
"typedoc": "^0.25.
|
|
53
|
+
"typedoc": "^0.25.2",
|
|
54
54
|
"typescript": "^5.2.2"
|
|
55
55
|
},
|
|
56
56
|
"keywords": [
|
|
@@ -125,5 +125,5 @@
|
|
|
125
125
|
],
|
|
126
126
|
"year": 2018
|
|
127
127
|
},
|
|
128
|
-
"gitHead": "
|
|
128
|
+
"gitHead": "8d46d9326a9f9b81d65e7e274446f5964f9942ac\n"
|
|
129
129
|
}
|