Time Complexity and Big O Notation (with notes)

CodeWithHarry
31 Jul 202032:52

Summary

TLDRThe video script narrates a relatable story to explain the concept of time complexity in computer algorithms. It compares two methods of obtaining a game—via internet download and physical transportation—to illustrate how different algorithms perform with varying input sizes. The analogy of file transfer speeds and physical travel time is used to distinguish between linear (Big O of n) and constant time complexities (Big O of 1). The script also emphasizes the importance of algorithm analysis, especially when scaling to larger inputs, and provides a visual representation of different Big O notations to help viewers understand their relative efficiencies.

Takeaways

  • 😀 The story of being bored and wanting to play a game serves as an analogy for discussing algorithm efficiency and time complexity.
  • 🚀 The script introduces two scenarios: downloading a large game (60GB) using limited internet bandwidth (1GB/day) versus physically transporting the game from a friend's house.
  • 🔍 The importance of analyzing algorithms based on input size and the corresponding runtime is emphasized, highlighting the need to choose the most efficient method for a given problem.
  • 📚 The concept of Big O notation is introduced as a way to express the time complexity of an algorithm, indicating how the runtime grows with the size of the input.
  • 🛣️ The difference between the mathematical definition and the industry definition of Big O notation is explained, with the industry definition focusing on the most impactful term.
  • 📈 The script uses graphs to illustrate how algorithms with different time complexities (e.g., Big O of 1 and Big O of n) behave as input size increases.
  • 📝 The process of determining the time complexity of an algorithm is outlined, including identifying the highest order term and disregarding constants and lower-order terms.
  • 🏁 The significance of understanding time complexity for real-world applications, such as sorting large datasets, is discussed, showing how different algorithms may be more efficient under different conditions.
  • 📊 A visual comparison of various Big O complexities on a graph is presented, with green indicating more efficient algorithms and red indicating less efficient ones.
  • 📚 The speaker provides notes and resources, such as Ds0.pdf and Ds1.pdf, to further aid understanding of time complexity and Big O notation.
  • 🎓 The script concludes by encouraging viewers to review the material, like the video, and subscribe to the playlist for further learning on data structures and algorithms.

Q & A

  • What is the main theme of the video script?

    -The main theme of the video script is to explain the concept of time complexity and Big O notation in algorithms using a relatable story about choosing between downloading a game over the internet or physically going to a friend's house to get it.

  • Why does the storyteller decide to go to his friend's house to get the game instead of downloading it?

    -The storyteller decides to go to his friend's house to get the game because he has a limited internet speed of 1GB per day, which is insufficient for downloading large game files like GTA5, which is 60GB in size.

  • What is the significance of the 250kb game size mentioned in the story?

    -The 250kb game size is used as an example to illustrate a scenario where downloading the game over the internet would be faster and more convenient than making a physical trip, as the file size is small enough to be quickly downloaded within the 1GB daily limit.

  • What does the storyteller mean by 'algorithm 1' and 'algorithm 2'?

    -In the context of the story, 'algorithm 1' refers to the method of downloading the game over the internet, while 'algorithm 2' refers to physically going to the friend's house to get the game. These terms are used to draw a parallel with different approaches to solving a problem in computer algorithms.

  • What is the 'runtime' of an algorithm as discussed in the script?

    -The 'runtime' of an algorithm, as discussed in the script, refers to the time it takes for an algorithm to execute or complete its task. It is compared in the context of the two different methods of obtaining the game file.

  • Why does the storyteller emphasize the importance of analyzing algorithms before using them?

    -The storyteller emphasizes the importance of analyzing algorithms to determine their efficiency and suitability for a given task, especially considering how the runtime of the algorithm scales with the size of the input data.

  • What is the 'Big O' notation and why is it significant in the script?

    -The 'Big O' notation is a symbolic way to express the upper bound of the time complexity of an algorithm, indicating how the runtime grows as the input size increases. It is significant in the script as it helps to compare and understand the efficiency of different algorithms.

  • What does the storyteller mean by 'constant time' in the context of algorithm analysis?

    -In the context of algorithm analysis, 'constant time' refers to the runtime of an algorithm that does not change with the size of the input data. The storyteller uses this term to describe the time it takes to physically get the game from the friend's house, regardless of the file size.

  • How does the storyteller use the pizza example to explain time complexity?

    -The storyteller uses the pizza example to illustrate how the time it takes to get pizzas (the output) is affected by the number of pizzas ordered (the input size). This example helps to demonstrate the concept of time complexity in a real-world scenario.

  • What is the conclusion the storyteller draws about choosing the right algorithm based on the size of the input?

    -The storyteller concludes that the choice of algorithm depends on the size of the input data. For small inputs, one algorithm might be more efficient, while for larger inputs, a different algorithm might perform better. This is demonstrated through the sorting example with Shubham and Rohan's algorithms.

Outlines

00:00

🎮 The Dilemma of Game Acquisition

The speaker begins with a personal anecdote about the boredom experienced at home and the desire for entertainment. They mention a friend, Shubham, who lives 5km away and possesses a collection of games like Pubg and GTA5. The dilemma arises from the limited internet data available (1GB per day on Jio), which makes downloading large games impractical. The proposed solution is to physically visit the friend's house to copy the game using a hard disk, highlighting the trade-off between internet limitations and physical effort for immediate entertainment satisfaction.

05:00

🔍 Analyzing Algorithmic Efficiency Through Analogy

The narrative shifts to a discussion on algorithm efficiency by drawing a parallel between the game acquisition story and algorithmic problem-solving. The speaker explores the concept of time complexity in algorithms, using the example of transferring a small 250kb game versus a large 60GB game. They explain how the choice of method (internet vs. physical visit) is akin to selecting an algorithm based on input size, and how this choice affects the 'runtime' or time required to achieve the desired outcome. The speaker emphasizes the importance of understanding time complexity and choosing the most efficient algorithm as input size increases.

10:02

📈 Understanding Time Complexity with Real-World Examples

This paragraph delves deeper into time complexity analysis, contrasting two hypothetical algorithms for transferring data, represented by the internet method and a physical visit. The speaker uses the constant time of a physical visit (ignoring copying time) and compares it with the variable time of an internet transfer, which scales with the size of the data. The concept of Big O notation is introduced to describe the time complexity of algorithms, with the physical visit being O(1) and the internet transfer being O(n), where n is the size of the input. The speaker encourages the audience to consider the best algorithm based on the input size and to analyze algorithms for their time complexity.

15:02

📚 The Importance of Time Complexity Analysis

The speaker underscores the significance of time complexity analysis in algorithm selection, using the previous examples to illustrate how different algorithms perform under varying input sizes. They discuss the importance of understanding how the runtime of an algorithm changes with input size and the need to ask critical questions about the efficiency of an algorithm before its implementation. The speaker also introduces the concept of Big O notation in both its mathematical and industrial definitions, explaining the difference between the two and emphasizing the need to know both for academic and professional purposes.

20:03

📈 Visualizing Algorithm Performance with Graphs

The speaker discusses the visualization of algorithm performance through graphs, using the examples of Big O of 1 (constant time) and Big O of n (linear time) algorithms. They describe a scenario where the time required for a physical visit (constant time) and the time required for an internet transfer (linear time) intersect, indicating a point at which one algorithm may become more efficient than the other as input size increases. The speaker also mentions the importance of understanding different Big O notations and how they can be compared visually to make informed decisions about algorithm selection.

25:05

🍕 Practical Examples of Time Complexity in Daily Life

In this paragraph, the speaker brings the concept of time complexity into everyday life with an example involving ordering pizzas for a group of friends. They explain how the time it takes to receive the pizzas can be affected by the number of pizzas ordered, similar to how algorithm runtime is affected by input size. The speaker then transitions to a real-world example of sorting algorithms, where two developers' sorting algorithms are tested with different input sizes, demonstrating how an algorithm that performs well with small inputs may not be the best choice for larger datasets.

30:06

📘 Summarizing Time Complexity Analysis

The speaker concludes the discussion by summarizing the key points about time complexity analysis. They emphasize the importance of understanding the relationship between input size and runtime, and how to identify the best algorithm for a given task based on this relationship. The speaker also provides a visual chart comparing different Big O notations and their performance, encouraging the audience to review the provided notes for a deeper understanding of the concepts discussed in the video.

Mindmap

Keywords

💡Algorithm

An algorithm is a set of rules or steps used to solve a problem or perform a computation. In the context of the video, the term is used to describe different methods for transferring a game file, illustrating how the choice of algorithm can affect the efficiency and runtime depending on the size of the input, which in this case is the file size.

💡Input Size

Input size refers to the amount of data that an algorithm processes. The video uses the analogy of file sizes to explain how the input size can influence the performance of an algorithm. As the input size increases, the runtime of certain algorithms may increase at different rates, which is a key concept in analyzing algorithm efficiency.

💡Runtime

Runtime is the duration it takes for an algorithm to complete its task. The video script discusses how runtime changes with different algorithms as the input size varies. For instance, the runtime for an algorithm that copies a game file over the internet increases with the file size, whereas the runtime for physically transporting a game on a hard disk remains constant.

💡Time Complexity

Time complexity is a measure of how the runtime of an algorithm grows with the size of the input. The video explains time complexity through the lens of two different 'algorithms' for acquiring a game, emphasizing that as the input size (game file size) increases, the time complexity of the internet transfer algorithm is linear (Big O of n), while the physical transfer has a constant time complexity (Big O of 1).

💡Big O Notation

Big O notation is a symbolic way to express the upper bound of the time complexity of an algorithm. The video script uses Big O notation to classify and compare the efficiency of different algorithms. For example, an algorithm with a time complexity of Big O of n is said to run in linear time, while an algorithm with Big O of 1 has a constant runtime, regardless of input size.

💡Constant Time

Constant time refers to an algorithm's runtime that does not change with the size of the input. In the video, the physical visit to copy a game file is described as having a constant time complexity, meaning the time taken is the same whether the file is small or large.

💡Linear Time

Linear time indicates that the runtime of an algorithm is directly proportional to the size of the input. The video uses the example of transferring a game file over the internet to illustrate an algorithm with linear time complexity, where the time taken increases as the file size grows.

💡Internet Speed

Internet speed is the rate at which data is transferred over the internet. The script mentions internet speed as a factor affecting the time it takes to transfer a game file online, with the speed being a constant (L2) in the example used to calculate the time complexity of the internet transfer algorithm.

💡Physical Visit

A physical visit, in the context of the video, refers to the act of going to a friend's house to copy a game file onto a hard disk. The video contrasts the time complexity of a physical visit (Big O of 1, constant time) with that of an internet transfer (Big O of n, linear time) to demonstrate different algorithmic efficiencies.

💡Efficiency

Efficiency in algorithms refers to how well an algorithm performs in terms of time and resources used. The video script discusses the efficiency of different methods for transferring a game file by comparing their time complexities, highlighting that the choice of method can greatly affect efficiency based on the input size.

Highlights

The story analogy used to explain the concept of choosing between different algorithms based on input size.

The importance of analyzing algorithms based on their time complexity as the input size increases.

The introduction of two hypothetical algorithms, Algorithm 1 for internet file transfer and Algorithm 2 for physical file transfer.

The concept that Algorithm 1's runtime increases linearly with file size, while Algorithm 2 remains constant regardless of file size.

The practical example of choosing between downloading a small game versus physically visiting a friend to get a larger game.

The mathematical representation of algorithms' runtime, emphasizing the highest degree term in the equation.

The explanation of Big O notation as a measure of an algorithm's scalability and efficiency.

The difference between the mathematical and industrial definitions of Big O notation.

The graphical representation of algorithms' runtime, illustrating the difference between constant time and linear time complexity.

The real-world example of pizza delivery to demonstrate the impact of input size on the time required for task completion.

The use of sorting algorithms as a practical example to compare the efficiency of different algorithms with varying input sizes.

The importance of choosing the right algorithm based on the expected input size for optimal performance.

The detailed explanation of how to calculate and understand time complexity using Big O notation.

The significance of the highest order term in determining the time complexity of an algorithm.

The visualization of various Big O complexities on a graph to compare their performance as input size grows.

The guidance on how to approach the study of algorithms and time complexity for better understanding and application.

The encouragement for viewers to revisit the material if they did not understand it the first time, emphasizing the importance of grasping these concepts.

The conclusion that highlights the value of the video in teaching the concepts of time complexity and Big O notation.

Transcripts

play00:00

I want to tell you guys one story.

play00:02

It happened like this, I was bored in my house.

play00:06

What was happening? I was bored. This is my house.

play00:08

This is me.

play00:10

What was happening in my house? I was getting bored.

play00:12

I have a friend who stays 5km away.

play00:15

And 5kms away,

play00:18

Let's assume that friend's name, shubham ok.

play00:22

And this friend stays 5km away.

play00:26

Listen carefully,

play00:28

Now what happened, I was getting bored a lot.

play00:30

I was so bored that I needed some entertainment.

play00:34

This guy has amazing games like Pubg and GTA5.

play00:38

So he has a collection of games. He likes playing games a lot.

play00:42

And you can get every type of game from him.

play00:45

But there is one problem,

play00:46

I also use jio.

play00:49

He also uses jio. What does he use? He also uses jio.

play00:51

We get 1Gb. We get just 1 Gb for one day.

play00:55

And we are not much into broadband internet.

play00:59

And with more internet, we can't sell files and all.

play01:01

So if I have to take the game from him,

play01:04

So for me, what is the fastest way?

play01:08

If I want to play that game now itself.

play01:12

So what is the fastest way for me,

play01:15

To take the game from this friend.

play01:19

Tell me that,

play01:20

So you will say there is nothing to doubt in this,

play01:23

Take a hard disk, copy it, and bring.

play01:26

So what will I do?

play01:27

I will take my bike and bring that game by bike.

play01:33

So this is me, I am going through the bike.

play01:36

I will go and bring that game.

play01:39

And I will enjoy the game and satisfy my soul.

play01:44

Ok, this is done, but why am I telling this story to you guys?

play01:48

If I say that game is 250kb

play01:54

There is one simple game of 250kb.

play01:57

Then will I go to his house and bring it with the hard disk?

play02:01

Tell me, no right?

play02:02

I will tell him to add it to the drive,

play02:04

Or email it or upload it somewhere.

play02:08

I will tell him to upload it anywhere.

play02:10

There is dropbox, there is Gmail's google drive

play02:14

In Gmail also 250kb can come.

play02:17

There are many ways even you know it.

play02:20

Now, over here

play02:22

Tell me one thing, if I ask you one question,

play02:25

What is the size?

play02:28

When you need to change your way.

play02:32

To get the game.

play02:34

To get the game.

play02:36

Now look,

play02:38

What way did you take to get the 250kb game?

play02:41

You used the internet.

play02:42

Because for you it was fast.

play02:45

And for 60 GB what did you take?

play02:47

For 60 GB you took a physical visit.

play02:54

You applied it because if you will visit physically,

play02:57

So quickly I will bring my game to the hard disk.

play03:00

Internet is 1GB per day.

play03:02

How will 60 GB come? ok.

play03:05

So you used this.

play03:06

If I question you,

play03:08

As the size of this input will get increased

play03:12

Consider this as an input, now let's move towards technical terms.

play03:15

Enough of analogy, Enough of stories.

play03:20

This is input.

play03:21

And what is the output?

play03:23

Output is you have to bring it.

play03:25

What do you have to achieve?

play03:26

What is your problem? You need a game.

play03:29

You have to solve the problem.

play03:31

Consider this as an algorithm.

play03:33

Consider this algorithm 1 and this one as algorithm 2.

play03:36

Ok

play03:39

Algorithm 1,

play03:41

You applied that for 250kb input.

play03:44

You used algorithm 2 for 60 GB input.

play03:48

This means that,

play03:50

As the size of the input was increased

play03:55

Like that, the runtime of the algorithms

play03:59

Do you understand runtime?

play04:01

The time you required to bring it.

play04:02

Understand that.

play04:03

In that much time, the runtime of the algorithms keeps on changing.

play04:07

As the input will keep on increasing

play04:10

The runtime of those algorithms will increase.

play04:11

Let us analyze if it is true or not.

play04:14

If I am sending with through the internet,

play04:16

If I am sending 230kb

play04:18

Consider it comes in 2 sec ok.

play04:21

Maybe if I send 1 MB

play04:24

Then in how much time it will arrive?

play04:26

Here 250 kb, I will keep things very simple.

play04:28

Consider 250 kbps speed ok

play04:31

Kbps speed is coming and you sent

play04:34

250 kb then it came in 2 sec ok.

play04:36

And if you sent 1 MB then you will need 4 secs.

play04:42

So if your file is 250 kb

play04:45

Then it needs 1 sec and if the file is of 1MB

play04:48

Then you will need 4 secs ok.

play04:51

It means that as the input size is increasing like that

play04:56

The time required to send the file,

play05:00

That is also increasing.

play05:01

So here its runtime is changing,

play05:06

Along with input size.

play05:07

Will this also change its runtime along with input time?

play05:09

There is a hard disk then there is your motorcycle.

play05:13

You will go on that bike.

play05:15

And you will take it and in hard disk whether you bring 250kb

play05:19

Or after that you bring Tb

play05:21

Assuming we will ignore the copying time.

play05:24

Copying time is negligible.

play05:26

So if you will go and bring it then you will require the same time.

play05:29

It is the correct thing & let's assume you call your friend

play05:32

That copy it and keep it ready, I am coming.

play05:34

So that time we will consider a constant ok

play05:37

That time won't be required.

play05:38

Then what will happen?

play05:40

After increasing your input there was no change in your runtime

play05:47

Ok

play05:48

So as the input size of algo2 increased

play05:53

Like that what happened?

play05:55

For that, there was no change in the runtime.

play05:58

Runtime remained the same.

play06:00

So you will say what is this?

play06:02

Ok, so both of them are different algorithms.

play06:05

And of these both algorithms

play06:07

We are to trying to remove the time complexity of them.

play06:12

When we ask questions like as the input will increase,

play06:16

Then the runtime will change as per what?

play06:19

According to what? Take a guess at it.

play06:21

Now, maybe I take to you copy 5-6TB

play06:26

Then you will forget by the internet.

play06:27

If you have a NASA internet connection then it is ok,

play06:31

But if you jio then go sleep.

play06:33

Don't sleep just forget about the file.

play06:36

It won't come.

play06:37

It won't go.

play06:38

From your friend, it won't come to you ok.

play06:40

It will take so much time.

play06:42

So when we analyze algorithms

play06:46

Before using any algorithm we analyze it

play06:49

And we ask questions to ourselves

play06:52

Is it the best algorithm for me?

play06:56

And is there any other algorithm that performs better?

play07:00

So from where do, we take the father of good?

play07:02

So we say as the size of the input keeps on increasing,

play07:06

Similarly, what is the effect of the algorithm on runtime

play07:11

We ask for the answer to this question

play07:14

And asking the answer to this question is called analysis

play07:18

Time complexity analysis.

play07:20

In these two algorithm,

play07:21

You tell me if overall I need time

play07:25

If I say algo 1 time ok

play07:28

I will do one thing, I will go mathematical

play07:30

And here I will write T algo 1

play07:36

I will say that you have to make a formula.

play07:38

Ok, you have to make a formula.

play07:40

That will denote T algo 1.

play07:44

Now, consider you need K1 secs to change clothes.

play07:47

Ok

play07:48

Because we have to go to friend's house,

play07:50

If you will wear dirty clothes what will aunty say?

play07:53

What are you wearing?

play07:54

You need to get ready and make hair and all.

play07:57

So you need k1 time for that.

play08:00

Now consider your vehicle your bike,

play08:04

And the time required to reach the friend's house

play08:08

That is k2 ok.

play08:11

And after that

play08:13

Now you will go to aunty's house

play08:15

You will be treated.

play08:17

Aunty will make some tea and all.

play08:19

So consider you need K3 time in that.

play08:21

Now, you will go and his father will ask you

play08:24

Does he come to school or not? Does he bunk the college?

play08:28

How is he? Does he get scolded?

play08:30

So consider for all this K3 time is needed.

play08:33

And after that, you came back in k4 time.

play08:36

Consider there are different routes to come and go.

play08:39

Whatever

play08:40

So does it depend on the time

play08:43

On the size of the input

play08:44

If I consider the size of input as n

play08:47

What is n? It is the size of the game.

play08:51

N is game size. Size of game.

play08:55

Which you are going to bring.

play08:56

Now here if you add 10Gb

play09:00

Or here if you add 100Gb

play09:02

Or you add 2Tb. You will copy and bring it.

play09:05

In that much time.

play09:06

Now, if I say write it in n terms.

play09:08

You will say we can't write in n terms as constant time is required

play09:12

I will say write it.

play09:14

You will say, how are you behaving harry.

play09:16

If it can't be written then how will I write it?

play09:18

Now, if I stand on your head and say no write it.

play09:22

Or else it won't be good.

play09:23

Then you will say if you are insisting then take it.

play09:29

K1 n to the power 0+k2+k3+k4

play09:34

Even I am happy and you are happy as always.

play09:37

Ok, K1 n to the power 0

play09:40

N to the power 0 is 1.

play09:41

So what did I do?

play09:42

I wrote it in n terms. Now, listen carefully to this,

play09:46

Now, I am telling about Big O.

play09:49

What is Big O?

play09:50

Now here forget Big O. Forget what is Big O.

play09:53

Remove your brain from all these things,

play09:57

Just understand what I am saying.

play09:58

Here you are understanding a story. we are listening to a story.

play10:01

We are not studying algorithms

play10:03

Let's assume we are listening to a story.

play10:05

We are doing a real-world analysis of things.

play10:07

T algo 1

play10:08

If I write in n terms, time taken by algo 1

play10:12

I think it should be algo 2, I am sorry it's algo 2

play10:15

Because the physical visit was algo 2. no worries.

play10:18

Algo 2 is a physical visit.

play10:20

So let me correct,

play10:21

This time is required in algo 2.

play10:23

So algo does not depend

play10:26

Now, if I say in these 4 terms

play10:30

Which term can make it big?

play10:34

Biggest

play10:35

You will say n to the power 0 in all these, so n to the power 0.

play10:39

So this is Big O of n to power 0.

play10:42

Because what was the most impactful term in n

play10:46

It was n to the power 0.

play10:48

And so I said bigger n to the power 0.

play10:51

What is the n to the power 0?

play10:53

Why complicate things?

play10:54

When we can write n to power 0 as 1.

play10:56

So this is Big O of 1 ok.

play10:58

So this is the algorithm that runs in constant time.

play11:02

Big O is called a constant runtime algorithm

play11:05

Because it was constant.

play11:07

Because it was constant we remove n to power 0 and make it 1.

play11:10

So Big O of 1,

play11:12

Run time of it, there are some things that we will recite.

play11:15

Because we won't constantly use our brains again and again.

play11:19

As we see Big O of 1 it is constant.

play11:22

We have to declare it constant.

play11:24

Now, come here and listen to another story.

play11:26

Now, look what is here?

play11:28

If we do an analysis of the first algorithm

play11:32

If I do T algo1

play11:36

Then what will happen here?

play11:39

What do I have to do? I have to upload it.

play11:42

When I am sending data, then I have to upload and send.

play11:45

My main time is required in that ok.

play11:47

Now, considering I turn on my computer

play11:49

In that, I will need time L1

play11:52

After that what happened?

play11:53

Consider all preparation I required L1 which will be a constant

play11:57

5 secs,2 secs, 10 secs

play12:00

If there is an SSD in your computer then it will open in 4 secs.

play12:03

If you are using a supercomputer then it can even open in 1 sec.

play12:05

L1+ consider your speed is L2.

play12:09

Ok, so it takes a constant speed of your connection

play12:13

Which is not present in jio,

play12:14

Ok, but let's consider it is constant ok.

play12:16

Your internet speed is L2 Kbps ok.

play12:20

Ok, so id L2 kbps is your speed

play12:26

Where should I write? I will write here.

play12:28

L2 Kb per sec is your speed.

play12:31

And along with consider that game is of L3 kb.

play12:38

Ok

play12:39

Sorry L3 is not kb, game is of n.

play12:42

L is the size of the game.

play12:43

Consider that game is of N kb.

play12:45

Then what will you do?

play12:46

If the game is of N kb then how much time will you need?

play12:48

You will require n/L2

play12:50

Ok, so n/L2

play12:56

Speed= distance /time

play12:58

You have studied simple physics. What should I teach you?

play13:01

Ok, so here we removed the distance.

play13:04

Sorry, we used distance and speed to calculate time.

play13:08

That is how much time will be required.

play13:10

If I use my algo 1.

play13:14

Which means the internet way.

play13:16

So I will divide it by my internet speed.

play13:20

Ok, so the speed is distance/time.

play13:23

And time= distance /speed.

play13:25

So my distance,

play13:27

I divided my size by game.

play13:30

Ok, n/L2.

play13:33

Now here can I consider 1/L2 as another constant?

play13:36

Consider L2'

play13:38

Just to simplify a little bit.

play13:42

Ok, so what did I do? I considered that

play13:44

My this 1/L2

play13:47

It is another constant that I called L2'

play13:52

And after calling it L2'

play13:54

I will write it like this.

play13:58

L1 n0+ L2' n1

play14:03

Now tell me which is the most impactful term in these both?

play14:07

Among these two.

play14:08

Obviously, it is this.

play14:10

And what is its degree? What will it be if we remove constant

play14:13

N to the power 1.

play14:15

And I will write it like this.

play14:17

This is not equal to. Writing equal to is wrong here.

play14:20

So here I will add one tilt.

play14:22

Approximately I will say that

play14:24

The most difference that will be visible

play14:26

It will be because of this term.

play14:29

Compared to this term.

play14:31

Because n to power 1, if I increase input and make it 10 lakh

play14:35

So this was of 2 lines but this will become 10 lakh.

play14:38

So the higher degree term in the polynomial

play14:42

In any equation

play14:44

The most impactful term

play14:46

It is taken ok.

play14:49

So I picked it over here.

play14:50

I picked this because in the comparison with n to the power 0 it is big.

play14:55

And I want to see things in a simple way.

play14:58

I want to simplify ok.

play14:59

Now, I will write this as

play15:01

Big O of n to the power 1, that means Big O of n.

play15:05

So this algorithm is big O of n.

play15:09

Which means it runs in linear time.

play15:12

I will call it linear b.

play15:15

Ok, if you go into the real world or industry

play15:21

You get a job in any product-based company

play15:23

Then what will happen? There you won't listen to a story.

play15:26

You will be given an algorithm and input.

play15:30

And accoring to that,

play15:32

You have to realize In how much time you will get the output.

play15:36

You have to choose the algorithm according to yourself.

play15:39

So I have made nodes

play15:41

That is amazing, we will see one more example.

play15:44

But before that, I want to understand this thing.

play15:47

And this thing is very basic.

play15:52

And it makes a foundation of algorithms.

play15:55

It is very important I want to tell you that.

play15:57

It is very important, so understand it very carefully.

play16:00

I have tried to simplify it.

play16:02

Even now if you haven't understood then the reason is

play16:05

While watching the video you were not paying attention.

play16:07

You can go back and watch it again.

play16:09

There is nothing wrong with it, go back if you haven't understood.

play16:13

I don't recommend moving forward if you haven't understood.

play16:16

From where you haven't understood watch it from there.

play16:18

It is a simple story ok.

play16:20

You can send file by internet or can physically go and copy in hard disk.

play16:24

If you will send with the internet,

play16:26

The time required will be dependent on the size of the file.

play16:29

If you do a physical visit,

play16:30

Then the time required will be as per the size, The time required will be constant.

play16:34

So I told you how I wrote constant as Big O of 1.

play16:37

How I wrote this as Big O of n.

play16:39

I told you that.

play16:40

Now there are many types of algorithms

play16:42

Like Big O of n of n.

play16:44

Now what this log is, we will talk about it later.

play16:46

Big O of n square.

play16:48

And Big O of n cube.

play16:50

There is also Big O of factorial n.

play16:52

So we will see all that things.

play16:55

But here I just want to tell you,

play16:58

Linearly if your time

play17:01

Is scaling with the input size,

play17:04

Big O of n.

play17:05

And if it is constant then Big O of 1 ok.

play17:07

So time complexity analysis of Big O tells that

play17:12

That your time, the time that is required

play17:17

To run your algorithm

play17:20

It scales according to what?

play17:23

If it runs in linear time

play17:26

Big O of n.

play17:27

If it runs in constant time,

play17:30

Big O of 1 ok.

play17:32

So here this thing is clear.

play17:35

I hope it is cleared.

play17:36

I will quickly clean the board.

play17:38

So I have kept this duster properly.

play17:40

And it gives me a lot of convience.

play17:42

Now, we will put some light on Big O.

play17:45

What is this Big O?

play17:46

What have I written this O O?

play17:49

Ok, I will explain this now.

play17:51

And this thing is very necessary so listen carefully.

play17:54

You will have questions about it, so further we will do questions of Big O.

play17:58

Then question will become a game of your fingers.

play18:02

Of fingers.

play18:03

That too of reverse hand fingers.

play18:06

I have to adjust this like this.

play18:08

Just it does not fall, No it won't fall.

play18:10

It is a good kid. Now, what we will do over here?

play18:12

We will understand what is Big O?

play18:15

Now, look the mathematical definition of big O is different.

play18:19

And Big O's industry definition is different.

play18:22

And this thing,

play18:24

I will tell you one is the mathematical definition.

play18:27

And one is the industrial definition.

play18:30

It will be asked in the industry ok.

play18:33

So what will happen in the industrial definition?

play18:35

You have to precisely tell what is Big O.

play18:41

And here I will also call it the order of.

play18:46

So further we will learn Big O, big omega and big thita.

play18:49

But now just understand that,

play18:51

O in the industry means the order of

play18:53

And its mathematical definition that I will tell you

play18:56

We will see it further.

play18:57

But I want to put this thing in your mind

play18:59

If we define Big O in mathematics.

play19:03

Then it is slightly different

play19:05

As the industry person will take your interview

play19:07

He will ask or else if you are giving exams like GATE and all

play19:09

By the way, you can watch this course for GATE.

play19:12

There is no problem.

play19:13

Here I told you I am making it for placement preparation primarily

play19:18

But it does not mean you can't watch it for GATE.

play19:21

All concepts are the same.

play19:22

When you are solving a question then there is the mathematical definition.

play19:26

But when you are answering in industry

play19:28

Then industry definition is used.

play19:29

What is the difference between both of them?

play19:30

The big O of n

play19:33

It is automatically big O of n square ok.

play19:38

And that automatically is also big O of n cube.

play19:43

So write higher-order terms like this.

play19:45

This is its mathematical definition.

play19:47

But your industry definition expects that

play19:51

Write it in as smaller as you can.

play19:53

Ok, so here

play19:56

Industry definition is a minimum of this.

play19:58

You will have to analyze the outcome and write the minimum.

play20:02

Now, further, we will see what is its value.

play20:05

Further, we will see what mathematics is involved in it.

play20:08

Why does it happen?

play20:09

But now if you find this thing strange

play20:12

Leave it, we will see it further.

play20:14

But I just wanted to tell you ok.

play20:16

So when I use its mathematical definition then I say Big O

play20:23

But when I give industry definition

play20:25

Or I am answering any interview.

play20:27

Then I will say an order of.

play20:30

I will say the order of because big O has a different definition.

play20:33

But they are used interchangeably.

play20:36

But you should know the story. This is a storyline.

play20:39

What is in industry and what is its mathematical definition?

play20:43

Here it is used in a different way.

play20:46

So you should know that thing. Ok.

play20:49

So quickly I will erase it.

play20:51

And along with that what will I do?

play20:53

I will make a graph. A graph is very important.

play20:55

Where is my duster? It is up.

play20:56

Previously I use to keep it there.

play20:58

Now I will erase it.

play20:59

Now what we will do?

play21:02

We will make a graph ok.

play21:04

So you saw two algorithms.

play21:06

Our algo 1

play21:08

Please stay here.

play21:09

Our algo 1

play21:13

That was we will send the file through the internet.

play21:16

And our algo 2

play21:19

It was a physical visit. I will write it as PV.

play21:22

It is not an IIT's question PV.

play21:24

It is a physical visit ok.

play21:26

So here what am I doing?

play21:29

Algo 1 is the internet.

play21:32

And we saw that this is Big O of n.

play21:35

And this was Big O of 1.

play21:37

It was a constant time run algorithm ok.

play21:39

Now listen to me very carefully.

play21:41

If I say,

play21:45

That I want to plot.

play21:48

Input and time.

play21:52

What is this time? It is the time at which you reached your friend's house.

play21:56

And what is this n?

play21:58

This is the n

play22:00

What is this? It is the size of the game.

play22:07

It is your game size ok.

play22:09

So it is game size.

play22:11

So if I plot algo 2 then this was constant.

play22:18

Every said K1+

play22:20

You watch it.

play22:21

Go back in the video, I wrote the equation over here.

play22:23

Whatever it is constant, its graph will be plotted like this.

play22:25

So Big O of 1 is plotted like this.

play22:29

It does not mean it is 1's graph.

play22:31

Don't confuse it with the x=1 graph.

play22:33

This is the graph of x=k.

play22:34

Constant, whatever constant was there in constant time it was running.

play22:38

And whose graph is this that I am making? It is a graph of Big O of n.

play22:44

And this y=mx+c

play22:48

It can be somewhat distorted.

play22:50

We have just taken higher-order term ok.

play22:52

So what is it? It is the graph of Big O of n.

play22:55

Consider, when you wanted to send 250kb

play22:59

So when you wanted to send a 250 kb file

play23:01

Then where did you need less time?

play23:03

Here is less time.

play23:04

Clearly in Big O of n.

play23:06

You were sending it through the internet.

play23:08

Then as your file size was increasing,

play23:10

Then it became difficult for you to take a decision.

play23:13

Can I send it?

play23:14

Can I send it or not?

play23:16

Then here there will be one such time, consider that

play23:18

I will consider 1 Gb ok.

play23:20

That is 1 Gb you need that much time only.

play23:22

Let's consider. Let's assume it's ok.

play23:24

That you require the same time in 1GB as you needed in sending internet.

play23:30

That much time you will need in the physical visit.

play23:33

So at one time, they will meet Big O of 1 and Big O of n

play23:36

And after that in Big O of 1, you will have a benefit.

play23:39

Ok, as input size will increase.

play23:40

So consider as the size of the game becomes 20GB

play23:45

You won't want that you will have to wait for so much time.

play23:47

You will want to go home and bring it.

play23:50

So in that, you require less time.

play23:51

So I made this graph of Big O of 1 and Big O of n.

play23:54

It is not a graph of y=1 and y=n.

play23:57

It is the graph of the Big O of 1 algorithms equation.

play24:01

It is the graph of the Big O of n algorithms equation.

play24:04

Ok,This y=1, y=n

play24:07

Or else y= whatever equation was there of algorithm runtime

play24:12

It is a graph of that approximately.

play24:14

So we will approximately plot that.

play24:16

And we will compare those all ok.

play24:19

We will compare, so

play24:20

I will also show you other graphs.

play24:23

At this time I would like to take you,

play24:25

Towards the notes that I have made for you all.

play24:27

And let's see some concepts from it.

play24:29

And let's try to understand this thing more.

play24:33

So the notes I have prepared for you all,

play24:36

You must be knowing the Ds0 pdf

play24:39

I have already given that.

play24:40

And the Ds1.pdf

play24:42

I have said 0 to the introduction so it is Ds0 pdf.

play24:45

And we will open Ds1.pdf and as you can see

play24:49

I have opened Ds1.pdf

play24:51

And along with that, I am giving you an example.

play24:56

Of some pizzas

play24:57

So here I have given an example of time complexity.

play25:00

If I want to eat a pizza I can send my brother to bring pizza.

play25:04

But my friends also come.

play25:07

Here I have written you can read it,

play25:09

Here I have written if some friends come to your house.

play25:11

Then you can't order 68 pizzas at once.

play25:15

It can't come in an optimal or short time.

play25:18

Maybe he needs to do 10 rounds.

play25:20

Or maybe he needs 15 rounds to bring it.

play25:23

You got the point.

play25:24

The size means the pizzas that you want to order,

play25:27

It is affecting the time over here.

play25:31

The time in which you get the pizzas.

play25:34

It is affecting that.

play25:36

As you saw over here.

play25:37

So we saw time complexity over here.

play25:39

So if I define time complexity,

play25:41

Then that is the study of the efficiency of algorithms.

play25:45

How time taken to execute an algorithm

play25:49

Grows with the size of the input.

play25:51

As the size of the input will increase,

play25:53

In that way what will happen to time?

play25:56

The size will increase and time will increase or decrease

play25:59

So here I took an example,

play26:01

Of two developers.

play26:02

What are they doing?

play26:03

They want to sort n numbers.

play26:05

So here I am giving a real-world example because

play26:07

The examples we took were very naive.

play26:10

They were very simple.

play26:12

Now, here I am taking a real-world example.

play26:15

Of sorting of n numbers.

play26:17

If of n numbers

play26:18

N can be anything. It can be 10,20,100

play26:22

I can make it 1000 numbers.

play26:24

In sort, consider there is an element in an array.

play26:26

There are elements in arrays.

play26:28

I want to sort them.

play26:30

So when I will run it for input size n,

play26:35

So I told shubham to make an algorithm

play26:38

And I told Rohan to make an algorithm.

play26:41

And both made the algorithms.

play26:44

And after that I tested them.

play26:46

So the algorithm which was of shubham

play26:49

To sort10 elements he took 90 milliseconds.

play26:52

In 1 second there are 1000 milliseconds.

play26:55

And Rohan's algorithm took 122 milliseconds.

play26:57

So from here, I found that Rohan's algorithm is bad.

play27:01

What are you doing? What have you written?

play27:03

Then I gave 70 elements.

play27:05

Again shubham's algorithm won.

play27:07

I said Rohan you don't know how to code.

play27:09

What have you written?

play27:10

It is taking 124 ms to run. Learn something from shubham.

play27:12

His algorithm runs in 110ms.

play27:14

After that, it was time for 110 elements.

play27:18

And over there shubham's algorithm took 180ms.

play27:22

And over there Rohan's algorithm was around 120-130ms.

play27:27

It took 121 ms.

play27:29

After that when I gave 1000 elements for their algorithms,

play27:32

Then shubham's algorithm got busted over there.

play27:35

It took 2 seconds.

play27:36

But Rohan's algorithm did it in 800 ms.

play27:40

So what will you conclude from this?

play27:42

Here you can see, initially, if we have to use for less elements

play27:45

Then Shubham's algorithm is better.

play27:47

But if I have to run for big elements

play27:50

If I have to run on big data then I will use Rohan's only.

play27:53

Even though it takes more time for short elements

play27:59

In comparison to Shubham's algorithm.

play28:01

Then here I have asked one question,

play28:02

Whose algorithm is better?

play28:04

Whose algorithm is better?

play28:06

So here the answer is both are better in their places.

play28:09

If I compare it overall then I will give it to Rohan.

play28:12

The prize of writing a good algorithm.

play28:15

As the size increases, he wrote as per that.

play28:19

Shubham for the small elements,

play28:21

Maybe he coded something for small elements.

play28:24

Which can sort. Maybe I am just saying.

play28:27

And the good algorithm runs in less time for big input.

play28:33

Ok, so it is an identity of a good algorithm.

play28:36

So here if we have to do small elements

play28:40

Consider if I have to do for 10 or less than 10 elements,

play28:42

Then I won't see Rohan's algorithm.

play28:45

But if I need to sort big elements,

play28:48

In general, if I have to plugin an algorithm in any system

play28:50

Then I will plugin Rohan's algorithm

play28:52

Because it optimally performs for big inputs too.

play28:56

I don't want to read full notes.

play28:58

I have written the same thing that I told you before.

play29:00

Jio story which you must have seen.

play29:04

And we saw as the file size was growing

play29:07

The time for online sending was increasing.

play29:10

Physical sending was happening in constant time.

play29:12

Now, here I have explained properly how to calculate order time.

play29:17

Here I told you the highest order term

play29:19

That gives a big impact on the formula.

play29:23

It gives a big impact,

play29:25

That is n square ok.

play29:26

Because if you add a big value of n,

play29:28

Then this will be nothing in comparison to as big this will.

play29:31

Put 10k value and see what is the value of this term.

play29:35

This is a full overall term and overall equation

play29:38

How much importance is it giving?

play29:41

How much value it is adding?

play29:42

So you will know this.

play29:43

So we take the highest order term of this equation.

play29:46

Not necessary it is the highest degree polynomial.

play29:50

It is not necessary to be a polynomial.

play29:52

In this equation, it can be factorial.

play29:53

It can be anything, ok.

play29:55

But we take the highest order term.

play29:57

And after taking the highest order term,

play30:00

What we will do? We will add in Big O.

play30:03

And Big O n square is here.

play30:05

And here n whatever it is,

play30:08

Like here it was 5 size n.

play30:10

In that term, we will write the equation.

play30:13

After that, we will take the highest degree term.

play30:15

As here we wrote Big O n to the power 0.

play30:18

I did it Big O of 1 as n to the power 0 is 1.

play30:21

Because there was no n in it. It was running in constant time.

play30:23

So in this way we analyze algorithms.

play30:26

Now, look over here

play30:27

Note that these are the formulas for time taken by them.

play30:31

Ok

play30:32

These are the formulas.

play30:33

Some constants are used, by removing some constants

play30:35

You can pick the highest order term ok.

play30:37

Then I visualized it for you guys.

play30:40

And here I showed you guys that

play30:42

How you can plot Big O of 1 and Big O of 1 in a graph?

play30:46

And here we can also plot other algorithms.

play30:51

Big O of 1, Big of n,

play30:53

We can plot algorithms on one sheet.

play30:57

And here I have added one thing over here

play30:59

As I plotted and showed it to you,

play31:01

On board that Big O of 1

play31:03

And Big O of n can be plotted on the graph.

play31:06

Here I have added a very beautiful chart.

play31:09

In which Big O's are compared.

play31:12

How will big O of log look in graph?

play31:14

How will big O of 1 look?

play31:16

This is Big 0 of n and Big O of n logn.

play31:19

This is big O of n square.

play31:20

This is Big O of 2 to power n.

play31:21

This is Big O of factorial N.

play31:23

And here they have also marked it with colors.

play31:26

How does this algorithm go in a good and bad way?

play31:30

Green means good and red means bad.

play31:33

Bad as in it takes more time to run.

play31:35

And I have taken it from stack overflow, you can go and check the answers.

play31:39

But I got most of the value in this figure over here.

play31:42

So I took this over here.

play31:44

You can understand it properly just by looking at it.

play31:47

Definitely read these notes once.

play31:51

It is very important for you.

play31:53

If you read notes along with the video.

play31:54

Then I don't think you will have a problem understanding time complexity of Big O.

play31:59

Now, in the next video,

play32:02

In upcoming videos we will solve problems & see other concepts

play32:05

But I hope this video added value.

play32:08

If you think this video is good

play32:10

If you liked it then do like this video.

play32:12

Along with that if you haven't accessed the playlist

play32:16

Of data structures and algorithms

play32:18

Then definitely access it,

play32:19

Because it is the place where everything is going to be uploaded.

play32:21

This is the playlist, I will add everything here.

play32:23

All videos will come in this.

play32:25

If you bookmark it then it will be good for you.

play32:27

By clicking here and here save it.

play32:30

This much only for this video guys.

play32:31

Thank you so much guys for watching this video.

play32:33

And I will see you next time.

Rate This

5.0 / 5 (0 votes)

関連タグ
Algorithm EfficiencyTime ComplexityBig O NotationInternet SpeedPhysical VisitData AnalysisCoding TutorialEducational ContentTechnical ExplanationStorytelling in Tech
英語で要約が必要ですか?