Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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.

3 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?

4 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 (555-1212) –We once went through an algorithm as how to do it Others require written instructions—recipe, assembly instructions, driving directions

5 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.

6 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.

7 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, …

8 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?

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

10 1-10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-10 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.

11 1-11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-11 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

12 1-12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-12 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

13 1-13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-13 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

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

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

16 1-16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-16 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.

17 1-17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-17 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.

18 1-18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-18 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

19 1-19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-19 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

20 1-20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-20 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

21 1-21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-21 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

22 1-22 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-22 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

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

24 1-24 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-24 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

25 1-25 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-25 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

26 1-26 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-26 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.

27 1-27 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-27 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

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

29 1-29 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-29 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.

30 1-30 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-30 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

31 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. 10-31

32 1-32 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-32 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

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


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

Similar presentations


Ads by Google