Skip to content

Graph-js is a Node.js module to create and manipulate graphs.

License

Notifications You must be signed in to change notification settings

ChrisKraljevicD2L/graph-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Graph-js

Graph-js is a module to create and manipulate graphs. Nodes can be all objects who you want and edges are oriented and valued at 1 by default. If you wish one graph not oriented, edges must be declared in one direction and then in the other. The documentation is located in the folder graph-js_doc. Future improvements, such as the browse of the graph, should be implemented soon.

Installation

Package

Via npm:

$ npm install graph-js

Getting started

// add module
var Graph = require('graph-js');

// create a new graph
var graph = new Graph();

// create n1, n2 and n3, three nodes of graph
graph.addNode("I'm n1", "n1");
graph.addNode("I'm n2", "n2");
graph.addNode("I'm n3", "n3");

// create edge e1 such as n1->n2 with weight = 1.5,
// edge e2 such as n3->n2 with weight by default = 1
// and edge e3 such as n1->n3 with weight by default = 1
graph.addEdge("n1", "n2", "e1", 1.5);
graph.addEdge("n3", "n2", "e2");
graph.addEdge("n1", "n3", "e3");

// retrieve the list graph's nodes
var nodes = graph.getNodes();

// display the graph's nodes
for(var n in nodes){

  // display the id of nodes
  console.log(nodes[n].getId());

  // display the content of nodes
  console.log(nodes[n].getContent());
}

// display the graph's edges
var edges = graph.getEdges();

for(var e in edges){
  console.log(edges[e].getNodeStart().getId() + " --- " +
  edges[e].getId() + " = " + edges[e].getWeight() +
  " ---> " + edges[e].getNodeEnd().getId());
}

// remove the node n2 and all edges binded to node n2
graph.removeNode("n2");

// remove the edge e3
graph.removeEdge("e3");

Author

If you have any questions or suggestions, please don't hesitate to contact me : belaich.david@outlook.fr .

About

Graph-js is a Node.js module to create and manipulate graphs.

Resources

License

Stars

Watchers

Forks

Packages

No packages published