forked from Seneca-CDOT/thecollectors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.js
More file actions
68 lines (56 loc) · 1.96 KB
/
Copy pathGraph.js
File metadata and controls
68 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function Graph() {
// Dictionary of Node ID/Node Connection List pairs
this.nodeDictionary = {};
}
Graph.prototype.addNode = function(node, connectionIdList) {
if (this.nodeDictionary[node.id] != undefined) {
console.warn("Node already exists in the graph. Duplicate attempt to add node terminated.");
return;
}
if (connectionIdList && connectionIdList.indexOf(node.id) > -1) {
console.warn("Attempt to add node to itself terminated.");
return;
}
if (node instanceof Node) {
this.nodeDictionary[node.id].push([node]);
var len = connectionIdList ? connectionIdList.length : 0;
for (i = 0; i < len; i++) {
// Push the new node into its connections' lists
this.addConnections(connectionIdList[i], node);
// Push the existing node connections into the new node's list
this.addConnection(node.id, this.findNodeArray(connectionIdList[i])[0]);
}
}
}
Graph.prototype.clearGraph = function() {
this.nodeDictionary = {};
}
Graph.prototype.addConnection = function(nodeID, nodeToConnect) {
var nodeArray = this.findNodeArray(nodeID);
if (nodeArray != undefined) {
nodeArray.push(nodeToConnect);
} else {
console.error("Cannot add node connection. Node array not found!");
}
}
Graph.prototype.findNodeArray = function(nodeID) {
return this.nodeDictionary[nodeID];
}
Graph.prototype.areNodesConnected = function(nodeID, nodeIDToMatch) {
if (nodeID == nodeIDToMatch) {
console.warn("Node cannot be connected to itself.");
return false;
}
var nodeArray = this.findNodeArray(nodeID);
if (nodeArray != undefined) {
var len = nodeArray.length;
for (i = 1; i < len; i++) {
if (nodeArray[i].id == nodeIDToMatch) {
return true;
}
}
return false;
} else {
console.error("Invalid NodeID. Node does not exist.");
}
}