-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.js
More file actions
53 lines (43 loc) · 1.17 KB
/
Copy pathgraph.js
File metadata and controls
53 lines (43 loc) · 1.17 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
const GraphNode = function(data) {
this.data = data
this.edges = new Set()
}
const Graph = function() {
this.nodes = new Set()
}
Graph.prototype.addNode = function(data) {
const newNode = new GraphNode(data)
this.nodes.add(newNode)
return newNode
}
Graph.prototype.addEdge = function(node_a, node_b) {
if (this.nodes.has(node_a) && this.nodes.has(node_b)) {
node_a.edges.add(node_b)
node_b.edges.add(node_a)
}
else {
console.log('WARNING! tried to add an edge for one or more nodes not in graph')
}
}
Graph.prototype.printNodesData = function() {
this.nodes.forEach(node => console.log(node.data))
}
/**
*
* @param {GraphNode} origin
* @param {GraphNode} destination
*/
Graph.prototype.dfs = function(origin, destination) {
}
const lg = new Graph()
const Celeste = lg.addNode('Celeste')
const Kelcey = lg.addNode('Kelcey')
const Jonathan = lg.addNode('Jonathan')
const Mira = lg.addNode('Mira')
const Sabrin = lg.addNode('Sabrin')
const Patrick = lg.addNode('Patrick')
const James = lg.addNode('James')
const Bonnie = lg.addNode('Bonnie')
const Voldemort = new GraphNode('Voldemort')
lg.addEdge(Jonathan, James)
lg.addEdge(Jonathan, Voldemort)