@graphty/algorithms 1.0.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/LICENSE +21 -0
- package/README.md +193 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts +34 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/betweenness.js +223 -0
- package/dist/src/algorithms/centrality/betweenness.js.map +1 -0
- package/dist/src/algorithms/centrality/closeness.d.ts +43 -0
- package/dist/src/algorithms/centrality/closeness.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/closeness.js +202 -0
- package/dist/src/algorithms/centrality/closeness.js.map +1 -0
- package/dist/src/algorithms/centrality/degree.d.ts +17 -0
- package/dist/src/algorithms/centrality/degree.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/degree.js +86 -0
- package/dist/src/algorithms/centrality/degree.js.map +1 -0
- package/dist/src/algorithms/centrality/index.d.ts +11 -0
- package/dist/src/algorithms/centrality/index.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/index.js +8 -0
- package/dist/src/algorithms/centrality/index.js.map +1 -0
- package/dist/src/algorithms/centrality/pagerank.d.ts +74 -0
- package/dist/src/algorithms/centrality/pagerank.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/pagerank.js +191 -0
- package/dist/src/algorithms/centrality/pagerank.js.map +1 -0
- package/dist/src/algorithms/components/connected.d.ts +58 -0
- package/dist/src/algorithms/components/connected.d.ts.map +1 -0
- package/dist/src/algorithms/components/connected.js +234 -0
- package/dist/src/algorithms/components/connected.js.map +1 -0
- package/dist/src/algorithms/components/index.d.ts +5 -0
- package/dist/src/algorithms/components/index.d.ts.map +1 -0
- package/dist/src/algorithms/components/index.js +5 -0
- package/dist/src/algorithms/components/index.js.map +1 -0
- package/dist/src/algorithms/index.d.ts +8 -0
- package/dist/src/algorithms/index.d.ts.map +1 -0
- package/dist/src/algorithms/index.js +12 -0
- package/dist/src/algorithms/index.js.map +1 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts +52 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.js +131 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -0
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts +26 -0
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path/dijkstra.js +199 -0
- package/dist/src/algorithms/shortest-path/dijkstra.js.map +1 -0
- package/dist/src/algorithms/shortest-path/index.d.ts +7 -0
- package/dist/src/algorithms/shortest-path/index.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path/index.js +6 -0
- package/dist/src/algorithms/shortest-path/index.js.map +1 -0
- package/dist/src/algorithms/traversal/bfs.d.ts +25 -0
- package/dist/src/algorithms/traversal/bfs.d.ts.map +1 -0
- package/dist/src/algorithms/traversal/bfs.js +189 -0
- package/dist/src/algorithms/traversal/bfs.js.map +1 -0
- package/dist/src/algorithms/traversal/dfs.d.ts +32 -0
- package/dist/src/algorithms/traversal/dfs.d.ts.map +1 -0
- package/dist/src/algorithms/traversal/dfs.js +259 -0
- package/dist/src/algorithms/traversal/dfs.js.map +1 -0
- package/dist/src/algorithms/traversal/index.d.ts +7 -0
- package/dist/src/algorithms/traversal/index.d.ts.map +1 -0
- package/dist/src/algorithms/traversal/index.js +6 -0
- package/dist/src/algorithms/traversal/index.js.map +1 -0
- package/dist/src/core/graph.d.ts +100 -0
- package/dist/src/core/graph.d.ts.map +1 -0
- package/dist/src/core/graph.js +276 -0
- package/dist/src/core/graph.js.map +1 -0
- package/dist/src/data-structures/index.d.ts +6 -0
- package/dist/src/data-structures/index.d.ts.map +1 -0
- package/dist/src/data-structures/index.js +6 -0
- package/dist/src/data-structures/index.js.map +1 -0
- package/dist/src/data-structures/priority-queue.d.ts +60 -0
- package/dist/src/data-structures/priority-queue.d.ts.map +1 -0
- package/dist/src/data-structures/priority-queue.js +152 -0
- package/dist/src/data-structures/priority-queue.js.map +1 -0
- package/dist/src/data-structures/union-find.d.ts +54 -0
- package/dist/src/data-structures/union-find.d.ts.map +1 -0
- package/dist/src/data-structures/union-find.js +145 -0
- package/dist/src/data-structures/union-find.js.map +1 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +13 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/types/index.d.ts +74 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +5 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/test/browser/basic.test.d.ts +2 -0
- package/dist/test/browser/basic.test.d.ts.map +1 -0
- package/dist/test/browser/basic.test.js +70 -0
- package/dist/test/browser/basic.test.js.map +1 -0
- package/dist/test/unit/bellman-ford.test.d.ts +2 -0
- package/dist/test/unit/bellman-ford.test.d.ts.map +1 -0
- package/dist/test/unit/bellman-ford.test.js +231 -0
- package/dist/test/unit/bellman-ford.test.js.map +1 -0
- package/dist/test/unit/betweenness-centrality.test.d.ts +2 -0
- package/dist/test/unit/betweenness-centrality.test.d.ts.map +1 -0
- package/dist/test/unit/betweenness-centrality.test.js +311 -0
- package/dist/test/unit/betweenness-centrality.test.js.map +1 -0
- package/dist/test/unit/bfs.test.d.ts +2 -0
- package/dist/test/unit/bfs.test.d.ts.map +1 -0
- package/dist/test/unit/bfs.test.js +277 -0
- package/dist/test/unit/bfs.test.js.map +1 -0
- package/dist/test/unit/closeness.test.d.ts +2 -0
- package/dist/test/unit/closeness.test.d.ts.map +1 -0
- package/dist/test/unit/closeness.test.js +266 -0
- package/dist/test/unit/closeness.test.js.map +1 -0
- package/dist/test/unit/connected-components.test.d.ts +2 -0
- package/dist/test/unit/connected-components.test.d.ts.map +1 -0
- package/dist/test/unit/connected-components.test.js +355 -0
- package/dist/test/unit/connected-components.test.js.map +1 -0
- package/dist/test/unit/degree-centrality.test.d.ts +2 -0
- package/dist/test/unit/degree-centrality.test.d.ts.map +1 -0
- package/dist/test/unit/degree-centrality.test.js +260 -0
- package/dist/test/unit/degree-centrality.test.js.map +1 -0
- package/dist/test/unit/dfs.test.d.ts +2 -0
- package/dist/test/unit/dfs.test.d.ts.map +1 -0
- package/dist/test/unit/dfs.test.js +350 -0
- package/dist/test/unit/dfs.test.js.map +1 -0
- package/dist/test/unit/dijkstra.test.d.ts +2 -0
- package/dist/test/unit/dijkstra.test.d.ts.map +1 -0
- package/dist/test/unit/dijkstra.test.js +311 -0
- package/dist/test/unit/dijkstra.test.js.map +1 -0
- package/dist/test/unit/graph.test.d.ts +2 -0
- package/dist/test/unit/graph.test.d.ts.map +1 -0
- package/dist/test/unit/graph.test.js +175 -0
- package/dist/test/unit/graph.test.js.map +1 -0
- package/dist/test/unit/pagerank.test.d.ts +2 -0
- package/dist/test/unit/pagerank.test.d.ts.map +1 -0
- package/dist/test/unit/pagerank.test.js +281 -0
- package/dist/test/unit/pagerank.test.js.map +1 -0
- package/dist/test/unit/priority-queue.test.d.ts +2 -0
- package/dist/test/unit/priority-queue.test.d.ts.map +1 -0
- package/dist/test/unit/priority-queue.test.js +189 -0
- package/dist/test/unit/priority-queue.test.js.map +1 -0
- package/dist/test/unit/union-find.test.d.ts +2 -0
- package/dist/test/unit/union-find.test.d.ts.map +1 -0
- package/dist/test/unit/union-find.test.js +245 -0
- package/dist/test/unit/union-find.test.js.map +1 -0
- package/package.json +100 -0
- package/src/algorithms/centrality/betweenness.ts +303 -0
- package/src/algorithms/centrality/closeness.ts +296 -0
- package/src/algorithms/centrality/degree.ts +105 -0
- package/src/algorithms/centrality/index.ts +11 -0
- package/src/algorithms/centrality/pagerank.ts +302 -0
- package/src/algorithms/components/connected.ts +298 -0
- package/src/algorithms/components/index.ts +17 -0
- package/src/algorithms/index.ts +15 -0
- package/src/algorithms/shortest-path/bellman-ford.ts +214 -0
- package/src/algorithms/shortest-path/dijkstra.ts +256 -0
- package/src/algorithms/shortest-path/index.ts +7 -0
- package/src/algorithms/traversal/bfs.ts +236 -0
- package/src/algorithms/traversal/dfs.ts +367 -0
- package/src/algorithms/traversal/index.ts +7 -0
- package/src/core/graph.ts +326 -0
- package/src/data-structures/index.ts +6 -0
- package/src/data-structures/priority-queue.ts +180 -0
- package/src/data-structures/union-find.ts +170 -0
- package/src/index.ts +35 -0
- package/src/types/index.ts +95 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Adam Powers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# @graphty/algorithms
|
|
2
|
+
|
|
3
|
+
A comprehensive TypeScript graph algorithms library optimized for browser environments and visualization applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **TypeScript-first**: Full type safety with comprehensive type definitions
|
|
8
|
+
- **Browser-optimized**: Designed to run efficiently in web browsers
|
|
9
|
+
- **Modular**: Import only the algorithms you need
|
|
10
|
+
- **Comprehensive**: Implements essential graph algorithms for analysis and visualization
|
|
11
|
+
- **Well-tested**: Extensive test suite with high coverage
|
|
12
|
+
- **Standards-compliant**: Follows conventional commits and semantic versioning
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @graphty/algorithms
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { Graph } from '@graphty/algorithms';
|
|
24
|
+
|
|
25
|
+
// Create a new graph
|
|
26
|
+
const graph = new Graph();
|
|
27
|
+
|
|
28
|
+
// Add nodes and edges
|
|
29
|
+
graph.addNode('A');
|
|
30
|
+
graph.addNode('B');
|
|
31
|
+
graph.addEdge('A', 'B', 1); // source, target, weight
|
|
32
|
+
|
|
33
|
+
// Basic graph operations
|
|
34
|
+
console.log(graph.nodeCount); // 2
|
|
35
|
+
console.log(graph.totalEdgeCount); // 1
|
|
36
|
+
console.log(graph.hasEdge('A', 'B')); // true
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Algorithms (Planned)
|
|
40
|
+
|
|
41
|
+
### Priority 1: Essential Algorithms
|
|
42
|
+
|
|
43
|
+
#### Graph Traversal
|
|
44
|
+
- [x] **Graph Data Structure**: Core graph implementation with adjacency lists
|
|
45
|
+
- [ ] **Breadth-First Search (BFS)**: Level-by-level traversal and shortest paths
|
|
46
|
+
- [ ] **Depth-First Search (DFS)**: Deep traversal with cycle detection
|
|
47
|
+
|
|
48
|
+
#### Shortest Path
|
|
49
|
+
- [ ] **Dijkstra's Algorithm**: Single-source shortest paths for weighted graphs
|
|
50
|
+
- [ ] **Bellman-Ford Algorithm**: Shortest paths with negative edge weights
|
|
51
|
+
|
|
52
|
+
#### Centrality Measures
|
|
53
|
+
- [ ] **Degree Centrality**: Node importance based on connections
|
|
54
|
+
- [ ] **Betweenness Centrality**: Node importance based on shortest path frequency
|
|
55
|
+
- [ ] **Closeness Centrality**: Node importance based on average distance
|
|
56
|
+
- [ ] **PageRank**: Google's web page ranking algorithm
|
|
57
|
+
|
|
58
|
+
#### Community Detection
|
|
59
|
+
- [ ] **Louvain Algorithm**: Fast modularity-based community detection
|
|
60
|
+
- [ ] **Girvan-Newman Algorithm**: Hierarchical community detection
|
|
61
|
+
|
|
62
|
+
#### Connected Components
|
|
63
|
+
- [ ] **Connected Components**: Find connected subgraphs
|
|
64
|
+
- [ ] **Strongly Connected Components**: Tarjan's algorithm for directed graphs
|
|
65
|
+
|
|
66
|
+
## API Reference
|
|
67
|
+
|
|
68
|
+
### Graph Class
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
class Graph {
|
|
72
|
+
constructor(config?: Partial<GraphConfig>)
|
|
73
|
+
|
|
74
|
+
// Node operations
|
|
75
|
+
addNode(id: NodeId, data?: Record<string, unknown>): void
|
|
76
|
+
removeNode(id: NodeId): boolean
|
|
77
|
+
hasNode(id: NodeId): boolean
|
|
78
|
+
getNode(id: NodeId): Node | undefined
|
|
79
|
+
|
|
80
|
+
// Edge operations
|
|
81
|
+
addEdge(source: NodeId, target: NodeId, weight?: number, data?: Record<string, unknown>): void
|
|
82
|
+
removeEdge(source: NodeId, target: NodeId): boolean
|
|
83
|
+
hasEdge(source: NodeId, target: NodeId): boolean
|
|
84
|
+
getEdge(source: NodeId, target: NodeId): Edge | undefined
|
|
85
|
+
|
|
86
|
+
// Graph properties
|
|
87
|
+
get nodeCount(): number
|
|
88
|
+
get totalEdgeCount(): number
|
|
89
|
+
get isDirected(): boolean
|
|
90
|
+
|
|
91
|
+
// Degree calculations
|
|
92
|
+
degree(nodeId: NodeId): number
|
|
93
|
+
inDegree(nodeId: NodeId): number
|
|
94
|
+
outDegree(nodeId: NodeId): number
|
|
95
|
+
|
|
96
|
+
// Iteration
|
|
97
|
+
nodes(): IterableIterator<Node>
|
|
98
|
+
edges(): IterableIterator<Edge>
|
|
99
|
+
neighbors(nodeId: NodeId): IterableIterator<NodeId>
|
|
100
|
+
|
|
101
|
+
// Utilities
|
|
102
|
+
clone(): Graph
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Development
|
|
107
|
+
|
|
108
|
+
### Prerequisites
|
|
109
|
+
|
|
110
|
+
- Node.js 18+
|
|
111
|
+
- npm 9+
|
|
112
|
+
|
|
113
|
+
### Setup
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
# Clone the repository
|
|
117
|
+
git clone https://github.com/graphty-org/algorithms.git
|
|
118
|
+
cd algorithms
|
|
119
|
+
|
|
120
|
+
# Install dependencies
|
|
121
|
+
npm install
|
|
122
|
+
|
|
123
|
+
# Set up git hooks
|
|
124
|
+
npm run prepare
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Scripts
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
# Development
|
|
131
|
+
npm run dev # Watch mode compilation
|
|
132
|
+
npm run build # Build the library
|
|
133
|
+
npm run typecheck # Type checking
|
|
134
|
+
|
|
135
|
+
# Testing
|
|
136
|
+
npm run test # Run tests in watch mode
|
|
137
|
+
npm run test:run # Run tests once
|
|
138
|
+
npm run test:coverage # Generate coverage report
|
|
139
|
+
npm run test:browser # Run browser tests
|
|
140
|
+
|
|
141
|
+
# Code Quality
|
|
142
|
+
npm run lint # Run ESLint
|
|
143
|
+
npm run lint:fix # Fix ESLint issues
|
|
144
|
+
npm run lint:pkg # Check for unused dependencies
|
|
145
|
+
|
|
146
|
+
# Git
|
|
147
|
+
npm run commit # Conventional commit helper
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Project Structure
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
src/
|
|
154
|
+
├── core/ # Core graph data structures
|
|
155
|
+
├── algorithms/ # Algorithm implementations
|
|
156
|
+
│ ├── traversal/ # BFS, DFS
|
|
157
|
+
│ ├── shortest-path/ # Dijkstra, Bellman-Ford
|
|
158
|
+
│ ├── centrality/ # Degree, Betweenness, PageRank
|
|
159
|
+
│ ├── community/ # Louvain, Girvan-Newman
|
|
160
|
+
│ └── components/ # Connected components
|
|
161
|
+
├── data-structures/ # Supporting data structures
|
|
162
|
+
├── types/ # TypeScript type definitions
|
|
163
|
+
└── utils/ # Utility functions
|
|
164
|
+
|
|
165
|
+
test/
|
|
166
|
+
├── unit/ # Unit tests
|
|
167
|
+
├── browser/ # Browser-specific tests
|
|
168
|
+
└── helpers/ # Test utilities
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Contributing
|
|
172
|
+
|
|
173
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
174
|
+
|
|
175
|
+
### Commit Convention
|
|
176
|
+
|
|
177
|
+
This project uses [Conventional Commits](https://www.conventionalcommits.org/):
|
|
178
|
+
|
|
179
|
+
```
|
|
180
|
+
feat(scope): add new algorithm
|
|
181
|
+
fix(scope): resolve edge case in traversal
|
|
182
|
+
docs(scope): update API documentation
|
|
183
|
+
test(scope): add coverage for centrality measures
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT © Adam Powers
|
|
189
|
+
|
|
190
|
+
## Related Projects
|
|
191
|
+
|
|
192
|
+
- [@graphty/layout](https://github.com/graphty-org/layout) - Graph layout algorithms
|
|
193
|
+
- [@graphty/graphty-element](https://github.com/graphty-org/graphty-element) - 3D graph visualization web component
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Graph } from "../../core/graph.js";
|
|
2
|
+
import type { NodeId } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Betweenness centrality implementation using Brandes' algorithm
|
|
5
|
+
*
|
|
6
|
+
* Measures the extent to which a node lies on paths between other nodes.
|
|
7
|
+
* Uses the fast O(VE) algorithm by Ulrik Brandes.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Betweenness centrality options
|
|
11
|
+
*/
|
|
12
|
+
export interface BetweennessCentralityOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Whether to normalize the centrality values (default: false)
|
|
15
|
+
*/
|
|
16
|
+
normalized?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Whether to use endpoints in path counting (default: false)
|
|
19
|
+
*/
|
|
20
|
+
endpoints?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Calculate betweenness centrality for all nodes using Brandes' algorithm
|
|
24
|
+
*/
|
|
25
|
+
export declare function betweennessCentrality(graph: Graph, options?: BetweennessCentralityOptions): Record<string, number>;
|
|
26
|
+
/**
|
|
27
|
+
* Calculate betweenness centrality for a specific node
|
|
28
|
+
*/
|
|
29
|
+
export declare function nodeBetweennessCentrality(graph: Graph, targetNode: NodeId, options?: BetweennessCentralityOptions): number;
|
|
30
|
+
/**
|
|
31
|
+
* Calculate edge betweenness centrality
|
|
32
|
+
*/
|
|
33
|
+
export declare function edgeBetweennessCentrality(graph: Graph, options?: BetweennessCentralityOptions): Map<string, number>;
|
|
34
|
+
//# sourceMappingURL=betweenness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"betweenness.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/centrality/betweenness.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,sBAAsB,CAAC;AAEjD;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,4BAA4B;IACzC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACjC,KAAK,EAAE,KAAK,EACZ,OAAO,GAAE,4BAAiC,GAC3C,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAkIxB;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CACrC,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,4BAAiC,GAC3C,MAAM,CAOR;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CACrC,KAAK,EAAE,KAAK,EACZ,OAAO,GAAE,4BAAiC,GAC3C,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAqHrB"}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculate betweenness centrality for all nodes using Brandes' algorithm
|
|
3
|
+
*/
|
|
4
|
+
export function betweennessCentrality(graph, options = {}) {
|
|
5
|
+
const nodes = Array.from(graph.nodes()).map((node) => node.id);
|
|
6
|
+
const centrality = {};
|
|
7
|
+
// Initialize centrality scores
|
|
8
|
+
for (const nodeId of nodes) {
|
|
9
|
+
centrality[String(nodeId)] = 0;
|
|
10
|
+
}
|
|
11
|
+
// Brandes' algorithm
|
|
12
|
+
for (const source of nodes) {
|
|
13
|
+
const stack = [];
|
|
14
|
+
const predecessors = new Map();
|
|
15
|
+
const sigma = new Map(); // Number of shortest paths
|
|
16
|
+
const distance = new Map();
|
|
17
|
+
const delta = new Map();
|
|
18
|
+
// Initialize
|
|
19
|
+
for (const nodeId of nodes) {
|
|
20
|
+
predecessors.set(nodeId, []);
|
|
21
|
+
sigma.set(nodeId, 0);
|
|
22
|
+
distance.set(nodeId, -1);
|
|
23
|
+
delta.set(nodeId, 0);
|
|
24
|
+
}
|
|
25
|
+
sigma.set(source, 1);
|
|
26
|
+
distance.set(source, 0);
|
|
27
|
+
const queue = [source];
|
|
28
|
+
// BFS to find shortest paths
|
|
29
|
+
while (queue.length > 0) {
|
|
30
|
+
const current = queue.shift();
|
|
31
|
+
if (!current) {
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
stack.push(current);
|
|
35
|
+
for (const neighbor of graph.neighbors(current)) {
|
|
36
|
+
const currentDistance = distance.get(current);
|
|
37
|
+
let neighborDistance = distance.get(neighbor);
|
|
38
|
+
if (currentDistance === undefined || neighborDistance === undefined) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
// First time we reach this neighbor
|
|
42
|
+
if (neighborDistance < 0) {
|
|
43
|
+
queue.push(neighbor);
|
|
44
|
+
distance.set(neighbor, currentDistance + 1);
|
|
45
|
+
neighborDistance = currentDistance + 1; // Update local variable
|
|
46
|
+
}
|
|
47
|
+
// Shortest path to neighbor via current
|
|
48
|
+
if (neighborDistance === currentDistance + 1) {
|
|
49
|
+
const neighborSigma = sigma.get(neighbor) ?? 0;
|
|
50
|
+
const currentSigma = sigma.get(current) ?? 0;
|
|
51
|
+
sigma.set(neighbor, neighborSigma + currentSigma);
|
|
52
|
+
const preds = predecessors.get(neighbor);
|
|
53
|
+
if (preds) {
|
|
54
|
+
preds.push(current);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Accumulation - back-propagation of dependencies
|
|
60
|
+
while (stack.length > 0) {
|
|
61
|
+
const w = stack.pop();
|
|
62
|
+
if (!w) {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
const wPreds = predecessors.get(w) ?? [];
|
|
66
|
+
const wSigma = sigma.get(w) ?? 0;
|
|
67
|
+
const wDelta = delta.get(w) ?? 0;
|
|
68
|
+
for (const v of wPreds) {
|
|
69
|
+
const vSigma = sigma.get(v) ?? 0;
|
|
70
|
+
const vDelta = delta.get(v) ?? 0;
|
|
71
|
+
if (vSigma > 0 && wSigma > 0) {
|
|
72
|
+
const contribution = (vSigma / wSigma) * (1 + wDelta);
|
|
73
|
+
delta.set(v, vDelta + contribution);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (w !== source) {
|
|
77
|
+
const currentCentrality = centrality[String(w)] ?? 0;
|
|
78
|
+
centrality[String(w)] = currentCentrality + wDelta;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// For undirected graphs, divide by 2 (each shortest path is counted twice)
|
|
83
|
+
if (!graph.isDirected) {
|
|
84
|
+
for (const nodeId of nodes) {
|
|
85
|
+
const key = String(nodeId);
|
|
86
|
+
const currentValue = centrality[key];
|
|
87
|
+
if (currentValue !== undefined) {
|
|
88
|
+
centrality[key] = currentValue / 2;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Normalization
|
|
93
|
+
if (options.normalized) {
|
|
94
|
+
const n = nodes.length;
|
|
95
|
+
let normalizationFactor;
|
|
96
|
+
if (graph.isDirected) {
|
|
97
|
+
normalizationFactor = (n - 1) * (n - 2);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
normalizationFactor = ((n - 1) * (n - 2)) / 2;
|
|
101
|
+
}
|
|
102
|
+
if (normalizationFactor > 0) {
|
|
103
|
+
for (const nodeId of nodes) {
|
|
104
|
+
const key = String(nodeId);
|
|
105
|
+
const currentValue = centrality[key];
|
|
106
|
+
if (currentValue !== undefined) {
|
|
107
|
+
centrality[key] = currentValue / normalizationFactor;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return centrality;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Calculate betweenness centrality for a specific node
|
|
116
|
+
*/
|
|
117
|
+
export function nodeBetweennessCentrality(graph, targetNode, options = {}) {
|
|
118
|
+
if (!graph.hasNode(targetNode)) {
|
|
119
|
+
throw new Error(`Node ${String(targetNode)} not found in graph`);
|
|
120
|
+
}
|
|
121
|
+
const allCentralities = betweennessCentrality(graph, options);
|
|
122
|
+
return allCentralities[String(targetNode)] ?? 0;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Calculate edge betweenness centrality
|
|
126
|
+
*/
|
|
127
|
+
export function edgeBetweennessCentrality(graph, options = {}) {
|
|
128
|
+
const nodes = Array.from(graph.nodes()).map((node) => node.id);
|
|
129
|
+
const edgeCentrality = new Map();
|
|
130
|
+
// Initialize edge centrality scores
|
|
131
|
+
for (const edge of graph.edges()) {
|
|
132
|
+
const edgeKey = `${String(edge.source)}-${String(edge.target)}`;
|
|
133
|
+
edgeCentrality.set(edgeKey, 0);
|
|
134
|
+
}
|
|
135
|
+
// Modified Brandes' algorithm for edge betweenness
|
|
136
|
+
for (const source of nodes) {
|
|
137
|
+
const stack = [];
|
|
138
|
+
const predecessors = new Map();
|
|
139
|
+
const sigma = new Map();
|
|
140
|
+
const distance = new Map();
|
|
141
|
+
const delta = new Map();
|
|
142
|
+
// Initialize
|
|
143
|
+
for (const nodeId of nodes) {
|
|
144
|
+
predecessors.set(nodeId, []);
|
|
145
|
+
sigma.set(nodeId, 0);
|
|
146
|
+
distance.set(nodeId, -1);
|
|
147
|
+
delta.set(nodeId, 0);
|
|
148
|
+
}
|
|
149
|
+
sigma.set(source, 1);
|
|
150
|
+
distance.set(source, 0);
|
|
151
|
+
const queue = [source];
|
|
152
|
+
// BFS to find shortest paths
|
|
153
|
+
while (queue.length > 0) {
|
|
154
|
+
const current = queue.shift();
|
|
155
|
+
if (!current) {
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
stack.push(current);
|
|
159
|
+
for (const neighbor of graph.neighbors(current)) {
|
|
160
|
+
const currentDistance = distance.get(current);
|
|
161
|
+
let neighborDistance = distance.get(neighbor);
|
|
162
|
+
if (currentDistance === undefined || neighborDistance === undefined) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (neighborDistance < 0) {
|
|
166
|
+
queue.push(neighbor);
|
|
167
|
+
distance.set(neighbor, currentDistance + 1);
|
|
168
|
+
neighborDistance = currentDistance + 1; // Update local variable
|
|
169
|
+
}
|
|
170
|
+
if (neighborDistance === currentDistance + 1) {
|
|
171
|
+
const neighborSigma = sigma.get(neighbor) ?? 0;
|
|
172
|
+
const currentSigma = sigma.get(current) ?? 0;
|
|
173
|
+
sigma.set(neighbor, neighborSigma + currentSigma);
|
|
174
|
+
const preds = predecessors.get(neighbor);
|
|
175
|
+
if (preds) {
|
|
176
|
+
preds.push(current);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// Accumulation for edges
|
|
182
|
+
while (stack.length > 0) {
|
|
183
|
+
const w = stack.pop();
|
|
184
|
+
if (!w) {
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
const wPreds = predecessors.get(w) ?? [];
|
|
188
|
+
const wSigma = sigma.get(w) ?? 0;
|
|
189
|
+
const wDelta = delta.get(w) ?? 0;
|
|
190
|
+
for (const v of wPreds) {
|
|
191
|
+
const vSigma = sigma.get(v) ?? 0;
|
|
192
|
+
if (vSigma > 0 && wSigma > 0) {
|
|
193
|
+
const edgeContribution = vSigma / wSigma * (1 + wDelta);
|
|
194
|
+
// Update edge centrality
|
|
195
|
+
const edgeKey = `${String(v)}-${String(w)}`;
|
|
196
|
+
const currentEdgeCentrality = edgeCentrality.get(edgeKey) ?? 0;
|
|
197
|
+
edgeCentrality.set(edgeKey, currentEdgeCentrality + edgeContribution);
|
|
198
|
+
// Update node delta
|
|
199
|
+
const vDelta = delta.get(v) ?? 0;
|
|
200
|
+
delta.set(v, vDelta + edgeContribution);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// For undirected graphs, divide by 2 (each shortest path is counted twice)
|
|
206
|
+
if (!graph.isDirected) {
|
|
207
|
+
for (const [edgeKey, centrality] of edgeCentrality) {
|
|
208
|
+
edgeCentrality.set(edgeKey, centrality / 2);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// Normalization for edges
|
|
212
|
+
if (options.normalized) {
|
|
213
|
+
const n = nodes.length;
|
|
214
|
+
const normalizationFactor = graph.isDirected ? (n - 1) * (n - 2) : ((n - 1) * (n - 2)) / 2;
|
|
215
|
+
if (normalizationFactor > 0) {
|
|
216
|
+
for (const [edgeKey, centrality] of edgeCentrality) {
|
|
217
|
+
edgeCentrality.set(edgeKey, centrality / normalizationFactor);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return edgeCentrality;
|
|
222
|
+
}
|
|
223
|
+
//# sourceMappingURL=betweenness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"betweenness.js","sourceRoot":"","sources":["../../../../src/algorithms/centrality/betweenness.ts"],"names":[],"mappings":"AAwBA;;GAEG;AACH,MAAM,UAAU,qBAAqB,CACjC,KAAY,EACZ,UAAwC,EAAE;IAE1C,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC/D,MAAM,UAAU,GAA2B,EAAE,CAAC;IAE9C,+BAA+B;IAC/B,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACzB,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;IAED,qBAAqB;IACrB,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,MAAM,YAAY,GAAG,IAAI,GAAG,EAAoB,CAAC;QACjD,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC,CAAC,2BAA2B;QACpE,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;QAC3C,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC;QAExC,aAAa;QACb,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE,CAAC;YACzB,YAA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,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAE3F,IAAI,mBAAmB,GAAG,CAAC,EAAE,CAAC;YAC1B,KAAK,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,IAAI,cAAc,EAAE,CAAC;gBACjD,cAAc,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,GAAG,mBAAmB,CAAC,CAAC;YAClE,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,cAAc,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Graph } from "../../core/graph.js";
|
|
2
|
+
import type { NodeId } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Closeness centrality implementation
|
|
5
|
+
*
|
|
6
|
+
* Measures how close a node is to all other nodes in the graph.
|
|
7
|
+
* Uses BFS to compute shortest path distances efficiently.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Closeness centrality options
|
|
11
|
+
*/
|
|
12
|
+
export interface ClosenessCentralityOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Whether to normalize the centrality values (default: false)
|
|
15
|
+
*/
|
|
16
|
+
normalized?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Use harmonic mean instead of reciprocal of sum (default: false)
|
|
19
|
+
* Better for disconnected graphs
|
|
20
|
+
*/
|
|
21
|
+
harmonic?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Consider only nodes within this distance (default: undefined = all nodes)
|
|
24
|
+
*/
|
|
25
|
+
cutoff?: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Calculate closeness centrality for all nodes
|
|
29
|
+
*/
|
|
30
|
+
export declare function closenessCentrality(graph: Graph, options?: ClosenessCentralityOptions): Record<string, number>;
|
|
31
|
+
/**
|
|
32
|
+
* Calculate closeness centrality for a specific node
|
|
33
|
+
*/
|
|
34
|
+
export declare function nodeClosenessCentrality(graph: Graph, node: NodeId, options?: ClosenessCentralityOptions): number;
|
|
35
|
+
/**
|
|
36
|
+
* Calculate weighted closeness centrality using Dijkstra's algorithm
|
|
37
|
+
*/
|
|
38
|
+
export declare function weightedClosenessCentrality(graph: Graph, options?: ClosenessCentralityOptions): Record<string, number>;
|
|
39
|
+
/**
|
|
40
|
+
* Calculate weighted closeness centrality for a specific node using Dijkstra
|
|
41
|
+
*/
|
|
42
|
+
export declare function nodeWeightedClosenessCentrality(graph: Graph, node: NodeId, options?: ClosenessCentralityOptions): number;
|
|
43
|
+
//# sourceMappingURL=closeness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"closeness.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/centrality/closeness.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,sBAAsB,CAAC;AAEjD;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACvC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAC/B,KAAK,EAAE,KAAK,EACZ,OAAO,GAAE,0BAA+B,GACzC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CASxB;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACnC,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,0BAA+B,GACzC,MAAM,CAoDR;AAkDD;;GAEG;AACH,wBAAgB,2BAA2B,CACvC,KAAK,EAAE,KAAK,EACZ,OAAO,GAAE,0BAA+B,GACzC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CASxB;AAED;;GAEG;AACH,wBAAgB,+BAA+B,CAC3C,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,0BAA+B,GACzC,MAAM,CAoDR"}
|