320 likes | 335 Views
Understand the key properties and definitions of algorithms, including input, output, definiteness, correctness, effectiveness, finiteness, and generality. Learn how algorithms work and their importance in computing.
E N D
CSCE 222Discrete Structures for Computing Algorithms
An algorithm is a finite sequence of precise instructions for performing a computation or solving a problem. source: Discrete Mathematics and Its Applications, 7th edition by Rosen
The set of steps to assemble a Piece of Furniture is an Algorithm.
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)
Properties of Algorithms • Input • Output • Definiteness • Correctness • Effectiveness • Finiteness • Generality
Properties of Algorithms • Input • Output • Definiteness • Correctness • Effectiveness • Finiteness • Generality An algorithm has input values from a specified set.
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.
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.
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.
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. The steps of an algorithm must be defined precisely. An algorithm should produce the correct output values for each set of input values.
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.
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.
Activity • What properties of algorithms do the following have? • Instant Ramen Instructions • IKEA Bookshelf Assembly Instructions Time limit: 5 minutes
Psuedocode is an intermediate between an English description and an implementation in a particular language of an algorithm.
Does the max-finding algorithm have all of these properties? • Input • Output • Definiteness • Correctness • Finiteness • Effectiveness • Generality
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 for . • 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
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.
Example Solution 2 (pseudocode) Procedurehailstone Inputs:n (integer) Outputs:m (integer) ifn is even then m = n / 2 else m = 3 * n + 1 returnm Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path
Basic Elements of Pseudocode Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path
Basic Elements of Pseudocode Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path
Basic Elements of Pseudocode Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path
Basic Elements of Pseudocode: Assignment Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path
Basic Elements of Pseudocode: Loop Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path
Basic Elements of Pseudocode: Subroutine Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path Procedurehailstone Inputs:n (integer) Outputs:m (integer) ifn is even then m = n / 2 else m = 3 * n + 1 returnm // the next element of the hailstone path
Basic Elements of Pseudocode: Conditional Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path Procedurehailstone Inputs:n (integer) Outputs:m (integer) ifn is even then m = n / 2 else m = 3 * n + 1 returnm // the next element of the hailstone path
Basic Elements of Pseudocode: Returned Values Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path
Basic Elements of Pseudocode: Comments Procedurehailstone-path-length Inputs:n (integer) Outputs:l (integer) l = 0 whilen > 1 n = hailstone(n) l = l + 1 returnl// the length of the hailstone path
Basic Outline of Pseudocode Procedure name-of-procedure Inputs:name (type), … Outputs:name (type), … // comments assumptions / initializations processing return name, …