[SER222] Characterizing Algorithms (3/5): Reviewing Terminology

Ruben Acuna
15 Aug 201709:49

Summary

TLDREn este video se revisan conceptos clave de cursos previos como SER200, CSE205 y MAT243, centrándose en el tamaño del problema (n) y cómo influye en el rendimiento de un algoritmo. Se explica la relación entre el tamaño de los datos de entrada y el tiempo de ejecución, utilizando ejemplos como listas, arrays y funciones recursivas. También se aborda el concepto de funciones de crecimiento, que cuentan operaciones en relación con el tamaño del problema, y la notación Big-O, que mide cómo crece el tiempo de ejecución a medida que aumenta el tamaño del problema.

Takeaways

  • 📏 La variable (n) se utiliza para representar el tamaño del problema, como el tamaño de una lista o un arreglo que afecta el tiempo de ejecución de un algoritmo.
  • 📊 Algunas entradas, como un número simple, no afectan el tiempo de ejecución del algoritmo, pero otras como colecciones (listas, arreglos) sí lo hacen.
  • 🔄 En algoritmos recursivos, el valor de una entrada puede afectar la cantidad de llamadas recursivas, como en el caso de calcular un factorial recursivo.
  • 📝 En algoritmos que procesan cadenas, como la inversión de una cadena, el tamaño de la entrada (longitud de la cadena) afecta el tiempo que tarda el algoritmo en ejecutarse.
  • ⚖️ Las funciones de crecimiento, f(n), indican cómo crece el número de operaciones requeridas por un algoritmo a medida que aumenta el tamaño del problema.
  • 📈 Un ejemplo de función de crecimiento es f(n)=1, lo que significa que el algoritmo siempre toma la misma cantidad de tiempo, independientemente del tamaño de la entrada.
  • 🧮 La función f(n)=1+n representa un caso donde el algoritmo realiza una operación fija y luego realiza n operaciones adicionales, dependiendo del tamaño de la entrada.
  • 💡 Las funciones de crecimiento son exactas, mientras que Big-O proporciona una estimación aproximada del comportamiento de un algoritmo a medida que crece el tamaño del problema.
  • 🔠 La notación Big-O (como O(1), O(n), O(n²)) describe el comportamiento asintótico de un algoritmo, es decir, cómo se comporta cuando el tamaño del problema se vuelve grande.
  • 📉 Big-O se utiliza para expresar el límite superior del tiempo de ejecución de un algoritmo, lo que significa que representa el peor caso posible en términos de operaciones necesarias.

Q & A

  • ¿Qué significa el término 'tamaño del problema' (n) en el contexto de los algoritmos?

    -El 'tamaño del problema' (n) se refiere a la cantidad de datos que un algoritmo debe procesar, como el tamaño de una lista o un arreglo. El rendimiento del algoritmo depende del tamaño de la entrada.

  • ¿Por qué un número como entrada no suele considerarse parte del tamaño del problema?

    -Un número como entrada no suele cambiar el tiempo de ejecución del algoritmo de manera significativa, a menos que esté involucrado en un proceso recursivo, como en el caso del factorial recursivo.

  • ¿Cómo afecta el tamaño de una lista al rendimiento de un algoritmo?

    -Si la lista es más grande, el algoritmo tendrá que procesar más elementos, lo que aumenta el trabajo y el tiempo de ejecución. Por ejemplo, en un algoritmo de ordenación, el tiempo dependerá del número de elementos en la lista.

  • ¿Qué es una función de crecimiento (growth function) en el análisis de algoritmos?

    -Una función de crecimiento es una fórmula que describe cómo crece el número de operaciones de un algoritmo en función del tamaño de la entrada. Se utiliza para calcular cuántas operaciones se necesitan para un tamaño de entrada determinado.

  • ¿Cuál es la diferencia entre una función de crecimiento constante y una que varía con el tamaño del problema?

    -Una función de crecimiento constante, como f(n)=1, realiza la misma cantidad de operaciones sin importar el tamaño de la entrada. En cambio, una función como f(n)=1+n indica que a medida que el tamaño de la entrada crece, también aumenta el número de operaciones.

  • ¿Cómo se relaciona una función de crecimiento con el tamaño de la entrada (n)?

    -Una función de crecimiento vincula el tamaño de la entrada (n) con la cantidad de trabajo que un algoritmo debe realizar. Por ejemplo, f(n)=n indica que por cada elemento adicional en la entrada, el número de operaciones aumenta en proporción directa.

  • ¿Qué es la notación Big-O y cómo se utiliza?

    -La notación Big-O es una forma de describir el comportamiento asintótico de un algoritmo, indicando su rendimiento en función del tamaño de la entrada (n). Se utiliza para mostrar una cota superior del crecimiento de la función, ignorando constantes y términos pequeños.

  • ¿Cuál es la diferencia entre una función de crecimiento exacta y la notación Big-O?

    -Una función de crecimiento exacta describe el número preciso de operaciones que un algoritmo ejecuta, mientras que la notación Big-O proporciona una aproximación que solo considera el comportamiento general cuando el tamaño de la entrada es grande.

  • ¿Por qué la notación Big-O ignora constantes y términos menores?

    -La notación Big-O se enfoca en el comportamiento a largo plazo del algoritmo cuando el tamaño de la entrada (n) es grande. Las constantes y términos menores no tienen un impacto significativo en el crecimiento a gran escala, por lo que se omiten.

  • ¿Cuándo es útil aplicar la notación Big-O en el análisis de algoritmos?

    -La notación Big-O es útil para analizar el rendimiento de un algoritmo cuando el tamaño de la entrada es grande. Ayuda a comparar la eficiencia de diferentes algoritmos en términos de su escalabilidad y rendimiento en situaciones reales.

Outlines

00:00

🧠 Revisión de Terminología: Tamaño del Problema (n)

Este párrafo aborda la importancia de entender el tamaño del problema (n) en el contexto de los algoritmos. Se explica que el tamaño del problema puede variar dependiendo de los tipos de entrada, como listas o arreglos, y cómo el tamaño afecta el tiempo de ejecución del algoritmo. Algunos parámetros, como números individuales, no afectan el tiempo de ejecución, mientras que colecciones como listas o arreglos sí lo hacen. Se mencionan ejemplos como el factorial recursivo y el proceso de ordenar arreglos, donde el tamaño de la entrada impacta directamente el comportamiento del algoritmo.

05:02

📈 Funciones de Crecimiento

En este párrafo, se explica el concepto de las funciones de crecimiento, que describen cómo crece una función en términos del número de operaciones que se ejecutan en un algoritmo dependiendo del tamaño de la entrada (n). Las funciones de crecimiento son exactas y permiten calcular cuántas operaciones requiere un algoritmo según el tamaño del problema. Ejemplos como f(n) = 1 y f(n) = 1 + n muestran cómo diferentes operaciones varían con el tamaño de la entrada. El crecimiento de una función depende de las operaciones y el número de elementos procesados.

Mindmap

Keywords

💡Tamaño del problema

El tamaño del problema (n) se refiere a la cantidad de datos que un algoritmo necesita procesar. Por ejemplo, en el caso de una lista o un arreglo, el tamaño del problema sería la cantidad de elementos que contiene. A medida que este tamaño aumenta, también lo hace el tiempo que toma el algoritmo para completarse, como en el caso del ordenamiento por inserción donde el tamaño del arreglo determina el número de operaciones necesarias.

💡Algoritmo

Un algoritmo es un conjunto de pasos o instrucciones que resuelven un problema específico. En el contexto del video, se mencionan diferentes tipos de algoritmos, como el de ordenamiento por inserción, que dependen del tamaño de la entrada (como una lista o un arreglo) para determinar el tiempo que tomarán en ejecutarse.

💡Recursividad

La recursividad es un concepto en el que una función se llama a sí misma para resolver subproblemas más pequeños. Un ejemplo mencionado en el video es la función factorial recursiva, donde el tamaño del número de entrada (como 'n' en el factorial) determina cuántas veces la función se llamará a sí misma, afectando el tiempo total de ejecución.

💡Función de crecimiento

Una función de crecimiento, f(n), describe cómo crece el número de operaciones necesarias para procesar un problema en función del tamaño del problema (n). Esta función mide cuántas operaciones se realizarán a medida que aumenta el tamaño de la entrada, por ejemplo, cuando se aumenta el tamaño de un arreglo en un algoritmo de ordenamiento.

💡Operación

Una operación es una acción ejecutada por un algoritmo, como una comparación o un intercambio de elementos en un arreglo. En el video, se menciona que las funciones de crecimiento cuentan cuántas operaciones se requieren para ejecutar un algoritmo, y cómo varía la cantidad de estas según el tamaño de la entrada.

💡Big-Oh

El Big-Oh (O grande) es una notación que se utiliza para describir el comportamiento asintótico de un algoritmo, es decir, cómo crece su tiempo de ejecución o espacio necesario en función del tamaño de la entrada (n). Por ejemplo, O(n) significa que el número de operaciones crece linealmente con el tamaño de la entrada. Se usa para simplificar la descripción del rendimiento de los algoritmos, omitiendo detalles menores como constantes.

💡Complejidad temporal

La complejidad temporal mide cuánto tiempo tarda un algoritmo en ejecutarse en función del tamaño de la entrada. Por ejemplo, un algoritmo con una complejidad de O(n²) tomará más tiempo en completarse si el tamaño de la entrada aumenta significativamente, en comparación con un algoritmo con O(n).

💡Entrada del algoritmo

La entrada de un algoritmo son los datos que el algoritmo necesita procesar, como una lista, un número o una cadena de caracteres. El video explica que el tipo y tamaño de la entrada afectan directamente la cantidad de trabajo que el algoritmo debe realizar. Por ejemplo, ordenar una lista de 10 elementos tomará menos tiempo que ordenar una lista de 100 elementos.

💡Factorial

El factorial es un ejemplo de función matemática que puede implementarse de manera recursiva. En el video, se menciona cómo el número de llamadas recursivas en una función factorial depende del valor de la entrada, lo que afecta el tiempo que toma completarse. A mayor número, más llamadas recursivas y mayor el tiempo de ejecución.

💡Asintótico

El comportamiento asintótico de un algoritmo describe cómo se comporta su rendimiento cuando el tamaño de la entrada es muy grande. En el video, se hace referencia a esto en el contexto de la notación Big-Oh, que se usa para representar el rendimiento de un algoritmo cuando el tamaño de la entrada crece indefinidamente.

Highlights

Review of terminology related to algorithm problem size and growth functions.

The importance of problem size (n) and its effect on algorithm performance, particularly with arrays and lists.

Distinction between inputs that impact algorithm runtime, such as arrays or lists, versus inputs that don't, like constants.

Recursive functions' dependence on the size of the integer input, with recursive factorial as an example.

Definition of growth functions (f(n)) to show how an algorithm's number of operations grows with the problem size.

The relationship between the input size of an algorithm and the number of operations required, exemplified with an input array of size 10.

Explanation of different types of growth functions, such as f(n) = 1 (constant) and f(n) = 1 + n (linear).

Understanding how growth functions vary based on the size of the input, particularly in list traversal or element processing.

Emphasis on growth functions being exact in representing the number of operations required by an algorithm.

Introduction to Big-O notation (O(1), O(n), O(n^2)) to express the upper bound or long-term behavior of an algorithm's runtime.

Big-O notation's purpose in simplifying the expression of algorithm efficiency, removing constants and lower-order terms.

Example of 2n + 15 being O(n), as constants and coefficients are considered insignificant as n grows.

Big-O notation primarily represents the worst-case scenario or upper bound of an algorithm's performance.

Focus on Big-O notation for large input sizes, as small inputs can cause irregularities in performance comparisons.

General guidance that Big-O notation is used to represent long-term behavior, when input sizes become 'sufficiently large.'

Transcripts

play00:05

- In this video, I wanna spend a few minutes

play00:06

to review some terminology from previous classes.

play00:08

So this should all be material that you see

play00:10

either in SER200 or CSE205, or MAT243.

play00:15

Hopefully, none of this is new,

play00:16

I'm not going to spend all that much time on it.

play00:17

Really, all I want to do is just make sure

play00:19

we're kind of on the same page.

play00:20

So the first thing I wanna talk about here

play00:22

is what we call N, or the problem size.

play00:24

So a lot of times where we have an algorithm,

play00:26

and this algorithm has a bunch of inputs,

play00:27

and those inputs maybe have different types, right,

play00:29

maybe I given it a number, maybe I give it a list.

play00:31

Well, the list in particular is a little bit interesting

play00:33

because basically, that algorithm, right,

play00:35

will take a different amount of time to run,

play00:37

whether that list has one thing in it,

play00:39

or ten things in it, or twenty things in it.

play00:40

There's a dependency between an algorithm and it's input.

play00:44

And so that list that is being processed, right,

play00:46

we have to care about how big it is, right?

play00:48

And so what we'll say is it is size (n),

play00:50

and in fact, it defines the problem size.

play00:53

I'm just using this here as an example,

play00:54

it also could be something like an array.

play00:55

Right, so if you think back to insertion sort,

play00:58

insertion sort takes an array to be sorted.

play01:00

The problem size links that array.

play01:01

And so we would say that the length of that array

play01:03

is (n).

play01:05

Now typically an algorithm will have multiple inputs

play01:07

some of those will just be something like a number.

play01:08

A number doesn't really count as problem size

play01:11

the reason for that is that adjusting that number

play01:14

typically doesn't change how long

play01:15

that algorithm takes to run.

play01:16

So maybe that number has a cutoff you know,

play01:18

throw away all the values less than (k) right,

play01:21

in this input list.

play01:23

Well what we choose for (k)

play01:24

really doesn't matter all that much, right?

play01:25

It's the cut off that will always be applied the same way.

play01:28

Versus if I have a list going in, right?

play01:30

And it has different elements in there.

play01:31

Well, the algorithm has to do more work

play01:33

if there's more elements, right.

play01:34

And maybe through some like sorting right,

play01:36

in which case basically your problem

play01:37

is right, that you have to move around more things.

play01:38

Right, that's clearly more work

play01:40

versus just you know changing what is

play01:41

the cut off or the threshold that's being enforced

play01:43

and something else.

play01:45

So some inputs really matter,

play01:46

some don't.

play01:47

Some basically you treat as constants because

play01:49

they don't effect the running time of the program.

play01:51

But when you have a collection, right

play01:52

an array or a list.

play01:53

Then we care about and typically we say is

play01:54

that defines the problem size, right, (n).

play01:56

Sometimes if you take in more than one collection,

play01:59

you'll say maybe you have (m) and (n), right,

play02:01

it depends on the size of two things.

play02:04

So (n) can vary in a lot of different ways.

play02:06

We're just talking about collection size here,

play02:08

so maybe that's something like sorting.

play02:11

Another thing we could have if maybe we were talking

play02:13

about recursive function is maybe we do have an integer,

play02:16

but that integer actually doesn't pack how long

play02:20

the algorithm takes to run.

play02:21

So let's say something like recursive factorial

play02:23

and recursive factorial,

play02:24

the value of that parameter is going to control

play02:26

how many recursive calls you make.

play02:28

So the larger that number,

play02:30

the more calls you will make.

play02:31

Right, lower number, fewer calls.

play02:33

So that actually does change the algorithm's performance.

play02:35

It's not just, you know, inside of an if statement

play02:37

that does that cuts out all of these values less than (k)

play02:40

Right, it's actually having some impact on

play02:41

the number of times that function loops,

play02:43

the number of times that function calls itself.

play02:45

Now, in that case, that algorithm actually depends

play02:47

on that input.

play02:48

So in that case, it would be fair to say,

play02:50

that that's part of our problem size, right.

play02:52

So the factorial number, you know may be the factorial

play02:54

of (i),

play02:55

that also doubles as a problem size,

play02:56

and then maybe we just call it (n).

play02:57

Because that's how big our problem is.

play02:59

All right, we are trying to compute with the

play03:00

factorial of ten.

play03:01

And it's going to take me basically ten step to get there.

play03:03

So that's another idea of size.

play03:05

The last example here, is in terms of strings.

play03:08

Maybe I have an algorithm that reverses a string,

play03:10

well in that case then,

play03:10

I am going to need to through

play03:12

and touch every character inside there

play03:13

and change it's position.

play03:14

Well, again, right that algorithm is going to

play03:15

take a different amount of time to run,

play03:16

whether it has ten characters or 100 characters.

play03:19

There some sort of dependence between

play03:20

the length of that input and how long the algorithm takes.

play03:23

So, again now, that would kind of fall under

play03:24

things we need to care about.

play03:26

You know, how big they are, right?

play03:27

Because it defines our problem size.

play03:30

So in general, right, an algorithm

play03:32

is going to be defined under some sort of parameters,

play03:34

and then some of those parameters are going to have

play03:36

some sort of sizes associated with them, right.

play03:38

And those sizes are what we are going to have to

play03:39

keep track of if we are trying to figure out

play03:41

how this thing acts.

play03:42

Because how long it takes to run will vary

play03:44

with the sizes of the parameters

play03:47

that are going into it.

play03:49

All right, so that is our idea of (n).

play03:51

Our problem size, right.

play03:53

How big is something?

play03:53

How much work do I have to do?

play03:55

The next thing that I want to talk about

play03:57

is growth functions.

play03:58

So, growth functions have kind of a specific meaning.

play04:02

So, growth functions are really just some f(n).

play04:04

Right, so they're some function.

play04:06

The significance though,

play04:07

and why we call them growth functions,

play04:08

is because growth functions are showing

play04:09

how a function grows.

play04:12

Grows in terms of operations,

play04:13

so the typical f(n) is counting up a number of operations

play04:15

and really though we should be more specific there.

play04:17

We want to count up some specific action.

play04:21

Right, some specific thing the algorithm is doing.

play04:23

For now, I'll just say operation.

play04:24

So you can assume that maybe it's executing

play04:25

a line of code.

play04:26

Right, the number of things that have happened.

play04:27

You know, the number of instructions

play04:29

that need to be executed on the CPU.

play04:30

Something like that.

play04:31

Well a growth function is basically a normal function.

play04:34

That's meant to count up how many operations will

play04:38

be required for a given input size.

play04:40

Right, so I can say

play04:41

"Hey, I have an input array of size ten."

play04:44

And then my growth function will compute for me,

play04:46

oh that means it will take a hundred operations

play04:48

for you to process that.

play04:49

That is the basic idea of a growth function.

play04:51

It's giving us a way to calculate a number of

play04:53

operations from the problem size.

play04:54

We're linking (n) to amount of work that needs to be done.

play04:57

So for a lot of analysis,

play04:58

usually what we want to do is,

play05:00

you want to find out what is the growth function.

play05:02

And that's something you hopefully have done

play05:03

in a previous course,

play05:05

is actually write a growth function from a bit of code.

play05:07

We'll be doing it again here in this course,

play05:09

but hopefully you have done it before as well.

play05:12

I have some quick examples down there,

play05:13

f(n)=1 is an example of a potential growth function.

play05:17

What that is, is saying,

play05:18

"Okay, I have a growth function that no matter

play05:19

input I give it, it always takes one operation to complete."

play05:25

All right, so maybe it's something that doesn't do very much

play05:28

it just does a fixed amount of work,

play05:29

maybe it adds a number,

play05:30

right, maybe it just increases something,

play05:32

and that's really just one line of code, right.

play05:34

Or returns something that's just one line of code.

play05:36

Very simple.

play05:37

Versus something like f(n)=1+(n)

play05:41

what that's saying is,

play05:42

"Oh you're doing a little bit of work, right,

play05:43

you're doing one operation,

play05:44

plus (n) other things."

play05:46

So maybe that's where you're going through a list,

play05:48

and maybe you're increasing everything in it.

play05:49

Right, you need to go in and touch

play05:50

every element in there.

play05:52

In that case, that (n) parameter,

play05:53

the problem size,

play05:54

is based on the size of the input collection.

play05:56

The number of elements in that list.

play05:58

And so as that list gets bigger,

play06:00

the growth functions basically going to grow.

play06:03

Every time you add one to the input size,

play06:05

it's going to take one extra operation to process it.

play06:08

So in that case, basically,

play06:09

our growth function varies as our input size does,

play06:12

where in the first example it does not vary, right.

play06:14

It's a constant amount of operations are executed

play06:17

based off of any input.

play06:19

And really there in f(n)=1,

play06:20

it's a little bit awkward because

play06:22

that (n) is still there,

play06:23

so we're still saying that the problem size can vary,

play06:26

but somehow there is just no link, right,

play06:28

between that and the number of operations it takes

play06:30

to run.

play06:31

So it's a little bit awkward,

play06:32

but we do see that in some cases.

play06:36

As a final note here,

play06:37

one thing that is going to be different

play06:38

about growth functions for us in this course,

play06:39

is that growth functions are meant to be exact.

play06:41

So, they should exactly tell us how many operations

play06:44

are going to be required,

play06:45

or maybe how much time will this algorithm take.

play06:47

Right, so they should be exact solutions,

play06:49

they are not intended to be approximate.

play06:51

For approximate, we have something else.

play06:52

We'll have different names of things.

play06:55

Growth function is supposed to be exact.

play06:57

The last terminology that I want to talk about

play06:58

is Big-Oh.

play07:00

So, big-oh means something like,

play07:01

0(1) or 0(n) or 0(n²), right, or 0(n³) or 0(n)² or whatever.

play07:07

You should have seen this before in a previous class.

play07:10

These are all big-oh expressions,

play07:12

maybe I would call them big-oh terms.

play07:14

They're using what is called big-oh notation.

play07:16

What big-oh notation is,

play07:17

is a way to write an exact quality.

play07:20

Well, an inexact quantity.

play07:24

So, 0(1) is basically saying,

play07:25

"Oh, it's just some number."

play07:27

0(n) is saying,

play07:28

"Oh, it's some number times (n)."

play07:30

So, I'm not giving a specific number,

play07:32

I'm not saying it's 2(n) or 4(n) or 5(n),

play07:33

I'm just saying it's something that is a multiple of (n).

play07:37

Right, grows like (n) with a constant in front.

play07:40

And so, basically, big-oh is used as a summary.

play07:43

Right, so take a look down there at the expression I have,

play07:47

so I have the expression 2(n)+15=0(n),

play07:50

what that is saying is that expression 2(n)+15

play07:52

is big-oh of (n).

play07:56

Right, so 2(n)+15 is 0(n).

play07:58

So basically we are saying,

play08:00

"Oh hey, this function, right, as I change the value of (n),

play08:04

it's going to basically act like a linear function."

play08:05

It's going to act like just (n).

play08:07

Yeah, in the full function,

play08:09

that plus 15 is in there as well,

play08:10

but it really doesn't matter.

play08:11

It's a constant, its going to be small.

play08:13

If (n) becomes really large,

play08:14

the impact of that 15 doesn't really matter.

play08:18

And then, just for convenience,

play08:19

big-oh is basically hiding an invisible coefficient

play08:21

in front of that letter.

play08:22

In front of that variable.

play08:23

So that 2 kind of goes away as well.

play08:25

Now if we had something else like for example,

play08:27

say we had 10(N)+2,

play08:29

that as well would be 0(n),

play08:31

because again it's just some expression that varies

play08:33

with some multiple of (n).

play08:35

It says that expression is inside that 0 notaton.

play08:39

Overall, right, big-oh is giving us this way

play08:42

to basically write a quantity that varies a little bit

play08:47

and somehow there is also this typical notion in there

play08:49

that it is going to be larger than something.

play08:52

So, that big-oh typically is representing what we call

play08:53

an upper bound.

play08:54

I'll talk about that a lot more in a minute.

play08:58

That's going to be some value,

play08:59

that always stays above something else.

play09:01

So 0(n) should always stay above,

play09:03

right picture you have a graph in your head,

play09:05

it'll always stay above that 2n+15.

play09:09

So that's what we would typically use big-oh for

play09:11

as it represents upper bound on something.

play09:14

It shadows something.

play09:15

A final note there,

play09:16

is that we typically use big-oh in terms of

play09:18

long term behavioral function.

play09:21

So once, you know our input size gets really big.

play09:24

For various reasons,

play09:25

and we will talk about them,

play09:26

it's hard basically to compare functions,

play09:28

or to talk about functions,

play09:28

when their input size basically is really small.

play09:31

So it's like zero, one, two, three, four,

play09:33

somehow things are going to be a little bit noisy.

play09:36

And you can look at that graph on the right,

play09:37

maybe to get a some conceptual idea there.

play09:39

So big-oh somehow is used generally when we

play09:41

expect (n) to be "sufficiently large"

play09:43

and I using air quotes there because

play09:45

sufficiently large isn't really defined,

play09:47

it's just what people say.

Rate This

5.0 / 5 (0 votes)

Related Tags
AlgoritmosCrecimientoBig-OhComputaciónEficienciaOptimizaciónTamaño del problemaAnálisisRecursividadTiempo de ejecución
Do you need a summary in English?