1 / 44

Algorithmic Thinking

Algorithmic Thinking. and the order of things. http://www.flickr.com/photos/grzegorz_rogala/4812173573/sizes/l/in/photostream/. Algorithms. Algorithm : a sequence of discrete actions that when followed, will result in achieving some goal or solving some problem. Examples:

keziah
Télécharger la présentation

Algorithmic Thinking

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. Algorithmic Thinking and the order of things http://www.flickr.com/photos/grzegorz_rogala/4812173573/sizes/l/in/photostream/

  2. Algorithms • Algorithm: a sequence of discrete actions that when followed, will result in achieving some goal or solving some problem. • Examples: • A football player executing a play on the field • A pianist playing a musical score • A mathematician performing long division • A driver following a GPS directed map • A chef following a recipe

  3. Computational Algorithms • Algorithm: a sequence of computational actions that transform some input into the desired output. • A computational view of chocolate chip cookie recipe: • input: raw ingredients • algorithms: the directions • output: the cookies • The directions describe an orderly sequence of discrete actions that transform the input into the desired output.

  4. Properties of Good Algorithms • An algorithm must ensure that several important properties are maintained. • Each action must be computable/meaningful. • Each action must be unambiguous; can’t have two or more different meanings. • The ordering of actions must be well defined. • The number of actions in the sequence must be finite.

  5. Properties of Good Algorithms • The meaning of an action is known as the semantics. • Ambiguity makes knowing the semantics difficult or impossible. • My mom likes climbing vines. • If x < 0 then if x > 10 then A else B • Ordering of actions is vitally important • Swap steps 6 and 7 in the cookie recipe and things don’t turn out well. • An infinite number of actions cannot be executed.

  6. Programs and Languages • Computer software(program) is a sequence of instruction that a computer follows to achieve some goal. • Software cannot be expressed in human languages such as English or Chinese. Not precise or unambiguous. • Computer software is expressed in a programming language. • A programming language is designed to precisely and compactly express computational algorithms. • When a computer executes a computer program, the computer simply follows the steps that are given by the program in order to obtain the desired output from the provided input.

  7. Computable Actions • Computational algorithms must be expressed as a well-defined sequence of actions that the computer must follow. • We must know what actions a computer can take • Must also know what actions a computer cannot take! • Computable actions are actions that a computer can perform.

  8. Computable Action : Name Binding • Name binding is the association of a name, also known as an identifier, with a value. • A name must start with a letter and be followed by letters or digits or an underscore. • An expression is either a value or a formula that produces a value.

  9. Computable Action : Name Binding • Consider the following sequence of actions • Associate the name X with the value 3 • Associate the name Y with the value 4. • Associate the name Z with the value produced by the formula “3 + 4” • Whenever a computer follows a formula to produce a value, the expression is evaluated. • Name binding first evaluates the expression on the right side and then associates the resulting value with the name given on the left side. • The effect line 3 is to associate the name Z with the value 7.

  10. Name Binding When a name is evaluated, the value is determined by the name binding

  11. Proper Naming • Proper naming is crucial. • The importance of proper naming is not a modern invention of computation. • The ability to properly name things indicated a mastery over those things in ancient literature. • In Genesis, Adam demonstrated his mastery over other creatures by giving a name to them. • A name is good if it properly and correctly describes the central essence of the data.

  12. a ← 3.75 b ← 10 c ← a * b Which of these uses a more proper naming? Cents ← 3.75 Size ← 10 Money ← Cents × Size DollarsPerGallon← 3.75 TankCapacity← 10 DollarsToFill ← DollarsPerGallon×TankCapacity

  13. Name Binding • Name binding allows algorithms that solve a wide range of problems. • Consider any problem that involves converting a quantity from one unit of measure into another. • Travel to the 2016 Summer Olympic Games in Rio Di Janeiro, Brazil. How many Brazilian reais(BRL) is one thousand U.S. dollars (USD) is worth. • Depends on exchange rate. • Write an algorithm for computing the answer

  14. Computational State • Name bindings change during execution. • What does the following algorithm compute? • The computational state of a program is the collection of name bindings that are active an a single point in time.

  15. Computational State

  16. Computational State

  17. Control Flow • Control flow : the specific order in which the individual actions of a computer program are executed. • Actions are normally done in the order that they are written by the programmer. • The ordering must be flexible and allow the computer to respond to inputs of various types. • This flexibility is supported by control flow statements. • Control flow statements are commands that control the order of execution. • Selection • Repetition

  18. Selection • Selection : a control flow statement that allows a computer to choose which set of actions to perform. • One-way : either perform an action or not. • Two-way : choose one of exactly two actions • Multi-way : choose one of several actions

  19. One way selection • Case study. • An online store charges a $10 shipping cost for any order under $40 • Free shipping for any order of at least $40. • If the order amount is less than $40 then the shipping cost is $10 otherwise the shipping cost is $0.

  20. Syntax • Flowcharts are graphical (pictures) and are cumbersome to draw • Drag and drop lines, arrows, rectangles. • Position them on the screen. • Programming languages are usually textual. • Easy to type. Just type top-to-bottom. • Highly structured. Must follow the grammar rules. • Rules for writing a one-way selection • CONDITION is an expression that produces a LOGICAL VALUE • ACTIONS is a sequence of actions that will be performed if the condition is TRUE

  21. Case Study Solution

  22. Two way selection • The one-way solution is inefficient since it will sometimes require two bindings when only one binding is necessary. • bind shipping to 0 • almost immediately bind shipping to 10 • A two-way selection is a better fit. Selects exactly one action of two alternatives.

  23. Syntax Condition : an expression yields a LOGICAL VALUE IF-TRUE-ACTIONS : The sequence of actions to perform if the condition is TRUE IF-FALSE-ATIONS : The sequence of actions to perform if the condition is FALSE

  24. Case Study Solution 2

  25. Multi way selection There are often more than two choices. What about a policy with three-levels of shipping cost?

  26. Multi way flow chart Most programming languages express multi-way selections as a chain of two-way selection statements. Each condition distinguishes between two choices. We must express multiple conditions to distinguish between more than two choices.

  27. Multi-way solution

  28. Ordering of Conditions

  29. Repetition • “Wash, rinse, repeat” • A part of pop culture. • A metaphor for anyone who robotically follows instructions without thinking. • Anyone following these instructions will spend the rest of their lives endlessly repeating the same steps over and over. http://www.flickr.com/photos/themysteryman/4431189771/sizes/l/in/photostream/

  30. Loop • Loop : a control structure that repeatedly executes a sequence of actions. • While loop : a type of loop where a sequence of actions is repeated as long as some logical condition holds.

  31. Example • Consider the instructions : “while the steak has not yet reached a temperature of 135 °F, cook for three minutes.” • Condition : the steak has not yet reached 135 °F • Action(s) to repeat : “cook for three minutes”.

  32. Syntax CONDITION : an expression that produces a logical value ACTIONS : a sequence of actions to perform if the condition is TRUE

  33. Example

  34. Details and Computational State

  35. Variant Counting loop : effectively counts how many times we execute this loop. The chef is cooking a large meal that includes steak, baked potatoes and steamed broccoli. The chef needs to know the time required for grilling the steak in order to ensure that all three dishes are completed at about the same time. How to modify our program?

  36. Loop Structure • All well-designed loops have the following elements • Initialization : every variable involved in the loop is given an initial value (a starting point) • Condition : The condition must be precise • Progress : The actions within the loop must ensure that the condition will eventually produce a ‘false’. This ensures that the loop will terminate.

  37. Loops are easy to get wrong An infinite loop is where the condition is never false.

  38. Modules and Abstraction An algorithm is a sequence of instructions. We can name a sequence of instructions and thereby introduce a ‘new instruction’. A sub-processes or module is a named sequence of instructions The syntax we will use to name a sequence of actions

  39. Example • We can name our steak-grilling algorithm as ‘grillSteak’. • Effectively introduces a new instruction • The instruction can be used by name • Introduces a higher level of abstraction

  40. Abstraction Modules are vital when writing large programs that consist of thousands or millions of parts.

  41. Flexibility module NAME(V1,V2,…,Vn) is ACTIONS endmodule • A well-designed module should be useful in a wide variety of contexts. • Flexibility is introduced via parameters • Formal parameter: a variable name associated with a module when the module is written • Actual parameter: a value associated with a formal parameter when the module is used.

  42. Example • The grillSteak algorithm assumes a starting temp of 75 degrees. What if we start grilling at a different temp? • We can now use this module by supplying a value for ‘steakTemp’. • grillSteak(94)

  43. Example Might want to have the steam well-done or rare (different finishing temperatures). May want to model a different temperature-change for three-minutes of cooking.

More Related