Modules@robinpath/graph
graph

@robinpath/graph

0.1.1Node.jsPublic

Graph 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.create to perform this operation
  • Add node -- Use graph.addNode to perform this operation
  • Add edge -- Use graph.addEdge to perform this operation
  • Remove node and its edges -- Use graph.removeNode to perform this operation
  • Remove edge -- Use graph.removeEdge to perform this operation

Quick Reference

FunctionDescriptionReturns
createCreate graph{name, directed}
addNodeAdd nodetrue
addEdgeAdd edgetrue
removeNodeRemove node and its edgestrue
removeEdgeRemove edgetrue
nodesList all nodesNode IDs
edgesList all edgesEdge objects
neighborsGet node neighborsNeighbor IDs
degreeGet node degreeDegree count
bfsBreadth-first searchVisit order
dfsDepth-first searchVisit order
shortestPathDijkstra's shortest path{path, distance}
topologicalSortTopological sort (DAG only)Sorted node IDs
hasCycleCheck for cyclestrue if has cycle
isConnectedCheck if graph is connectedtrue if connected
hasPathCheck if path exists between nodestrue if path exists
sizeGet graph size{nodes, edges}
destroyDestroy graphtrue
listList all graphsGraph names

Functions

create

Create graph

Module: graph | Returns: object -- {name, directed}

graph.create {"name": "deps", "directed": true}
ParameterTypeRequiredDescription
optionsobjectNo{name, directed}

addNode

Add node

Module: graph | Returns: boolean -- true

graph.addNode "A" {"label": "Start"}
ParameterTypeRequiredDescription
idstringYesNode ID
dataanyNoNode data
graphstringNoGraph name

addEdge

Add edge

Module: graph | Returns: boolean -- true

graph.addEdge "A" "B" 5
ParameterTypeRequiredDescription
fromstringYesFrom node
tostringYesTo node
weightnumberNoEdge weight
graphstringNoGraph name

removeNode

Remove node and its edges

Module: graph | Returns: boolean -- true

graph.removeNode "A"
ParameterTypeRequiredDescription
idstringYesNode ID
graphstringNoGraph name

removeEdge

Remove edge

Module: graph | Returns: boolean -- true

graph.removeEdge "A" "B"
ParameterTypeRequiredDescription
fromstringYesFrom node
tostringYesTo node
graphstringNoGraph name

nodes

List all nodes

Module: graph | Returns: array -- Node IDs

graph.nodes
ParameterTypeRequiredDescription
graphstringNoGraph name

edges

List all edges

Module: graph | Returns: array -- Edge objects

graph.edges
ParameterTypeRequiredDescription
graphstringNoGraph name

neighbors

Get node neighbors

Module: graph | Returns: array -- Neighbor IDs

graph.neighbors "A"
ParameterTypeRequiredDescription
nodestringYesNode ID
graphstringNoGraph name

degree

Get node degree

Module: graph | Returns: number -- Degree count

graph.degree "A"
ParameterTypeRequiredDescription
nodestringYesNode ID
graphstringNoGraph name

bfs

Breadth-first search

Module: graph | Returns: array -- Visit order

graph.bfs "A"
ParameterTypeRequiredDescription
startstringYesStart node
graphstringNoGraph name

dfs

Depth-first search

Module: graph | Returns: array -- Visit order

graph.dfs "A"
ParameterTypeRequiredDescription
startstringYesStart node
graphstringNoGraph name

shortestPath

Dijkstra's shortest path

Module: graph | Returns: object -- {path, distance}

graph.shortestPath "A" "D"
ParameterTypeRequiredDescription
startstringYesStart node
endstringYesEnd node
graphstringNoGraph name

topologicalSort

Topological sort (DAG only)

Module: graph | Returns: array -- Sorted node IDs

graph.topologicalSort "deps"
ParameterTypeRequiredDescription
graphstringNoGraph name

hasCycle

Check for cycles

Module: graph | Returns: boolean -- true if has cycle

graph.hasCycle "deps"
ParameterTypeRequiredDescription
graphstringNoGraph name

isConnected

Check if graph is connected

Module: graph | Returns: boolean -- true if connected

graph.isConnected
ParameterTypeRequiredDescription
graphstringNoGraph name

hasPath

Check if path exists between nodes

Module: graph | Returns: boolean -- true if path exists

graph.hasPath "A" "D"
ParameterTypeRequiredDescription
fromstringYesFrom node
tostringYesTo node
graphstringNoGraph name

size

Get graph size

Module: graph | Returns: object -- {nodes, edges}

graph.size
ParameterTypeRequiredDescription
graphstringNoGraph name

destroy

Destroy graph

Module: graph | Returns: boolean -- true

graph.destroy
ParameterTypeRequiredDescription
graphstringNoGraph name

list

List all graphs

Module: graph | Returns: array -- Graph names

graph.list
ParameterTypeRequiredDescription
(none)NoCall with no arguments

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Topological sort requires a directed graphCheck the error message for details
Graph has a cycleCheck the error message for details
Graph "..." not foundCheck 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)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/graph

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size6.0 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

data