graph-typed 1.20.0 → 1.21.2
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 +128 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -21,12 +21,138 @@ Undirected Graph
|
|
|
21
21
|

|
|
22
22
|
### snippet
|
|
23
23
|
#### TS
|
|
24
|
+
##### DirectedGraph
|
|
24
25
|
```typescript
|
|
25
|
-
|
|
26
|
+
import {DirectedGraph, DirectedVertex, DirectedEdge} from 'data-structure-typed';
|
|
27
|
+
// /* or if you prefer */ import {DirectedGraph, DirectedVertex, DirectedEdge} from 'graph-typed';
|
|
28
|
+
|
|
29
|
+
const graph = new DirectedGraph();
|
|
30
|
+
const vertexA = new DirectedVertex('A');
|
|
31
|
+
const vertexB = new DirectedVertex('B');
|
|
32
|
+
const vertexC = new DirectedVertex('C');
|
|
33
|
+
const edgeAB = new DirectedEdge('A', 'B');
|
|
34
|
+
const edgeBC = new DirectedEdge('B', 'C');
|
|
35
|
+
|
|
36
|
+
graph.addVertex(vertexA);
|
|
37
|
+
graph.addVertex(vertexB);
|
|
38
|
+
graph.addVertex(vertexC);
|
|
39
|
+
graph.addEdge(edgeAB);
|
|
40
|
+
graph.addEdge(edgeBC);
|
|
41
|
+
|
|
42
|
+
const topologicalOrder = graph.topologicalSort();
|
|
43
|
+
if (topologicalOrder) expect(topologicalOrder).toEqual(['A', 'B', 'C'])
|
|
44
|
+
```
|
|
45
|
+
##### MapGraph
|
|
46
|
+
```typescript
|
|
47
|
+
import {MapGraph, MapVertex} from 'data-structure-typed';
|
|
48
|
+
// /* or if you prefer */ import {MapGraph, MapVertex} from 'graph-typed';
|
|
49
|
+
|
|
50
|
+
const mapGraph = new MapGraph([5.500338, 100.173665]);
|
|
51
|
+
|
|
52
|
+
mapGraph.addVertex(new MapVertex('Surin', 5.466724, 100.274805));
|
|
53
|
+
mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', 5.475141, 100.276670));
|
|
54
|
+
mapGraph.addVertex(new MapVertex('Lotus', 5.459044, 100.308767));
|
|
55
|
+
mapGraph.addVertex(new MapVertex('The Breeza', 5.454197, 100.307859));
|
|
56
|
+
mapGraph.addVertex(new MapVertex('Hard Rock Hotel', 5.467850, 100.241876));
|
|
57
|
+
mapGraph.addVertex(new MapVertex('Mira', 5.456749, 100.286650));
|
|
58
|
+
mapGraph.addVertex(new MapVertex('Penang Bible Church', 5.428683, 100.314825));
|
|
59
|
+
mapGraph.addVertex(new MapVertex('Queensbay', 5.332760, 100.306651));
|
|
60
|
+
mapGraph.addVertex(new MapVertex('Saanen Goat Farm', 5.405738, 100.207699));
|
|
61
|
+
mapGraph.addVertex(new MapVertex('Trinity Auto', 5.401126, 100.303739));
|
|
62
|
+
mapGraph.addVertex(new MapVertex('Penang Airport', 5.293185, 100.265772));
|
|
63
|
+
mapGraph.addEdge('Surin', 'Lotus', 4.7);
|
|
64
|
+
mapGraph.addEdge('Lotus', 'The Breeza', 1);
|
|
65
|
+
mapGraph.addEdge('Batu Feringgi Beach', 'Hard Rock Hotel', 5.2);
|
|
66
|
+
mapGraph.addEdge('Surin', 'Mira', 2.8);
|
|
67
|
+
mapGraph.addEdge('Mira', 'Penang Bible Church', 7.0);
|
|
68
|
+
mapGraph.addEdge('Lotus', 'Penang Bible Church', 5.7);
|
|
69
|
+
mapGraph.addEdge('Penang Bible Church', 'Queensbay', 13.9);
|
|
70
|
+
mapGraph.addEdge('Hard Rock Hotel', 'Saanen Goat Farm', 18.5);
|
|
71
|
+
mapGraph.addEdge('The Breeza', 'Trinity Auto', 9.1);
|
|
72
|
+
mapGraph.addEdge('Trinity Auto', 'Saanen Goat Farm', 26.3);
|
|
73
|
+
mapGraph.addEdge('The Breeza', 'Penang Airport', 24.8);
|
|
74
|
+
mapGraph.addEdge('Penang Airport', 'Saanen Goat Farm', 21.2);
|
|
75
|
+
const expected1 = ['Surin', 'Lotus', 'The Breeza', 'Trinity Auto', 'Saanen Goat Farm'];
|
|
76
|
+
|
|
77
|
+
const minPathBetween = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm');
|
|
78
|
+
expect(minPathBetween?.map(v => v.id)).toEqual(expected1);
|
|
79
|
+
const surinToSaanenGoatFarmDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true);
|
|
80
|
+
expect(surinToSaanenGoatFarmDij?.minPath.map(v => v.id)).toEqual(expected1);
|
|
81
|
+
expect(surinToSaanenGoatFarmDij?.minDist).toBe(41.1);
|
|
82
|
+
mapGraph.addEdge('Surin', 'Batu Feringgi Beach', 1.5);
|
|
83
|
+
const expected2 = ['Surin', 'Batu Feringgi Beach', 'Hard Rock Hotel', 'Saanen Goat Farm'];
|
|
84
|
+
const minPathBetweenViaBFB = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm', true);
|
|
85
|
+
expect(minPathBetweenViaBFB?.map(v => v.id)).toEqual(expected2);
|
|
86
|
+
const surinToSaanenGoatFarmViaDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true);
|
|
87
|
+
expect(surinToSaanenGoatFarmViaDij?.minPath.map(v => v.id)).toEqual(expected2);
|
|
88
|
+
expect(surinToSaanenGoatFarmViaDij?.minDist).toBe(25.2);
|
|
26
89
|
```
|
|
27
90
|
#### JS
|
|
91
|
+
##### DirectedGraph
|
|
92
|
+
```typescript
|
|
93
|
+
const {DirectedGraph, DirectedVertex, DirectedEdge} = require('data-structure-typed');
|
|
94
|
+
// /* or if you prefer */ const {DirectedGraph, DirectedVertex, DirectedEdge} = require('graph-typed');
|
|
95
|
+
|
|
96
|
+
const graph = new DirectedGraph();
|
|
97
|
+
const vertexA = new DirectedVertex('A');
|
|
98
|
+
const vertexB = new DirectedVertex('B');
|
|
99
|
+
const vertexC = new DirectedVertex('C');
|
|
100
|
+
const edgeAB = new DirectedEdge('A', 'B');
|
|
101
|
+
const edgeBC = new DirectedEdge('B', 'C');
|
|
102
|
+
|
|
103
|
+
graph.addVertex(vertexA);
|
|
104
|
+
graph.addVertex(vertexB);
|
|
105
|
+
graph.addVertex(vertexC);
|
|
106
|
+
graph.addEdge(edgeAB);
|
|
107
|
+
graph.addEdge(edgeBC);
|
|
108
|
+
|
|
109
|
+
const topologicalOrder = graph.topologicalSort();
|
|
110
|
+
if (topologicalOrder) expect(topologicalOrder).toEqual(['A', 'B', 'C'])
|
|
111
|
+
```
|
|
112
|
+
##### MapGraph
|
|
28
113
|
```javascript
|
|
29
|
-
|
|
114
|
+
const {MapGraph, MapVertex} = require('data-structure-typed');
|
|
115
|
+
// /* or if you prefer */ const {MapGraph, MapVertex} = require('graph-typed');
|
|
116
|
+
|
|
117
|
+
const mapGraph = new MapGraph([5.500338, 100.173665]);
|
|
118
|
+
|
|
119
|
+
mapGraph.addVertex(new MapVertex('Surin', 5.466724, 100.274805));
|
|
120
|
+
mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', 5.475141, 100.276670));
|
|
121
|
+
mapGraph.addVertex(new MapVertex('Lotus', 5.459044, 100.308767));
|
|
122
|
+
mapGraph.addVertex(new MapVertex('The Breeza', 5.454197, 100.307859));
|
|
123
|
+
mapGraph.addVertex(new MapVertex('Hard Rock Hotel', 5.467850, 100.241876));
|
|
124
|
+
mapGraph.addVertex(new MapVertex('Mira', 5.456749, 100.286650));
|
|
125
|
+
mapGraph.addVertex(new MapVertex('Penang Bible Church', 5.428683, 100.314825));
|
|
126
|
+
mapGraph.addVertex(new MapVertex('Queensbay', 5.332760, 100.306651));
|
|
127
|
+
mapGraph.addVertex(new MapVertex('Saanen Goat Farm', 5.405738, 100.207699));
|
|
128
|
+
mapGraph.addVertex(new MapVertex('Trinity Auto', 5.401126, 100.303739));
|
|
129
|
+
mapGraph.addVertex(new MapVertex('Penang Airport', 5.293185, 100.265772));
|
|
130
|
+
mapGraph.addEdge('Surin', 'Lotus', 4.7);
|
|
131
|
+
mapGraph.addEdge('Lotus', 'The Breeza', 1);
|
|
132
|
+
mapGraph.addEdge('Batu Feringgi Beach', 'Hard Rock Hotel', 5.2);
|
|
133
|
+
mapGraph.addEdge('Surin', 'Mira', 2.8);
|
|
134
|
+
mapGraph.addEdge('Mira', 'Penang Bible Church', 7.0);
|
|
135
|
+
mapGraph.addEdge('Lotus', 'Penang Bible Church', 5.7);
|
|
136
|
+
mapGraph.addEdge('Penang Bible Church', 'Queensbay', 13.9);
|
|
137
|
+
mapGraph.addEdge('Hard Rock Hotel', 'Saanen Goat Farm', 18.5);
|
|
138
|
+
mapGraph.addEdge('The Breeza', 'Trinity Auto', 9.1);
|
|
139
|
+
mapGraph.addEdge('Trinity Auto', 'Saanen Goat Farm', 26.3);
|
|
140
|
+
mapGraph.addEdge('The Breeza', 'Penang Airport', 24.8);
|
|
141
|
+
mapGraph.addEdge('Penang Airport', 'Saanen Goat Farm', 21.2);
|
|
142
|
+
const expected1 = ['Surin', 'Lotus', 'The Breeza', 'Trinity Auto', 'Saanen Goat Farm'];
|
|
143
|
+
|
|
144
|
+
const minPathBetween = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm');
|
|
145
|
+
expect(minPathBetween?.map(v => v.id)).toEqual(expected1);
|
|
146
|
+
const surinToSaanenGoatFarmDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true);
|
|
147
|
+
expect(surinToSaanenGoatFarmDij?.minPath.map(v => v.id)).toEqual(expected1);
|
|
148
|
+
expect(surinToSaanenGoatFarmDij?.minDist).toBe(41.1);
|
|
149
|
+
mapGraph.addEdge('Surin', 'Batu Feringgi Beach', 1.5);
|
|
150
|
+
const expected2 = ['Surin', 'Batu Feringgi Beach', 'Hard Rock Hotel', 'Saanen Goat Farm'];
|
|
151
|
+
const minPathBetweenViaBFB = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm', true);
|
|
152
|
+
expect(minPathBetweenViaBFB?.map(v => v.id)).toEqual(expected2);
|
|
153
|
+
const surinToSaanenGoatFarmViaDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true);
|
|
154
|
+
expect(surinToSaanenGoatFarmViaDij?.minPath.map(v => v.id)).toEqual(expected2);
|
|
155
|
+
expect(surinToSaanenGoatFarmViaDij?.minDist).toBe(25.2);
|
|
30
156
|
```
|
|
31
157
|
|
|
32
158
|
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,4 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
export { AbstractVertex, AbstractEdge, AbstractGraph, DirectedVertex, DirectedEdge, DirectedGraph, UndirectedVertex, UndirectedEdge, UndirectedGraph } from 'data-structure-typed';
|
|
8
|
+
export { AbstractVertex, AbstractEdge, AbstractGraph, DirectedVertex, DirectedEdge, DirectedGraph, UndirectedVertex, UndirectedEdge, UndirectedGraph, MapGraph, MapVertex, MapEdge } from 'data-structure-typed';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UndirectedGraph = exports.UndirectedEdge = exports.UndirectedVertex = exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
3
|
+
exports.MapEdge = exports.MapVertex = exports.MapGraph = exports.UndirectedGraph = exports.UndirectedEdge = exports.UndirectedVertex = exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* data-structure-typed
|
|
6
6
|
*
|
|
@@ -18,3 +18,6 @@ Object.defineProperty(exports, "DirectedGraph", { enumerable: true, get: functio
|
|
|
18
18
|
Object.defineProperty(exports, "UndirectedVertex", { enumerable: true, get: function () { return data_structure_typed_1.UndirectedVertex; } });
|
|
19
19
|
Object.defineProperty(exports, "UndirectedEdge", { enumerable: true, get: function () { return data_structure_typed_1.UndirectedEdge; } });
|
|
20
20
|
Object.defineProperty(exports, "UndirectedGraph", { enumerable: true, get: function () { return data_structure_typed_1.UndirectedGraph; } });
|
|
21
|
+
Object.defineProperty(exports, "MapGraph", { enumerable: true, get: function () { return data_structure_typed_1.MapGraph; } });
|
|
22
|
+
Object.defineProperty(exports, "MapVertex", { enumerable: true, get: function () { return data_structure_typed_1.MapVertex; } });
|
|
23
|
+
Object.defineProperty(exports, "MapEdge", { enumerable: true, get: function () { return data_structure_typed_1.MapEdge; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graph-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.21.2",
|
|
4
4
|
"description": "Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"typescript": "^4.9.5"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"data-structure-typed": "^1.
|
|
65
|
+
"data-structure-typed": "^1.21.2",
|
|
66
66
|
"zod": "^3.22.2"
|
|
67
67
|
}
|
|
68
68
|
}
|