Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Computers CS1100.01 Dr. Zhizhang Shen Chapter 10: How to.

Slides:



Advertisements
Similar presentations
Chapter 1 An Overview of Computers and Programming Languages.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Algorithmic Thinking What’s the Plan? lawrence snyder c h a p t e r 10.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
6-1 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer problem-solving process and relate it to Polya’s.
Chapter 6 Problem Solving and Algorithm Design. 6-2 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer.
Concurrent & Distributed Systems Lecture 2: Introduction to interacting processes Recap –Concurrent processes could be pseudo-parallel Sharing a single.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Chapter 10 Algorithmic Thinking. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives List the five essential.
Chapter 10 What's The Plan?: Algorithmic Thinking.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Algorithm Efficiency and Sorting
Chapter 2: Algorithm Discovery and Design
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Algorithms Algorithms are a familiar idea. Our goal is to learn to specify them right so someone or something else does the work.
Invitation to Computer Science, Java Version, Second Edition.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Introduction to problem solving. Introductory Discussion? What problems are you faced with daily? Do you think a computer can be designed to solve those.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 2 Graphs and Functions Copyright © 2013, 2009, 2005 Pearson Education, Inc.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Analysis of Algorithms
Some quotes…. I really hate this damned machine I wish that they would sell it. It never does quite what I want But only what I tell it. ~Anon These machines.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
“The study of algorithms is the cornerstone of computer science.” Algorithms Fall 2011.
Algorithms CS139 – Aug 30, Problem Solving Your roommate, who is taking CS139, is in a panic. He is worried that he might lose his financial aid.
Lecture 6 Problem Solving: Algorithm Design & Analysis.
Algorithms & Flowchart
Algorithms Algorithms are a familiar idea. Our goal is to learn to specify them right so someone or something else does the work.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
What's The Plan? Algorithmic Thinking Step-by-step directions for whatever someone, or the computer, needs to do © 2004 Lawrence Snyder.
Program Design. The design process How do you go about writing a program? –It’s like many other things in life Understand the problem to be solved Develop.
ALGORITHMS.
Complexity & Computability. Limitations of computer science  Major reasons useful calculations cannot be done:  execution time of program is too long.
CS 203: Introduction to Formal Languages and Automata
Chapter 10 Algorithmic Thinking. Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the.
Algorithms CS280 – 10/20/05. Announcement  Part 1 of project 2 due.  Read chapters 10, 7 for this unit  Tuesday we will also be in the classroom We.
CSE 311 Foundations of Computing I Lecture 28 Computability: Other Undecidable Problems Autumn 2011 CSE 3111.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Sorting Algorithms. Algorithms, revisited What is an algorithm? Wikipedia Definition: an algorithm is a definite list of well-defined instructions for.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
CPS Today’s topics l Algorithms and pseudocode l Notes adapted from Marti Hearst at UC Berkeley and David Smith at Georgia Tech Upcoming ä Beginning.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Introduction to Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
Algorithms Chapter 3 With Question/Answer Animations
Creativity in Algorithms
Objective of This Course
Algorithm Discovery and Design
CSCI 1250 Information Technologies
Programming We have seen various examples of programming languages
Algorithm and Ambiguity
Chapter 10: Algorithm TECH Prof. Jeff Cheng.
Algorithms and Problem Solving
Introduction to Algorithms
What's The Plan?: Algorithmic Thinking
What's The Plan?: Algorithmic Thinking
What's The Plan?: Algorithmic Thinking
Presentation transcript:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Computers CS Dr. Zhizhang Shen Chapter 10: How to Solve the Problem?

1-2 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-2 What about it? It is complicated to solve a problem with the help of a computer. We have to write computer programs to tell a computer how to solve a problem. Algorithm is the very basis for any program. We will check out the basics and look at one example on how to sort things out.

1-3 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-3 What is an algorithm? A precise, systematic method for producing a specified result We have already seen several examples: –Recipe: with all the material, how to make something? –Recognition of a button Where is the button? What do you want me to do here? –Hex to bits: Given a hex, e.g., 6C, what is the associated byte?

1-4 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-4 Algorithms in everyday life Some algorithms are learned—arithmetic –Given two numbers, how to add them up? –How to estimate 34,000 on the highest place? Some we figure out ourselves—looking up a phone number ( ) –We once went through an algorithm as how to do it Others require written instructions—recipe, assembly instructions, driving directions

1-5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-5 Five essential properties of algorithms 1.Specify the input –Data to be transformed during the computation to produce the output, e.g., a name –Must specify type, amount, and form of data 2.Output specified –Data resulting from the computation—intended result, e.g., a phone number –It is possible to have no output, when nothing is found for that name.

1-6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-6 Five essential properties (cont'd) 3.Definiteness –Specify the sequence of events –Details of each step, including how to handle errors Computer can’t make a guess 4.Effectiveness –The operations are doable: can’t divide by 0. 5.Finiteness, i.e., it m ust eventually stop: –we are not patient.

1-7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-7 Language Used to Write Algorithms Natural language –For people, we use a natural language like English –Ambiguity is common in natural language, thus not specific “The girl touches the boy with a bouquet of flowers.” Programming Language –Pseudo languages, English like, but more specific and precisely defined; no ambiguity –Programming languages such as C, Java, Fortran, HTML, XML, …

1-8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-8 The context of a program Program can fulfill five properties of an algorithm, be unambiguous, and still not work right because it is executed in the wrong context –e.g., last name in Western countries means family name; in Asian countries it may mean given name Context matters: Driving instructions –Assumes you are traveling in a specific direction. If you are not, the directions you got from Google will fail. –You assume your friends come from south. What happens if they want to go to White Mountain National Forest first?

1-9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-9

1-10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Program vs. algorithm Algorithm is a general solution to a given problem. It is often language independent, i.e., it can be implemented in any programming language. A program is an implementation of an algorithm using a specific language, thus it can be executed in a computer.

1-11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley An Algorithm: Sort through CDs Imagine CDs in a slotted rack, not organized, thus tough to find You want to alphabetize by name of group, performing musician, or composer How do you solve this problem? Sort them into the alphabetical order

1-12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Alphabetize CDs Input: An unordered sequence of CDs filling a slotted rack Output: The same CDs in the rack in alphabetical order Instructions: 1.Use Name to refer to the name or the group or musician or composer on a CD 2.Decide which end is the beginning of the sequence and call the slot at that end the Alpha slot, i.e., where to start 3.Call the slot next to Alpha the Beta slot

1-13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Alphabetize CDs (cont'd) 4.If Name of the CD in position Alpha comes later in alphabet than Name of the CD in position Beta, swap the CDs; otherwise continue on to the next step 5.If there is a slot following Beta, call it Beta and go back to Instruction 4; otherwise continue on to the next step 6.If there are two or more slots following Alpha, call the slot following Alpha the new Alpha and call the slot following it new Beta, and go to Instruction 4; otherwise stop, the list has been sorted

1-14 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-14

1-15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-15

1-16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 1 Use the term Name to refer to the name of the group or musician or composer on a CD –This gives a name to the operation of locating the name used for alphabetizing –In this case, we sort out CDs by their names –In a different case, we might want to sort out a bunch of phone records by the addresses. –This step thus shows the generality of the algorithm.

1-17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 2 Decide which end of the rack is the beginning of the alphabetic sequence and call the slot at that end the Alpha slot –Gives the process a starting point. You could start with the other end, too to give it a reverse order –Defines position Alpha. At the start, Alpha refers to the first slot of the sequence; as the algorithm progresses it refers to successive slots.

1-18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 3 Call the slot next to the Alpha slot the Beta slot –Gives the term Beta its initial meaning. –Alpha and Beta have no inherent meaning; they are chosen by the programmer to name slots in the rack –We often need to use such names, or Identifiers, or even variables, in programming

1-19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 4 If Name of the CD in the Alpha slot comes later alphabetically than Name of the CD in the Beta slot, swap the CD's; otherwise continue on –This is the workhorse instruction of the algorithm. It compares the names of the artists of the CDs in Alpha and Beta and, if necessary, swaps them so that this pair is in the correct order

1-20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 5 If there is a slot following the Beta slot, call it the Beta slot and go to Instruction 4; otherwise, continue to the next step –Re-define Beta slot so that it refers to the next slot in the sequence, if any. Then Instruction 4 can be executed again, comparing a different pair of CDs –This step guarantees that Alpha holds the “least” one among all the ones from position Alpha to the end

1-21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 6 If there are two or more slots following the Alpha slot, call the slot following the Alpha slot the new Alpha and the slot following it Beta, and go to Instruction 4; otherwise stop –By the time we get to this instruction, the “first” CD from Alpha down is in the Alpha slot. We advance Alpha to the next slot and sweep through the CD rack again, locating the next smallest CD to fill this Alpha slot. – When there is only one slot left, the entire rack has been sorted, and the algorithm stops

1-22 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Algorithm analysis (CS3220) Illustrates the five basic properties of algorithms –Inputs and Outputs were listed –Each instruction was defined precisely (definiteness) –Operations are effective because they are simple and mechanically doable Alphabetizing is mechanical, so our algorithm is effective –Finiteness is satisfied There are only a finite number of slots that can be paired, so instructions 4, 5, and 6 cannot be repeated indefinitely

1-23 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-23

1-24 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley A deeper analysis Structural features –The algorithm goes back and forth in two instructions, 5 and 6. This is called a loop –Loops and tests A loop must include a test to determine whether the instructions should be repeated one more time Is there one or more slots left after Alpha –Assumptions We assume that –The CD rack is full (instructions do not handle the case of an empty slot) –The word "following" means a slot further from the start point

1-25 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Exchange Sort algorithm The Alphabetize CDs example illustrates the standard Exchange Sort algorithm, or BubbleSort –The idea of comparing pairs of items chosen in a particular way, exchanging them if they are out of order, and continuing to sweep through the items –We could use the same algorithm to sort on a different list of cards, phone numbers, addresses, etc –There are over 50 different ways of sorting things out

1-26 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Algorithm comparison To compare algorithms, we have to find out how much work each does –The number of comparison this sorting algorithm has to do to sort out a list of n pieces is n 2 Once we know much each of them does, we can compare them to pick up the one that takes least amount of time/space, and implement it into a program.

1-27 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What does it do? Comparisons Instructions 4 and 5 illustrate abstract computational unit, which is really a comparison –Consider in order all CDs following a specific Alpha –While Alpha points to a fixed slot, Beta visits each slot following Alpha, in sequence, comparing its CD with the CD in the Alpha slot, and swapping when necessary –It essentially finds the next CD in order and moving it to the Alpha slot

1-28 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-28

1-29 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Alpha sweep abstraction Alpha sweeps from the slot where alphabetization begins through all slots (except the last), performing the Beta sweep instructions each time –For Alpha to go from 1 to n-1, we need to make n-1, n-2, …, 1 comparisons. –1+2+…+n-1= (n-1)*(n/2) is about n 2/ /2.

1-30 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Properties of the algorithm 1.Exhaustive. Considers all CDs from the first to (but not including) the last 2.Nonredundant. No slot is assigned to Alpha more than once, so the process stops if the Beta sweep stops 3.Progressive. At the end of each Beta sweep, the alphabetically next earliest CD is in Alpha

1-31 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More properties… 4. Complete. When the last Beta sweep is completed, the CD in the last slot is later than the CD in the next-to-last slot 5.Goal-achieving. The alphabetically earliest CD is in the first slot at the end of the first Beta sweep, by its property 4 and the fact that all CDs are considered; in every new position for Alpha the Beta sweep assigns the next earliest CD

1-32 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Abstracting for Other Algorithms and Programs The Alpha sweep and Beta sweep abstractions are specific to the Exchange Sort algorithm and to programs derived from it Other algorithms and programs exhibit different behaviors and require different abstractions, which will have different properties Every situation is different, but the approach—abstracting the behavior and understanding the properties—is always the same CS majors study about this stuff in CS3220

1-33 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Homework Multiple choices: even numbered Short answers: odd numbered Complete Exercises 1, 2, 4, 5 and 9.