Skip to content

jackhp95/graph-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

graph-js

This module is an implementation of an undirected graph data structure using ES6 Maps and Sets.

Install

jsdelivr

<script src="https://cdn.jsdelivr.net/gh/jackhp95/graph-js@master/index.min.js"></script>

Purpose

Someone would need a module like this when they need to represent a graph data structure in their JavaScript project. Graphs are used to represent complex relationships between objects or entities, and are commonly used in areas such as computer networking, social network analysis, and recommendation systems. This implementation uses ES6 Maps and Sets, which are efficient data structures for working with key-value pairs and collections of unique values, respectively. The module provides methods for adding and removing nodes and edges, checking for the presence of nodes and edges, and retrieving information about the graph such as the size, nodes, and edges. It can be used in a wide range of applications that require graph-based data structures and algorithms.

Usage

Creating a graph

To create a new graph, call the Graph constructor function with an optional array of [key, value] pairs to initialize the graph:

const Graph = require('./graph');

const myGraph = Graph([
  ['A', 'C'],
  ['C', 'B'],
]);

console.log(myGraph); // { clear: [Function], delete: [Function], remove: [Function], size: [Function], has: [Function], get: [Function], set: [Function], nodes: [Function], edges: [Function] }

Adding and removing nodes and edges

You can add edges to the graph by calling the set method and passing in two keys:

myGraph.set('A')('B');

This will add an edge between node 'A' and node 'B'.

You can remove edges from the graph by calling the remove method and passing in two keys:

myGraph.remove('A')('B');

This will remove the edge between node 'A' and node 'B'.

You can delete a node from the graph and all connected edges by calling the delete method and passing in the node's key:

myGraph.delete('A');

This will delete node 'A' from the graph, along with any edges that connected it to other nodes.

Checking for nodes and edges

You can check if a node exists in the graph by calling the has method and passing in the node's key:

myGraph.has('A'); // returns false
myGraph.has('B'); // returns true

You can check if an edge exists between two nodes by calling the has method and passing in both nodes' keys:

myGraph.has('A')('B'); // returns false
myGraph.has('B')('C'); // returns false
myGraph.set('A')('B');
myGraph.set('B')('C');
myGraph.has('A')('B'); // returns true
myGraph.has('B')('C'); // returns true

Retrieving nodes and edges

You can retrieve a set of all nodes in the graph by calling the nodes method:

myGraph.nodes(); // returns Set { 'B', 'C' }

You can retrieve a set of all nodes connected to a given node by calling the get method and passing in the node's key:

myGraph.get('B'); // returns Set { 'C' }

You can retrieve an array of all edges in the graph by calling the edges method:

myGraph.edges(); // returns [ [ 'B', 'C' ] ]

Clearing the graph

You can clear all nodes and edges from the graph by calling the clear method:

myGraph.clear();

Examples

Creating a social network graph

const Graph = require('./graph');

const socialNetwork = Graph([
  ['Alice', 'Tom'],
  ['Bob', 'Tom'],
  ['Greg', 'Bob'],
  ]);
socialNetwork.get('Bob') // returns [ 'Tom', 'Greg' ]
socialNetwork.has('Bob')('Alice') // returns false
socialNetwork.set('Bob')('Alice') 
socialNetwork.has('Bob')('Alice') // returns true
socialNetwork.has('Alice')('Bob') // returns true
socialNetwork.get('Bob') // returns [ 'Tom', 'Greg', 'Alice' ]
socialNetwork.remove('Bob')('Tom')
socialNetwork.get('Bob') // returns [ 'Greg', 'Alice' ]