• Nem Talált Eredményt

Introduction to algorithms, Big O notation

N/A
N/A
Protected

Academic year: 2022

Ossza meg "Introduction to algorithms, Big O notation"

Copied!
30
0
0

Teljes szövegt

(1)

Introduction to algorithms, Big O notation

László Papp

BME

2022. 02. 14.

(2)

Course information

I Lecture: Room E405 at 8:30-10:00 on Mondays, all weeks I Practice lecture: Room E405 at 8:30-10:00 on Thursdays,

only on even weeks

I All official information will be announced on the

Homepage, its location iswww.cs.bme.hu/~lazsa/

combopt2022spring/combopt2022spring.xhtml Please follow it.

I Slides will be uploaded to the homepage.

I There will be no slides for practice sessions.

(3)

Contact

I You can send me a message on Teams.

I My email addres: lazsa@cs.bme.hu

I You can meet me at my office after organizing a metting.

I You can ask me after the lectures/practice sessions

(4)

Requirements

There will be two midterms during the semester:

I 28th of March, 18:00??

I 19th of May, 18:00??

I To complete the course you have to receiveat least 40%

of the points at each midterm.

I If you fail them or want to improve your grade, there will be two occasions at the end of the semester where you can retake a midterm.

I More details (location for example) will be announced later in homepage.

(5)

What is an optimization problem?

A setAand an objective functionf :A→Ris given. Acontains the possible solutions of a problem and for anyx ∈Af(x)is a real number.

We are looking for an elementy inAwhich satisfies thatf(y)is the smallest/biggest possible.

Example:A= [−1,4],f(x) =2−x2+3x. Find an element of Awheref attains its maximum value (if such an element exists)!

We can find such an element by using calculus:

f0(x) =−2x +3,f0(32) =0 and f00(x) =−2. Therefore 32 is a maximum point and the maximum value isf(32) = 174.

(6)

What is an optimization problem?

A setAand an objective functionf :A→Ris given. Acontains the possible solutions of a problem and for anyx ∈Af(x)is a real number.

We are looking for an elementy inAwhich satisfies thatf(y)is the smallest/biggest possible.

Example:A= [−1,4],f(x) =2−x2+3x. Find an element of Awheref attains its maximum value (if such an element exists)!

We can find such an element by using calculus:

f0(x) =−2x +3,f0(32) =0 and f00(x) =−2. Therefore 32 is a maximum point and the maximum value isf(32) = 174.

(7)

What is combinatorial optimization?

Now the set of possible solutionsAis finite and contains combinatorial objects. We are looking for a solutionx which maximizes (or minimizes) the objective function.

Example:

Find the fastest route from Budapest to Venice!

(8)

What is the difference between continuous and combinatorial (discrete) optimization?

Optimization Continuous Combinatorial Search space

Number of elements infinit finite

Topology continuous discrete

Tools to solve

Calculus important less useful

Linear algebra useful useful

Algorithmic theory useful very important Graph theory useless important

During the semester we are going to focus on three mayor areas: Algorithmic theory, Graph theory and Linear algebra.

(9)

What is an algorithm?

We do not give a formal definition for what an algorithm is. We usually say the following:

An algorithm is a method which can be implemented as a computer program and can be executed on a computer.

Input Algorithm Output

Numbers a and b

Addition algorithm

Number a+b Example:

An algorithm usually has an input and an output. The algorithm receives its input, works with it, then it gives us its output.

(10)

Example: An algorithm for addition

Input: Integersaandb.

The algorithm is what we have learnt in elementary school:

We add multi digit numbers by digits starting from the ones column at the right and if the sum of the two digits is bigger than one then we carry the “extra” digit to the next column.

147 +105 252

10010011 +1101001 11111100

There are several algorithms for addition, but most of us have learnt this one. Why?

Because it is simple and “fast”, but what does “ fast” mean?

(11)

How to measure the efficiency of an algorithm?

Problem: We have a problem and we found two or more algorithms which solve it. Which one should we use?

There are several metrics which measure how good an algorithm is. For example: How much memory does it need, how many processor cores does it utilize, how much time does a computer need to execute it on a specified input, etc.

In this course we are focusing only at the running time of an algorithm. The running time of an algorithm depends on several things, for example the type of the computer which we use. To get rid of the differences between computers, instead of

measuring time, we count the number of elementary operations which the algorithm makes before it gives back its output. The obtained number still depends on the input. For example multiplying big numbers requires more time than multiplying small ones.

(12)

How to measure the efficiency of an algorithm?

Problem: We have a problem and we found two or more algorithms which solve it. Which one should we use?

There are several metrics which measure how good an algorithm is. For example: How much memory does it need, how many processor cores does it utilize, how much time does a computer need to execute it on a specified input, etc.

In this course we are focusing only at the running time of an algorithm. The running time of an algorithm depends on several things, for example the type of the computer which we use. To get rid of the differences between computers, instead of

measuring time, we count the number of elementary operations which the algorithm makes before it gives back its output. The obtained number still depends on the input. For example multiplying big numbers requires more time than multiplying small ones.

(13)

How to measure the efficiency of an algorithm?

Problem: We have a problem and we found two or more algorithms which solve it. Which one should we use?

There are several metrics which measure how good an algorithm is. For example: How much memory does it need, how many processor cores does it utilize, how much time does a computer need to execute it on a specified input, etc.

In this course we are focusing only at the running time of an algorithm. The running time of an algorithm depends on several things, for example the type of the computer which we use. To get rid of the differences between computers, instead of

measuring time, we count the number of elementary operations which the algorithm makes before it gives back its output.

The obtained number still depends on the input. For example multiplying big numbers requires more time than multiplying small ones.

(14)

The size of the input Definition

Fix an alphabet. Thesize of the inputover this alphabet is the number of symbols (contained in the alphabet) needed to encode the input.

Example

Determining the parity of an integer:

I Input: An integera

I Alphabet: {1,2,3,4,5,6,7,8,9,0}(we use the decimal number system now)

I If the input is 168, then its size is 3.

I If the input is an integera, then its size isdlog10(a+1)e.

In the computer’s memory everything is a binary number. Therefore the input is encoded as a binary number.

If the input is a natural numbera, then its size isdlog2(a+1)e. We will writelog2(a)in the later slides for simplicity.

(15)

The size of the input Definition

Fix an alphabet. Thesize of the inputover this alphabet is the number of symbols (contained in the alphabet) needed to encode the input.

Example

Determining the parity of an integer:

I Input: An integera

I Alphabet: {1,2,3,4,5,6,7,8,9,0}(we use the decimal number system now)

I If the input is 168, then its size is 3.

I If the input is an integera, then its size isdlog10(a+1)e.

In the computer’s memory everything is a binary number.

Therefore the input is encoded as a binary number.

If the input is a natural numbera, then its size isdlog2(a+1)e.

We will writelog2(a)in the later slides for simplicity.

(16)

The size of the input

Question: What is the size of 168 if we encode it as a binary number?

Answer:dlog2(169)e=8. 168 in binary form is 10101000 and the number of digits is 8.

Remark: The logarithmic identitylogk(x) = loglog2(x)

2(k) guarantees that using binary encoding instead of an alphabet containingk symbols results in anlog2k increase of the size of the input. For example the size of a number in the binary number system is approximatelylog210≈3.2 times its size in the decimal system.

Therefore the size of the alphabet is not important. From now,log(n)denotes the base 2 logarithm ofn.

(17)

The size of the input

Question: What is the size of 168 if we encode it as a binary number?

Answer:dlog2(169)e=8. 168 in binary form is 10101000 and the number of digits is 8.

Remark: The logarithmic identitylogk(x) = loglog2(x)

2(k) guarantees that using binary encoding instead of an alphabet containingk symbols results in anlog2k increase of the size of the input.

For example the size of a number in the binary number system is approximatelylog210≈3.2 times its size in the decimal system.

Therefore the size of the alphabet is not important.

From now,log(n)denotes the base 2 logarithm ofn.

(18)

Estimating the running time

The running time does depend not just on the size of the input, but also on the structure of the input itself.

For example calculating 10000·1 is much easier than

calculating 432·167, but the sizes of these inputs are the same.

For safety reasons, (think about mission critical applications like airplanes, cars, powerplants, etc.) we are generally interested in the worst case scenario.

Definition

Thetime complexity of an algorithmAis an integer-valued functionf whose value atntells us the maximum number of steps (elementary operations) which need to be executed on any input of sizen.

Usually it is hard to calculate the time complexity of an

algorithm, but a close upper bound is good enough for us. For example if the time complexity is 3n2−2n+1 then 3n2is an upper bound and it is much easier to work with.

(19)

Estimating the running time

The running time does depend not just on the size of the input, but also on the structure of the input itself.

For example calculating 10000·1 is much easier than

calculating 432·167, but the sizes of these inputs are the same.

For safety reasons, (think about mission critical applications like airplanes, cars, powerplants, etc.) we are generally interested in the worst case scenario.

Definition

Thetime complexity of an algorithmAis an integer-valued functionf whose value atntells us the maximum number of steps (elementary operations) which need to be executed on any input of sizen.

Usually it is hard to calculate the time complexity of an

algorithm, but a close upper bound is good enough for us. For example if the time complexity is 3n2−2n+1 then 3n2is an upper bound and it is much easier to work with.

(20)

Estimating the running time

The running time does depend not just on the size of the input, but also on the structure of the input itself.

For example calculating 10000·1 is much easier than

calculating 432·167, but the sizes of these inputs are the same.

For safety reasons, (think about mission critical applications like airplanes, cars, powerplants, etc.) we are generally interested in the worst case scenario.

Definition

Thetime complexity of an algorithmAis an integer-valued functionf whose value atntells us the maximum number of steps (elementary operations) which need to be executed on any input of sizen.

Usually it is hard to calculate the time complexity of an

algorithm, but a close upper bound is good enough for us. For example if the time complexity is 3n2−2n+1 then 3n2is an upper bound and it is much easier to work with.

(21)

Estimating the running time

The running time does depend not just on the size of the input, but also on the structure of the input itself.

For example calculating 10000·1 is much easier than

calculating 432·167, but the sizes of these inputs are the same.

For safety reasons, (think about mission critical applications like airplanes, cars, powerplants, etc.) we are generally interested in the worst case scenario.

Definition

Thetime complexity of an algorithmAis an integer-valued functionf whose value atntells us the maximum number of steps (elementary operations) which need to be executed on any input of sizen.

Usually it is hard to calculate the time complexity of an

algorithm, but a close upper bound is good enough for us. For example if the time complexity is 3n2−2n+1 then 3n2is an upper bound and it is much easier to work with.

(22)

The bigO notation Definition

Letf(n)andg(n)be non negative functions. Then

f(n)∈O(g(n))means that there exists a natural numberN and a positive constantc, such that for everyn>N,f(n)≤cg(n).

Example:2n3−8n2+25∈O(n3)because 2n3−8n2+25≤2n3for alln>2=N.

(23)

The bigO notation Definition

Letf(n)andg(n)be real valued functions. Thenf(n)∈O(g(n)) means that there exists a natural numberNand a positive constantc, such that for everyn>N,f(n)≤cg(n).

Example:2n3−8n2+25∈O(n3)because 2n3−8n2+25≤2n3for alln>2=N.

(24)

The meaning of the big O notation

I 2n3−8n2+25∈O(n3)means that after a while 2n3−8n2+25 does not grow faster than a constant multiple ofn3.

I 2n3−8n2+25∈/O(n2)means that 2n3−8n2+25 grows faster than any quadratic function.

I If you are good at calculus, then you can think about the following equivalent definition: f(n)∈O(g(n))if and only if lim supn→∞g(n)f(n) <∞.

(25)

Examples forOnotation

Question: Isf(n) =3n3+2nlog(n)∈O(n3)?

Answer:Yes, because there is a pair ofc,Nwhich satisfies the definition:

3n3+2nlog(n)≤3n3+2n2≤5n3ifn≥2, sof(n)≤cn3for all n≥Nifc =5,N =2.

Note that the pairc=6,N=3 is also good. Question: Isf(n) =3n3+2nlog(n)∈O(n4)? Answer:Yes, we can verify the definition again: 3n3+2nlog(n)≤3n3+2n2≤5n3≤5n4ifn≥2, so f(n)≤cn4for alln≥Bifc =5,N =2.

Question: Isf(n) =3n3+2nlog(n)∈O(n2)? Answer:No.

Homework: Prove it!

(26)

Examples forOnotation

Question: Isf(n) =3n3+2nlog(n)∈O(n3)?

Answer:Yes, because there is a pair ofc,N which satisfies the definition:

3n3+2nlog(n)≤3n3+2n2≤5n3ifn≥2, sof(n)≤cn3for all n≥Nifc =5,N =2.

Note that the pairc=6,N=3 is also good.

Question: Isf(n) =3n3+2nlog(n)∈O(n4)? Answer:Yes, we can verify the definition again: 3n3+2nlog(n)≤3n3+2n2≤5n3≤5n4ifn≥2, so f(n)≤cn4for alln≥Bifc =5,N =2.

Question: Isf(n) =3n3+2nlog(n)∈O(n2)? Answer:No.

Homework: Prove it!

(27)

Examples forOnotation

Question: Isf(n) =3n3+2nlog(n)∈O(n3)?

Answer:Yes, because there is a pair ofc,N which satisfies the definition:

3n3+2nlog(n)≤3n3+2n2≤5n3ifn≥2, sof(n)≤cn3for all n≥Nifc =5,N =2.

Note that the pairc=6,N=3 is also good.

Question: Isf(n) =3n3+2nlog(n)∈O(n4)?

Answer:Yes, we can verify the definition again: 3n3+2nlog(n)≤3n3+2n2≤5n3≤5n4ifn≥2, so f(n)≤cn4for alln≥Bifc =5,N =2.

Question: Isf(n) =3n3+2nlog(n)∈O(n2)? Answer:No.

Homework: Prove it!

(28)

Examples forOnotation

Question: Isf(n) =3n3+2nlog(n)∈O(n3)?

Answer:Yes, because there is a pair ofc,N which satisfies the definition:

3n3+2nlog(n)≤3n3+2n2≤5n3ifn≥2, sof(n)≤cn3for all n≥Nifc =5,N =2.

Note that the pairc=6,N=3 is also good.

Question: Isf(n) =3n3+2nlog(n)∈O(n4)?

Answer:Yes, we can verify the definition again:

3n3+2nlog(n)≤3n3+2n2≤5n3≤5n4ifn≥2, so f(n)≤cn4for alln≥Bifc=5,N =2.

Question: Isf(n) =3n3+2nlog(n)∈O(n2)? Answer:No.

Homework: Prove it!

(29)

Examples forOnotation

Question: Isf(n) =3n3+2nlog(n)∈O(n3)?

Answer:Yes, because there is a pair ofc,N which satisfies the definition:

3n3+2nlog(n)≤3n3+2n2≤5n3ifn≥2, sof(n)≤cn3for all n≥Nifc =5,N =2.

Note that the pairc=6,N=3 is also good.

Question: Isf(n) =3n3+2nlog(n)∈O(n4)?

Answer:Yes, we can verify the definition again:

3n3+2nlog(n)≤3n3+2n2≤5n3≤5n4ifn≥2, so f(n)≤cn4for alln≥Bifc=5,N =2.

Question: Isf(n) =3n3+2nlog(n)∈O(n2)?

Answer:No.

Homework: Prove it!

(30)

Examples forOnotation

Question: Isf(n) =3n3+2nlog(n)∈O(n3)?

Answer:Yes, because there is a pair ofc,N which satisfies the definition:

3n3+2nlog(n)≤3n3+2n2≤5n3ifn≥2, sof(n)≤cn3for all n≥Nifc =5,N =2.

Note that the pairc=6,N=3 is also good.

Question: Isf(n) =3n3+2nlog(n)∈O(n4)?

Answer:Yes, we can verify the definition again:

3n3+2nlog(n)≤3n3+2n2≤5n3≤5n4ifn≥2, so f(n)≤cn4for alln≥Bifc=5,N =2.

Question: Isf(n) =3n3+2nlog(n)∈O(n2)?

Answer:No.

Homework: Prove it!

Hivatkozások

KAPCSOLÓDÓ DOKUMENTUMOK

We can think of a pattern P as the bipartite adjacency matrix of some ordered graph H P of interval chromatic number 2, where the order of the vertices is inherited from the order

Here we study the existence of subexponential-time algorithms for the problem: we show that for any t ≥ 1, there is an algorithm for Maximum Independent Set on P t -free graphs

For a family F of r-uniform hypergraphs (or graphs if r = 2), and for any natural number n, we denote by ex(n, F) the corresponding Tur´ an number ; that is, the maximum number of

For a family F of r-uniform hypergraphs (or graphs if r = 2), and for any natural number n, we denote by ex(n, F) the corresponding Tur´ an number ; that is, the maximum number of

For topology changes leading to generation of a large number of LSAs that arrive at a router over an extended time interval, the hold time is expected to quickly reach its maximum

Tense tells us the time reference of the verb (i.e. past, present, future), and aspect tells us how the speaker interprets the action’s time-related characteristics (i.e.. Look at

The number of inverse-conjugate compositions of an odd integer n &gt; 0 equals the number of compositions of n which are self-inverse.... We describe a bijection α between the

An FBD program always prescribes the execution order of function blocks (i.e. the elementary operations). After transforming the FBD into an FDFG, at least one