graphs-all
Advanced tools
Comparing version 0.0.1 to 0.1.0
{ | ||
"name": "graphs-all", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Graph Data Structure Library", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -1,14 +0,66 @@ | ||
function Graph() { | ||
var counterId = 0; | ||
var keyToIdTable = {}; | ||
this.nodes = []; | ||
function Node(key, value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
function Graph(isDirected, isWeighted) { | ||
isDirected = isDirected || false; | ||
isWeighted = isWeighted || false; | ||
var counterId = 0, | ||
keyToIdTable = {}; | ||
this.nodes = {}; | ||
this.links = {}; | ||
this.addNode = function(key, value) { | ||
if (keyToIdTable[key] === undefined) { | ||
var id = counterId++; | ||
keyToIdTable[key] = id; | ||
this.nodes[id] = new Node(key, value); | ||
this.links[id] = {}; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
this.addLink = function(key1, key2, weight) { | ||
if (!isWeighted || weight === undefined) { | ||
weight = 1; | ||
} | ||
if (isNaN(weight)) { | ||
throw "Weight must be a number"; | ||
} | ||
var id1 = keyToIdTable[key1], | ||
id2 = keyToIdTable[key2]; | ||
if (id1 === undefined) { | ||
throw "'" + key1 + "' is not found." | ||
} | ||
if (id2 === undefined) { | ||
throw "'" + key2 + "' is not found." | ||
} | ||
this.links[id1][id2] = weight; | ||
if (!isDirected) { | ||
this.links[id2][id1] = weight; | ||
} | ||
}; | ||
this.hasNode = function(key) { | ||
return keyToIdTable[key] !== undefined; | ||
}; | ||
this.hasLink = function(key1, key2) { | ||
return this.hasNode(key1) && this.hasNode(key2) && this.links[key1][key2] !== undefined; | ||
} | ||
} | ||
function emptyGraph() { | ||
return new Graph(); | ||
function createGraph(isDirected, isWeighted) { | ||
return function(){ | ||
return new Graph(isDirected, isWeighted); | ||
}; | ||
} | ||
module.exports = { | ||
emptyGraph: emptyGraph | ||
Graph: Graph, | ||
createGraph: createGraph | ||
}; |
@@ -6,3 +6,6 @@ "use strict"; | ||
module.exports = { | ||
emptyGraph: graph.emptyGraph | ||
UndirectedUnweightedGraph: graph.createGraph(false, false), | ||
DirectedUnweightedGraph: graph.createGraph(true, false), | ||
DirectedWeightedGraph: graph.createGraph(true, true), | ||
UndirectedWeightedGraph: graph.createGraph(false, true) | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63
4133
5