Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 222 Discrete Structures for Computing

Similar presentations


Presentation on theme: "CSCE 222 Discrete Structures for Computing"— Presentation transcript:

1 CSCE 222 Discrete Structures for Computing
Algorithms

2 What is an Algorithm? ACTIVITY 1 This should take 2 or 3 minutes
Have the students volunteer to give definitions. There are basically no wrong answers. Write them (or keywords from them) down on the slide. Keywords to look for: steps, instructions, sequence, list, do a computation, solve a problem, perform a task, finite, simple, precise This could be done as a PollEverywhere or Menti activity

3 An algorithm is a finite sequence of precise instructions for performing a computation or solving a problem. This is one very popular definition of an algorithm. source: Discrete Mathematics and Its Applications, 7th edition by Rosen

4 A Recipe is an Algorithm
Recipes are actually one of the more obvious examples of algorithms. How would it be if every pair of shoes came with instructions for how to tie them? Or, a book with instructions for how to read it. Actually, eBooks do this. You can read books about how to read books on your Kindle or Nook. Instant Ramen instructions work for most instances (flavors, brands) of Instant Ramen

5 The set of steps to assemble a Piece of Furniture is an Algorithm.
Another obvious example of algorithms in real life. Like baking, the consequences of not following the algorithm exactly often results in a result which is clearly incorrect. This also illustrates why care must be taken in constructing algorithms: an algorithm with imprecise or incorrect steps will generate garbage results which are useful to no one and can actually do real harm (example: NASA orbiter crashes into Mars because Lockheed Martin engineers used English units and everyone else used metric. Cost: $125M and a delay in the progress of science). IKEA instructions are not general for all furniture, or even all IKEA furniture, or even all IKEA furniture of the same function (chair, table, bed, etc). But, they are general for all instances of the same type (the particular oddly named form of the IKEA furniture). The same instructions work for any valid set of pieces.

6 Describe an algorithm for finding the maximum value in a list of integers.
Take 2 minutes Express the algorithm naturally (i.e. don’t use code) Solution Set the temporary maximum to the first element of the list. For each remaining element in the list, compare it to the temporary maximum. If it is larger, set the temporary maximum to this integer. Return the temporary maximum as the answer.

7 Properties of Algorithms
Input Output Definiteness Correctness Effectiveness Finiteness Generality There are several properties that algorithms generally share. These properties are useful to keep in mind when algorithms are described. Most algorithms have most of these, but not all have all of them.

8 Properties of Algorithms
Input Output Definiteness Correctness Effectiveness Finiteness Generality An algorithm has input values from a specified set. Example algorithm: find max value procedure max(a_1,…a_n: integers) max := a_1 for i=2:n if max < a_i then max := a_i return max Input: a list of numbers

9 Properties of Algorithms
Input Output Definiteness Correctness Effectiveness Finiteness Generality An algorithm has input values from a specified set. From each set of input values, an algorithm produces output values from a specified set. The output values are the solution to the problem. Example algorithm: find max value procedure max(a_1,…a_n: integers) max := a_1 for i=2:n if max < a_i then max := a_i return max Output: the maximum number in the list

10 Properties of Algorithms
Input Output Definiteness Correctness Effectiveness Finiteness Generality An algorithm has input values from a specified set. From each set of input values, an algorithm produces output values from a specified set. The output values are the solution to the problem. The steps of an algorithm must be defined precisely. Example algorithm: find max value procedure max(a_1,…a_n: integers) max := a_1 for i=2:n if max < a_i then max := a_i return max Each step is defined precisely (assignment, iteration, conditional)

11 Properties of Algorithms
Input Output Definiteness Correctness Effectiveness Finiteness Generality An algorithm has input values from a specified set. From each set of input values, an algorithm produces output values from a specified set. The output values are the solution to the problem. The steps of an algorithm must be defined precisely. An algorithm should produce the correct output values for each set of input values. Example algorithm: find max value procedure max(a_1,…a_n: integers) max := a_1 for i=2:n if max < a_i then max := a_i return max The initial value of max is the first element of the list. If any larger element is found, max takes on that element’s value. Every element is checked. Therefore, max ends up with the value of the largest element of the list.

12 Properties of Algorithms
Input Output Definiteness Correctness Effectiveness Finiteness Generality It must be possible to perform each step of an algorithm exactly and in a finite amount of time. An algorithm has input values from a specified set. From each set of input values, an algorithm produces output values from a specified set. The output values are the solution to the problem. The steps of an algorithm must be defined precisely. An algorithm should produce the correct output values for each set of input values. Example algorithm: find max value procedure max(a_1,…a_n: integers) max := a_1 for i=2:n if max < a_i then max := a_i return max Assignment takes finite time, iteration (of n-1 comparisons) takes finite time. Things that would not be effective: Divide by zero Solve SAT in poly-time Count the reals

13 Properties of Algorithms
Input Output Definiteness Correctness Effectiveness Finiteness Generality An algorithm has input values from a specified set. It must be possible to perform each step of an algorithm exactly and in a finite amount of time. From each set of input values, an algorithm produces output values from a specified set. The output values are the solution to the problem. An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set. The steps of an algorithm must be defined precisely. An algorithm should produce the correct output values for each set of input values. Example algorithm: find max value procedure max(a_1,…a_n: integers) max := a_1 for i=2:n if max < a_i then max := a_i return max Terminates after all elements of list are seen, thus takes finite time.

14 Properties of Algorithms
Input Output Definiteness Correctness Effectiveness Finiteness Generality An algorithm has input values from a specified set. It must be possible to perform each step of an algorithm exactly and in a finite amount of time. From each set of input values, an algorithm produces output values from a specified set. The output values are the solution to the problem. An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set. The steps of an algorithm must be defined precisely. An algorithm should produce the correct output values for each set of input values. The procedure should be applicable for all problems of the desired form, not just for a particular set of input values. Example algorithm: find max value procedure max(a_1,…a_n: integers) max := a_1 for i=2:n if max < a_i then max := a_i return max Can be used to find the maximum value of any list of integers.

15 Activity What properties of algorithms do the following have?
Instant Ramen Instructions IKEA Bookshelf Assembly Instructions Time limit: 5 minutes ACTIVITY 2: groups of 2-4, pick one (ramen or ikea), find an example of instructions online to use as reference, go down the list of properties and decide (yes/no) whether the instructions have that property Volunteer groups for each to give/explain their answers Ramen: Input: noodles, seasoning, water Output: tasty ramen Definiteness: yes Correctness: yes Effectiveness: yes, assuming you can boil water Finiteness: yes Generality: works for pretty much any brand and flavor of instant ramen IKEA Assemply: Input: parts and tools Ouput: Bookshelf Definiteness: ???, are the picture “precise”? Effectiveness: yes, possible but not for everyone Generality: works for all sets of parts and tools meant to become the particular version of an IKEA bookshelf, assuming the parts and tools are all there Food for thought: can you make the instructions more algorithmic by adding in one or more of the missing properties? For example, can you devise a set of instructions for assembling bookshelfs that works for IKEA parts, but also for some other manufacturer’s parts, or all manufacturers parts?

16 Psuedocode is an intermediate between an English description and an implementation in a particular language of an algorithm. English is very high-level, not always well-suited to precise descriptions of algorithms Programming languages are very precise, but can make algorithms hard to understand

17 Pseudocode for Finding the Max
procedure max(a_1,…a_n: integers) max := a_1 for i=2:n if max < a_i then max := a_i return max

18 Does the max-finding algorithm have all of these properties?
Input Output Definiteness Correctness Finiteness Effectiveness Generality Input. A list of integers Output The largest integer in the list. Definiteness Assignments, finite loops, and comparisons all have precise definitions. Correctness Yes. Informal proof: temp_max is updated every time a large value is seen; all values seen; therefore temp_max is the largest value in the list after the loop ends. Finiteness Stops after seeing all elements of the list. Effectiveness Assignments, finite loops, and comparisons all take finite time. Generality Finds the maximum of any list of integers.

19 Activity The hailstone path length of an integer 𝒏 is the number of times the hailstone function can be applied until the result becomes 1, i.e. the value of 𝑖 such that 𝐴 𝑖 =1 for 𝐴 0 =𝑛. 𝐴 𝑖 = 𝐴 𝑖− 𝐴 𝑖−1 is even 3 𝐴 𝑖−1 +1 𝐴 𝑖−1 is odd hailstone What are the inputs? What are the outputs? What are the steps to solve the problem? Write your algorithm in text form. Do not write in a programming language. Time limit: 5 minutes ACTIVITY 4 Individual or partners (teams of 2) Have the students try their hand at it first. Instruct them to write the algorithm in text form. NO CODE! AKA Collatz conjecture: all integers eventually reach 1.

20 Example Solution 1 (natural language)
To compute the hailstone path length of an integer n, the input is the integer n, the output is an integer giving the length of the path, and the processing is to: declare a variable with initial value 0 to keep track of the path length, do the following until n is equal to 1: if n is even, then divide n by 2; otherwise, multiply n by 3 and add 1; add 1 to the value of the length counter; return the value of the length counter. DO NOT READ OFF THE SLIDE

21 Example Solution 2 (pseudocode)
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path Procedure hailstone Inputs: n (integer) Outputs: m (integer) if n is even then m = n / 2 else m = 3 * n + 1 return m

22 Basic Elements of Pseudocode
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path Procedure statement gives name of algorithm Appendix 3 gives Rosen’s pseudocode syntax. It’s good.

23 Basic Elements of Pseudocode
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path names of input and output variables and their types.

24 Basic Elements of Pseudocode
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path Write the procedure to transform the inputs to the outputs Initial indentation is optional. Subsequent indentation is mandatory for readability.

25 Basic Elements of Pseudocode: Assignment
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path Assignment statements set the values of variables. If the left-hand side of the assignment is a variable which is new, the statement creates that variable (e.g. l is a new variable that is created when it gets the initial value 0, n already exists as an input variable). Typical assignment operators: = :=

26 Basic Elements of Pseudocode: Loop
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path This is a repetition structure. The block of statement inside of it will be repeatedly executed until the value of the variable n is no longer greater than 1, that is until n is less than or equal to 1. There is also the for loop, which is used to iterate a definite number of time, or over a list of a finite number of things. For that reason, for loops are called definite loops, and while loops are called indefinite loops. Just like in flow diagrams, loops (and conditionals) can be nested.

27 Basic Elements of Pseudocode: Subroutine
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path Procedure hailstone Inputs: n (integer) Outputs: m (integer) if n is even then m = n / 2 else m = 3 * n + 1 return m // the next element of the hailstone path This is an example of using a procedure inside of another procedure. The hailstone procedure computes the hailstone function: if n is even, divide it by two, otherwise multiply by three and add one. This kind of thing is the same idea as the subroutine in flow diagrams. We can define other procedures, sometimes very complex ones, and then use them as if they were simple statements (because now they are!).

28 Basic Elements of Pseudocode: Conditional
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path Procedure hailstone Inputs: n (integer) Outputs: m (integer) if n is even then m = n / 2 else m = 3 * n + 1 return m // the next element of the hailstone path This is a conditional structure. Here, the condition is expressed in English. We could also have written the math that defines even numbers as an equivalent expression: n == 0 (mod 2). If the then or else clauses contain more than one statement, we can list those statements as an indented block starting on the next line. Note that the loop includes an implicit conditional: do X while the condition is true.

29 Basic Elements of Pseudocode: Returned Values
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path This is a return statement. It specifies which values to produce as output of the procedure. Here, the output is the value of the variable “l”, which is the length of the hailstone path. (this may be omitted since the preamble contains the list of outputs, a la MATLAB) Be aware that order matters in the case of multiple outputs.

30 Basic Elements of Pseudocode: Comments
Procedure hailstone-path-length Inputs: n (integer) Outputs: l (integer) l = 0 while n > 1 n = hailstone(n) l = l + 1 return l // the length of the hailstone path This is a [C-style] comment. Comments help remind us what statements do, or what the value of variables represent. In this case, this comment tells us that the variable l contains the length of the hailstone path, the number of steps taken to transform n into the number 1 using the hailstone procedure. Code should be self-documenting. Had we named the variable “lengthOfHailstonePath” instead of just “l”, we would not have needed the comment.

31 Basic Outline of Pseudocode
Procedure name-of-procedure Inputs: name (type), … Outputs: name (type), … // comments assumptions / initializations processing return name, … Every procedure has a name List the inputs and their types List the outputs and their types Use comments to document assumptions and make simple explanations Initialize all outputs and necessary auxiliary variables with correct/safe initial/default values Specify the processing steps Return the output variables (this may be omitted since the preamble contains the list of outputs, a la MATLAB) Use of bold and italics for clarity is encouraged. Bold: control structures (for, for each, while, if then else) Italics: variables, subroutines No style: text (comments)

32 Questions? If time remains, do practice problems Next time: search


Download ppt "CSCE 222 Discrete Structures for Computing"

Similar presentations


Ads by Google