What did algorithms have done for me lately

  • Slides: 94
Download presentation
What did algorithms have done for me lately? Guy Kortsarz

What did algorithms have done for me lately? Guy Kortsarz

A typical discussion Person: What do you do?

A typical discussion Person: What do you do?

A typical discussion Person: What do you do? Me: I am a computer science

A typical discussion Person: What do you do? Me: I am a computer science professor.

A typical discussion Person: What do you do? Me: I am a computer science

A typical discussion Person: What do you do? Me: I am a computer science professor.

A typical discussion Person: What do you do? Me: I am a computer science

A typical discussion Person: What do you do? Me: I am a computer science professor. Person: I have a problem with my PC, can you fix it? Me: I cant do that. Person: You will not fix my PC? Me: I cannot fix my PC, let alone yours. Person: Then what exactly do you do? Me: I study algorithms. Person: Oh, I know that. Me: Really? Person: Yes! I learn logarithms in high school.

Algorithms not Logarithms! L O G A R I T H M Al-Khowarizmi

Algorithms not Logarithms! L O G A R I T H M Al-Khowarizmi

What can a computer do? • A computer can only store numbers. That is

What can a computer do? • A computer can only store numbers. That is almost all. • If the computer stored 1000 numbers we can get to the 500 in the order, in ONE STEP. • The place the computer stores numbers is called the memory of the computer.

What else can a computer do? • • • There is a calculator inside

What else can a computer do? • • • There is a calculator inside the computer. This is called the Central Processing Unit. Or CPU Can add, multiply numbers, etc. It has the ability of scanning the numbers stored one after the other. • We model the scan by going over the numbers from left to right.

Do you have a 7 inside? 8 IS THIS A 7? 9 11 4

Do you have a 7 inside? 8 IS THIS A 7? 9 11 4 7 3

Do you have a 7 inside? 8 9 IS THIS A 7? 11 4

Do you have a 7 inside? 8 9 IS THIS A 7? 11 4 No 7 3

Do you have a 7 inside? 8 9 11 IS THIS A 7? 4

Do you have a 7 inside? 8 9 11 IS THIS A 7? 4 7 3

Do you have a 7 inside? 8 9 11 IS THIS A 7? 4

Do you have a 7 inside? 8 9 11 IS THIS A 7? 4 7 NO 3

Do you have a 7 inside? 8 9 11 4 IS THIS A 7?

Do you have a 7 inside? 8 9 11 4 IS THIS A 7? 7 3

Do you have a 7 inside? 8 9 11 4 IS THIS A 7?

Do you have a 7 inside? 8 9 11 4 IS THIS A 7? 7 3 No

Do you have a 7 inside? 8 9 11 4 7 IS THIS A

Do you have a 7 inside? 8 9 11 4 7 IS THIS A 7? 3

Do you have a 7 inside? 8 9 11 4 7 IS THIS A

Do you have a 7 inside? 8 9 11 4 7 IS THIS A 7? 3 YES!

A computer runs a program • There are many languages that the computer understands.

A computer runs a program • There are many languages that the computer understands. • Pascal, C, Lisp, Prolog, Ada, C++, Java, Algol, Fortran, Basic, Visual Basic, ML, Python. • A program looks very much like a text in English. • A program is a collection of ordered lines with a specific syntax.

The computer is amazingly fast • Electric current runs (theoretically) at the speed of

The computer is amazingly fast • Electric current runs (theoretically) at the speed of light. • Without the computer we would have never reached the moon. • A computer is the world champion in chess (brute force. No intelligence). • Computers monitor “everything”. Like our nuclear bombs.

Nevertheless a computer is not what they say in the movies • • •

Nevertheless a computer is not what they say in the movies • • • “ 2001, Space odyssey” Stanley Kubrick A very smart computer HAL. Hal feels. Hal is afraid. Hall murderers out of fear. Kubrick knows that it cant be. It is just an horror movie. Nobody understands that.

An algorithm • Is a high level description of the program • An algorithm

An algorithm • Is a high level description of the program • An algorithm ignores syntax issues • Also input issues: the computer would ask the user to enter the numbers. • Like in the ATM you provide you pin number.

Entering a pin number • Your information can be stored with the collection of

Entering a pin number • Your information can be stored with the collection of numbers. • When the computer reads your pin, it searches for your file in the way we saw. • Getting the pin number is called reading the input. • We ignore that in Algorithms. • In fact, an algorithm is an idea.

Algorithms Introduction Recipe for baking a cake…. • 2 sticks butter • 2 cups

Algorithms Introduction Recipe for baking a cake…. • 2 sticks butter • 2 cups flour • 1 cup sugar • 4 eggs • 1 cup milk • 1 tsp baking powder • Cocoa powder (1/2 pound) Mix the sugar, baking powder and flour, mix in beaten eggs, melted butter and bake at 325 F for 40 mins.

Computer Science • Therefore we write an algorithm • But, then we have to

Computer Science • Therefore we write an algorithm • But, then we have to change it to something precise. • Something written in a programming language.

Intelligence of animals • The intelligence of animals is proportional to: size of the

Intelligence of animals • The intelligence of animals is proportional to: size of the brain over the size of its body. • The human brain is huge. A large part of the head. • This made us much weaker than a chimpanzee. A chimpanzee will get the upper hand against any human.

Consider the Ostrich

Consider the Ostrich

Consider the Ostrich Was it a mistake of evolution?

Consider the Ostrich Was it a mistake of evolution?

Look at the small head (brain)

Look at the small head (brain)

But they have a huge body

But they have a huge body

But they have a huge body

But they have a huge body

On of the most STUPID animals

On of the most STUPID animals

Runs at 70 km/hour Roughly like a lion Faster than a Kangaroo

Runs at 70 km/hour Roughly like a lion Faster than a Kangaroo

A computer is like

A computer is like

A computer is like • An Ostrich

A computer is like • An Ostrich

A computer is like • An Ostrich • Fast and stupid.

A computer is like • An Ostrich • Fast and stupid.

A computer is like • An Ostrich. • Fast and stupid. • There is

A computer is like • An Ostrich. • Fast and stupid. • There is no such thing as artificial intelligence. • There will never be such a thing • A computer does not think • It only does what it is told. • Sorry HAL. Blade Runner, Terminator, and the others.

A computer is like

A computer is like

A computer is like • My wife.

A computer is like • My wife.

A computer is like • My wife. Always right!

A computer is like • My wife. Always right!

A computer is like • My wife. Always right!

A computer is like • My wife. Always right!

A computer is like • My wife. Always right!

A computer is like • My wife. Always right!

A computer is like • • My wife. Always right! A computer error? There

A computer is like • • My wife. Always right! A computer error? There is no such thing. He is always right! I hate machines. Story 1: a satellite in the eighties did not get into orbit. • Constantly floats away from earth. • Story 2: a woman from Canada age 106 was told she must attend first grade

Knowing computers makes you prepared to be

Knowing computers makes you prepared to be

Knowing computers makes you prepared to be • A husband

Knowing computers makes you prepared to be • A husband

Knowing computers makes you better prepared to be • A husband • Like a

Knowing computers makes you better prepared to be • A husband • Like a computer, I do what my wife tells me.

Knowing computers makes you better prepared to be • A husband • Like a

Knowing computers makes you better prepared to be • A husband • Like a computer, I do what my wife tells me. • By the way children really brighten the household.

Knowing computers makes you better prepared to be • A husband • Like a

Knowing computers makes you better prepared to be • A husband • Like a computer, I do what my wife tells me. • By the way children really brighten the household. • They never turn off the light.

Knowing computers makes you better prepared to be • A husband • Like a

Knowing computers makes you better prepared to be • A husband • Like a computer, I do what my wife tells me. • By the way children really brighten the household. • They never turn off the light. • I know machines are not alive. But to me it feels they are.

Knowing computers makes you better prepared to be • A husband • Like a

Knowing computers makes you better prepared to be • A husband • Like a computer, I do what my wife tells me. • By the way children really brighten the household. • They never turn off the light. • I know machines are not alive. But to me it feels they are. And the toaster hates me!

A question • Say that you drive on a road, its completely dark and

A question • Say that you drive on a road, its completely dark and coming towards you are two lights with large gap between them

A question • Say that you drive on a road, its completely dark and

A question • Say that you drive on a road, its completely dark and coming towards you are two lights with large gap between them • How can you tell if it is two motorcycles or a big track?

A question • Say that you drive on a road, its completely dark and

A question • Say that you drive on a road, its completely dark and coming towards you are two lights with large gap between them • How can you tell if it is two motorcycles or a big track? • The answer is simple: go in the middle.

A question • Say that you drive on a road, its completely dark and

A question • Say that you drive on a road, its completely dark and coming towards you are two lights with large gap between them • How can you tell if it is two motorcycles or a big track? • The answer is simple: go in the middle. • If you are alive it was two motorcycles.

A question • Say that you drive on a road, its completely dark and

A question • Say that you drive on a road, its completely dark and coming towards you are two lights with large gap between them • How can you tell if it is two motorcycles or a big track? • The answer is simple: go in the middle. • If you are alive it was two motorcycles. • If you are dead, it was a truck.

We can get to any number in the middle in one step

We can get to any number in the middle in one step

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 71 10 90 92 11 12 100 115 120 13 14 15 Say that the number are increasing and Say that we search for 71 16

An algorithm is an idea • • Then happens what

An algorithm is an idea • • Then happens what

An algorithm is an idea • HALF!!!!!! • Then happens what

An algorithm is an idea • HALF!!!!!! • Then happens what

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 71 10 90 92 11 12 Compare 71 to the ninth’s number. One comparison 100 115 120 13 14 15 16

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 71 10 90 92 11 12 We get that 71>56 100 115 120 13 14 15 16

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 HALF!!!!! 71 10 90 92 11 12 100 115 120 13 14 15 16

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 71 10 90 92 11 12 100 115 120 13 14 Compare 71 to 100. A second comparison 15 16

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 HALF!!!!! 71 10 90 92 11 12 100 115 120 13 14 15 16

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 71 10 90 92 11 12 100 115 120 13 14 Compare 71 to 90. A third comparison 15 16

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 HALF!!!!! 71 10 90 92 11 12 100 115 120 13 14 15 16

We can get into the middle in one step 1 3 9 14 1

We can get into the middle in one step 1 3 9 14 1 2 3 4 23 33 5 6 45 52 56 7 8 9 71 10 90 92 11 12 100 115 120 13 Compare 71 to 71 Forth comparison: FOUND!!!!! 14 15 16

The number of comparisons • The number of comparisons is 4. Much less that

The number of comparisons • The number of comparisons is 4. Much less that 16. • The idea of comparing to the middle is the algorithm. • An algorithm is best described in words. • The further it is from a program, the better.

Changing a number to coins 1 5 25 10 10 Change 63 to minimum

Changing a number to coins 1 5 25 10 10 Change 63 to minimum number of coins

What to do?

What to do?

What to do? • • Select two quarters. 63 -50=13. Next choose a dime.

What to do? • • Select two quarters. 63 -50=13. Next choose a dime. 13 -10=3 Next choose 3 cents. A total of 6 coins. It turns out that this algorithm always works!

Greedy algorithm • We take the largest coin that is still smaller than the

Greedy algorithm • We take the largest coin that is still smaller than the number we still have. • Such a strategy is called greedy. • It turns out that for American coins and any number the greedy strategy works! • But usually not in life.

Greedy algorithm • We take the largest coin that is still smaller than the

Greedy algorithm • We take the largest coin that is still smaller than the number we still have. • Such a strategy is called greedy. • It turns out that for American coins and any number the greedy strategy works! • But usually not in life. • If you eat the whole cake a day later you will regret it.

Greedy historical mistakes • • • Saddam Hussein. Conquered Kuwait Greedy choice. G. H.

Greedy historical mistakes • • • Saddam Hussein. Conquered Kuwait Greedy choice. G. H. W Bush freed Kuwait. Saddam Hussein tried to kill G. H. W Bush Another greedy move. W, made sure S. H will be hanged. Remember the movie: Riding in cars with boys?

What about

What about

What about Swedish coins?

What about Swedish coins?

Say that the coins in Sweden are 1 5 11

Say that the coins in Sweden are 1 5 11

We want to change 15 to coins • First take 11, leaving 4 •

We want to change 15 to coins • First take 11, leaving 4 • Then take 1 four times. • A total of 5 coins • Better is to take 3 coins of 5.

A question for those who do not know what an algorithms is. • We

A question for those who do not know what an algorithms is. • We have an apartment house with 100 floors • We have a crystal sphere. • We want to give an algorithm to the following question: what is the lowest floor so that if we throw the sphere from the floor, it breaks. • With one sphere we are doomed.

We must do a silly thing • Throw from floor 1. • If it

We must do a silly thing • Throw from floor 1. • If it does not break, throw from floor 2 • And so on until the sphere brakes or does not. • We cant even try to throw the sphere from the second floor. • If it breaks, we will not know if it breaks at the first floor!

Say that we have two spheres • The rules are • 1) The two

Say that we have two spheres • The rules are • 1) The two spheres break on the same floors. • 2) If a sphere breaks in floor 40 then it will break also if we throw it from any floor that is 41 or larger. • Now we do not have to use 100 throws. Since if a sphere breaks, we have another.

Say that we have two spheres • Any better idea? • Give me some

Say that we have two spheres • Any better idea? • Give me some suggestion.

Say that we have two spheres • Any better idea? • Give me some

Say that we have two spheres • Any better idea? • Give me some suggestion. • The best strategy is to throw from floor 10 then 20 then 30 and so on. • In the worst case we get to floor 100. • And it breaks (did not break in 90!) • Then, just check floors 91, 92, …. , 99

The total number of throws • 10, 20, 30, 40 , 50, 60 ,

The total number of throws • 10, 20, 30, 40 , 50, 60 , 70, 80, 90 100 • A total of 10 throws. • Now 91, 92, 93, 94, 95, 96, 97, 98, 99 which is 9 more throws. This gives the best possible. 19. • For a general n its 2*sqrt{n} (you know what sqrt{n} is, right? ). • A challenge for you: 3 spheres.

Factoring numbers • Our national security is based on the unproven assumption that if

Factoring numbers • Our national security is based on the unproven assumption that if we have a prime number p with 512 bits (namely of value about 2 512 ) and another number q of 512 bits, and we are given n=p∙q, we can not find p and q. • The number we want to “factor” has size about 21024 size.

The silly algorithm does not work. • Try all possible dividers is not possible.

The silly algorithm does not work. • Try all possible dividers is not possible. • 2300 is more than the number of atoms in the known universe. • But this is MATH. Maybe (we do not know) there is a smart way to find p and q? • We do a very dangerous thing: we assume its hard on average. • Not to mention that a computer that keeps our nuclear arsenal may malfunction!

The last movie • Mercury Rising. • The government develops a code at a

The last movie • Mercury Rising. • The government develops a code at a billion dollars cost. • But they put it in a puzzle for children. To check the geek factor. • An autistic boy named Simon breaks the code and calls the NSA.

Don’t mess with the NSA • The choice was between loosing a billion dollars

Don’t mess with the NSA • The choice was between loosing a billion dollars or killing the kid. • Since we are in a democracy the choice was clear.

Don’t mess with the NSA • The choice was between loosing a billion dollars

Don’t mess with the NSA • The choice was between loosing a billion dollars or killing the kid. • Since we are in a democracy the choice was clear. • Kill the boy.

Don’t mess with the NSA • The choice was between loosing a billion dollars

Don’t mess with the NSA • The choice was between loosing a billion dollars or killing the kid. • Since we are in a democracy the choice was clear. • Kill the boy. • They send killers to the house of simon and kill both his parents. But they do not find Simon.

Then the government make a big mistake. • What mistake? Well this mistake:

Then the government make a big mistake. • What mistake? Well this mistake:

Then the government make a big mistake. • What mistake? Well this mistake.

Then the government make a big mistake. • What mistake? Well this mistake.

Bruce Willis finds Simon. • We saw movies. • We saw Die Hard 1

Bruce Willis finds Simon. • We saw movies. • We saw Die Hard 1 to Die Hard 300 • In fact I think there were less sequels. Six films in total. But Bruce Willis is still alive. • What did we learn from the movies? • Bruce Willis finds the boy.

Bruce Willis finds Simon. • We saw movies. • We saw Die Hard 1

Bruce Willis finds Simon. • We saw movies. • We saw Die Hard 1 to Die Hard 300 • In fact I think there were less sequels. Six films in total. But Bruce Willis is still alive. • What did we learn from the movies? • Bruce Willis finds the boy. • Nobody beats Bruce Willis!

I will leave you with some happy thought • We cant make sure that

I will leave you with some happy thought • We cant make sure that our programs are correct. Thus a nuclear disaster may come at every moment. • Any day a (probably autistic) child may be born and he/she may find a fast way to factor large numbers. • The world economy goes down the drain. • All our spies will die.

I will leave you with some happy thought • We cant make sure that

I will leave you with some happy thought • We cant make sure that our programs are correct. Thus a nuclear disaster may come at every moment. • Any day a (probably autistic) child may be born and he/she may find a fast way to factor large numbers. • The world economy goes down the drain. • All our spies will die. • Have a lovely day!