npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphs-all

v1.3.0

Published

Graph Data Structure Library

Downloads

15

Readme

Graphs

Travis build Code coverage version

This library contain an implementation of graph data structure with various method that can be useful in operations by graphs.

Graphs can directed and undirected, weighted and unweighted. So we have 4 variations and this library can works with them.

Install

npm install graphs-all

Getting Started

Import the library

var graphs = require("graphs-all");
var g = graphs.UndirectedUnweightedGraph();
g.addNode("A");
g.addLink("A", "B");
g.addLink("A", "C");
g.addLink("B", "C");
g.hasNode("C") // true
g.hasLink("B", "A") // true
g.removeLink("C", "A");
g.connectedWith("B") // ["A", "B"]

Create a graph

You can create four variations of graphs with shortcuts functions:

var simpleGraph = graphs.UndirectedUnweightedGraph(), 
    directedGraph = graphs.DirectedUnweightedGraph(),
    weightedGraph = graph.UndirectedWeightedGraph(),
    complexGraph = graph.DirectedWeightedGraph();

Or with Graph class:

new Graph(isDirected, isWeighted)

var simpleGraph = new graphs.Graph(false, false), 
    directedGraph = new graphs.Graph(true, false),
    weightedGraph = new graphs.Graph(false, true),
    complexGraph = new graphs.Graph(true, true);

Add nodes

Graph.addNode(nodeKey)

Add a node in the graph with key nodeKey. If node is exist already, then return false, for successive addition returns true.

simpleGraph.addNode("A")
simpleGraph.hasNode("B") // true
simpleGraph.hasNode("A") // false

Check nodes

Graph.hasNode(nodeKey)

Check is a node with key nodekey in the graph

simpleGraph.hasNode("A") // true
simpleGraph.hasNode("E") // false

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.

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).

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.

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.

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).

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.

simpleGraph.addLink("5", "6");
simpleGraph.removeLink("6", "5");
simpleGraph.hasLink("5", "6"); // false
simpleGraph.hasNode("5"); // true