-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.js
More file actions
38 lines (34 loc) · 906 Bytes
/
10.js
File metadata and controls
38 lines (34 loc) · 906 Bytes
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
// graph ma'lumotlar tuzilmasi va breadth first search algoritm
let graph = {
zokirkhon: ["ali", "vali", "tohir"],
ali: ["aziza", "olim"],
vali: ["botir", "ziyoda"],
tohir: ["elonmusk", "mohir"],
aziza: [],
olim: [],
botir: [],
ziyoda: ["aziza"],
elonmusk: [],
mohir: [],
};
function search(graph, startNode, target) {
let searchQueue = [];
searchQueue.push(...graph[startNode]);
let searched = [];
while(searchQueue){
// console.log(searchQueue)
let shifted = searchQueue.shift()
if(!searched.includes(shifted)){
if(shifted == target){
console.log(`${target} ni topdik!`)
console.log(searched)
return true;
} else {
searchQueue.push(...graph[shifted])
searched.push(shifted)
}
}
}
return false
}
console.log(search(graph, "zokirkhon", "mohir"))