const queue = [start];
const visited = new Set([key(start)]);
while (queue.length > 0) {
const current = queue.shift();
for (const next of neighbors(current)) {
if (visited.has(key(next))) continue;
visited.add(key(next));
previous.set(key(next), key(current));
queue.push(next);
}
}
Searching
Linear vs binary search
O(n) vs O(log n)
線形探索は左から順に調べます。二分探索は整列済み配列の中央を見て、探索範囲を半分ずつ捨てます。
Linear
Binary
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (items[mid] === target) return mid;
if (items[mid] < target) low = mid + 1;
else high = mid - 1;
}