Toshkent axborot texnologiyalari universiteti fargona filiali


Download 260.5 Kb.
bet3/6
Sana21.04.2023
Hajmi260.5 Kb.
#1373796
1   2   3   4   5   6
Bog'liq
734-20 D Xolmatov

Chizma:

Yechim:
const graph = { 1: [2, 3, 4], 2: [4], 3: [4, 5], 4: [5] };

// 1 tug'unidan 5 tug'uniga borish yo'lini topish


const queue = [1];
const visited = new Set();
const parent = {};
let found = false;


while (queue.length > 0) {
const current = queue.shift();
visited.add(current);
if (current === 5) {
found = true;
break;
}
for (const neighbor of graph[current]) {
if (!visited.has(neighbor)) {
parent[neighbor] = current;
queue.push(neighbor);
}
}
}

// Natijani ekranga chiqaramiz


if (found) {
console.log("5 tug'uniga borish mumkin!");
let path = "5";
let node = 5;
while (node !== 1) {
path = parent[node] + " -> " + path;
node = parent[node];
}
console.log("Yo'l: " + path);
} else {
console.log("5 tug'uniga borish imkonsiz!");
}

Natija:


Laborato‘riya ishi – 6

Mavzu: Kesishmaydigan to‘plam ostilari va birlashmalarini qidirish algoritmi. NP-to‘liq masalalar. NP-to‘liq masalalarga keltirish usullari.

Ishdan maqsad. Kesishmaydigan to‘plam ostilari va birlashmalarini qidirish algoritmi. NP-to‘liq masalalar. NP-to‘liq masalalarga keltirish usullari.
Qo’yilgan masala. Kesishmaydigan to’plam ostilari va birlashmalarini qidirish algoritmi.
Ish tartibi:



Download 260.5 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling