algorithms an algorithm is a finite sequence of instructions, logic, an explicit step-by-step...

27
Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step- by-step procedure for solving a problem. Specific algorithms sometimes also go by the name method, procedure, or technique. One of the simplest algorithms is finding the largest number in an unsorted list of numbers.

Upload: sophia-nutt

Post on 14-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Algorithms

An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem.

Specific algorithms sometimes also go by the name method, procedure, or technique.

One of the simplest algorithms is finding the largest number in an unsorted list of numbers.

Page 2: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Expressing algorithms

Algorithms can be expressed in many kinds of notation, including:

Natural languages Pseudocode Flowcharts Box diagrams Programming languages

Page 3: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Natural language expressions

Natural language expressions of algorithms tend to be verbose and ambiguous, and rarely used for complex or technical algorithms.

Page 4: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Pseudocode

Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language.

Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations.

Page 5: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Pseudocode

Textbooks and specific publications related to computer science and numerical computation often use pseudocode in description of algorithms, so that all programmers can understand them, even if they do not all know the same programming languages.

Page 6: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Syntax of pseudocode

Pseudocode generally does not actually obey the syntax rules of any particular languages.

There is no systematic standard form, although any particular writer will generally borrow style and syntax from some conventional programming language, such as BASIC.

Variable declarations are typically omited.

Page 7: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Syntax of pseudocode

<variable> = <expression> if <condition>

do stuffelse do other stuff

while <condition> do stuff

Page 8: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Syntax of pseudocode

for <variable> = <first value> to <last value> step <step value> do stuff with variable

function <function name>(<arguments>) do stuff with arguments return something

<function name>(<arguments>) ' function call

Page 9: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Flowcharts

Flowchart is a common type of chart that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting these with arrows.

The first structured method for documenting process flow, “flow process chart”, was introduced by Frank Gilbreth in 1921.

Page 10: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Flowcharts

Herman Goldstine and John von Neumann developed the flow chart to plan computer programs in 1947.

Flowcharts used to be a popular means for describing computer algorithms and are still used for this purpose.

Page 11: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Types of flowcharts

Sterneckert (2003) divides four more general types of flowcharts:

Document flowcharts, showing a document flow through system.

Data flowcharts, showing data flows in a system. System flowcharts, showing controls at a

physical or resource level. Program flowcharts, showing the controls in a

program within a system.

Page 12: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Symbols of program flowcharts

Flow: line with an arrow

Terminal: round rectangle

Start of subroutine

START END RETURN x

FUNCTION sum(x, y)

Page 13: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Symbols of program flowcharts

Preparation: hexagon

Input/Output: parallelogram

Process: rectangle

INPUT x PRINT x

x = x + 1

x = 1

Page 14: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Symbols of program flowcharts

Decision: diamond

Connector: circle Off-page connector

x = 1

yes

no

1 2

Page 15: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Box diagrams

Box diagram is a graphical design representation for structured programming.

Another name is Nassi-Shneiderman diagram, because it is developed by Isaac Nassi and Ben Shneiderman in 1972.

Box diagram is also called structogram, as it shows a program structure.

Page 16: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Box diagrams vs flowcharts

Everything you can represent with a box diagram you can also represent with a flowchart.

For flowchart of programs, just about everything you can represent with a flowchart you can represent with a box diagram. The exceptions are things like goto, break, and continue statements.

Page 17: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Blocks in a box diagram

Process blocks

Branching blocks

ACTION

CONDITIONtrue false

Page 18: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Blocks in a box diagram

Top tested loops

Bottom tested loops

ACTION

LOOP CONDITION

LOOP CONDITION

ACTION

Page 19: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (1)

Calculation of electricity costs Known:

number of meter digits = 5metric: KWhfor first 20 KWh = 385 rupiah / KWhfor next 40 KWh = 445 rupiah / KWhfor the rest = 495 rupiah / KWh

Input: old and new meter digits Output: a bill that contains

a number of KWh used and total cost

Page 20: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (2)

Calculation of roots of a quadratic equation Input: coefficients of a quadratic equation Output: roots Formula:

D = b^2 – 4 a cif D > 0 then roots = two real numbersif D = 0 then roots = a real numberif D < 0 then roots = two imaginary numbersroots = (- b +/- √D) / (2 a)

Page 21: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (3)

Calculation of factorial of a number Input: n Output: n! Formula:

n! = n x (n – 1) x (n – 2) x … for n > 00! = 1

Examples: 0! = 1, 1! = 1, 2! = 2, 3! = 6, 4! = 24

Page 22: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (4)

Calculation of n-th fibonacci number Input: n Output: n-th fibonacci number Formula:

F(n) = F(n – 1) + F(n – 2) for n > 2F(1) = F(2) = 1

Fibonacci series: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Page 23: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (5)

Calculation of sine of a numberusing Maclaurin series

Input: x Output: sin(x) Formula:

x^3 x^5 x^7 x^9sin(x) = x - ---- + ---- - ---- + ---- - … 3! 5! 7! 9!number of iterations used is 10

Page 24: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (6)

Calculation of a real root of a cubic equationusing Newton-Raphson method

Input: coefficients of a cubic equation Output: a real root Formula:

x1 = x0 – f(x0) / f'(x0)iterates until a relative error < a thresholdrelative error = | (x1 – x0) / x1 |

Page 25: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (7)

Finding minimum and maximum values of an unordered vector

Input: n and n-length of unordered vector Output: minimum and maximum values Example:

2 3 1 5 0 4 7 3 6 5 (n = 10)Minimum value = 0Maximum value = 7

Page 26: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (8)

Displaying a Pascal's triangle Input: n Output: a Pascal's triangle of height-n

Tip: use a 2D-arrayfor n = 5 11 11 2 11 3 3 11 4 6 4 1

Page 27: Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes

Examples of problems (9)

Displaying a magic square using Siamese method (1688)

Input: n Output: a magic square of order-n (n = odd)

for n = 517 24 1 8 1523 5 7 14 16 4 6 13 20 2210 12 19 21 311 18 25 2 9