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.

Slides:



Advertisements
Similar presentations
Chapter 11: Models of Computation
Advertisements

Slides modified by Erin Chambers Problem Solving and Algorithm Design.
Chapter 23 A Fluency Summary. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives Discuss how being Fluent.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Computers CS Dr. Zhizhang Shen Chapter 10: How to.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Chapter 2: Algorithm Discovery and Design
Chapter 2 The Algorithmic Foundations of Computer Science
1. By the end of this lecture you should be able … To describe in general terms how computers function To describe the fetch-execute cycle To explain.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 2: Algorithm Discovery and Design
Chapter 1 Program Design
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Simple Program Design Third Edition A Step-by-Step Approach
Algorithms Algorithms are a familiar idea. Our goal is to learn to specify them right so someone or something else does the work.
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science 6th Edition
Invitation to Computer Science, Java Version, Second Edition.
Introduction to problem solving. Introductory Discussion? What problems are you faced with daily? Do you think a computer can be designed to solve those.
1 Ethics of Computing MONT 113G, Spring 2012 Session 13 Limits of Computer Science.
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.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Chapter 1 Program design Objectives To describe the steps in the program development process To introduce the current program design methodology To introduce.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
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.
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.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
ALGORITHMS.
Complexity & Computability. Limitations of computer science  Major reasons useful calculations cannot be done:  execution time of program is too long.
Chapter 10 Algorithmic Thinking. Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the.
Capabilities of computing systems Numeric and symbolic Computations A look at Computability theory Turing Machines.
Chapter 2 Principles of Programming and Software Engineering.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Maitrayee Mukerji. INPUT MEMORY PROCESS OUTPUT DATA INFO.
INTRODUCTION TO PROBLEM SOLVING
Introduction to Computer Programming
Algorithms Chapter 3 With Question/Answer Animations
Objective of This Course
Algorithm Discovery and Design
CSCI 1250 Information Technologies
Programming We have seen various examples of programming languages
Chapter 10: Algorithm TECH Prof. Jeff Cheng.
What's The Plan?: Algorithmic Thinking
What's The Plan?: Algorithmic Thinking
What's The Plan?: Algorithmic Thinking
Presentation transcript:

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 © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-2 Algorithm A precise, systematic method used to produce a specified result We have already seen several –Recognition of a button click (Ch.1) –Placeholder technique (Ch.2) –Hex to bits (Ch.8)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-3 Algorithms in Everyday Life Some algorithms are learned—arithmetic Some we figure out ourselves—looking up a phone number Others require written instructions—recipe, assembly instructions, driving directions

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-4 Five Essential Properties of Algorithms 1.Input specified –Data to be transformed during the computation to produce the output –Must specify type, amount, and form of data 2.Output specified –Data resulting from the computation—intended result –It is possible to have no output

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-5 Five Essential Properties (cont'd) 3.Definiteness –Specify the sequence of events –Details of each step, including how to handle errors 4.Effectiveness –The operations are doable 5.Finiteness –Must eventually stop

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-6 Language in Algorithms Natural language –For people, we use a natural language like English –Ambiguity is common in natural language Programming Language –Formal languages designed to express algorithms –Precisely defined; no ambiguity

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-7 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 –"From the Limmat River go to Bahnhof Strasse and turn right." –Assumes you are traveling in a specific direction. If you are not, the directions will fail.

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-8

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-9 Giving Better Directions Follow these steps: –Give the starting point –State the direction of travel –Give landmarks, especially when turning –Give measured distances (2.3 miles) instead of blocks, cross streets, or traffic lights, all of which can be ambiguous –Include an “overshot” test to help detect error Less ambiguity Reduces the dependence on context

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-10

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Program vs. Algorithm A program is an algorithm that has been customized to solve a specific task under a specific set of circumstances, expressed in a specific language Algorithm is a general method; program is a specific method

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley An Algorithm: Alphabetize CDs Imagine CDs in a slotted rack, not otherwise organized You want to alphabetize by name of group, performing musician, or composer How do you solve this problem?

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Alphabetize CDs (cont'd) Input: An unordered sequence of CDs filling a slotted rack Output: The same CDs in the rack in alphabetical order Instructions: 1.Use the term Artist_Of 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 3.Call the slot next to Alpha the Beta slot

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Alphabetize CDs (cont'd) 4.If the Artist_Of the CD in Alpha comes later in alphabet than the Artist_Of in Beta, swap the CDs; otherwise continue on 5.If there is a slot following Beta, begin calling it Beta and go to Instruction 4; otherwise continue on 6.If there are two or more slots following Alpha, begin calling the slot following Alpha, Alpha and begin calling the slot following it Beta, and go to Instruction 4; otherwise stop

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 1 Use the term Artist_Of 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 word that will be used for alphabetizing

Copyright © 2011 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. Defines the word Alpha. At the start, Alpha refers to the first slot in the sequence; as the algorithm progresses it refers to successive slots

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 3 Call the slot next to the Alpha slot the Beta slot –Gives Beta its initial meaning. Alpha and Beta have no inherent meaning; they are chosen by the programmer to name slots in the rack

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 4 If the Artist_Of the CD in the Alpha slot comes later in the alphabet than the Artist_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 they are in the correct order

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 5 If there is a slot following the Beta slot, begin calling it the Beta slot and go to Instruction 4; otherwise, continue –Re-defines 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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instruction 6 If there are two or more slots following the Alpha slot, begin calling the slot following the Alpha slot Alpha and the slot following it Beta, and go to Instruction 4; otherwise stop –By the time we get to this instruction, the alphabetically earliest CD is in the Alpha slot. We advance Alpha to the next slot and sweep through the CD rack again, locating the alphabetically earliest CD. When there are no longer enough slots to call one Alpha and one Beta, the entire rack has been alphabetized and the algorithm stops

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-21

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-22

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Analyzing Alphabetize CDs Algorithm Exhibits 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 because there are only a finite number of slots that can be paired, so instructions 4, 5, and 6 cannot be repeated indefinitely

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley A Deeper Analysis Structural features –Two instructions, 5 and 6, in which the agent is directed to go back and repeat instructions. This is called a loop –See this illustrated in the flow chart following Loops and Tests –A loop must include a test to determine whether the instructions should be repeated one more time Assumptions we made –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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-25

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Exchange Sort Algorithm The Alphabetize CDs example illustrates a standard algorithm called Exchange Sort –The basic idea is 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 comparison principle (release date, length of play, time of ownership, etc.)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Abstraction in Algorithmic Thinking We can think of parts of the algorithm's behavior as cohesive units rather than individual instructions Work proportional to n 2 –Where n is the number of items to sort Why?

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Beta Sweep Abstraction Instructions 4 and 5 illustrate abstract computational unit, which we'll call Beta Sweep –Operation of considering in order all CDs following a specific Alpha (~ ½n comparisons on average) –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 –Has the effect of finding the next CD in order and moving it to the Alpha slot

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-29

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Properties of Beta Sweep Abstraction 1. Exhaustive. Considers all CDs from Alpha slot to the end of the rack, and none are skipped 2. Nonredundant. Visits each slot following Alpha only once. It never considers the same CD pair twice, which ensures that the sweep stops. 3. Progressive. At any given time the alphabetically earliest CD seen so far in this sweep is in the Alpha slot. 4. Goal-achieving. After the sweep completes, the alphabetically earliest CD among all CDs considered in this sweep (including Alpha) is in Alpha Properties 1,2, and 3 imply truth of 4

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Alpha Sweep Abstraction Alpha sweep moves from the slot where alphabetization begins through all slots (except the last), Performs the entire Beta sweep set of instructions each time Alpha sweep uses the Beta sweep abstraction as a component of its algorithm description

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-32

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Properties of Alpha Sweep Abstraction 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 (which is does, by property 2 of the Beta sweep) 3. Progressive. At the end of each Beta sweep, the alphabetically next earliest CD is in Alpha

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Properties of Alpha Sweep (cont’d) 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 (by property 3 of Beta sweep) 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); thereafter, in every new position for Alpha the Beta sweep assigns the next earliest CD. Therefore, the program alphabetizes.

Copyright © 2011 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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Where Are We Headed? Entities we created and manipulated in the Alphabetize CDs program exemplify concepts we will be learning in detail soon Variables. Alpha and Beta are variables in the program Locations. The slots in the CD rack are like computer memory locations Values. The CDs are the values stored in the locations

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Where Are We Headed? (cont’d) Function. Artist_Of is a function for locating the name of the group or performer on a CD Initialization. Instructions 2 and 3 give initial values to variables Alpha and Beta respectively Loops. Instructions 4 and 5 together form a loop; instructions 4 through 6 also form a loop Array. The CD rack is a (linear) array

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary Everyday algorithms (e.g. recipes) can be ineffective because we write them in imprecise languages Algorithms have five fundamental properties Alphabetizing CDs in a filled rack is an algorithm; our six-instruction program defining it used two named slots, Alpha and Beta, and made repeated sweeps over the remaining CDs. Alphabetize CDs is a program built using the Exchange Sort algorithm

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary (cont’d) Processing Alphabetize CDs involves two interacting behaviors: the Beta sweep and the Alpha sweep. These abstractions have several properties, which allow us to reason that the algorithm produces alphabetized sequences. Abstractions and their properties are the essence of algorithmic thinking; algorithmic thinking can become second nature, making us effective problem solvers.