-
Notifications
You must be signed in to change notification settings - Fork 267
/
GraphSearch.js
194 lines (165 loc) · 5.34 KB
/
GraphSearch.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
/*
* (A)---------------(B)-------------(E)---------------(F)
* | | | |
* | | | |
* | | | |
* (C)---------------(D)-------------(G) |
* | |
* -----------------------------------
*
* 7 Vertex
* 9 Edges
*
*/
class No {
/**
* @type {Number}
*/
static MAX_VERTEX;
/**
* @type {Number}
*/
static MAX_EDGES;
static constructor() {
this.setMaxVertex(7);
}
/**
* @param {Number} n Max Vertex
*
* @description When setting the maximum vertex, the maximum edges are automatically calculated
*/
static setMaxVertex(n) {
this.MAX_VERTEX = n;
this.MAX_EDGES = (n * (n - 1));
}
/**
* @type {Array<No>}
*/
noNeighbors;
/**
* @param {Number|String} id
*/
constructor(id) {
this.id = id;
this.numberNeighbors = 0;
this.visited = false;
this.noNeighbors = [];
}
/**
* @param {No} vertexCurrent
* @param {No} vertexNeighbor
*/
static linkVertex(vertexCurrent, vertexNeighbor) {
// Adds the new neighbor to the list of neighbors
vertexCurrent.noNeighbors.push(vertexNeighbor);
vertexCurrent.numberNeighbors++;
vertexNeighbor.noNeighbors.push(vertexCurrent);
vertexNeighbor.numberNeighbors++;
}
}
class GraphSearch {
/**
* In-Depth Search - DFS - Depth First Search
* Cycles through all neighbors that were first connected to the vertex
*
* @param {No} vertexOrigin
* @param {No} vertexDestiny
* @param {Number} visited Position visited
*
* @returns {number} -1 Not found destiny or depth position value
*/
inDepthSearch(vertexOrigin, vertexDestiny, visited) {
vertexOrigin.visited = true;
// If it is the desired destination, it will return
if (vertexOrigin.id === vertexDestiny.id) return visited;
let aux = 0;
// While there is neighbor
while (vertexOrigin.noNeighbors[aux]) {
if (vertexOrigin.noNeighbors[aux].visited === false) {
// Recursively calls passing a new neighbor as a start, that is, it will go through all its neighbors, and so on, successively
const result = this.inDepthSearch(vertexOrigin.noNeighbors[aux], vertexDestiny, visited + 1);
// If the result is different from -1, then the target value was found
if (result !== -1) return result;
}
// Next of list
aux++;
}
// Vertex not found
return -1;
}
/**
*
* @param {No} vertexOrigin
* @param {No} vertexDestiny
*
* @returns {number} -1 Not found destiny or depth position value
*/
widthSearch(vertexOrigin, vertexDestiny) {
let startQueue = 0;
let lengthQueue = 1;
/**
* @type {Array<No>} Vertex queue be compared
*/
let queue = new Array(No.MAX_VERTEX);
queue[startQueue] = vertexOrigin;
while (startQueue < lengthQueue) {
// If it is the desired destination, it will return
if (queue[startQueue].id === vertexDestiny.id) return startQueue;
/*
* For all neighbors of the vertex that is about to 'exit' the queue:
* Mark everyone as visited, so as not to put them in the queue again, and then put them in the queue, and increase the size of the queue.
*/
for (let i = 0; i < queue[startQueue].numberNeighbors; i++) {
const vertexCurrent = queue[startQueue];
if (!vertexCurrent.noNeighbors[i].visited) {
vertexCurrent.noNeighbors[i].visited = true;
queue[lengthQueue] = vertexCurrent.noNeighbors[i];
lengthQueue++;
}
}
// Increments 1 at the beginning of the queue, as if it excluded the first one who entered the queue (FIFO - First In First Out)
startQueue++;
}
return -1;
}
}
function main() {
// Graph - set of independent vertices
let A = new No('A');
let B = new No('B');
let C = new No('C');
let D = new No('D');
let E = new No('E');
let F = new No('F');
let G = new No('G');
// Connects all vertex according to the GRAPH shown in the introduction
No.linkVertex(A, B);
No.linkVertex(A, C);
No.linkVertex(B, D);
No.linkVertex(B, E);
No.linkVertex(C, D);
No.linkVertex(D, F);
No.linkVertex(D, G);
No.linkVertex(E, F);
No.linkVertex(E, G);
const graphSearch = new GraphSearch();
let distance = graphSearch.inDepthSearch(A, G, 0);
if (distance !== -1)
console.log(`DFS - Distance: ${distance}`);
else
console.log("DFS - Not found.");
// Clears all visits
A.visited = false;
B.visited = false;
C.visited = false;
D.visited = false;
E.visited = false;
F.visited = false;
G.visited = false;
distance = graphSearch.widthSearch(A, G);
if (distance !== -1)
console.log(`BFS - Distance: ${distance}`);
else
console.log("BFS - Not found.");
}
main();