-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweightedGraph.js
203 lines (190 loc) · 5.53 KB
/
weightedGraph.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
class Graph {
constructor() {
this.adjList = {};
}
addVertex(vertex) {
if (!this.adjList[vertex]) {
this.adjList[vertex] = {};
}
}
addEdge(vertex1, vertex2, weight) {
if (!this.adjList[vertex1]) {
this.addVertex(vertex1);
}
if (!this.adjList[vertex2]) {
this.addVertex(vertex2);
}
this.adjList[vertex1][vertex2] = weight;
this.adjList[vertex2][vertex1] = weight;
}
removeEdge(vertex1, vertex2) {
delete this.adjList[vertex1][vertex2];
delete this.adjList[vertex2][vertex1];
}
hasEdge(vertex1, vertex2) {
return (
this.adjList[vertex1] &&
this.adjList[vertex2] &&
vertex2 in this.adjList[vertex1] &&
vertex1 in this.adjList[vertex2]
);
}
remove(vertex) {
if (!this.adjList[vertex]) {
return;
}
for (let adjV in this.adjList[vertex]) {
this.removeEdge(vertex, adjV);
}
delete this.adjList[vertex];
}
bfs(startVertex) {
const queue = [];
const visited = {};
visited[startVertex] = true;
queue.push(startVertex);
while (queue.length) {
const currentVertex = queue.shift();
console.log(currentVertex);
for (let neighbor in this.adjList[currentVertex]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.push(neighbor);
}
}
}
}
dfs(startVertex) {
const visited = {};
const stack = [];
visited[startVertex] = true;
stack.push(startVertex);
while (stack.length > 0) {
const currentVertex = stack.pop();
console.log(currentVertex);
for (let neighbor in this.adjList[currentVertex]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
stack.push(neighbor);
}
}
}
}
dijkstra(startVertex) {
const distances = {};
const visited = {};
for (let vertex in this.adjList) {
distances[vertex] = Infinity;
visited[vertex] = false;
}
distances[startVertex] = 0;
while (true) {
const currentVertex = this.getMinDistanceVertex(distances, visited);
if (!currentVertex) break;
visited[currentVertex] = true;
for (let neighbor in this.adjList[currentVertex]) {
const weight = this.adjList[currentVertex][neighbor];
const totalDistance = distances[currentVertex] + weight;
if (!visited[neighbor] && totalDistance < distances[neighbor]) {
distances[neighbor] = totalDistance;
}
}
}
for (let vertex in distances) {
console.log(
`Shortest distance from ${startVertex} to ${vertex}: ${distances[vertex]}`
);
}
}
getMinDistanceVertex(distances, visited) {
let minDistance = Infinity;
let minVertex = null;
for (let vertex in distances) {
if (!visited[vertex] && distances[vertex] < minDistance) {
minDistance = distances[vertex];
minVertex = vertex;
}
}
return minVertex;
}
prim() {
const result = [];
const visited = {};
const startVertex = Object.keys(this.adjList)[0];
visited[startVertex] = true;
while (Object.keys(visited).length < Object.keys(this.adjList).length) {
let minWeight = Infinity;
let minEdge = null;
for (let vertex in visited) {
for (let neighbor in this.adjList[vertex]) {
if (
!visited[neighbor] &&
this.adjList[vertex][neighbor] < minWeight
) {
minWeight = this.adjList[vertex][neighbor];
minEdge = [vertex, neighbor];
}
}
}
if (minEdge) {
const [from, to] = minEdge;
result.push({ edge: [from, to], weight: minWeight });
visited[to] = true;
}
}
console.log("Minimum Spanning Tree (Prim's Algorithm):", result);
}
kruskal() {
const result = [];
const edges = [];
for (let vertex in this.adjList) {
for (let neighbor in this.adjList[vertex]) {
edges.push({
edge: [vertex, neighbor],
weight: this.adjList[vertex][neighbor],
});
}
}
edges.sort((a, b) => a.weight - b.weight);
const parent = {};
const find = (vertex) => {
if (parent[vertex] === undefined) return vertex;
return find(parent[vertex]);
};
const union = (x, y) => {
const rootX = find(x);
const rootY = find(y);
if (rootX !== rootY) {
parent[rootX] = rootY;
return true;
}
return false;
};
edges.forEach(({ edge, weight }) => {
const [from, to] = edge;
if (union(from, to)) {
result.push({ edge, weight });
}
});
console.log("Minimum Spanning Tree (Kruskal's Algorithm):", result);
}
display() {
for (let vertex in this.adjList) {
const neighbors = this.adjList[vertex];
let neighborsStr = "";
for (let neighbor in neighbors) {
const weight = neighbors[neighbor];
neighborsStr += `${neighbor} (${weight})`;
}
console.log(vertex + "-->" + neighborsStr);
}
}
}
const g = new Graph();
g.addVertex("A");
g.addVertex("B");
g.addVertex("C");
g.addEdge("A", "B", 7);
g.addEdge("A", "E", 1);
g.addEdge("B", "C", 3);
g.addEdge("B", "E", 8);