๐Ÿš€ Mastering Asymptotic Notation in Data Structures & Algorithms (Part 2)

Understanding T(n), f(n), and the Real Reason Behind Big-O

“Don’t just learn the formulas. Learn why mathematicians invented them.” ๐Ÿ’ก


๐Ÿ“– Quick Recap of Part 1

In Part 1, we discovered that Asymptotic Notation is not about measuring the exact running time of an algorithm.

Instead, it studies how the running time grows as the input size becomes extremely large.

We also learned an important concept.

Whenever we count the exact number of operations performed by an algorithm, we represent it asT(n)\boxed{T(n)}T(n)โ€‹

For example,

for(int i = 0; i < n; i++)
{
System.out.println(i);
}

After counting all the operations, we obtainedT(n)=3n+2T(n)=3n+2T(n)=3n+2

This is called the Running-Time Function.

At this point, everything seems perfect.

We know exactly how many operations our algorithm performs.

So, a very interesting question arises…


๐Ÿค” If We Already Know T(n), Why Do We Need Another Function?

Imagine your teacher asks:

“What is the running time of your algorithm?”

You confidently answer:T(n)=3n+2T(n)=3n+2T(n)=3n+2

The teacher smiles and asks another question.

“Can you compare your algorithm with another algorithm?”

Suppose another student writes a different algorithm whose running time isT(n)=5n+100T(n)=5n+100T(n)=5n+100

Now both of you have exact running-time functions.

But comparing them directly is not very convenient.

Why?

Because every algorithm will have a different mathematical expression.

For example,

Algorithm AT(n)=3n+2T(n)=3n+2T(n)=3n+2

Algorithm BT(n)=5n+100T(n)=5n+100T(n)=5n+100

Algorithm CT(n)=7n+20T(n)=7n+20T(n)=7n+20

Algorithm DT(n)=100n+500T(n)=100n+500T(n)=100n+500

Imagine comparing thousands of algorithms using their exact running-time equations!

It quickly becomes complicated.

Computer Scientists wanted something simpler.

Instead of comparing exact equations,

they wanted to compare growth patterns.

And that is why they introduced another function calledf(n)\boxed{f(n)}f(n)โ€‹


๐Ÿค” What is f(n)?

This is one of the most misunderstood concepts in DSA.

Many students think

f(n) is the running time.

This is incorrect.

Let’s understand the difference.

T(n)

represents

The exact running-time function of an algorithm.

ExampleT(n)=3n+2T(n)=3n+2T(n)=3n+2


f(n)

represents

A simpler function that captures the growth pattern of T(n).

For the above example,

the dominant term isnnn

Therefore,

we choosef(n)=n\boxed{f(n)=n}f(n)=nโ€‹

Notice the wording.

We did not calculate f(n).

We selected it.

This is a very important distinction.


๐ŸŽฏ T(n) is Calculated, f(n) is Chosen

Let’s understand this carefully.

Suppose we analyze a Java program.

for(int i = 0; i < n; i++)
{
System.out.println(i);
}

After counting every operation,

we obtainT(n)=3n+2T(n)=3n+2T(n)=3n+2

This value comes from mathematics.

We actually counted

  • Initialization
  • Condition
  • Increment
  • Loop Body

Therefore,T(n)T(n)T(n)

is calculated.

Now we ask another question.

What is the dominant growth of this function?

The answer isnnn

Therefore,

we choosef(n)=nf(n)=nf(n)=n

Notice that nobody counted operations to obtainf(n)f(n)f(n)

It is simply chosen because it represents the overall growth.


๐Ÿ“Œ Example 1

SupposeT(n)=3n+2T(n)=3n+2T(n)=3n+2

The terms are

  • 3n3n3n
  • 222

Which term grows faster?

Obviously,3n3n3n

Ignoring the constant,

we getnnn

Therefore,f(n)=n\boxed{f(n)=n}f(n)=nโ€‹


๐Ÿ“Œ Example 2

SupposeT(n)=5n2+7n+20T(n)=5n^2+7n+20T(n)=5n2+7n+20

Which term grows the fastest?

Clearly,5n25n^25n2

Ignoring the constant coefficient,

we obtainn2n^2n2

Therefore,f(n)=n2\boxed{f(n)=n^2}f(n)=n2โ€‹


๐Ÿ“Œ Example 3

SupposeT(n)=8logโกn+100T(n)=8\log n+100T(n)=8logn+100

The dominant term islogโกn\log nlogn

Therefore,f(n)=logโกn\boxed{f(n)=\log n}f(n)=lognโ€‹


๐Ÿ“Œ Example 4

SupposeT(n)=4n+25T(n)=4\sqrt n+25T(n)=4nโ€‹+25

The dominant term isn\sqrt nnโ€‹

Therefore,f(n)=n\boxed{f(n)=\sqrt n}f(n)=nโ€‹โ€‹


๐Ÿ“Š T(n) vs f(n)

This is probably the most important table in this entire chapter.

T(n)f(n)
Exact running-time functionSimplified growth function
Obtained by counting operationsChosen by observing the dominant growth
Includes constantsIgnores constant factors
Includes lower-order termsKeeps only the dominant term
Different for every algorithmUsed to classify algorithms into growth categories

๐ŸŽฏ Understanding the Difference Through an Example

Suppose you own a car.

Every day you drive different distances.

Monday

51 km

Tuesday

49 km

Wednesday

53 km

Thursday

50 km

Friday

52 km

These are your exact daily distances.

Think of them asT(n)T(n)T(n)

Now suppose someone asks

“How much do you usually travel?”

You don’t answer

51 km
49 km
53 km
50 km
52 km

Instead,

you simply say

Around 50 km every day.

That simplified description is likef(n)f(n)f(n)

Notice something.

The exact values still exist.

But for understanding the overall pattern,

the simplified value is much more useful.

That is exactly why mathematicians introducedf(n)f(n)f(n)


๐Ÿš— Another Real-Life Analogy

Suppose two people earn salaries.

Person A

โ‚น59,850

Person B

โ‚น60,200

When someone asks,

“How much do they earn?”

Most people answer

Around โ‚น60,000 per month.

Why?

Because

  • โ‚น59,850
  • โ‚น60,200

both represent approximately the same scale.

Similarly,3n+23n+23n+2

and5n+1005n+1005n+100

both belong to the same growth categorynnn


๐Ÿค” Why Can’t We Simply Use T(n)?

This is an excellent question.

Suppose someone invents ten different algorithms.

Their running times are3n+23n+23n+2 5n+1005n+1005n+100 7n+207n+207n+20 10n+50010n+50010n+500 100n+1000100n+1000100n+1000

Although all these expressions are different,

their growth is identical.

Every one of them grows linearly.

Instead of remembering five different equations,

it is much easier to classify them into one category.

That category isn\boxed{n}nโ€‹

This is the purpose off(n)f(n)f(n)

It acts as a classification function.


๐Ÿ’ก Think of f(n) as a Category

Imagine a classroom.

Students have different heights.

171 cm

174 cm

176 cm

179 cm

181 cm

Instead of remembering every height,

we classify them as

Tall Students

Similarly,

instead of remembering3n+23n+23n+2 5n+1005n+1005n+100 100n+50100n+50100n+50

we classify them asO(n)O(n)O(n)

Notice that

we are no longer interested in the exact equation.

We are only interested in the growth category.


โš ๏ธ Common Mistake

Many beginners think

T(n) and f(n) are the same thing.

They are not.

Remember this carefully.T(n)T(n)T(n)

is

The exact running-time function.

Whereasf(n)f(n)f(n)

is

A simplified function used for comparison and classification.


๐Ÿง  Memory Trick

Whenever you seeT(n)T(n)T(n)

think

True Running Time

Whenever you seef(n)f(n)f(n)

think

Final Growth Pattern

Although this isn’t the official meaning of the letters, it’s a useful memory aid for beginners.


๐ŸŽฏ Key Takeaways

By now, you should understand:

โœ… T(n) is the exact running-time function obtained by counting operations.

โœ… f(n) is a simplified function that represents the dominant growth of T(n).

โœ… T(n) is calculated.

โœ… f(n) is chosen.

โœ… T(n) tells us the exact running time.

โœ… f(n) helps classify algorithms into growth categories.


๐Ÿ“Œ Coming Up Next (Part 2B)

Now that we understand the difference between T(n) and f(n), the next logical question is:

๐Ÿค” If we already have T(n), why do we ignore constants?

๐Ÿค” Why do we ignore lower-order terms?

๐Ÿค” Why is only the dominant term important?

We’ll answer these questions with detailed numerical examples, intuitive analogies, and the mathematical reasoning behind them. Finally, we’ll introduce the famous formula:0โ‰คT(n)โ‰คcโ‹…f(n)0 \le T(n) \le c \cdot f(n)0โ‰คT(n)โ‰คcโ‹…f(n)

and explain why mathematicians compare T(n) with f(n) before we dive into its full proof in Part 3.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *