This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathunweightedGraph.js
More file actions
154 lines (145 loc) · 3.67 KB
/
Copy pathunweightedGraph.js
File metadata and controls
154 lines (145 loc) · 3.67 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* @author Rashik Ansar
* Implemtaion of graph with the help of Adjacency List
* Its Unweighted graph implemetation
*/
class Graph {
constructor() {
this.adjacencyList = {};
}
/**
* Adding a vertex to the graph
* @param {*} vertex The data of the vertex to add into graph
* @returns {Graph}
*/
addVertex(vertex) {
if (this.adjacencyList[vertex]) {
throw Error(`${vertex} already exist in graph... `);
} else {
this.adjacencyList[vertex] = [];
}
// return this;
}
/**
* Removing a vertex from the graph
* @param {*} vertex The data of the vertex to remove from graph
*/
removeVertex(vertex) {
if (this.adjacencyList[vertex]) {
while (this.adjacencyList[vertex].length) {
const itemInArray = this.adjacencyList[vertex].pop();
this.removeEdge(vertex, itemInArray);
}
delete this.adjacencyList[vertex];
// return this; // Added for testing before traversal methods
} else {
throw Error(`${vertex} doesn't exist...`);
}
}
/**
* Adding an edge between two vertices in the graph
* @param {*} vertex1
* @param {*} vertex2
*/
addEdge(vertex1, vertex2) {
if (this.adjacencyList[vertex1] && this.adjacencyList[vertex2]) {
this.adjacencyList[vertex1].push(vertex2);
this.adjacencyList[vertex2].push(vertex1);
// return this;
} else {
throw Error('Given vertex/vertices might not exist in graph...');
}
}
/**
* Removing an existing edge between two vertices in the graph
* @param {*} vertex1
* @param {*} vertex2
*/
removeEdge(vertex1, vertex2) {
if (this.adjacencyList[vertex1] && this.adjacencyList[vertex2]) {
this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter(
i => i !== vertex2
);
this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter(
i => i !== vertex1
);
// return this;
} else {
throw Error('Given vertex/vertices might not exist in graph...');
}
}
/**
* Travesal of the graph by breadth-first approach
* @param {*} start Starting vertex for traversal
* @returns {[]}
*/
breadthFirstTraversal(start) {
const queue = [];
const result = [];
const visited = {};
let current;
queue.push(start);
visited[start] = true;
while (queue.length) {
current = queue.shift();
result.push(current);
this.adjacencyList[current].forEach(x => {
if (!visited[x]) {
visited[x] = true;
queue.push(x);
}
});
}
return result;
}
/**
* Depth First Traversal (recursive approach)
*
* @param {*} start Starting vertex for traversal
* @returns {[]}
*/
DFTrecursuive(start) {
const visited = {};
const result = [];
const adjacencyList = this.adjacencyList;
function dfs(vertex) {
if (!vertex) {
throw Error('Incorrect starting vertex!');
}
visited[vertex] = true;
result.push(vertex);
adjacencyList[vertex].forEach(x => {
if (!visited[x]) {
return dfs(x);
}
});
}
dfs(start);
return result;
}
/**
* Depth First Traversal (Iterative approach)
*
* @param {*} start Starting vertex for traversal
* @returns {[]}
*/
DFTiterative(start) {
const stack = [];
const visited = {};
const result = [];
let current;
stack.push(start);
visited[start] = true;
while (stack.length) {
current = stack.pop();
result.push(current);
this.adjacencyList[current].forEach(x => {
if (!visited[x]) {
visited[x] = true;
stack.push(x);
}
});
}
return result;
}
}