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

Presentation on theme: "Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes."— Presentation transcript:

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.

Expressing algorithms Algorithms can be expressed in many kinds of notation, including:  Natural languages  Pseudocode  Flowcharts  Box diagrams  Programming languages

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

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.

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.

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.

Syntax of pseudocode = if do stuff else do other stuff while do stuff

Syntax of pseudocode for = to step do stuff with variable function ( ) do stuff with arguments return something ( ) ' function call

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.

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.

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.

Symbols of program flowcharts Flow: line with an arrow Terminal: round rectangle Start of subroutine STARTENDRETURN x FUNCTION sum(x, y)

Symbols of program flowcharts Preparation: hexagon Input/Output: parallelogram Process: rectangle INPUT xPRINT x x = x + 1 x = 1

Symbols of program flowcharts Decision: diamond Connector: circle Off-page connector x = 1 yes no 1 2

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.

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.

Blocks in a box diagram Process blocks Branching blocks ACTION CONDITION truefalse

Blocks in a box diagram Top tested loops Bottom tested loops ACTION LOOP CONDITION ACTION

Examples of problems (1) Calculation of electricity costs  Known: number of meter digits = 5 metric: KWh for first 20 KWh = 385 rupiah / KWh for next 40 KWh = 445 rupiah / KWh for the rest = 495 rupiah / KWh  Input: old and new meter digits  Output: a bill that contains a number of KWh used and total cost

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 c if D > 0 then roots = two real numbers if D = 0 then roots = a real number if D < 0 then roots = two imaginary numbers roots = (- b +/- √ D) / (2 a)

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 > 0 0! = 1  Examples: 0! = 1, 1! = 1, 2! = 2, 3! = 6, 4! = 24

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 > 2 F(1) = F(2) = 1  Fibonacci series: 1, 1, 2, 3, 5, 8, 13, 21, 34,...

Examples of problems (5) Calculation of sine of a number using Maclaurin series  Input: x  Output: sin(x)  Formula: x^3 x^5 x^7 x^9 sin(x) = x - ---- + ---- - ---- + ---- - … 3! 5! 7! 9! number of iterations used is 10

Examples of problems (6) Calculation of a real root of a cubic equation using 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 threshold relative error = | (x1 – x0) / x1 |

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 = 0 Maximum value = 7

Examples of problems (8) Displaying a Pascal's triangle  Input: n  Output: a Pascal's triangle of height-n Tip: use a 2D-array for n = 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

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 = 5 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

Similar presentations