Skip to content

📦 Graph & Directed Graph implementation in javascript

License

Notifications You must be signed in to change notification settings

bieblebrox/graph

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@datastructures-js/graph

build:? npm npm npm

graph

Table of Contents

install

npm install --save @datastructures-js/graph

API

require

const { Graph, DirectedGraph } = require('@datastructures-js/graph');

import

import { Graph, DirectedGraph } from '@datastructures-js/graph';

create a graph

creates an empty graph

Example

const directedGraph = new DirectedGraph();

const graph = new Graph();

.addVertex(key, value)

adds a vertex to the graph.

params
nametype
keynumber or string
valueobject
return
Vertex
runtime
O(1)

Example

directedGraph.addVertex('v1', 1);
directedGraph.addVertex('v1', 1);
directedGraph.addVertex('v2', 2);
directedGraph.addVertex('v3', 3);
directedGraph.addVertex('v4', 4);
directedGraph.addVertex('v5', 5);

graph.addVertex('v1', true);
graph.addVertex('v2', true);
graph.addVertex('v3', true);
graph.addVertex('v4', true);
graph.addVertex('v5', true);

.hasVertex(key)

checks if the graph has a vertex by its key.

params
nametype
keynumber or string
return
boolean
runtime
O(1)

Example

console.log(directedGraph.hasVertex('v7')); // false
console.log(graph.hasVertex('v1')); // true

.verticesCount()

gets the number of vertices in the graph.

return
number
runtime
O(1)

Example

console.log(directedGraph.verticesCount()); // 5
console.log(graph.verticesCount()); // 5

.addEdge(srcKey, destKey, weight)

adds an edge with a weight between two existings vertices. Default weight is 1 if not defined. The edge is a direction from source to destination when added in a directed graph, and a connecting two-way edge when added in a graph.

params
nametypedescription
srcKeynumber or stringthe source vertex key
destKeynumber or stringthe destination vertex key
weightnumberthe weight of the edge
runtime
O(1)

Example

directedGraph.addEdge('v1', 'v2', 2);
directedGraph.addEdge('v1', 'v3', 3);
directedGraph.addEdge('v1', 'v4', 1);
directedGraph.addEdge('v2', 'v4', 1);
directedGraph.addEdge('v3', 'v5', 2);
directedGraph.addEdge('v4', 'v3', 1);
directedGraph.addEdge('v4', 'v5', 4);

graph.addEdge('v1', 'v2', 2);
graph.addEdge('v2', 'v3', 3);
graph.addEdge('v1', 'v3', 6);
graph.addEdge('v2', 'v4', 1);
graph.addEdge('v4', 'v3', 1);
graph.addEdge('v4', 'v5', 4);
graph.addEdge('v3', 'v5', 2);

.hasEdge(srcKey, destKey)

checks if the graph has an edge between two existing vertices. In directed graph, it returns true only if there is a direction from source to destination.

params
nametypedescription
srcKeynumber or stringthe source vertex key
destKeynumber or stringthe destination vertex key
return
boolean
runtime
O(1)

Example

console.log(directedGraph.hasEdge('v1', 'v2')); // true
console.log(directedGraph.hasEdge('v2', 'v1')); // false

console.log(graph.hasEdge('v1', 'v2')); // true
console.log(graph.hasEdge('v2', 'v1')); // true

.edgesCount()

gets the number of edges in the graph.

return
number
runtime
O(1)

Example

console.log(directedGraph.edgesCount()); // 7
console.log(graph.edgesCount()); // 7

.getWeight(srcKey, destKey)

gets the edge's weight between two vertices in the graph. If there is no direct edge between the two vertices, it returns null. It also returns 0 if the source key is equal to destination key.

params
nametypedescription
srcKeynumber or stringthe source vertex key
destKeynumber or stringthe destination vertex key
return
number
runtime
O(1)

Example

console.log(directedGraph.getWeight('v1', 'v2')); // 2
console.log(directedGraph.getWeight('v2', 'v1')); // null
console.log(directedGraph.getWeight('v1', 'v1')); // 0

console.log(graph.getWeight('v1', 'v2')); // 2
console.log(graph.getWeight('v2', 'v1')); // 2
console.log(graph.getWeight('v1', 'v1')); // 0
console.log(graph.getWeight('v1', 'v4')); // null

.removeVertex(key)

removes a vertex with all its edges from the graph by its key.

params
nametypedescription
keynumber or stringthe vertex key
return
boolean
runtime
GraphO(K) : K = number of connected edges to the vertex
Directed GraphO(E) : E = number of edges in the graph

Example

directedGraph.removeVertex('v5');
console.log(directedGraph.verticesCount()); // 4
console.log(directedGraph.edgesCount()); // 5

graph.removeVertex('v5');
console.log(graph.verticesCount()); // 4
console.log(graph.edgesCount()); // 5

.removeEdge(srcKey, destKey)

removes an edge between two existing vertices

params
nametypedescription
srcKeynumber or stringthe source vertex key
destKeynumber or stringthe destination vertex key
return
boolean
runtime
O(1)

Example

directedGraph.removeEdge('v1', 'v3'); // true
console.log(directedGraph.edgesCount()); // 4

graph.removeEdge('v2', 'v3'); // true
console.log(graph.edgesCount()); // 4

.removeEdges(key)

removes all connected edges to a vertex by its key.

params
nametypedescription
keynumber or stringthe vertex key
returndescription
numbernumber of removed edges
runtime
GraphO(K) : K = number of connected edges to the vertex
Directed GraphO(E) : E = number of edges in the graph

Example

const dg = new DirectedGraph();
dg.addVertex('v1');
dg.addVertex('v2');
dg.addVertex('v3');
dg.addEdge('v1', 'v2');
dg.addEdge('v2', 'v1'); // this is counted as a direction in directed graph.
dg.addEdge('v1', 'v3');
dg.removeEdges('v1'); // 3

const g = new Graph();
g.addVertex('v1');
g.addVertex('v2');
g.addVertex('v3');
g.addEdge('v1', 'v2');
g.addEdge('v1', 'v3');
g.removeEdges('v1'); // 2

.traverseDfs(srcKey, cb)

traverses the graph using the depth-first recursive search.

params
nametypedescription
srcKeynumber or stringthe starting vertex key
cbfunctionthe callback that is called with each vertex
runtime
O(V) : V = the number of vertices in the graph

Example

directedGraph.traverseDfs('v1', (v) => console.log(`${v.getKey()}:${v.getValue()}`));
/*
v1:1
v2:2
v4:4
v3:3
*/

graph.traverseDfs('v1', (v) => console.log(v.serialize()));
/*
{ key: 'v1', value: true }
{ key: 'v2', value: true }
{ key: 'v4', value: true }
{ key: 'v3', value: true }
*/

.traverseBfs(srcKey, cb)

traverses the graph using the breadth-first search with a queue.

params
nametypedescription
srcKeynumber or stringthe starting vertex key
cbfunctionthe callback that is called with each vertex
runtime
O(V) : V = the number of vertices in the graph

Example

directedGraph.traverseBfs('v1', (v) => console.log(`${v.getKey()}:${v.getValue()}`));
/*
v1:1
v2:2
v4:4
v3:3
*/

graph.traverseBfs('v1', (v) => console.log(v.serialize()));
/*
{ key: 'v1', value: true }
{ key: 'v2', value: true }
{ key: 'v3', value: true }
{ key: 'v4', value: true }
*/

.clear()

clears all vertices and edges in the graph.

runtime
O(1)

Example

directedGraph.clear();
console.log(directedGraph.verticesCount()); // 0
console.log(directedGraph.edgesCount()); // 0

graph.clear();
console.log(graph.verticesCount()); // 0
console.log(graph.edgesCount()); // 0

Vertex

.getKey()

returns the vertex key.

return
string or number

.getValue()

returns the vertex associated value.

return
object

Build

grunt build

License

The MIT License. Full License is here

About

📦 Graph & Directed Graph implementation in javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%