For n≥1, 10n≤10n², so T(n)≤13n². This proves O(n²).
Week 02 · Lecture 2
Counting work and growth
Count a chosen basic operation, distinguish input cases, and use O, Ω and Θ to describe growth. Timing is supporting evidence, not a proof.
What changes when the input doubles?
A procedure performs T(n)=3n²+10n basic operations. Give a tight asymptotic bound.
Also T(n)≥3n² for n≥1, proving Ω(n²).
Together these bounds give Θ(n²). Constants do not change the growth class.
Doubling n gives a ratio approaching 4, not exactly 4 at every finite n.
Change the case. An O(n³) upper bound is also true but less informative. Big-O does not itself mean worst case; first specify which cost function is being bounded.
Explore the animations
Each demonstration states its own input and code. Check its loop bounds before comparing counts with the worked example.
Questions and answers are together. Hard questions are optional; some tests revisit earlier ideas.
Practice with answers
10 test questions · 14 written questions · 24 total
Each question is followed by its answer and explanation. Hard questions also include a starting hint and smaller reasoning steps.
Test questions
Choose one option. Cost questions state their model and whether the bound is tight, expected or worst-case. Here lg means log₂, and heap positions start at 1.
Question 1 · easy
In the RAM model, the statement “sort the list” costs:
Answer: option C. A fixed-size primitive operation has constant cost in the chosen RAM model. Sorting a list is a subroutine, so count the operations performed inside it.
Question 2 · easy
The worst-case running time of an algorithm is:
Answer: option B. It is a guarantee, which is why the course uses it by default.
Question 3 · easy
Which statement about 5n² + 3n is TRUE?
Answer: option C. n³ is a valid, if loose, upper bound; the tight bound is Θ(n²).
Question 4 · easy
The Big-Theta of 4n³ + 100n² + 7 is:
Answer: option B. Keep the fastest-growing term and drop its constant.
Question 5 · medium
Which pair of constants proves 3n² + 10n = O(n²)?
Answer: option B. 3n² + 10n ≤ 4n² exactly when n ≥ 10. (a) never holds, (c) never holds, and (d) fails at n = 1.
Question 6 · medium
Which function eventually grows fastest as n tends to infinity?
Answer: option B. An exponential dominates every polynomial, however high its degree.
Question 7 · medium
Assume time is approximately c·n² with the same c and negligible overhead. A run at n = 1,000 takes 1 second. Estimate the time at n = 4,000.
Answer: option C. The size ratio is 4, so the model predicts 4² × 1 = 16 seconds. A Θ(n²) classification alone does not determine a finite-size timing ratio.
Question 8 · medium
For nonnegative cost functions f(n) = O(n²) and g(n) = O(n), what is the tightest upper bound on f(n)·g(n) guaranteed by this information?
Answer: option C. Products of bounds multiply.
Question 9 · medium
The relationship between log₂ n and log₁₀ n is:
Answer: option A. They differ by the constant factor log₂ 10 ≈ 3.32.
Question 10 · hard · optional challenge
Which statement is FALSE?
In simpler words: Compare eventual growth, not values at one small input.
Starting hint: Any fixed positive power of n eventually beats any fixed power of log n.
Answer: option C. Any positive power of n, including √n, dominates every power of log n.
Step by step
- Option (c) reverses this rule: (log n)²/√n tends to zero.
- The other comparisons follow the ladder: logarithms, powers, exponentials, then factorials.
Written questions
Read each question together with its explanation, trace or proof. Numbering continues from the test questions.
Question 11 · easy
In the RAM model, what does the statement x = a + b cost, and what does the statement “sort the list” cost?
Answer & reasoning
Choose and state an operation-count convention. For fixed-size values, x = a + b uses a fixed number of primitive actions, so it has constant cost. A simplified model may count the whole statement as one step; a finer model counts reads, addition and assignment separately. “Sort the list” costs the work of the sorting algorithm, not one step. Large-integer arithmetic requires a bit-cost model.
Question 12 · easy
Define worst-case running time in one sentence, and say why the course prefers it.
Answer & reasoning
Worst-case running time is the maximum cost over all legal inputs of a fixed size n, under the stated cost model. It provides an upper guarantee, useful when a controller has a deadline. It need not be close to average-case cost. A bound on operation count is not by itself a measured or certified time bound on a real controller.
Question 13 · easy
True or false: 5n² + 3n = O(n³).
Answer & reasoning
true. Big-O is an upper bound, and n³ is a valid (though not tight) upper bound; the tight statement is 5n² + 3n = Θ(n²).
Question 14 · easy
Is 100n = O(n²)? Is n² = O(100n)?
Answer & reasoning
yes (100n ≤ n² once n ≥ 100); no (n² eventually exceeds any constant multiple of n).
Question 15 · easy
Give the Big-Theta of 7n³ − 2n² + 40, and of 3n log n + 50n.
Answer & reasoning
Θ(n³) and Θ(n log n). Keep the fastest-growing term, drop its constant.
Question 16 · easy
Order these from slowest-growing to fastest: 2ⁿ, n log n, n!, log n, n², n, 1.
Answer & reasoning
1, log n, n, n log n, n², 2ⁿ, n!.
Question 17 · medium
Find constants c and n₀ that prove 3n² + 10n = O(n²).
Answer & reasoning
c = 4 and n₀ = 10 work: 3n² + 10n ≤ 4n² is the same as 10n ≤ n², which holds whenever n ≥ 10. (Many other pairs work too, such as c = 13, n₀ = 1.)
Question 18 · medium
Prove that n² is not O(n).
Answer & reasoning
suppose n² ≤ c·n for all n ≥ n₀. Dividing by n gives n ≤ c for all n ≥ n₀, which is false for any n larger than both c and n₀. So no constants exist.
Question 19 · medium
Which grows faster: n^1.01 or n log n? And 2ⁿ or n¹⁰⁰?
Answer & reasoning
n^1.01 eventually grows faster than n log n because log n / n^0.01 tends to zero. Also 2ⁿ eventually exceeds n¹⁰⁰. Near n = 1,000 they are close; for integer n ≥ 1, the final crossover is at n = 997. These are eventual-growth statements, not rankings at every small n.
Question 20 · medium
A program takes 1 second at n = 1,000. Assuming its time is approximately proportional to n², n log₂ n, or 2ⁿ over the range being considered, estimate the time at n = 10,000. What assumption makes the estimate possible?
Answer & reasoning
Quadratic: (10,000/1,000)² = 100 seconds. n log n: 10 × log₂(10,000)/log₂(1,000) = 40/3 ≈ 13.3 seconds. Exponential: 2⁹⁰⁰⁰ seconds under the same idealised model. These extrapolations assume the same leading constant and negligible lower-order effects; a Θ bound alone does not determine finite-size timings. Memory, caching and input-dependent work can invalidate the prediction.
Question 21 · medium
If f(n) = O(n²) and g(n) = O(n log n), what can you say about f(n) + g(n) and about f(n) · g(n)?
Answer & reasoning
f + g = O(n²) (the bigger term wins in a sum) and f · g = O(n³ log n) (products multiply).
Question 22 · medium
Show that log₂ n = Θ(log₁₀ n), and say what this means for Big-O.
Answer & reasoning
by the change-of-base rule, log₂ n = log₁₀ n / log₁₀ 2 ≈ 3.32 × log₁₀ n. A constant factor is invisible to Big-O, so O(log₂ n) = O(log₁₀ n) = O(log n), and we drop the base.
Question 23 · hard · optional challenge
Prove that Big-O is transitive: if f(n) = O(g(n)) and g(n) = O(h(n)), then f(n) = O(h(n)).
In simpler words: Show that two eventual upper bounds can be chained.
Starting hint: Write the two inequalities and choose a point where both are valid.
Answer & reasoning
Step by step
- Transitive means that f being bounded by g, and g by h, also bounds f by h.
- Beyond n₁, f ≤ c₁g. Beyond n₂, g ≤ c₂h. Choose n ≥ max(n₁,n₂).
- Substitute the second inequality into the first: f ≤ c₁c₂h. The new constant is c₁c₂.
there are c₁, n₁ with f(n) ≤ c₁ g(n) for n ≥ n₁, and c₂, n₂ with g(n) ≤ c₂ h(n) for n ≥ n₂. For n ≥ max(n₁, n₂), f(n) ≤ c₁ g(n) ≤ c₁ c₂ h(n). So the constant c₁c₂ and threshold max(n₁, n₂) witness f = O(h).
Question 24 · hard · optional challenge
Explain why log n grows slower than nᵏ for every k > 0, however small k is.
In simpler words: Compare a logarithm with even a tiny positive power.
Starting hint: Replace n by 2ᵐ so the logarithm becomes m.
Answer & reasoning
Step by step
- Take logarithms in base 2; another fixed base changes only a constant.
- Now log₂ n = m and nᵏ = 2^(km). Since k > 0, this is an exponential with base greater than 1.
- When m doubles, the linear part merely doubles, while the exponential part is squared. Repeating this makes m / 2^(km) tend to zero. A tiny k delays this behaviour but does not change the eventual result.
write n = 2ᵐ. Then log n = m while nᵏ = 2ᵏᵐ = (2ᵏ)ᵐ, an exponential in m with base 2ᵏ > 1. An exponential in m always dominates the linear function m, so the ratio log n / nᵏ tends to 0.
Document reading · Lecture 2
- Lecture 2: Asymptotic Notation
Warm-up problem: the knapsack problem; The RAM model: how we count time; Best, worst and average case; Why exact counts are the wrong goal; Big-O, Big-Omega, Big-Theta; Worked examples with f(n) = 3n² − 100n + 6; Adding and subtracting Big-O bounds.
Python explained for this lesson
Read n*n as n². Use arithmetic to check a small case; no plotting code is required.
for n in [10, 20, 40]:
print(n, 3*n*n + 10*n)
# 10 400; 20 1400; 40 5200Predict the result, then run the demonstration. No prior Python fluency is required for the paper trace.
English–Turkish explanations
Lecture 2 · Warm-up problem: the knapsack problem
Hazırlık problemi: sırt çantası problemi
The lecture opens with a puzzle in the spirit of Lecture 1. Given a set of integers S = {1, 2, 5, 9, 10} and a target T, find a subset of S that adds up to exactly T. Each number may be used at most once. For T = 22 the answer is {1, 2, 9, 10}. For T = 23 there is no answer.
Ders, Ders 1’in yaklaşımını sürdüren bir bulmacayla açılır. S = {1, 2, 5, 9, 10} tam sayılar kümesi ve bir T hedefi verildiğinde, toplamı tam olarak T olan bir S alt kümesi bulun. Her sayı en fazla bir kez kullanılabilir. T = 22 için yanıt {1, 2, 9, 10}’dur. T = 23 için çözüm yoktur.
Three tempting greedy rules are proposed, and the exercise is to break each one with a counterexample:
Üç çekici açgözlü kural önerilir; alıştırma, her birini bir karşı örnekle çürütmektir:
First fit: walk through S left to right, adding each number if it still fits. Breaks on S = {5, 10, 20} with T = 30: it takes 5 and 10 and then 20 no longer fits, although {10, 20} works.
İlk sığan: S üzerinde soldan sağa ilerleyin; hâlâ sığıyorsa her sayıyı ekleyin. S = {5, 10, 20} ve T = 30 için bozulur: 5 ve 10’u alır, ardından 20 artık sığmaz; oysa {10, 20} çözüm verir.
Best fit (smallest first): add numbers from smallest to largest while they fit. Breaks on S = {1, 3, 4} with T = 7: it takes 1 and 3, then 4 no longer fits, although {3, 4} works.
En iyi sığan (önce en küçük): sayıları sığdıkları sürece küçükten büyüğe ekleyin. S = {1, 3, 4} ve T = 7 için bozulur: 1 ve 3’ü alır, ardından 4 artık sığmaz; oysa {3, 4} çözüm verir.
Largest first: add numbers from largest to smallest while they fit. Breaks on S = {6, 5, 5} with T = 10: it takes 6, and then neither 5 fits, although {5, 5} works.
Önce en büyük: sayıları sığdıkları sürece büyükten küçüğe ekleyin. S = {6, 5, 5} ve T = 10 için bozulur: 6’yı alır ve ardından iki 5’ten hiçbiri sığmaz; oysa {5, 5} çözüm verir.
No simple rule works. This knapsack problem returns in Lecture 21 as one of the officially “hard” problems.
Hiçbir basit kural işe yaramaz. Bu sırt çantası problemi, Ders 21’de resmen “zor” kabul edilen problemlerden biri olarak yeniden karşımıza çıkar.
Lecture 2 · The RAM model: how we count time
RAM modeli: zamanı nasıl sayarız?
To talk about speed without picking a particular computer, the course uses the RAM model of computation (Random Access Machine). Its rules are deliberately crude:
Belirli bir bilgisayar seçmeden hızdan söz edebilmek için derste RAM hesaplama modeli (Random Access Machine, Rastgele Erişimli Makine) kullanılır. Kuralları bilerek kabaca tanımlanmıştır:
Every simple operation (+, −, =, an if-test, a function call) costs exactly one step.
Her basit işlem (+, −, =, bir if sınaması, bir fonksiyon çağrısı) tam olarak bir adım tutar.
Every memory access (reading or writing one value) costs exactly one step.
Her bellek erişimi (bir değeri okuma veya yazma) tam olarak bir adım tutar.
Loops and subroutine calls are not single steps. A loop costs the number of iterations times the cost of the body; “sort the list” is not one step, it is however many steps the sorting algorithm needs.
Döngüler ve alt program çağrıları tek bir adım değildir. Bir döngünün maliyeti, yineleme sayısı ile gövdesinin maliyetinin çarpımıdır; “listeyi sırala” tek adım değil, sıralama algoritmasının gerektirdiği kadar adımdır.
The running time of an algorithm is then just the number of steps it performs. Real computers do not work exactly like this (caches, pipelines, and multiplications that cost more than additions all exist), but the model predicts real behaviour well enough. Skiena compares it to the flat-earth model: strictly false, and still useful for finding your way to the shops.
Böylece bir algoritmanın çalışma süresi, gerçekleştirdiği adım sayısından ibaret olur. Gerçek bilgisayarlar tam olarak böyle çalışmaz (önbellekler, boru hatları ve toplamadan daha maliyetli çarpma işlemleri vardır), ancak model gerçek davranışı yeterince iyi öngörür. Skiena bunu düz dünya modeliyle karşılaştırır: kesin anlamda yanlıştır, yine de alışverişe giderken yolunuzu bulmak için işe yarar.
Lecture 2 · Best, worst and average case
En iyi, en kötü ve ortalama durum
An algorithm does not take the same time on every input of the same size. Sorting 1,000 numbers that are already in order may be faster than sorting 1,000 numbers in random order. So for each input size n we can ask three questions:
Bir algoritma, aynı boyuttaki her girdide aynı sürede çalışmaz. Zaten sıralı 1,000 sayıyı sıralamak, rastgele sıradaki 1,000 sayıyı sıralamaktan daha hızlı olabilir. Bu nedenle her n girdi boyutu için üç soru sorabiliriz:
| Measure | Definition | Why we might care |
| Ölçüt | Tanım | Neden önemseyebiliriz? |
| Worst case | The maximum steps taken over all inputs of size n | A guarantee: it never gets worse than this |
| En kötü durum | n boyutundaki tüm girdiler üzerinde atılan en yüksek adım sayısı | Bir garanti: bundan daha kötü olamaz |
| Best case | The minimum steps taken over all inputs of size n | Rarely useful; the lucky input almost never shows up |
| En iyi durum | n boyutundaki tüm girdiler üzerinde atılan en düşük adım sayısı | Nadiren yararlıdır; şanslı girdi neredeyse hiç karşımıza çıkmaz |
| Average case | The average steps over all inputs of size n | Realistic, but hard to compute and depends on what “typical” input means |
| Ortalama durum | n boyutundaki tüm girdiler üzerinde atılan ortalama adım sayısı | Gerçekçidir, ancak hesaplaması zordur ve “tipik” girdinin ne anlama geldiğine bağlıdır |
Each of these is a function of n: put in a size, get out a step count. Skiena’s lottery analogy: the best case of a lottery ticket is winning millions, the average case is losing a little, the worst case is losing the whole ticket price. Which number would you use to decide whether to buy?
Bunların her biri n’nin bir fonksiyonudur: bir boyut girer, bir adım sayısı elde edersiniz. Skiena’nın piyango benzetmesi: bir piyango biletinin en iyi durumu milyonlar kazanmaktır; ortalama durumu biraz kaybetmek, en kötü durumu ise bilet bedelinin tamamını kaybetmektir. Satın almaya karar verirken hangi sayıyı kullanırdınız?
This course uses worst-case complexity unless told otherwise. It is usually easy to work out, it is a guarantee, and in practice it usually tracks the average case anyway. The main exception is randomised algorithms (Lecture 8), whose benefit only shows up in an average-case analysis.
Bu derste aksi belirtilmedikçe en kötü durum karmaşıklığı kullanılır. Genellikle hesaplaması kolaydır, bir garanti sağlar ve uygulamada çoğunlukla ortalama durumu da izler. Başlıca istisna, yararı ancak ortalama durum analizinde ortaya çıkan rastgeleleştirilmiş algoritmalardır (Ders 8).
Lecture 2 · Why exact counts are the wrong goal
Kesin adım sayıları neden yanlış bir hedeftir?
Counting the exact number of steps gives ugly functions like 3n² − 100n + 6, and the exact constants depend on details we said we would ignore (which computer, which language, which compiler). What we really want is the shape of the function for large n: does it grow like n, like n², like 2ⁿ?
Adımların tam sayısını hesaplamak, 3n² − 100n + 6 gibi kullanışsız fonksiyonlar verir; kesin sabitler ise göz ardı edeceğimizi söylediğimiz ayrıntılara bağlıdır (hangi bilgisayar, hangi dil, hangi derleyici). Asıl istediğimiz, büyük n için fonksiyonun biçimidir: n gibi mi, n² gibi mi, 2ⁿ gibi mi büyür?
So instead of exact functions we work with upper bounds and lower bounds that are simple functions, ignoring constant factors and ignoring small values of n. That is what asymptotic notation does.
Bu yüzden kesin fonksiyonlar yerine, basit fonksiyonlarla ifade edilen üst sınırlar ve alt sınırlarla çalışır; sabit çarpanları ve küçük n değerlerini göz ardı ederiz. Asimptotik gösterimin yaptığı budur.
Lecture 2 · Big-O, Big-Omega, Big-Theta
Büyük-O, Büyük-Omega, Büyük-Theta
Let f(n) be the true running time and g(n) a simple function like n² that we compare it with. Three symbols:
f(n) gerçek çalışma süresi, g(n) ise onu karşılaştırdığımız n² gibi basit bir fonksiyon olsun. Üç simge vardır:
f(n) = O(g(n)) (“f is Big-O of g”): some constant multiple of g(n) is an upper bound on f(n). Roughly, “f grows no faster than g”.
f(n) = O(g(n)) (“f, g’nin Büyük-O’sudur”): g(n)’nin sabit bir katı, f(n) için üst sınırdır. Kabaca, “f, g’den daha hızlı büyümez”.
f(n) = Ω(g(n)) (“Big-Omega”): some constant multiple of g(n) is a lower bound on f(n). Roughly, “f grows at least as fast as g”.
f(n) = Ω(g(n)) (“Büyük-Omega”): g(n)’nin sabit bir katı, f(n) için alt sınırdır. Kabaca, “f en az g kadar hızlı büyür”.
f(n) = Θ(g(n)) (“Big-Theta”): both at once. f(n) is squeezed between two constant multiples of g(n). Roughly, “f grows exactly like g”.
f(n) = Θ(g(n)) (“Büyük-Theta”): ikisi aynı anda geçerlidir. f(n), g(n)’nin iki sabit katı arasında sıkışır. Kabaca, “f tam olarak g gibi büyür”.
The formal versions, which all say “beyond some starting size n₀, the inequality always holds”:
Biçimsel tanımların tümü, “belirli bir n₀ başlangıç boyutundan sonra eşitsizlik her zaman geçerlidir” der:
f(n) = O(g(n)) if there are positive constants c and n₀ such that f(n) ≤ c · g(n) for every n ≥ n₀.
Her n ≥ n₀ için f(n) ≤ c · g(n) olacak biçimde pozitif c ve n₀ sabitleri varsa f(n) = O(g(n))’dir.
f(n) = Ω(g(n)) if there are positive constants c and n₀ such that f(n) ≥ c · g(n) for every n ≥ n₀.
Her n ≥ n₀ için f(n) ≥ c · g(n) olacak biçimde pozitif c ve n₀ sabitleri varsa f(n) = Ω(g(n))’dir.
f(n) = Θ(g(n)) if there are positive constants c₁, c₂ and n₀ such that c₁ · g(n) ≤ f(n) ≤ c₂ · g(n) for every n ≥ n₀.
Her n ≥ n₀ için c₁ · g(n) ≤ f(n) ≤ c₂ · g(n) olacak biçimde pozitif c₁, c₂ ve n₀ sabitleri varsa f(n) = Θ(g(n))’dir.
Two details matter. The constant c is any fixed number: you are allowed to multiply g by 1,000,000 if that is what it takes. And n₀ says we only care about large inputs: a function may be bigger for n = 3 and still be “below” for the values that matter.
İki ayrıntı önemlidir. c sabiti herhangi bir sabit sayı olabilir: gerekiyorsa g’yi 1,000,000 ile çarpabilirsiniz. n₀ ise yalnızca büyük girdileri önemsediğimizi söyler: bir fonksiyon n = 3 için daha büyük olsa da önemli olan değerlerde yine “aşağıda” kalabilir.
One warning about the equals sign: “f(n) = O(n²)” does not mean the two sides are equal. Read it as “f(n) is in the set of functions bounded above by a multiple of n²”.
Eşittir işareti hakkında bir uyarı: “f(n) = O(n²)”, iki tarafın birbirine eşit olduğu anlamına gelmez. Bunu “f(n), n²’nin bir katıyla üstten sınırlanan fonksiyonlar kümesindedir” diye okuyun.
Lecture 2 · Worked examples with f(n) = 3n² − 100n + 6
f(n) = 3n² − 100n + 6 ile çözümlü örnekler
Upper bounds:
Üst sınırlar:
f(n) = O(n²), because 3n² > 3n² − 100n + 6 for every n ≥ 1, so c = 3 works.
f(n) = O(n²); çünkü her n ≥ 1 için 3n² > 3n² − 100n + 6’dır; dolayısıyla c = 3 işe yarar.
f(n) = O(n³) as well, because 0.01n³ eventually exceeds f(n). Big-O is a ceiling, and a higher ceiling is still a ceiling. It is true but not tight.
f(n) aynı zamanda O(n³)’tür; çünkü 0.01n³ sonunda f(n)’yi aşar. Büyük-O bir tavandır; daha yüksek bir tavan da hâlâ tavandır. Bu ifade doğrudur, ancak sıkı değildir.
f(n) is not O(n), because whatever constant c you choose, 3n² eventually beats c · n (as soon as n > c).
f(n), O(n) değildir; çünkü hangi c sabitini seçerseniz seçin, 3n² sonunda c · n’yi geçer (n > c olur olmaz).
Lower bounds:
Alt sınırlar:
f(n) = Ω(n²), because 2.99n² < 3n² − 100n + 6 once n is large enough.
f(n) = Ω(n²); çünkü n yeterince büyük olduğunda 2.99n² < 3n² − 100n + 6 olur.
f(n) = Ω(n) as well, because even 10¹⁰ · n is eventually smaller than f(n).
f(n) aynı zamanda Ω(n)’dir; çünkü 10¹⁰ · n bile sonunda f(n)’den küçük kalır.
f(n) is not Ω(n³), because f(n) eventually falls below n³/10, or any positive multiple of n³.
f(n), Ω(n³) değildir; çünkü f(n) sonunda n³/10’un veya n³’ün herhangi bir pozitif katının altına düşer.
Tight bound:
Sıkı sınır:
f(n) = Θ(n²), because it is both O(n²) and Ω(n²).
f(n) = Θ(n²); çünkü hem O(n²) hem de Ω(n²)’dir.
f(n) is not Θ(n³) (only the O part holds) and not Θ(n) (only the Ω part holds).
f(n), Θ(n³) değildir (yalnızca O kısmı geçerlidir) ve Θ(n) de değildir (yalnızca Ω kısmı geçerlidir).
The practical habit: to find the Big-Theta of a polynomial, keep the term with the highest power and drop its constant. 3n² − 100n + 6 is Θ(n²); 5n³ + 20n log n is Θ(n³).
Pratik alışkanlık şudur: bir polinomun Büyük-Theta’sını bulmak için en yüksek kuvvetli terimi tutun ve sabit katsayısını atın. 3n² − 100n + 6, Θ(n²)’dir; 5n³ + 20n log n, Θ(n³)’tür.
Lecture 2 · Adding and subtracting Big-O bounds
Büyük-O sınırlarını toplama ve çıkarma
Suppose f(n) = O(n²) and g(n) = O(n²).
f(n) = O(n²) ve g(n) = O(n²) olduğunu varsayalım.
Their sum f(n) + g(n) is O(n²): add the two constants and you have a constant that works.
Toplamları f(n) + g(n), O(n²)’dir: iki sabiti topladığınızda işe yarayan bir sabit elde edersiniz.
Their difference f(n) − |g(n)| is also O(n²): subtracting something non-negative from an O(n²) function cannot make it grow faster. (It does not become smaller in the Big-O sense either, because the constants do not have to cancel.)
Farkları f(n) − |g(n)| de O(n²)’dir: O(n²) olan bir fonksiyondan negatif olmayan bir nicelik çıkarmak, onun daha hızlı büyümesine yol açamaz. (Büyük-O anlamında daha küçük de olmaz; çünkü sabitlerin birbirini götürmesi gerekmez.)
We know nothing about lower bounds of the sum or difference, because Big-O told us nothing about lower bounds of f and g in the first place.
Toplamın veya farkın alt sınırları hakkında hiçbir şey bilmiyoruz; çünkü Büyük-O, başlangıçta da f ve g’nin alt sınırları hakkında bir şey söylemiyordu.
The general rule of thumb for sums: the bigger term wins. O(n²) + O(n) = O(n²).
Toplamlar için genel pratik kural: büyük terim kazanır. O(n²) + O(n) = O(n²).
Additional textbook problems with solutions
Problem 2-7 Big Oh
In plain words Two yes/no questions. (a) Does 2ⁿ⁺¹ (that is 2 to the power n+1) grow no faster than 2ⁿ? (b) Does 2²ⁿ (that is 2 to the power 2n) grow no faster than 2ⁿ?
(a) Rewrite it: 2ⁿ⁺¹ = 2 · 2ⁿ. That is just 2ⁿ with a 2 stuck on the front — a constant factor. Big-O throws away constant factors, so this stays the same rate. At n=10: 2ⁿ⁺¹ = 2048 and 2ⁿ = 1024. Always exactly double, never runs away.
(b) Rewrite it: 2²ⁿ = (2ⁿ)² = 4ⁿ. That is a bigger base, 4 instead of 2. Their ratio is 4ⁿ / 2ⁿ = 2ⁿ, which itself shoots off to infinity. No fixed number c can hold 4ⁿ below c · 2ⁿ. At n=10: 4ⁿ = 1,048,576 versus 2ⁿ = 1024 — already 1024 times bigger, and the gap keeps widening. Doubling the exponent changes the base; it is not a constant factor.
Answer (a) True. (b) False.
Why it matters It teaches the single most common trap: a constant on the exponent (+1) is harmless, but multiplying the exponent (2n) secretly changes the base and breaks the bound.
Problem 2-8 Big Oh
In plain words For each pair of functions f and g, is f a ceiling (O), a floor (Ω), or exactly the same rate (Θ) as g? Just read each pair off the ladder. (When both O and Ω hold, the honest answer is Θ.)
| f | g | Verdict | Plain reason | |
|---|---|---|---|---|
| (a) | log(n²) | log n + 5 | Θ | log(n²) = 2 log n; both are just log n up to a constant — same rung |
| (b) | √n | log(n²) = 2 log n | Ω | a power of n beats any log, so f is above g |
| (c) | (log n)² | log n | Ω | squaring a growing thing makes it grow faster |
| (d) | n | (log n)² | Ω | a plain power of n beats any power of a log |
| (e) | n log n + n | log n | Ω | n log n sits far above log n |
| (f) | 10 | log 10 | Θ | both are fixed numbers — neither grows, same rung (the bottom) |
| (g) | 2ⁿ | 10 n² | Ω | an exponential beats any polynomial |
| (h) | 2ⁿ | 3ⁿ | O | 2ⁿ grows slower than 3ⁿ (their ratio (2/3)ⁿ shrinks to 0) |
Answer (a) Θ · (b) Ω · (c) Ω · (d) Ω · (e) Ω · (f) Θ · (g) Ω · (h) O.
Why it matters It drills the ladder-reading skill you will lean on for every other problem in the chapter.
Problem 2-9 Big Oh
In plain words For each pair, which one is the ceiling of the other? Answer f = O(g) (f is the smaller/lower one), g = O(f) (g is the smaller one), or both (a tie, meaning Θ). Trick: simplify each function to its biggest piece, then compare on the ladder.
| f | g | Verdict | Plain reason | |
|---|---|---|---|---|
| (a) | (n² − n)/2 | 6n | g = O(f) | f is basically n²/2 (quadratic); g is only linear, so g is the smaller |
| (b) | n + 2√n | n² | f = O(g) | f is basically n, well below n² |
| (c) | n log n | n√n / 2 | f = O(g) | √n grows faster than log n, so n√n beats n log n |
| (d) | n + log n | √n | g = O(f) | f is basically n, above √n |
| (e) | 2(log n)² | log n + 1 | g = O(f) | (log n)² grows faster than log n |
| (f) | 4n log n + n | (n² − n)/2 | f = O(g) | n log n sits below n² |
None of these pairs lands on the same rung, so there is no Θ tie here.
Answer (a) g=O(f) · (b) f=O(g) · (c) f=O(g) · (d) g=O(f) · (e) g=O(f) · (f) f=O(g).
Why it matters Simplifying to the biggest piece before comparing is exactly how you read the running time of real code.
Problem 2-10 Big Oh
In plain words Show that n³ − 3n² − n + 1 grows at the same rate as n³ — that is, it is Θ(n³).
Θ(n³) means “trapped between two constant copies of n³ once n is big”. The clean trick: divide the whole thing by n³ and watch it settle.
(n³ − 3n² − n + 1) / n³ = 1 − 3/n − 1/n² + 1/n³
Each little fraction (3/n, 1/n², 1/n³) shrinks to 0 as n grows, so the whole ratio settles near 1. That means the expression is basically n³ for large n — caught between, say, 0.5 n³ and 1 · n³. Quick check at n=10: the value is 1000 − 300 − 10 + 1 = 691, and indeed 500 ≤ 691 ≤ 1000. Two constant copies of n³ trap it — that is the definition of Θ.
Answer It is Θ(n³).
Why it matters “Divide by the top term and watch it settle near 1” is the one move that proves any polynomial matches its highest power.
Problem 2-11 Big Oh
In plain words Show that n² has 2ⁿ as a ceiling — that is, n² = O(2ⁿ).
This is the headline ladder fact: an exponential eventually overtakes any polynomial and never looks back. To prove it we just need one constant c and a starting point n₀ so that n² ≤ c · 2ⁿ from there on. Watch the race:
| n | 1 | 2 | 3 | 4 | 5 | 6 | 8 | 10 |
|---|---|---|---|---|---|---|---|---|
n² | 1 | 4 | 9 | 16 | 25 | 36 | 64 | 100 |
2ⁿ | 2 | 4 | 8 | 16 | 32 | 64 | 256 | 1024 |
At n=3 the polynomial briefly leads (9 > 8), but from n=4 onward 2ⁿ catches up and then pulls away for good. At n=10 it is already ten times bigger, and the gap only grows. So with c = 1 and n₀ = 4 we have n² ≤ 2ⁿ for all n ≥ 4 — exactly what a ceiling needs.
Answer n² = O(2ⁿ), shown by c = 1, n₀ = 4.
Why it matters To prove an O bound you only need one c and one starting point — you do not have to win the race at the very beginning.
Problem 2-12 Big Oh
In plain words For each pair, find one positive number c so that f(n) ≤ c · g(n) for all n > 1. The tactic every time: replace each small term of f by a copy of its biggest term, then compare with g.
(a) f = n² + n + 1, g = 2n³. For n > 1, both n and 1 are at most n², so f ≤ 3n². And 3n² ≤ 4n³ = 2 · (2n³). So c = 2 works.
(b) f = n√n + n² (that is n¹·⁵ + n²), g = n². For n > 1, n¹·⁵ ≤ n², so f ≤ n² + n² = 2n². So c = 2.
(c) f = n² − n + 1, g = n²/2. Since −n + 1 ≤ 0 for n ≥ 1, we get f ≤ n² = 2 · (n²/2). So c = 2.
Many other constants would also work — you only need to show one.
Answer c = 2 works for all three: (a), (b), and (c).
Why it matters It turns the abstract phrase “there exists a constant c” into a concrete, do-able hunt: bound every term, then pick a c.
Problem 2-13 Big Oh
In plain words The sum rule. If f₁ has ceiling g₁ and f₂ has ceiling g₂, show that adding them keeps a ceiling: f₁ + f₂ = O(g₁ + g₂).
Remember what a ceiling means: f = O(g) is just “f ≤ c · g for big n”. So we start with two facts:
f₁ ≤ c₁ · g₁ and f₂ ≤ c₂ · g₂
Add them straight down: f₁ + f₂ ≤ c₁ g₁ + c₂ g₂. Now let c be the bigger of c₁ and c₂. Then the right side is at most c(g₁ + g₂). Done — the sum has ceiling g₁ + g₂.
Answer True: f₁ + f₂ = O(g₁ + g₂).
Why it matters This is the rule that lets you analyse two code blocks one after another and just add their costs (then keep the bigger).
Problem 2-14 Big Oh
In plain words The same two rules (sum and product) but with the floor Ω instead of the ceiling O. Show that if f₁ = Ω(g₁) and f₂ = Ω(g₂), then adding keeps a floor and multiplying keeps a floor.
It is the exact same argument as 2-13 and 2-15, just with the inequalities flipped. A floor means f ≥ c · g for big n. So:
- Sum: add
f₁ ≥ c₁ g₁andf₂ ≥ c₂ g₂; take the smaller of the two c's, and you get a floor on the sum. - Product: multiply the two (everything positive) to get a floor on the product, with constant
c₁ c₂.
And since Θ is just a ceiling and a floor together, both rules automatically hold for Θ too.
Answer True: the sum rule and product rule both hold for Ω (and therefore for Θ).
Why it matters Floors combine just like ceilings, so “same rate” (Θ) survives adding and multiplying too.
Problem 2-15 Big Oh
In plain words The product rule. If f₁ has ceiling g₁ and f₂ has ceiling g₂, show that multiplying them keeps a ceiling: f₁ · f₂ = O(g₁ · g₂).
Start with the same two facts (all quantities positive):
f₁ ≤ c₁ · g₁ and f₂ ≤ c₂ · g₂
Multiply them together: f₁ · f₂ ≤ (c₁ c₂) · g₁ · g₂. The single constant c₁ c₂ does the whole job, so the product has ceiling g₁ · g₂.
Answer True: f₁ · f₂ = O(g₁ · g₂).
Why it matters This is why a loop that runs O(g₁) times doing O(g₂) work each pass costs the product O(g₁ g₂) — the rule behind nested loops.
Problem 2-16 Big Oh
In plain words Prove that any polynomial of degree k — something like aknk + … + a1n + a0 — is O(nk). In short: the highest power wins.
This is the formal version of “keep the highest power”. Take any single term ai ni where i ≤ k. Once n ≥ 1, a lower power of n is no bigger than the top power nk, so |ai ni| ≤ |ai| · nk. Add up all k+1 terms:
|aknk + … + a0| ≤ (|ak| + … + |a0|) · nk
The bracket c = |ak| + … + |a0| is just a fixed number — it does not depend on n. So the whole polynomial is at most c · nk, which is exactly O(nk).
Answer Any degree-k polynomial aknk + … + a0 is O(nk), with constant c = |ak| + … + |a0|.
Why it matters It justifies the habit you already have: read off a polynomial's running time from its biggest term alone.
Problem 2-17 Big Oh
In plain words Show that shifting n by a constant does not change the growth rate: (n + a)ᵇ = Θ(nᵇ) for any fixed a and any power b > 0.
Intuition first: when n is huge, adding a fixed number a barely nudges it. 1,000,000 + 7 is still, for growth purposes, a million. So (n + a) and n grow at the same rate, and raising both to the same power b keeps them matched. To confirm, look at the ratio:
(n + a)ᵇ / nᵇ = (1 + a/n)ᵇ → 1 as n grows
Because a/n shrinks to 0, the ratio settles at 1. So for big n the expression stays trapped between two fixed constants (say ½ nᵇ and 2 nᵇ) — the sandwich that means Θ.
Answer (n + a)ᵇ = Θ(nᵇ).
Why it matters It lets you drop “+1” and “−3” kinds of shifts inside a running-time expression without a second thought.
Problem 2-18 Big Oh
In plain words List these functions from slowest-growing to fastest-growing, and mark any that grow at the same rate (a tie, Θ). This is Skiena's own set of 16 functions:
n 2n n lg n ln n n − n3 + 7n5 lg n √n en n2 + lg n n2 2n−1 lg lg n n3 (lg n)2 n! n1+ε (0<ε<1)
First, tidy each one into its shape:
n − n3 + 7n5→ keep the top term,n5.n2 + lg n→n2(thelg nis a speck next ton2) — so it ties with plainn2.ln nandlg ndiffer only by a constant factor → they tie (both Θ(log n)).2n−1 = ½·2n→ ties with2n.enis above2n, becausee > 2(soen/2n = (e/2)n → ∞).n1+εwith0<ε<1sits betweenn lg nandn2(a real power aboven, but belown2).
Now walk up the ladder. The only surprise to watch: any real power of n (even √n) beats every power of a logarithm.
Answer Slowest → fastest (braces mark ties):
lg lg n ≪ {ln n = lg n} ≪ (lg n)2 ≪ √n ≪ n ≪ n lg n ≪ n1+ε ≪ {n2 = n2+lg n} ≪ n3 ≪ (n − n3 + 7n5) ≪ {2n−1 = 2n} ≪ en ≪ n!
Why it matters Real problems hand you messy expressions; the skill is to strip each to its dominant term first, then read its rung — spotting the ties is part of the answer.
Problem 2-19 Big Oh
In plain words Same task, Skiena's trickier set of 18 functions — sort slowest to fastest, mark ties. Two functions here are sneaky: one shrinks to zero, and one is a constant.
√n n 2n n log n n − n3 + 7n5 n2 + log n n2 n3 log n n1/3 + log n (log n)2 n! ln n n/log n log log n (1/3)n (3/2)n 6
Tidy the tricky ones first:
(1/3)n→ a fraction to a growing power shrinks toward 0. It is the smallest of all.6→ a constant, Θ(1); above anything that goes to 0, below anything that grows.n − n3 + 7n5→n5;n2 + log n→n2(ties withn2);n1/3 + log n→n1/3.log nandln ntie (Θ(log n)).n/log nis just belown(a shade under linear, but far above√n).(3/2)nis below2n(smaller base), and both beat every polynomial.
The one easy-to-slip pair: (log n)2 versus n1/3. Squaring a logarithm still leaves a logarithm, and any real power of n (even the small power 1/3) eventually beats any power of log n. So (log n)2 < n1/3 — even though for small n the log looks bigger.
Answer Slowest → fastest (braces mark ties):
(1/3)n ≪ 6 ≪ log log n ≪ {log n = ln n} ≪ (log n)2 ≪ n1/3 ≪ √n ≪ n/log n ≪ n ≪ n log n ≪ {n2 = n2+log n} ≪ n3 ≪ (n − n3 + 7n5) ≪ (3/2)n ≪ 2n ≪ n!
Why it matters The pitfalls here — a function that shrinks, a constant, and a power of n that only overtakes a power of log much later — are exactly the ones that trip people up in interviews and exams.
Problem 2-20 Big Oh
In plain words For each condition, either give two functions f and g that satisfy it, or explain why it is impossible. Remember: little-o means “strictly slower”, Θ means “same rate”, O means “ceiling”, Ω means “floor”.
(a) Want f = o(g) (strictly slower) and f ≠ Θ(g) (not the same rate). Easy. Take f = n, g = n². n is strictly slower than n², and they are clearly not the same rate. In fact “strictly slower” always rules out “same rate”, so any little-o pair works.
(b) Want f = Θ(g) and f = o(g) at once. Impossible. Θ says “exactly the same rate”; little-o says “strictly slower”. Nothing can be both exactly as fast and strictly slower.
(c) Want f = Θ(g) but f ≠ O(g). Impossible. Θ is a ceiling and a floor together, so f = Θ(g) already includes f = O(g). You cannot keep the first and drop the second.
(d) Want f = Ω(g) (floor) but f ≠ O(g) (no ceiling). Easy. Take f = n², g = n. n² grows at least as fast as n (floor holds), but it is not capped by n (no ceiling). That is the picture of a strict lower bound.
Answer (a) possible, e.g. f = n, g = n² · (b) impossible · (c) impossible · (d) possible, e.g. f = n², g = n.
Why it matters It makes you feel how the five words fit together — which combinations are contradictions and which are everyday examples.
Problem 2-21 Big Oh
In plain words True or false for each claim, with a one-line reason from the ladder. Recipe: simplify each side to its biggest piece, then check whether the left really is a ceiling of the right.
| Claim | Verdict | Plain reason | |
|---|---|---|---|
| (a) | 2n² + 1 = O(n²) | True | constant factor plus a tiny term; still n² |
| (b) | √n = O(log n) | False | √n grows faster than log n, not slower |
| (c) | log n = O(√n) | True | a log sits below any power of n |
| (d) | n²(1 + √n) = O(n² log n) | False | left side is about n²·√n = n²·⁵, above n² log n |
| (e) | 3n² + √n = O(n²) | True | √n is a tiny term; n² dominates |
| (f) | √n log n = O(n) | True | √n · log n grows slower than √n · √n = n |
| (g) | log n = O(n−1/2) | False | log n grows to infinity while n−1/2 = 1/√n shrinks to 0 |
Answer (a) True · (b) False · (c) True · (d) False · (e) True · (f) True · (g) False.
Why it matters These are the exact patterns (drop small terms, drop constants, compare rungs) you will apply every time you eyeball a running time.
Problem 2-22 Big Oh
In plain words State the tightest single relationship of f to g: is f a ceiling (O), a floor (Ω), the same rate (Θ), or none? Simplify each to its biggest piece first.
(a) f = n² + 3n + 4, g = 6n + 7. f is quadratic, g is linear, so f grows faster: f = Ω(g) (and definitely not a ceiling).
(b) f = n√n = n¹·⁵, g = n² − n. g is basically n², above n¹·⁵, so f is the smaller one: f = O(g).
(c) f = 2ⁿ − n², g = n⁴ + n². The 2ⁿ completely swamps the −n² (a rounding error beside it), so f grows like 2ⁿ — an exponential, above the polynomial g: f = Ω(g).
Answer (a) Ω · (b) O · (c) Ω.
Why it matters It trains you to ignore the noise (small and subtracted terms) and judge by the one dominant piece.
Problem 2-23 Big Oh
In plain words A yes/no quiz about what Big-O really promises. Key idea: O is only a ceiling on the worst case — it never stops an algorithm from being faster. Θ on the worst case is stronger: it says the worst case really does reach that height.
(a) Worst case O(n²) — can it be O(n) on some inputs? Yes. Some inputs (like a best case) can finish far faster; the ceiling only caps the slowest.
(b) Worst case O(n²) — can it be O(n) on all inputs? Yes. O(n²) is only an upper bound; an algorithm that is actually linear everywhere is still (loosely) O(n²). O does not claim the bound is tight.
(c) Worst case Θ(n²) — can it be O(n) on some inputs? Yes. Θ pins the worst case at quadratic, but the best case (some inputs) can still be linear.
(d) Worst case Θ(n²) — can it be O(n) on all inputs? No. A Θ(n²) worst case means some inputs genuinely force quadratic work, so not every input can be linear.
(e) Is f(n) = Θ(n²) when f = 100n² for even n and f = 20n² − n·log²n for odd n? Yes. Either way, every value sits between constant copies of n² (roughly between 19n² and 100n² for large n), so the whole function is Θ(n²) no matter which branch it takes.
Answer (a) yes · (b) yes · (c) yes · (d) no · (e) yes.
Why it matters It fixes the most common misreading: O is a promise of “no worse than”, not “always exactly this” — only Θ makes the tight claim.
Problem 2-24 Big Oh
In plain words Yes, no, or can't-tell for each claim about exponentials and their logs. Handy fact: log(aⁿ) = n · log a — taking a log turns an exponential into a plain multiple of n.
(a) Is 3ⁿ = O(2ⁿ)? No. The ratio 3ⁿ / 2ⁿ = (3/2)ⁿ = 1.5ⁿ races off to infinity, so no constant can cap it. Different bases are not a constant factor apart.
(b) Is log(3ⁿ) = O(log(2ⁿ))? Yes. Take logs: log(3ⁿ) = n log 3 and log(2ⁿ) = n log 2. Their ratio is the fixed number log 3 / log 2 ≈ 1.585 — a constant factor — so each is a ceiling of the other. Taking the log tamed the exponential into a linear function of n.
(c) Is 3ⁿ = Ω(2ⁿ)? Yes. A floor asks whether 3ⁿ grows at least as fast as 2ⁿ — and it grows much faster, so certainly at least as fast.
(d) Is log(3ⁿ) = Ω(log(2ⁿ))? Yes. Same constant-ratio picture as (b): n log 3 versus n log 2. A constant factor apart means each is both a ceiling and a floor of the other.
Answer (a) no · (b) yes · (c) yes · (d) yes.
Why it matters It shows the magic of logs: two exponentials that are worlds apart become mere constant-factor neighbours once you take their logarithms.