0.0.2 ⢠Published 9 months ago
@hanzifinder-chao/networkxjs v0.0.2
š NetworkXJS - A Graph Library for JavaScript
NetworkXJS is a JavaScript library for working with graph data structures and graph algorithms, inspired by Python's NetworkX. It provides an easy-to-use API for building, analyzing, and manipulating graphs in JavaScript.
š Why NetworkXJS?
- Simple & Flexible: Designed for both beginners and advanced users.
- Strict Graph Handling: No implicit node creation. Nodes must be explicitly added.
- Supports Node & Edge Attributes: Store metadata on both nodes and edges.
- Fast & Lightweight: Uses
MapandSetfor efficient graph operations. - No Dependencies: Works in both Node.js and Browser environments.
š¦ Installation
To use NetworkXJS, simply install it via npm or pnpm:
# Using npm
npm install networkxjs
# Using pnpm
pnpm add networkxjsIn browser environments, you can include the library via a CDN:
<script src="/url/to/networkxjs.umd.js"></script>
<script>
const G = new NetworkXJS.Graph();
G.addNode(1);
G.addEdge(1, 2);
console.log(G.hasEdge(1, 2)); // true
</script>š Quick Start
1ļøā£ Import the library
import { Graph } from 'networkxjs';
// OR (if using CommonJS)
const { Graph } = require('networkxjs');2ļøā£ Create a graph
const G = new Graph();
G.addNode(1, { color: "red" });
G.addNode("A", { color: "blue" });
console.log(G.hasNode(1)); // true
console.log(G.hasNode("B")); // false3ļøā£ Add edges
G.addEdge(1, "A", { weight: 5 });
console.log(G.hasEdge(1, "A")); // true
// Retrieve edge attributes
console.log(G.edge(1, "A")); // { weight: 5 }4ļøā£ Remove nodes & edges
G.removeEdge(1, "A");
console.log(G.hasEdge(1, "A")); // false
G.removeNode("A");
console.log(G.hasNode("A")); // falsešÆ API Reference
š¹ Graph Methods
| Method | Description |
|---|---|
addNode(id, attr={}) | Add a node with an optional attribute object. |
addNodesFrom([...]) | Add multiple nodes at once. |
hasNode(id) | Check if a node exists. |
node(id) | Get node attributes (throws if not found). |
removeNode(id) | Remove a node and all connected edges. |
addEdge(u, v, attr={}) | Add an undirected edge (nodes must exist). |
hasEdge(u, v) | Check if an edge exists. |
edge(u, v) | Get edge attributes (throws if not found). |
removeEdge(u, v) | Remove an edge. |
neighbors(id) | Get all neighbors of a node. |
numberOfNodes() | Get the total number of nodes. |
numberOfEdges() | Get the total number of edges. |
š Common Issues & FAQs
ā Why do I get an error when adding an edge?
NetworkXJS does not auto-create nodes. You must add both nodes before adding an edge.
const G = new Graph();
G.addNode(1);
G.addNode(2);
G.addEdge(1, 2); // ā
Works fine
G.addEdge(3, 4); // ā Error: Node "3" does not exist.ā Can I use objects as node IDs?
No. Node IDs must be strings or numbers to ensure fast lookups.
ā Does it support directed graphs?
Not yet. Future versions may include DiGraph.
š„ Contributing
We welcome contributions! To contribute:
- Fork the repository on GitHub.
- Clone your fork and install dependencies:
git clone https://github.com/yourusername/networkxjs.git cd networkxjs pnpm install - Run tests before submitting a PR:
pnpm test - Submit a pull request š
š License
This project is licensed under the MIT License.
0.0.2
9 months ago