Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphs-all

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphs-all - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

2

package.json
{
"name": "graphs-all",
"version": "1.2.1",
"version": "1.3.0",
"description": "Graph Data Structure Library",

@@ -5,0 +5,0 @@ "main": "src/index.js",

# Graphs
[![Travis build](https://img.shields.io/travis/Bryukh/graphs.svg)](https://travis-ci.org/Bryukh/graphs)
[![Code coverage](https://img.shields.io/codecov/c/github/Bryukh/graphs.svg)](https://codecov.io/github/Bryukh)
[![version](https://img.shields.io/npm/v/graphs-all.svg)](https://www.npmjs.com/package/graphs-all)
This library contain an implementation of graph data structure

@@ -78,2 +83,97 @@ with various method that can be useful in operations by graphs.

#TODO
### Add links
`Graph.addLink(key1, key2)`
`Graph.addLink(key1, key2, weight)`
Add a link between `key1` and `key2` nodes. If nodes are not existed, then they are created.
For directed graphs, it's created a directed link from `key1` to `key2`.
For weighted graphs you can add the third argument `weight` (default == 1).
`weight` must be a number.
```javascript
simpleGraph.addLink("A", "E");
simpleGraph.hasNode("E"); // true
weightedGraph.addLink("E", "C", 5);
```
### Check links
`Graph.hasLink(key1, key2)`
`Graph.linkWeight(key1, key2)`
Check the existence of the link between `key1` and `key2` nodes (the order is important only for directed graphs).
It returns `true` if the link is exist, `false` otherwise.
Also you can use `linkWeight` - it has the same interface and returns `undefined` if the link doesn't exist,
otherwise the weight of the link (1 for unweighted graphs).
```javascript
simpleGraph.addLink("A", "E");
simpleGraph.hasLink("E", "A"); // true
weightedGraph.addLink("E", "C", 5);
weightedGraph.linkWeight("E", "C"); // 5
simpleGraph.linkWeight("Z", "A"); // undefined
```
### Get node list
`Graph.nodes()`
Returns an array on node keys.
```javascript
simpleGraph.addLink(1, 2);
simpleGraph.addLink(3, 4);
simpleGraph.nodes(); // [1, 2, 3, 4]
```
### Get node connections
`Graph.connectedWith(key)`
Returns an array of keys of nodes, which are connected with the given node.
For directed graphs return only nodes that are available from from the given node.
```javascript
simpleGraph.addLink("A", "D");
simpleGraph.addLink("A", "E");
simpleGraph.addLink("C", "A");
simpleGraph.nodeConnections("A"); // ["E", D", "C"]
directedGraph.addLink("A", "B");
directedGraph.addLink("C", "A");
directedGraph.nodeConnections("A"); // ["B"]
```
### Remove Nodes
`Graph.removeNode(key)`
Removes the node from the graph, also remove all connected links (input and output).
```javascript
directedGraph.addLink("1", "2");
directedGraph.addLink("3", "1");
directedGraph.addLink("1", "4");
directedGraph.removeNode("1");
directedGraph.hasNode("1"); // false
directedGraph.hasLink("1", "2"); // false
directedGraph.hasLink("3", "1"); // false
```
### Remove Links
`Graph.removeLink(key1, key2)`
Removes the link from the graph. The nodes are not removed, only the link.
For directed graphs remove only one-way link from `key1` to `key2`.
```javascript
simpleGraph.addLink("5", "6");
simpleGraph.removeLink("6", "5");
simpleGraph.hasLink("5", "6"); // false
simpleGraph.hasNode("5"); // true
```

@@ -103,4 +103,2 @@ function Graph(isDirected, isWeighted) {

var neighbours = this.connectedWith(key);
console.log(links);
for (var i = 0, n = neighbours.length; i < n; i++) {

@@ -120,2 +118,10 @@ this.removeLink(key, neighbours[i]);

this.nodes = function() {
var res = [];
for (var i = 0, k = Object.keys(idToKeyTable), L = k.length; i < L; i++) {
res.push(idToKeyTable[k[i]]);
}
return res;
}
}

@@ -122,0 +128,0 @@

@@ -151,18 +151,26 @@ var expect = require("chai").expect;

describe("Connections:", function () {
graphs.forEach(function(test){
graphs.forEach(function (test) {
var g = test.obj();
g.addLink(1, 2);
g.addLink(1, 3);
g.addLink(1, 4);
g.addLink(2, 4);
g.addLink(4, 6);
g.addNode(5);
expect(g.connectedWith(1)).to.include.members([2, 3, 4]);
expect(g.connectedWith(1)).to.have.length(3);
expect(g.connectedWith(5)).to.be.deep.equal([]);
expect(g.connectedWith(7)).to.be.undefined;
if (test.type.directed) {
expect(g.connectedWith(4)).to.be.deep.equal([6]);
}
it("Connected with " + test.name, function () {
g.addLink(1, 2);
g.addLink(1, 3);
g.addLink(1, 4);
g.addLink(2, 4);
g.addLink(4, 6);
g.addNode(5);
expect(g.connectedWith(1)).to.include.members([2, 3, 4]);
expect(g.connectedWith(1)).to.have.length(3);
expect(g.connectedWith(5)).to.be.deep.equal([]);
expect(g.connectedWith(7)).to.be.undefined;
if (test.type.directed) {
expect(g.connectedWith(4)).to.be.deep.equal([6]);
}
});
it("Node list in " + test.name, function () {
expect(g.nodes()).to.include.members([1, 2, 3, 4, 5, 6]);
expect(g.nodes(1)).to.have.length(6);
});
});
})

@@ -169,0 +177,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc