-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgraph.js
56 lines (49 loc) · 1.21 KB
/
graph.js
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
// create a graph class
// https://www.geeksforgeeks.org/implementation-graph-javascript/
// https://www.youtube.com/watch?v=pcKY4hjDrxk
class Graph {
constructor(noOfVertices) {
this.noOfVertices = noOfVertices;
this.AdjList = new Map();
}
addVertex(v) {
// initialize the adjacent list with a
// null array
this.AdjList.set(v, []);
}
addEdge(v, w) {
// get the list for vertex v and put the
// vertex w denoting edge between v and w
this.AdjList.get(v).push(w);
// Since graph is undirected,
// add an edge from w to v also
this.AdjList.get(w).push(v);
}
printGraph() {
let values = this.AdjList.keys(); //?
for (let i of values) {
let edge = i; //?
this.AdjList.get(edge); //?
}
}
bfs(startingNode) {
let queue = [startingNode];
}
}
// Using the above implemented graph class
var g = new Graph(6);
var vertices = ["A", "B", "C", "D", "E", "F"];
// adding vertices
for (var i = 0; i < vertices.length; i++) {
g.addVertex(vertices[i]);
}
// adding edges
g.addEdge("A", "B");
g.addEdge("A", "D");
g.addEdge("A", "E");
g.addEdge("B", "C");
g.addEdge("D", "E");
g.addEdge("E", "F");
g.addEdge("E", "C");
g.addEdge("C", "F");
g.printGraph();