algorithms - teacherjpt.files.wordpress.com 05, 2016 · • terminal – indicates the starting or...
Post on 09-Mar-2018
219 Views
Preview:
TRANSCRIPT
3
ALGORITHMS Example: Finding the Maximum Element in a
Finite Sequence. procedure max(a1, a2 , . . . , an : integers) max : = a1 for i : = 2 to n if max < ai then max := ai return max {max is the largest element}
4
ALGORITHMS Introduction There are many general classes of problems that
arise in discrete mathematics. For instance: given a sequence of integers, find the largest one; given a set, list all its subsets; given a set of integers, put them in increasing order; given a network, find the shortest path between
two vertices.
5
ALGORITHMS • When presented with such a problem, the first thing
to do is to construct a model that translates the problem into a mathematical context.
• Setting up the appropriate mathematical model is only part of the solution. To complete the solution, a method is needed that will solve the general problem using the model.
• Ideally, what is required is a procedure that follows a sequence of steps that leads to the desired answer.
6
ALGORITHMS • An algorithm is a finite sequence of precise
instructions for performing a computation or for solving a problem.
7
ALGORITHMS • Describe an algorithm for finding the maximum
(largest) value in a finite sequence of integers. • 1. Set the temporary maximum equal to the first integer in the
sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.)
• 2. Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer.
• 3. Repeat the previous step if there are more integers in the sequence.
• 4. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.
FLOW CHART • diagram that
represents an algorithm, workflow or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows.
8
6 BASIC SYMBOLS USED • Terminal
– Indicates the starting or ending
• Input/Output • Process
– Indicates the operation 9
6 BASIC SYMBOLS USED • Decision
– Yes or No
• Connector – Used to connect breaks
• Control Flow
– Show direction 10
FLOW CHART RULES • Top to bottom
• Boxes must connected with arrows
• Start with a Terminal or Process symbol
• Decision symbols have 2 exit points,
YES and NO 11
Create Flow Charts for the following
• Addition of 6 and 4
• Sum of 3 numbers
• Print “Passed” 5 times
• Logging in to Facebook
12
13
PSEUDOCODE • An algorithm can also be described using a
computer language
• A form of pseudocode is used instead of using a particular computer language like C, C++, Java, etc… to specify algorithms
• Pseudocode provides an intermediate step between an English language description of an algorithm and an implementation of this algorithm in a programming language.
14
PSEUDOCODE
• In pseudocode, the instructions used can include any well-defined operations or statements.
• A computer program can be produced in any computer language using the pseudocode description as a starting point.
15
COMMON PSEUDOCODE STATEMENTS
Procedure Assignment Comments Conditional Constructions Loop Constructions Return
16
Procedure Statements The pseudocode for an algorithm begins with a
procedure statement that gives the name of an algorithm, lists the input variables, and describes what kind of variable each input is.
Example: procedure maximum(L: list of integers)
is the first statement in the pseudocode
description of the algorithm, which we have named maximum, that finds the maximum of a list L of integers.
17
Assignment Statements • used to assign values to variables. symbol := is used • the left-hand side is the name of the variable and
the right-hand side is an expression that involves constants, variables that have been assigned values, or functions defined by procedures and may contain any of the usual arithmetic operations.
variable := expression • Example: max := a x := largest integer in the list L
18
Conditional Constructions • The simplest form of the conditional construction
that we will use is • if condition then statement or • if condition then block of statements or • if condition then statement 1 else statement 2 • Example: If a > b then a = c
else a = d
19
Loop Constructions • for variable := initial value to final value statement or • for variable := initial value to final value block of
statements
• Example: sum := 0 for i := 1 to n sum := sum +i
20
Loop Constructions • while condition statement or • while condition block of statements
• Example:
sum := 0 while n > 0 sum := sum +n n := n − 1
21
Comments • statements enclosed in curly braces are not
executed. Such statements serve as comments or reminders that help explain how the procedure works.
• For instance, the statement {x is the largest element in L} can be used to remind the reader that at that point
in the procedure the variable x equals the largest element in the list L.
22
RETURN • We use a return statement to show where a
procedure produces output.
• A return statement of the form return x produces the current value of x as output. The output x can involve the value of one or more functions, including the same function under evaluation, but at a smaller value.
• For instance, the statement return f (n − 1) is used to call the algorithm with input of n − 1. This means that the algorithm is run again with input equal to n − 1.
23
ALGORITHMS Example: Finding the Maximum Element in a
Finite Sequence. procedure max(a1, a2 , . . . , an : integers) max : = a1 for i : = 2 to n if max < ai then max := ai return max {max is the largest element}
24
PROPERTIES OF ALGORITHMS • Input. An algorithm has input values from a
specified set.
• Output. From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem.
• Definiteness. The steps of an algorithm must be defined precisely.
25
PROPERTIES OF ALGORITHMS • Correctness. An algorithm should produce
the correct output values for each set of input values.
• Finiteness. An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set.
26
PROPERTIES OF ALGORITHMS • Effectiveness. It must be possible to perform
each step of an algorithm exactly and in a finite amount of time.
• Generality. The procedure should be applicable for all problems of the desired form, not just for a particular set of input values.
27
USE OF ALGORITHMS • Searching Algorithms
• Sorting
• Greedy Algorithms
– -determine the best choice
28
SAMPLE SEARCHING ALGORITHMS
The Linear Search Algorithm procedure linear search(x: integer, a1, a2, . . . , an:
distinct integers) i := 1 while (i ≤ n and x = ai ) i := i + 1 if i ≤ n then location := i else location := 0 return location{location is the subscript of the term
that equals x, or is 0 if x is not found}
29
SAMPLE SEARCHING ALGORITHMS
• The Binary Search Algorithm procedure binary search (x: integer, a1, a2, . . . , an: increasing
integers) i := 1{i is left endpoint of search interval} j := n {j is right endpoint of search interval} while i < j m := (i + j)/2 if x > am then i := m + 1 else j := m if x = ai then location := i else location := 0 return location {location is the subscript i of the term ai equal to x, or 0 if x is not found}
top related