Mengapa log di big-O pencarian biner bukan basis 2?

35

Saya baru memahami algoritma ilmu komputer. Saya mengerti proses pencarian biner, tetapi saya memiliki sedikit kesalahpahaman dengan efisiensinya.

Dalam ukuran elemen, dibutuhkan, rata-rata, n langkah untuk menemukan elemen tertentu. Mengambil logaritma basis 2 dari kedua sisi menghasilkan log 2 ( s ) = n . Jadi bukankah jumlah rata-rata langkah untuk algoritma pencarian biner adalah log 2 ( s ) ?s=2nnlog2(s)=nlog2(s)

Artikel Wikipedia ini tentang algoritma pencarian biner mengatakan bahwa kinerja rata-rata adalah . Kenapa begitu? Mengapa angka ini bukan log 2 ( n ) ?O(logn)log2(n)

DrPepper
sumber
2
Duplicate on SO - Is Big O(logn) log base e?
Dukeling

Jawaban:

86

When you change the base of logarithm the resulting expression differs only by a constant factor which, by definition of Big-O notation, implies that both functions belong to the same class with respect to their asymptotic behavior.

For example

log10n=log2nlog210=Clog2n
where C=1log210.

So log10n and log2n differs by a constant C, and hence both are true:

log10n is O(log2n)
log2n is O(log10n)
In general logan is O(logbn) for positive integers a and b greater than 1.

Another interesting fact with logarithmic functions is that while for constant k>1, nk is NOT O(n), but lognk is O(logn) since lognk=klogn which differs from logn by only constant factor k.

fade2black
sumber
Not only for positive integers: For all real a,b>1, e.g. e.
nbubis
2
I would add that this is the reason why with big-O notation, the base of the logarithm is commonly not specified. It also means there is no confusion about which base O(logn) uses: it can be any of the commonly used based, since it doesn't make a difference.
svick
9

In addition to fade2black's answer (which is completely correct), it's worth noting that the notation "log(n)" is ambiguous. The base isn't actually specified, and the default base changes based on context. In pure mathematics, the base is almost always assumed to be e (unless specified), while in certain engineering contexts it might be 10. In computer science, base 2 is so ubiquitous that log is frequently assumed to be base 2. That wikipedia article never says anything about the base.

But, as has already been shown, in this case it doesn't end up mattering.

MattPutnam
sumber
7
It is probably further worth noting then that while "log(n)" might be ambiguous that "O(log(n))" is not because the latter only has one meaning, no matter what base you might be thinking of.
Chris