Algorithms, Programs, and Computers CS 110 Fall 2005.

29
Algorithms, Algorithms, Programs, and Programs, and Computers Computers CS 110 CS 110 Fall 2005 Fall 2005

Transcript of Algorithms, Programs, and Computers CS 110 Fall 2005.

Page 1: Algorithms, Programs, and Computers CS 110 Fall 2005.

Algorithms, Programs, Algorithms, Programs, and Computersand Computers

CS 110CS 110

Fall 2005Fall 2005

Page 2: Algorithms, Programs, and Computers CS 110 Fall 2005.

Problem SolvingProblem Solving

You want to make money and you You want to make money and you have some simpletons who are have some simpletons who are willing to helpwilling to help• Devise aDevise a planplan that will make money that will make money

(write a business plan for starting a (write a business plan for starting a restaurant)restaurant)

• Tell the simpletons to Tell the simpletons to execute the execute the business planbusiness plan

Page 3: Algorithms, Programs, and Computers CS 110 Fall 2005.

Problem SolvingProblem Solving

The simpletons can make mistakesThe simpletons can make mistakes• Refine the communicationRefine the communication of of

instructions to theminstructions to them Use time cards, tables, flow charts, picturesUse time cards, tables, flow charts, pictures Develop a bullet proof “process”Develop a bullet proof “process”

Page 4: Algorithms, Programs, and Computers CS 110 Fall 2005.

Problem SolvingProblem Solving

You want to make money and you You want to make money and you have a computer to help youhave a computer to help you• Devise an algorithm Devise an algorithm (plan)(plan)• Make the algorithmMake the algorithm very detailed so a very detailed so a

simpleton can do it (program)simpleton can do it (program)• Execute the programExecute the program (computer) (computer)

Page 5: Algorithms, Programs, and Computers CS 110 Fall 2005.

Hardware / SoftwareHardware / Software

A program is a sequence of A program is a sequence of instructions that tell the computer instructions that tell the computer what to dowhat to do• What language do computers speak?What language do computers speak?• What language do people speak?What language do people speak?

Page 6: Algorithms, Programs, and Computers CS 110 Fall 2005.

Computer LanguageComputer Language

Assembly LanguageAssembly Language• ADD R1, R2ADD R1, R2• MOV [R5], R1MOV [R5], R1• IF (R4 < R2) JMP A:IF (R4 < R2) JMP A:

Very sequentialVery sequential Like a flow chartLike a flow chart Not the language of human usersNot the language of human users

Page 7: Algorithms, Programs, and Computers CS 110 Fall 2005.

It Gets WorseIt Gets Worse

Computer logic is even more Computer logic is even more primitiveprimitive• AND AND • OROR• NOTNOT

All programs (nearly every electronic All programs (nearly every electronic device you use) operates with only device you use) operates with only these three logical operationsthese three logical operations

Page 8: Algorithms, Programs, and Computers CS 110 Fall 2005.

It Gets Much BetterIt Gets Much Better

Computer software exists to create Computer software exists to create an abstraction between AND/OR/NOT an abstraction between AND/OR/NOT and human language/logicand human language/logic

Programming LanguagesProgramming Languages• C++, Java, C#, PerlC++, Java, C#, Perl

Page 9: Algorithms, Programs, and Computers CS 110 Fall 2005.

C++C++

void main (int argc, char **argv) {void main (int argc, char **argv) {printf (“Hello, world\n”);printf (“Hello, world\n”);

}}

Page 10: Algorithms, Programs, and Computers CS 110 Fall 2005.

JavaJava

class program class program

{ {

public static void main(String args[]) { public static void main(String args[]) {

System.out.println("Hello World!"); System.out.println("Hello World!");

} }

}}

Page 11: Algorithms, Programs, and Computers CS 110 Fall 2005.

Why So Many Languages?Why So Many Languages?

Each has strengths and weaknessesEach has strengths and weaknesses• Error protectionError protection

Syntax in English vs. syntax in GermanSyntax in English vs. syntax in German

• ExpressivenessExpressiveness Eskimo words for snowEskimo words for snow

Page 12: Algorithms, Programs, and Computers CS 110 Fall 2005.

Multipurpose ComputersMultipurpose Computers

How can computers interpret so How can computers interpret so many different languages?many different languages?• Translate them all into a common Translate them all into a common

language (machine language)language (machine language)

Compilers!Compilers!

Page 13: Algorithms, Programs, and Computers CS 110 Fall 2005.

Recap: Problem SolvingRecap: Problem Solving

Computers execute machine Computers execute machine languagelanguage

Programs are a sequence of Programs are a sequence of instructions in some languageinstructions in some language

Compilers transform programs from Compilers transform programs from one language to another, usually a one language to another, usually a simpler onesimpler one

Page 14: Algorithms, Programs, and Computers CS 110 Fall 2005.

AlgorithmsAlgorithms

The way one solves a problemThe way one solves a problem• Divide and conquerDivide and conquer• Top-down or bottom-upTop-down or bottom-up• SimplifySimplify• DeductionDeduction• Trial and errorTrial and error

Page 15: Algorithms, Programs, and Computers CS 110 Fall 2005.

Dealing Out CardsDealing Out Cards

Poker: deal five cards to everyone at Poker: deal five cards to everyone at the tablethe table

How?How?(step by step)(step by step)

Page 16: Algorithms, Programs, and Computers CS 110 Fall 2005.

Draw a SquareDraw a Square

Page 17: Algorithms, Programs, and Computers CS 110 Fall 2005.

Draw a TriangleDraw a Triangle

Page 18: Algorithms, Programs, and Computers CS 110 Fall 2005.

Recap: AlgorithmsRecap: Algorithms

An approach to solving a problemAn approach to solving a problem A sequence of stepsA sequence of steps Break a complex problem into Break a complex problem into

simpler piecessimpler pieces

The first step in making computers The first step in making computers work for you!work for you!

Page 19: Algorithms, Programs, and Computers CS 110 Fall 2005.

ComplexityComplexity

There’s a danger hereThere’s a danger here• Time is scarceTime is scarce• Simple algorithms may entail inordinate Simple algorithms may entail inordinate

time and resourcestime and resources• Computers are simpletonsComputers are simpletons

We must think about an algorithm’s We must think about an algorithm’s complexity before using itcomplexity before using it

Page 20: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of CardsComplexity of Cards

Consider dealing five cardsConsider dealing five cards• One second per dealOne second per deal• How long to finish dealing five cards to:How long to finish dealing five cards to:

2 players2 players

Page 21: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of CardsComplexity of Cards

Consider dealing five cardsConsider dealing five cards• One second per dealOne second per deal• How long to finish dealing five cards to:How long to finish dealing five cards to:

3 players3 players

Page 22: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of CardsComplexity of Cards

Consider dealing five cardsConsider dealing five cards• One second per dealOne second per deal• How long to finish dealing five cards to:How long to finish dealing five cards to:

4 players4 players

Page 23: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of CardsComplexity of Cards

Consider dealing five cardsConsider dealing five cards• One second per dealOne second per deal• How long to finish dealing five cards to:How long to finish dealing five cards to:

100 players100 players

Page 24: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of CardsComplexity of Cards

It takes 5 * num_players timeIt takes 5 * num_players time

A Linear Time AlgorithmA Linear Time Algorithm

Number of players

time

Page 25: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of Other ProblemsComplexity of Other Problems

Many problems are polynomial (nMany problems are polynomial (n22))• Execution time grows as the square of Execution time grows as the square of

the problem size (sorting names)the problem size (sorting names)

Problem size

time

Page 26: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of Other ProblemsComplexity of Other Problems

Many problems are exponential (2Many problems are exponential (2nn))• Execution time grows as to the nExecution time grows as to the nthth with with

increase in problem size (Traveling increase in problem size (Traveling Salesperson Problem) Salesperson Problem)

Problem size

time

Page 27: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of Other ProblemsComplexity of Other Problems

Some problems are intractableSome problems are intractable• There are 10There are 107979 atoms in Universe atoms in Universe• It would be impossible to solve an It would be impossible to solve an

algorithm this complexalgorithm this complex

Page 28: Algorithms, Programs, and Computers CS 110 Fall 2005.

Complexity of Other ProblemsComplexity of Other Problems

Some problems are not computableSome problems are not computable• Literally no end to computation to be Literally no end to computation to be

donwdonw

Page 29: Algorithms, Programs, and Computers CS 110 Fall 2005.

Recap: AlgorithmsRecap: Algorithms

Algorithms have different Algorithms have different complexitiescomplexities

Complexity is important to Complexity is important to understandunderstand• What is knowable?What is knowable?• How difficult is it to acquire?How difficult is it to acquire?