1.03k likes | 1.72k Views
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:
E N D
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: • 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
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.
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.
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.
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.
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.
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.
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.
Name Binding When a name is evaluated, the value is determined by the name binding
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.
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
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
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.
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
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
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.
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
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.
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
Multi way selection There are often more than two choices. What about a policy with three-levels of shipping cost?
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.
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/
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.
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”.
Syntax CONDITION : an expression that produces a logical value ACTIONS : a sequence of actions to perform if the condition is TRUE
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?
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.
Loops are easy to get wrong An infinite loop is where the condition is never false.
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
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
Abstraction Modules are vital when writing large programs that consist of thousands or millions of parts.
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.
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)
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.