-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundirectedGraph.js
More file actions
128 lines (121 loc) · 3.47 KB
/
undirectedGraph.js
File metadata and controls
128 lines (121 loc) · 3.47 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// class to define our undirected graph
class Graph {
constructor() {
this.adjacencyList = {};
}
addNode(node) {
if (!this.adjacencyList[node]) return this.adjacencyList[node] = [];
return 'There is already a node with this key in the adjacency list';
}
addEdge(v1, v2) {
if (this.adjacencyList[v1] && (
!this.adjacencyList[v1].includes(v2) &&
!this.adjacencyList[v2].includes(v1)
)) {
this.adjacencyList[v1].push(v2);
this.adjacencyList[v2].push(v1);
}
}
removeEdge(v1, v2) {
if (this.adjacencyList[v1].includes(v2)) {
const v2Index = this.adjacencyList[v1].indexOf(v2);
this.adjacencyList[v1].splice(v2Index, 1);
}
if (this.adjacencyList[v2].includes(v1)) {
const v1Index = this.adjacencyList[v2].indexOf(v1);
this.adjacencyList[v2].splice(v1Index, 1);
}
}
removeNode(node) {
const edgesFromnode = this.adjacencyList[node];
while (edgesFromnode.length > 0) {
this.removeEdge(node, edgesFromnode[0]); // always remove the first edge in edges array for a node
}
delete this.adjacencyList[node];
}
dfsRecursive(node) {
const result = [];
const visited = {};
const adjacencyList = this.adjacencyList;
const dfs = (vertex) => {
if (!vertex) return null;
result.push(vertex);
visited[vertex] = true;
for (let neighbour of adjacencyList[vertex]) {
if (!visited[neighbour]) {
dfs(neighbour);
}
}
}
dfs(node);
return result;
}
dfsIterative(node) {
let stack = [node];
let result = [];
let visited = {};
let currentVertex;
visited[node] = true;
while (stack.length) {
currentVertex = stack.pop();
result.push(currentVertex);
this.adjacencyList[currentVertex].forEach(neighbour => {
if (!visited[neighbour]) {
visited[neighbour] = true;
stack.push(neighbour);
}
});
}
return result;
}
bfs(node) {
let queue = [node];
let result = [];
let visited = {};
let currentVertex;
visited[node] = true;
while (queue.length) {
currentVertex = queue.shift();
result.push(currentVertex);
this.adjacencyList[currentVertex].forEach(neighbour => {
if (!visited[neighbour]) {
visited[neighbour] = true;
queue.push(neighbour);
}
});
}
return result;
}
}
var undirectedGraph = new Graph();
const nodesToAdd = ['A', 'B', 'C', 'D', 'E', 'F'];
const edgesToAdd = [
{ firstnode: 'A', secondnode: 'B' },
{ firstnode: 'A', secondnode: 'C' },
{ firstnode: 'B', secondnode: 'D' },
{ firstnode: 'C', secondnode: 'E' },
{ firstnode: 'D', secondnode: 'E' },
{ firstnode: 'D', secondnode: 'F' },
{ firstnode: 'F', secondnode: 'D' },
{ firstnode: 'F', secondnode: 'E' }
];
const nodesToRemove = ['A', 'B', 'D', 'E', 'F', 'C'];
// add nodees to undirected graph
for (let node of nodesToAdd) {
undirectedGraph.addNode(node);
}
console.log(undirectedGraph);
// add edges between nodees in undirected graph
for (let edges of edgesToAdd) {
undirectedGraph.addEdge(edges.firstnode, edges.secondnode);
}
console.log(undirectedGraph);
// traverse graph
console.log(undirectedGraph.dfsRecursive("A"));
console.log(undirectedGraph.dfsIterative("A"));
console.log(undirectedGraph.bfs("A"));
// remove nodees and the edges between said nodees in undirected graph
for (let node of nodesToRemove) {
undirectedGraph.removeNode(node);
console.log(undirectedGraph);
}