Date post: | 23-Dec-2015 |
Category: |
Documents |
Upload: | marlene-eaton |
View: | 215 times |
Download: | 0 times |
Algorithms, Programs, Algorithms, Programs, and Computersand Computers
CS 110CS 110
Fall 2005Fall 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
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”
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)
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?
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
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
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
C++C++
void main (int argc, char **argv) {void main (int argc, char **argv) {printf (“Hello, world\n”);printf (“Hello, world\n”);
}}
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!");
} }
}}
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
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!
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
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
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)
Draw a SquareDraw a Square
Draw a TriangleDraw a Triangle
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!
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
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
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
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
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
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
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
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
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
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
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?