@robinpath/graph
0.1.1Node.jsPublicGraph data structures with BFS, DFS, Dijkstra's shortest path, topological sort, cycle detection, and connectivity
Graph
Graph data structures with BFS, DFS, Dijkstra's shortest path, topological sort, cycle detection, and connectivity
Package: @robinpath/graph | Category: Analytics | Type: Integration
Authentication
No authentication required. All functions are available immediately.
Use Cases
Use the graph module when you need to:
- Create graph -- Use
graph.createto perform this operation - Add node -- Use
graph.addNodeto perform this operation - Add edge -- Use
graph.addEdgeto perform this operation - Remove node and its edges -- Use
graph.removeNodeto perform this operation - Remove edge -- Use
graph.removeEdgeto perform this operation
Quick Reference
| Function | Description | Returns |
|---|---|---|
create | Create graph | {name, directed} |
addNode | Add node | true |
addEdge | Add edge | true |
removeNode | Remove node and its edges | true |
removeEdge | Remove edge | true |
nodes | List all nodes | Node IDs |
edges | List all edges | Edge objects |
neighbors | Get node neighbors | Neighbor IDs |
degree | Get node degree | Degree count |
bfs | Breadth-first search | Visit order |
dfs | Depth-first search | Visit order |
shortestPath | Dijkstra's shortest path | {path, distance} |
topologicalSort | Topological sort (DAG only) | Sorted node IDs |
hasCycle | Check for cycles | true if has cycle |
isConnected | Check if graph is connected | true if connected |
hasPath | Check if path exists between nodes | true if path exists |
size | Get graph size | {nodes, edges} |
destroy | Destroy graph | true |
list | List all graphs | Graph names |
Functions
create
Create graph
Module: graph | Returns: object -- {name, directed}
graph.create {"name": "deps", "directed": true}
| Parameter | Type | Required | Description |
|---|---|---|---|
options | object | No | {name, directed} |
addNode
Add node
Module: graph | Returns: boolean -- true
graph.addNode "A" {"label": "Start"}
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Node ID |
data | any | No | Node data |
graph | string | No | Graph name |
addEdge
Add edge
Module: graph | Returns: boolean -- true
graph.addEdge "A" "B" 5
| Parameter | Type | Required | Description |
|---|---|---|---|
from | string | Yes | From node |
to | string | Yes | To node |
weight | number | No | Edge weight |
graph | string | No | Graph name |
removeNode
Remove node and its edges
Module: graph | Returns: boolean -- true
graph.removeNode "A"
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Node ID |
graph | string | No | Graph name |
removeEdge
Remove edge
Module: graph | Returns: boolean -- true
graph.removeEdge "A" "B"
| Parameter | Type | Required | Description |
|---|---|---|---|
from | string | Yes | From node |
to | string | Yes | To node |
graph | string | No | Graph name |
nodes
List all nodes
Module: graph | Returns: array -- Node IDs
graph.nodes
| Parameter | Type | Required | Description |
|---|---|---|---|
graph | string | No | Graph name |
edges
List all edges
Module: graph | Returns: array -- Edge objects
graph.edges
| Parameter | Type | Required | Description |
|---|---|---|---|
graph | string | No | Graph name |
neighbors
Get node neighbors
Module: graph | Returns: array -- Neighbor IDs
graph.neighbors "A"
| Parameter | Type | Required | Description |
|---|---|---|---|
node | string | Yes | Node ID |
graph | string | No | Graph name |
degree
Get node degree
Module: graph | Returns: number -- Degree count
graph.degree "A"
| Parameter | Type | Required | Description |
|---|---|---|---|
node | string | Yes | Node ID |
graph | string | No | Graph name |
bfs
Breadth-first search
Module: graph | Returns: array -- Visit order
graph.bfs "A"
| Parameter | Type | Required | Description |
|---|---|---|---|
start | string | Yes | Start node |
graph | string | No | Graph name |
dfs
Depth-first search
Module: graph | Returns: array -- Visit order
graph.dfs "A"
| Parameter | Type | Required | Description |
|---|---|---|---|
start | string | Yes | Start node |
graph | string | No | Graph name |
shortestPath
Dijkstra's shortest path
Module: graph | Returns: object -- {path, distance}
graph.shortestPath "A" "D"
| Parameter | Type | Required | Description |
|---|---|---|---|
start | string | Yes | Start node |
end | string | Yes | End node |
graph | string | No | Graph name |
topologicalSort
Topological sort (DAG only)
Module: graph | Returns: array -- Sorted node IDs
graph.topologicalSort "deps"
| Parameter | Type | Required | Description |
|---|---|---|---|
graph | string | No | Graph name |
hasCycle
Check for cycles
Module: graph | Returns: boolean -- true if has cycle
graph.hasCycle "deps"
| Parameter | Type | Required | Description |
|---|---|---|---|
graph | string | No | Graph name |
isConnected
Check if graph is connected
Module: graph | Returns: boolean -- true if connected
graph.isConnected
| Parameter | Type | Required | Description |
|---|---|---|---|
graph | string | No | Graph name |
hasPath
Check if path exists between nodes
Module: graph | Returns: boolean -- true if path exists
graph.hasPath "A" "D"
| Parameter | Type | Required | Description |
|---|---|---|---|
from | string | Yes | From node |
to | string | Yes | To node |
graph | string | No | Graph name |
size
Get graph size
Module: graph | Returns: object -- {nodes, edges}
graph.size
| Parameter | Type | Required | Description |
|---|---|---|---|
graph | string | No | Graph name |
destroy
Destroy graph
Module: graph | Returns: boolean -- true
graph.destroy
| Parameter | Type | Required | Description |
|---|---|---|---|
graph | string | No | Graph name |
list
List all graphs
Module: graph | Returns: array -- Graph names
graph.list
| Parameter | Type | Required | Description |
|---|---|---|---|
| (none) | No | Call with no arguments |
Error Handling
All functions throw on failure. Common errors:
| Error | Cause |
|---|---|
Topological sort requires a directed graph | Check the error message for details |
Graph has a cycle | Check the error message for details |
Graph "..." not found | Check the error message for details |
@desc "Create and validate result"
do
set $result as graph.create {"name": "deps", "directed": true}
if $result != null
print "Success"
else
print "No result"
end
enddo
Recipes
1. List and iterate
Retrieve all items and loop through them.
@desc "List and iterate results"
do
set $result as graph.list
each $item in $result
print $item
end
enddo
2. Create a new item with create
Create a new resource and capture the result.
set $result as graph.create {"name": "deps", "directed": true}
print "Created: " + $result
3. Check before creating
List existing items and only create if needed.
@desc "List and create"
do
set $existing as graph.list
if $existing == null
graph.create {"name": "deps", "directed": true}
print "Item created"
else
print "Item already exists"
end
enddo
4. Multi-step Graph workflow
Chain multiple graph operations together.
@desc "Create, add node, and more"
do
set $r_create as graph.create {"name": "deps", "directed": true}
set $r_addNode as graph.addNode "A" {"label": "Start"}
set $r_addEdge as graph.addEdge "A" "B" 5
print "All operations complete"
enddo
5. Safe create with validation
Check results before proceeding.
@desc "Create and validate result"
do
set $result as graph.create {"name": "deps", "directed": true}
if $result != null
print "Success: " + $result
else
print "Operation returned no data"
end
enddo
Related Modules
- json -- JSON module for complementary functionality
Versions (1)
| Version | Tag | Published |
|---|---|---|
| 0.1.1 | latest | 1 months ago |
Related Modules
chart
JS@robinpathv0.1.2
Generate chart images (PNG/JPEG) using Chart.js. Supports bar, line, pie, doughnut, scatter, radar, polarArea, and bubble charts with auto-coloring and customizable titles, legends, and dimensions.
firebase
JS@robinpathv0.1.1
Firebase module for RobinPath.
hotjar
JS@robinpathv0.1.1
Hotjar module for RobinPath.
mixpanel
JS@robinpathv0.1.2
Mixpanel module for RobinPath.
$ robinpath add @robinpath/graph
