1 / 33

Introduction to Algorithms and Problem Solving with Computers

Learn the basics of algorithms and how to solve problems using computer programs. This chapter introduces the concept of algorithms and provides an example on how to sort items.

heatherlusk
Download Presentation

Introduction to Algorithms and Problem Solving with Computers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10: How to Solve the Problem? Introduction to Computers CS1100.01 Dr. Zhizhang Shen

  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. 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. 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. Five essential properties of algorithms • 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 • 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. 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 must eventually stop: • we are not patient.

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

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

  11. Alphabetize CDs • Input: An unordered sequence of CDs filling a slotted rack • Output: The same CDs in the rack in alphabetical order • Instructions: • Use Name to refer to the name or the group or musician or composer on a CD • Decide which end is the beginning of the sequence and call the slot at that end the Alpha slot, i.e., where to start • Call the slot next to Alpha the Beta slot

  12. Alphabetize CDs (cont'd) • 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 • If there is a slot following Beta, call it Beta and go back to Instruction 4; otherwise continue on to the next step • 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

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

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

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

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

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

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

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

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

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

  22. 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 n2 • 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.

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

  24. 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 n2/ /2.

  25. Properties of the algorithm • Exhaustive. Considers all CDs from the first to (but not including) the last • Nonredundant. No slot is assigned to Alpha more than once, so the process stops if the Beta sweep stops • Progressive. At the end of each Beta sweep, the alphabetically next earliest CD is in Alpha

  26. 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 • 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.

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

  28. Homework • Multiple choices: even numbered • Short answers: odd numbered • Complete Exercises 1, 2, 4, 5 and 9.

More Related